opencv 根據模板凸包求閾值化後的輪廓組合

2021-08-02 13:49:51 字數 1758 閱讀 6434

影象處理中,要求特徵與背景的對比度高,同時,合適的影象分割也是解決問題的關鍵。

博主以前的方法,預設為特徵必然是最大的連通域,所以閾值化後,查詢輪廓,直接提取面積最大的輪廓即可。

但可能會存在另一種情況,不論怎麼閾值化和膨脹,想要的特徵被分成好幾塊,也即斷開了。此時,再加上一些不可**的干擾和雜訊,findcontours之後,會得到很多輪廓。

那麼問題來了,我們需要的是哪個輪廓,或者是哪幾個輪廓組合的區域?

本文的意義也在於此。

根據模板的凸包,求出影象中最相似的輪廓組合。

本方法,主要用到matchshapes函式,並基於這樣乙個前提:模板凸包的2/3部分,與模板凸包的相似度,大於模板凸包的1/2部分。

話不多說,上**。

void getalikecontours(std::vectorinputlist, cv::mat inputimage, std::vector&outputlist)

} //可能會存在空的輪廓,把他們刪除

for (int idx = contours.size() - 1; idx >= 0; idx--)

while (true)

int maxcontouridx = 0;

int maxcontourptnum = 0;

for (int index = contours.size() - 1; index >= 0; index--)

}//第二大輪廓

int secondcontouridx = 0;

int secondcontourptnum = 0;

for (int index = contours.size() - 1; index >= 0; index--)

}vectormaxlist;

vectormaxandseclist;

vectormaxlisthull;

vectormaxandseclisthull;

maxlist.insert(maxlist.end(), contours[maxcontouridx].begin(), contours[maxcontouridx].end());

maxandseclist.insert(maxandseclist.end(), contours[maxcontouridx].begin(), contours[maxcontouridx].end());

maxandseclist.insert(maxandseclist.end(), contours[secondcontouridx].begin(), contours[secondcontouridx].end());

convexhull(mat(maxlist), maxlisthull, true);

convexhull(mat(maxandseclist), maxandseclisthull, true);

double maxcontourscore = matchshapes(inputlist, maxlisthull, cv_contours_match_i1, 0);

double maxandseccontourscore = matchshapes(inputlist, maxandseclisthull, cv_contours_match_i1, 0);

if (maxcontourscore>maxandseccontourscore)

contours.erase(contours.begin() + secondcontouridx);

}}

swust OJ 249 求凸包面積模板

用graham scan 求出凸點,再用叉積求面積,乙個三角形的面積等於叉積的一半。define ios ios sync with stdio false cin.tie 0 cout.tie 0 include define int long long using namespace std t...

凸包模板題

poj 1113 這道題是凸包第一題,也是我學習演算法的一題。直接看了題解,看別人怎麼寫的然後自己模仿,寫出自己的凸包模板 這道題,直接凸包然後算個長度就可以了 include include include include includeusing namespace std struct poi...

凸包模板 優化

const double eps 1e 9 struct point 儲存點 point int x,int y x x y y point operator point b 重構減法 bool operator point p intsgn int d 判斷d的符號函式,d大於0返回1,小於0返回...