[opencv]findcoutours鍑芥暟浣跨敤
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[opencv]findcoutours鍑芥暟浣跨敤相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/nal' title='nal'>nal c++ 鏂规硶 绛夌骇 binary 璁剧疆 鐩寸嚎 arc
杞粨鏄畾涔夋垨闄愬畾褰㈢姸鎴栧璞$殑杈规垨绾匡紝鏄満鍣ㄨ瑙変腑鐨勫父鐢ㄧ殑姒傚康锛屽鐢ㄤ簬鐩爣妫€娴嬨€佽瘑鍒瓑浠诲姟銆?/p>
鍏充簬OpenCV杞粨鎿嶄綔锛屽挨鍏舵槸绾у埆鍙婂浣曚娇鐢ㄨ疆寤撶骇鍒繘琛岀瓫閫夌瓑闂锛岀浉鍏虫枃绔犳瘮杈冨皯锛屾濂芥渶杩戠敤鍒帮紝鍥犳灏嗗叾鎬荤粨鎴愭枃銆?/p>
鏈枃涓昏浠嬬粛OpenCV鐨勬煡鎵捐疆寤撳嚱鏁癴indContours()缁樺埗鍑芥暟drawContours()锛屽強鍏惰疆寤撶骇鍒弬鏁癶ierarchy锛屾秹鍙婂埌棰勫鐞嗐€佽疆寤撶瓫閫夌瓑鍐呭锛屽苟鎻愪緵鍏ㄩ儴婧愪唬鐮侊紝甯屾湜鑳藉府鍔╁ぇ瀹剁悊瑙e熀鏈蹇靛苟鑳藉€熼壌绀轰緥浠g爜缂栧啓鑷繁鐨勭畻娉曘€?/p>
鏈枃浠g爜锛欳++
鏈枃鍖呮嫭濡備笅鍐呭锛?/p>
鍩烘湰姒傚康
1.鏌ユ壘鍜岀粯鍒惰疆寤撳嚱鏁癴indContous(),drawContours()
2.杞粨鍙傛暟锛氳疆寤撶骇鍒€佽疆寤撻暱搴?/p>
3.杞粨鐨勫舰鐘舵弿杩板瓙锛氭渶灏忚鐩栫煩褰€佸渾銆佸杈瑰舰閫艰繎銆佸嚫鍖?/p>
缂栫▼瀹炴垬
1.濡備綍绛涢€夎疆寤擄細鎸夎疆寤撶骇鍒拰闀垮害绛涢€?/p>
2.濡備綍缁樺埗杞粨鐨勫鎺ュ舰鐘?/p>
3.濡備綍鑾峰彇杞粨鐨勯噸蹇冨潗鏍囧苟鏍囨敞
鏈枃鐨勭洰鏍囷細
1.浠庡師濮嬪浘鍍忎腑鎵惧埌2鏋跺彲鍥炴敹鐏
2.鏍囨敞鐩爣鐨勪綅缃笌閲嶅績鍧愭爣
闃呰瀹屾垚鍚庯紝灏嗚兘浠庡師濮嬪浘鍍忎腑鎵惧埌2鏋剁伀绠紝骞舵爣娉ㄥ叾浣嶇疆涓庡潗鏍囥€傚涓嬪浘鎵€绀猴細
鐩綍
OpenCV杞粨鎿嶄綔涓€绔欏紡璇﹁В锛氭煡鎵?绛涢€?缁樺埗/褰㈢姸鎻忚堪涓庢爣娉?/p>
1.鏌ユ壘銆佺粯鍒惰疆寤撳嚱鏁?/p>
findContours()
drawContours()
2.棰勫鐞?/p>
3.鏌ユ壘杞粨
4.缁樺埗杞粨
5.绛涢€夎疆寤?/p>
5.1 hierarchy杞粨绾у埆璇﹁В
contours涓巋ierarchy鐨勫叧绯?/p>
浠€涔堟槸灞傛缁撴瀯hierarchy锛?/p>
5.2 OpenCV涓殑灞傛缁撴瀯琛ㄧず
Next
Previous
First_Child
Parent
5.3 鎸塰ierarchy绛涢€夎疆寤?/p>
5.4 鎸夐暱搴︾瓫閫夎疆寤?/p>
6.鑱旈€氬煙鍒嗘瀽
7.鏍囨敞杞粨閲嶅績
1.鏌ユ壘銆佺粯鍒惰疆寤撳嚱鏁?/p>
findContours() void cv::findContours ( InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset = Point() )
鍑芥暟鍙傛暟锛?/p>
image
杈撳叆锛氭簮鍥惧儚锛屼竴涓?浣嶅崟閫氶亾鍥惧儚锛屾敞鎰忎竴瀹氭槸CV_8UC1鐨勫崟閫氶亾鍥惧儚锛屽惁鍒欐姤閿欍€?闈為浂鍍忕礌琚涓?銆?闆跺儚绱犱繚鎸佷负0锛屽洜姝ゅ浘鍍忚瑙嗕负浜岃繘鍒躲€傚彲浠ヤ娇鐢╟ompare锛宨nRange锛宼hreshold锛宎daptiveThreshold锛孋anny绛夋潵浠庣伆搴︽垨褰╄壊鍥惧儚涓垱寤轰簩杩涘埗鍥惧儚銆傚鏋渕ode涓篟ETR_CCOMP鎴朢ETR_FLOODFILL锛屽垯杈撳叆涔熷彲浠ユ槸鏍囩鐨?2浣嶆暣鏁板浘鍍忥紙CV_32SC1锛夈€?/p>
contours
杈撳嚭锛氭娴嬪埌鐨勮疆寤撱€傛瘡涓疆寤撻兘瀛樺偍涓虹偣鍚戦噺锛堜緥濡俿td :: vector <std :: vector <cv :: Point >>锛夈€傚嵆鐢辫嫢骞蹭釜cv::Point绫诲瀷鐨勭偣缁勬垚浜嗗崟涓疆寤搒td :: vector <cv :: Point >锛屽啀鐢辫嫢骞蹭釜杞粨缁勬垚杈撳叆鍥惧儚涓殑鍏ㄩ儴杞粨std::vector<std :: vector <cv :: Point >>
hierarchy
杈撳嚭锛氳疆寤撶骇鍒俊鎭€侶ierarchy涓哄彲閫夎緭鍑哄彉閲忥紝鏄痵td::vector<cv::Vec4i>绫诲瀷鐨勫悜閲忥紙姣忎釜鍏冪礌閮芥槸涓€涓?涓猧nt鍊兼瀯鎴愮殑鍚戦噺锛夈€傚寘鍚湁鍏冲浘鍍忔嫇鎵戠殑淇℃伅銆傚畠鍏锋湁涓庤疆寤撴暟閲忎竴鏍峰鐨勫厓绱犮€備緥濡傦紝绗琲涓疆寤擄紝 hierarchy[i][0]锛宧ierarchy[i][1]锛宧ierarchy[i][2]鍜宧ierarchy[i][3]渚濇涓猴細绗琲涓疆寤撶殑[Next, Previous, First_Child, Parent]锛屽嵆杞粨i鐩稿悓绛夌骇鐨勪笅涓€杞粨銆佸墠涓€杞粨锛岀涓€涓瓙杞粨鍜岀埗杞粨锛堜笂涓€绾ц疆寤擄級鐨勭储寮曞彿锛堝嵆contours鍚戦噺涓殑杞粨搴忓彿锛夈€傚鏋滆疆寤搃娌℃湁涓嬩竴涓紝鍓嶄竴涓紝鐖剁骇鎴栧祵濂楄疆寤擄紝鍒欏眰娆$粨鏋刐i]鐨勭浉搴斿厓绱犲皢涓鸿礋鏁般€傝繖涓弬鏁版垜浠皢鍦ㄤ笅鏂囦腑閲嶇偣浠嬬粛銆?br />mode 杈撳叆锛氳疆寤撴绱㈡ā寮? 璇﹁ RetrievalModes
method 杈撳叆锛氳疆寤撹繎浼兼硶, 璇﹁ContourApproximationModes
offset 杈撳叆锛氭瘡涓疆寤撶偣绉诲姩鐨勫亸绉婚噺锛屽彲閫夊弬鏁帮紝cv::Point()绫诲瀷銆傚鏋滀粠鏁村箙鍥惧儚鐨勬煇涓猂OI涓彁鍙栬疆寤擄紝鐒跺悗鍙堝湪鏁翠釜鍥惧儚涓垎鏋愯疆寤擄紙灏哛OI涓殑杞粨鍧愭爣鎭㈠鍒版暣骞呭浘鍍忎腑鐨勫潗鏍囷級锛岃繖涓亸绉婚噺闈炲父鏈夌敤锛屽彲浠ュ厤鍘绘垜浠嚜宸卞啓浠g爜杞崲鍧愭爣绯荤殑楹荤儲銆?br />mode鍙傛暟锛?/p>
RETR_EXTERNAL
Python: cv.RETR_EXTERNAL
浠呮绱㈡瀬绔杞粨銆?瀹冧负鎵€鏈夎疆寤撹缃甴ierarchy [i][2] = hierarchy [i][3] = - 1銆?/p>
RETR_LIST
Python: cv.RETR_LIST
妫€绱㈡墍鏈夎疆寤撹€屼笉寤虹珛浠讳綍灞傛鍏崇郴銆?/p>
RETR_CCOMP
Python: cv.RETR_CCOMP
妫€绱㈡墍鏈夎疆寤撳苟灏嗗畠浠粍缁囨垚涓ょ骇灞傛缁撴瀯銆傚湪椤跺眰杞粨鏄閮ㄨ疆寤撱€傚湪绗簩灞傝疆寤撴槸“娲?rdquo;鐨勮疆寤撱€傚鏋滆繛鎺ョ粍浠剁殑娲炲唴鏈夊彟涓€涓疆寤擄紝瀹冪殑绾у埆浠嶇劧璁ゅ畾涓洪《灞傘€?/p>
RETR_TREE
Python: cv.RETR_TREE
妫€绱㈡墍鏈夎疆寤撳苟閲嶅缓宓屽杞粨鐨勫畬鏁村眰娆$粨鏋勩€?/p>
RETR_FLOODFILL
Python: cv.RETR_FLOODFILL
drawContours()
缁樺埗杞粨杞粨鎴栧~鍏呰疆寤撱€?/p>
void cv::drawContours ( InputOutputArray image, InputArrayOfArrays contours, int contourIdx, const Scalar & color, int thickness = 1, int lineType = LINE_8, InputArray hierarchy = noArray(), int maxLevel = INT_MAX, Point offset = Point() )
鍑芥暟鍙傛暟锛?/p>
image
杈撳叆锛氭簮鍥惧儚銆傚崟閫氶亾鎴?閫氶亾鍥惧儚銆?br />contours
杈撳叆锛氬緟缁樺埗鐨勮疆寤撱€俿td :: vector <std :: vector <cv :: Point >>绫诲瀷銆?br />contourIdx 杈撳叆锛氬緟缁樺埗鐨勮疆寤撳簭鍙枫€備緥濡傦細0涓虹粯鍒剁1涓疆寤揷ontours[0]锛?涓虹粯鍒剁2涓疆寤揷ontours[1]锛屼緷娆$被鎺紱-1涓虹粯鍒舵墍鏈夎疆寤撱€?br />color 杈撳叆锛氳疆寤撻鑹层€俢v::Scalar鍙橀噺锛屼緥濡傦細cv::Scalar(0,0,255)涓虹孩鑹茶疆寤擄紝cv::Scalar::all(0)涓洪粦鑹茶疆寤?br />thickness 杈撳叆锛氳疆寤撶矖缁嗐€俰nt鍨嬪彉閲忥紝榛樿涓?锛屽€艰秺澶ц秺绮?br />lineType 杈撳叆锛氱粯鍒惰疆寤撶殑绾垮瀷銆傞粯璁INE_8锛?鑱旈€氱嚎鍨嬶紙涓嬩竴涓偣杩炴帴涓婁竴涓偣鐨勮竟鎴栬锛夈€?br />hierarchy
杈撳叆锛氬緟缁樺埗鐨勮疆寤撶骇鍒€俿td::vector<cv::Vec4i>绫诲瀷鐨勫悜閲忥紙姣忎釜鍏冪礌閮芥槸涓€涓?涓猧nt鍊兼瀯鎴愮殑鍚戦噺锛夈€備笅涓€杞粨銆佸墠涓€杞粨锛岀涓€涓瓙杞粨鍜岀埗杞粨锛堜笂涓€绾ц疆寤擄級鐨勭储寮曞彿銆?br />maxLevel 杈撳叆锛氬緟缁樺埗鐨勮疆寤撴渶澶х骇鍒€?br />method 杈撳叆锛氳疆寤撹繎浼兼硶, 璇﹁ContourApproximationModes
offset 杈撳叆锛氭瘡涓疆寤撶偣绉诲姩鐨勫亸绉婚噺锛屽彲閫夊弬鏁般€?br />2.棰勫鐞?br />棰勫鐞嗙洰鐨勬槸涓鸿疆寤撴煡鎵炬彁渚涢珮璐ㄩ噺鐨勮緭鍏ユ簮鍥惧儚銆?/p>
棰勫鐞嗙殑涓昏姝ラ鍖呮嫭锛?/p>
鐏板害鍖栵細浣跨敤cv::cvtColor()
鍥惧儚鍘诲櫔锛氫娇鐢ㄩ珮鏂护娉v::Gaussian()
浜屽€煎寲锛氫娇鐢╟v::Threshold()
褰㈡€佸澶勭悊锛歝v::morphologyEx()
鍏朵腑鐏板害鍖栧彲浠ュ皢3閫氶亾鍥惧儚杞寲涓哄崟閫氶亾鍥惧儚锛屼互渚胯繘琛屼簩鍊煎寲闂ㄩ檺鍒嗗壊锛涘幓鍣彲浠ユ湁鏁堝墧闄ゅ浘鍍忎腑鐨勫紓甯哥嫭绔嬪櫔鐐癸紱浜屽€煎寲鏄负杞粨鏌ユ壘鍑芥暟鎻愪緵鍗曢€氶亾鍥惧儚锛涘舰鎬佸鐨勬煇浜涘鐞嗛€氬父鍙互鍓旈櫎缁嗗皬杞粨锛岃仈閫氭柇瑁傜殑杞粨銆?/p>
璇诲彇鍥惧儚浠g爜濡備笅锛?/p>
// 1.杞藉叆鍥惧儚
cv::Mat image = cv::imread("spaceX2.jpg",1);
cv::imshow("original", image);
cv::waitKey();
// 2.棰勫鐞?/p>
cv::Mat gray, binary, element; // 涓存椂鍙橀噺 cv::cvtColor(image, gray, CV_BGR2GRAY); cv::imshow("gray", gray); GaussianBlur(gray, gray, Size(3, 3), 1); cv::threshold(gray, binary, 80, 255, CV_THRESH_BINARY_INV); cv::imshow("binary", binary); element = getStructuringElement(MORPH_RECT, Size(3, 3));//3*3鍏?缁撴瀯鍏冪礌 cv::morphologyEx(binary, binary, cv::MORPH_CLOSE, element); cv::imshow("morphology", binary); cv::waitKey();
浣跨敤findContours鍑芥暟鏌ユ壘杞粨
// 3.鏌ユ壘杞粨
std::vector<std::vector<cv::Point>> contours; std::vector<cv::Vec4i> hierarchy; cv::findContours( binary, // 杈撳叆浜屽€煎浘 contours, // 瀛樺偍杞粨鐨勫悜閲?/span> hierarchy, // 杞粨灞傛淇℃伅 RETR_TREE, // 妫€绱㈡墍鏈夎疆寤撳苟閲嶅缓宓屽杞粨鐨勫畬鏁村眰娆$粨鏋?/span> CHAIN_APPROX_NONE); // 姣忎釜杞粨鐨勫叏閮ㄥ儚绱?/span> printf("find %d contours", contours.size());
4.缁樺埗杞粨
涓轰簡鏂逛究鏌ョ湅杞粨鏌ユ壘缁撴灉锛屼娇鐢╠rawContours()鏉ョ粯鍒惰疆寤撳苟鏄剧ず銆傜紪鍐欏嚱鏁癲rawMyContours()鍑芥暟鐢ㄤ簬鍦ㄧ櫧鑹茶儗鏅腑鎴栬€呭師鍥句笂鏌ョ湅杞粨銆?/p>
鍑芥暟鍙傛暟锛?/p>
绐楀彛鍚嶅瓧锛?br />鍘熷鍥惧儚浠ュ強杞粨鍙橀噺
鐧借壊鑳屾櫙涓婅繕鏄湪鍘熷浘涓婄粯鍒惰疆寤撶殑鏍囧織浣?br />// 4. 缁樺埗杞粨鍑芥暟
void drawMyContours(string winName, Mat &image, std::vector<std::vector<cv::Point>> contours, bool draw_on_blank) cv::Mat temp; if (draw_on_blank) // 鍦ㄧ櫧搴曚笂缁樺埗杞粨 temp = cv::Mat(image.size(), CV_8U, cv::Scalar(255)); cv::drawContours( temp, contours, -1,//鐢诲叏閮ㄨ疆寤?/span> 0, //鐢ㄩ粦鑹茬敾 2);//瀹藉害涓? else // 鍦ㄥ師鍥句笂缁樺埗杞粨 temp = image.clone(); cv::drawContours( temp, contours, -1,//鐢诲叏閮ㄨ疆寤?/span> cv::Scalar(0,0,255), //鐢╮ed鐢?/span> 1);//瀹藉害涓? 銆€銆€cv::imshow(winName, temp); 銆€銆€cv::waitKey();
鍦╩ain鍑芥暟涓皟鐢╠rawMyContours()濡備笅锛?/p>
// 4.缁樺埗鍘熷杞粨
drawMyContours("contours", image, contours, true);
棣栨鏌ユ壘鐨勮疆寤撳彉閲廲ontours涓湁16涓悜閲忥紝鍗虫壘鍒?6涓疆寤撱€?/p>
灞曞紑contours鍙橀噺锛屽彲浠ョ湅鍒版瘡涓厓绱犻兘鏄竴涓敱涓€绯诲垪杞粨涓婄殑鐐圭粍鎴愮殑鍚戦噺锛屽叾size灏辨槸姣忎釜杞粨鐨勯暱搴︺€?/p>
閫氳繃缁樺埗杞粨锛屽彲浠ョ湅鍒拌繖16涓疆寤擄紝闄や簡涓や釜鐩爣涔嬪锛岃繕鏈変簯銆佸湴闈㈣儗鏅互鍏朵腑鐨?ldquo;娲?rdquo;杞粨銆?/p>
5.绛涢€夎疆寤?br />鏌ユ壘鍒板ぇ杞粨鏄剧劧鏈夎澶氫笉绗﹀悎瑕佹眰锛屽洜姝ゅ彲浠ラ€氳繃鏌愪簺鍑嗗垯杩涜杞粨绛涢€夈€傞€氳繃瑙傚療锛屽彂鐜颁笂鍥句腑鏈夎澶氳疆寤撳寘鍚?ldquo;娲?rdquo;锛屽嵆瀛愯疆寤擄紝鑰岃繖浜涘瓙杞粨鏄剧劧涔熸湁鐖剁骇杞粨锛屽洜姝ゆ垜浠彲浠ヤ娇鐢╢indContours鐨刪ierarchy杞粨绾у埆鍙傛暟鍒犻櫎閭d簺鏈夊瓙杞粨涔熸湁鐖惰疆寤撶殑杞粨銆?/p>
5.1 hierarchy杞粨绾у埆璇﹁В
contours涓巋ierarchy鐨勫叧绯?br />浣跨敤findContours()鍑芥暟灏嗚繑鍥瀋ontours杞粨鍚戦噺浠ュ強瀵瑰簲鐨刪ierarchy杞粨绾у埆鍚戦噺锛堝彲閫夐」锛夈€備袱鑰呮湁鐩稿悓鐨勯暱搴﹀嵆contours.size() = hierarchy.size()锛屽苟涓斿悜閲忕殑搴忓彿琛ㄧず鎵惧埌鐨勮疆寤撶储寮曪紝涓斾竴涓€瀵瑰簲銆?/p>
浠€涔堟槸灞傛缁撴瀯hierarchy锛?br />閫氬父鎴戜滑浣跨敤findContours()鍑芥暟鏉ユ娴嬪浘鍍忎腑鐨勫璞°€傛湁鏃跺璞′綅浜庝笉鍚岀殑浣嶇疆銆備絾鍦ㄦ煇浜涙儏鍐典笅锛屾煇浜涘舰鐘跺湪鍏朵粬褰㈢姸鍐咃紙绫讳技宓屽锛夈€傚湪杩欑鎯呭喌涓嬶紝鎴戜滑灏嗗閮ㄨ疆寤撶О涓虹埗绾ц疆寤擄紝灏嗗唴閮ㄨ疆寤撶О涓哄瓙杞粨銆傝繖鏍凤紝鍥惧儚涓殑杞粨褰兼涔嬮棿瀛樺湪鏌愮鍏崇郴銆傚苟涓旀垜浠彲浠ユ寚瀹氫竴涓疆寤撳浣曠浉浜掕繛鎺ワ紝渚嬪锛屽畠鏄煇涓叾浠栬疆寤撶殑瀛愰」锛岃繕鏄埗椤圭瓑銆傛鍏崇郴鐨勮〃绀哄氨绉颁负灞傛缁撴瀯hierarchy銆?/p>
鑰冭檻涓嬮潰鐨勭ず渚嬪浘鐗囷細
鍦ㄤ笂鍥句腑锛屾湁涓€浜涘舰鐘讹紝鎴戜滑浠?-5缂栧彿杩?涓舰鐘躲€傚浘涓?鍜?a琛ㄧず鏈€澶栦晶鐭╁舰鐨勫閮ㄥ拰鍐呴儴杞粨銆?/p>
杞粨0,1,2鏄渶澶栭儴杞粨锛屼笁鑰呬负鍚屼竴绾у埆銆傛垜浠彲浠ヨ锛屽畠浠湪灞傛缁撴瀯0涓紝鎴栬€呭彧鏄畠浠浜庣浉鍚岀殑灞傛缁撴瀯绾у埆銆?/p>
鎺ヤ笅鏉ユ槸杞粨-2a鍙互琚涓烘槸杞粨-2鐨勫瓙杞粨锛堟垨鑰呯浉鍙嶏紝杞粨-2鏄疆寤?2a鐨勭埗绾ц疆寤擄級鎵€浠ヨ瀹冨湪灞傛缁撴瀯-1涓€?绫讳技鍦帮紝杞粨-3鏄疆寤?2a鐨勫瓙杞粨锛屽畠杩涘叆涓嬩竴灞傛銆?鏈€鍚庯紝杞粨4,5鏄疆寤?3a鐨勫瓙杞粨锛屽畠浠綅浜庢渶鍚庣殑灞傛缁撴瀯绾у埆銆?浠庣紪鍙锋鐨勬柟寮忥紝鍙互璇磋疆寤?4鏄疆寤?3a鐨勭涓€涓瓙杞粨锛屽綋鐒惰疆寤?5涔熸槸杞粨-3a鐨勫瓙杞粨銆?/p>
濡傛灉涓婇潰鐨勬弿杩扮湅鐫€澶存檿锛屼笉瑕佺揣锛屼竴寮€濮嬮兘杩欐牱銆?/p>
5.2 OpenCV涓殑灞傛缁撴瀯琛ㄧず
OpenCV涓瘡涓疆寤撻兘鏈夎嚜宸辩殑淇℃伅锛屽叧浜庡畠鏄粈涔堝眰娆$粨鏋勶紝璋佹槸瀹冪殑瀛愯疆寤擄紝璋佹槸瀹冪殑鐖惰疆寤撶瓑.OpenCV灏嗗畠琛ㄧず涓哄洓涓猧nt鍊肩殑鏁扮粍锛岀被鍨嬩负cv::Vec4i锛?涓猧nt鍊硷級锛?/p>
[Next锛孭revious锛孎irst_Child锛孭arent]
Next
Next琛ㄧず鍚屼竴绾у埆鐨勪笅涓€涓疆寤撶储寮曘€備緥濡傦紝鍦ㄦ垜浠殑鍥剧墖涓彇鍑鸿疆寤?0銆傚悓涓€姘村钩鐨勪笅涓€涓疆寤撴槸杞粨-1銆?鎵€浠ョ畝鍗曞湴璇碞ext = 1銆傜被浼煎湴锛屽浜庤疆寤?1锛宯ext鏄疆寤?2銆?鎵€浠ext = 2銆?/p>
杞粨-2鐨勫悓涓€绾у埆娌℃湁涓嬩竴涓疆寤擄紝鎵€浠ヨ疆寤?2鐨凬ext = -1銆傝疆寤?4鍛紵瀹冧笌杞粨-5澶勪簬鍚屼竴姘村钩銆傛墍浠ュ畠鐨勪笅涓€涓疆寤撴槸杞粨-5锛屾墍浠ヨ疆寤?4鐨凬ext = 5銆?/p>
Previous
Previous琛ㄧず鍚屼竴绾у埆鐨勪笂涓€涓疆寤撶储寮曘€備緥濡傦紝杞粨-1鐨勪笂涓€涓疆寤撳湪鍚屼竴绾у埆涓负杞粨-0銆?绫讳技鍦帮紝瀵逛簬杞粨-2锛屽畠鐨勪笂涓€涓疆寤撴槸杞粨-1銆傝€屽浜庤疆寤?0锛屾病鏈夊厛鍓嶇殑锛屾墍浠ユ妸瀹冪殑Previous = -1銆?/p>
First_Child
First_Child琛ㄧず褰撳墠杞粨鐨勭涓€涓瓙杞粨绱㈠紩銆備緥濡傦紝瀵逛簬杞粨-2锛屽瓙杞粨鏄疆寤?2a銆傚洜姝よ疆寤?2鐨凢irst_Child涓鸿疆寤?2a鐨勭浉搴旂储寮曞€笺€傝疆寤?3a鍛紵瀹冩湁涓や釜瀛愯疆寤撱€備絾hierarchy鍙傛暟鍙褰曠涓€涓瓙杞粨锛屽洜姝ゅ畠鏄疆寤?4鐨勭储寮曞€笺€傚洜姝わ紝瀵逛簬杞粨-3a锛孎irst_Child = 4銆?/p>
Parent
Parent琛ㄧず褰撳墠杞粨鐨勭埗杞粨绱㈠紩銆傚浜庤疆寤?4鍜岃疆寤?5锛屽畠浠殑鐖惰疆寤撻兘鏄疆寤?3a銆傚浜庤疆寤?3a锛屽畠鐨勭埗杞粨鏄疆寤?3锛屼緷姝ょ被鎺ㄣ€?/p>
娉ㄦ剰锛?/p>
Previous琛ㄧず鍚屼竴灞傜骇鐨勫墠涓€涓疆寤撶殑绱㈠紩锛?br />Parent琛ㄧず鍏剁埗杞粨鐨勭储寮曪紱
濡傛灉鏌愪釜杞粨娌℃湁瀛愯疆寤撻」鎴栫埗杞粨锛屽垯瀵瑰簲鐨勫瓧娈?-1.
5.3 鎸塰ierarchy绛涢€夎疆寤?br />鏈変簡涓婅堪瀵硅疆寤撳眰绾х殑鐞嗚В锛屼笅闈㈠氨鍙互鏍规嵁闇€瑕佺瓫閫夎疆寤撲簡銆備緥濡傛湰鏂囦换鍔℃槸鎵惧埌涓や釜鐏锛岃€岄娆℃煡鎵捐疆寤撴湁璁稿涓棿鏈夌┖娲炵殑杞粨涓嶇鍚堣姹傦紝涓嬮潰灏遍€氳繃閬嶅巻姣忎竴涓疆寤撶殑hierarchy绾у埆鍙傛暟鐨勭3绗?涓弬鏁版潵鎵惧埌閭d簺鏈夊瓙杞粨鎴栬€呮湁鐖惰疆寤撶殑杞粨锛屽苟鍒犻櫎涔嬨€傛敞鎰忓悜閲忚凯浠e櫒鐨勪娇鐢紝鍒犻櫎鍚庝細杩斿洖涓嬩竴涓悜閲忕殑鎸囬拡锛涙澶栵紝contours涓巋ierarchy鍏冪礌闇€瑕佸悓姝ュ垹闄ゅ拰骞堕€掑杩唬鍣紝浠ヤ繚鎸佺紪鍙峰搴斿叧绯伙紝鍚﹀垯浼氬垹閿欍€?/p>
// 5.绛涢€夎疆寤?br /> // 鍒濆鍖栬凯浠e櫒
std::vector<std::vector<cv::Point>>::iterator itc = contours.begin();
std::vector<cv::Vec4i>::iterator itc_hierarchy = hierarchy.begin();
// 5.1浣跨敤灞傜骇缁撴瀯绛涢€夎疆寤?/p>
int i = 0; while(itc_hierarchy != hierarchy.end()) 銆€銆€//楠岃瘉杞粨澶у皬 銆€銆€if (hierarchy[i][2] > 0 || hierarchy[i][3] > 0) // 瀛樺湪瀛愯疆寤?鐖惰疆寤?/span> 銆€銆€ 銆€銆€銆€銆€itc = contours.erase(itc); 銆€銆€銆€銆€itc_hierarchy = hierarchy.erase(itc_hierarchy); 銆€銆€ 銆€銆€else 銆€銆€ 銆€銆€銆€銆€++i; 銆€銆€銆€銆€++itc; 銆€銆€銆€銆€++itc_hierarchy; 銆€銆€ printf("%d contours remaining after hierarchy filtering", contours.size()); // 缁樺埗绾у埆绛涢€夊悗鐨勮疆寤?/span> drawMyContours("contours after hierarchy filtering", image, contours, true);
void Detect_Object(Mat img) Mat gray, binaryIMG; Mat correct_IMG; cvtColor(img, gray, CV_BGR2GRAY); Canny(gray, binaryIMG, 90, 180); blur(binaryIMG, binaryIMG, Size(3, 3)); // Find contours vector<vector<Point> > contours; vector<Vec4i> hierarchy; double TargetArea = 0; findContours(binaryIMG, contours, hierarchy, RETR_TREE, CV_CHAIN_APPROX_NONE, Point(0, 0)); for (int i = 0; i < contours.size(); i++) drawContours(img, contours, i, Scalar(0, 255, 0), 2, 8, hierarchy, 0, Point());
淇敼灞傜骇鍏崇郴
for (int i = 0; i < contours.size(); i++) if (hierarchy[i][3] == -1) drawContours(img, contours, i, Scalar(0, 0, 255), 1, 8, hierarchy, 0, Point());
hierarchy[i][3] == -1锛?杩欓噷鐨刪ierarchy[i]鎸囩殑鏄i涓疆寤撶殑闃跺眰鍏崇郴銆傝€宧ierarchy[i][0],hierarchy[i][1],hierarchy[i][2],hierarchy[i][3]鍒嗗埆鎸囩殑鏄疦ext, Previous, First_child, Parent銆?br />
鎴戜滑璁惧畾鐨刪ierarchy[i][3]== -1 鐨勬剰鎬濆氨鏄?“杞粨娌℃湁鐖舵瘝”锛?鍗?ldquo;杩欎釜杞粨娌℃湁涓婂眰闃剁骇鐨勮疆寤?rdquo;銆?br />
鍚岀悊锛?hierarchy[i][2]== -1 鐨勬剰鎬濆氨鏄?“姝よ疆寤撴病鏈夌涓€涓瀛?rdquo;锛屽嵆“姝よ疆寤撴病鏈変笅灞傞樁绾х殑杞粨”銆?br />
鑷充簬锛宧ierarchy[][0],hierarchy[][1] 鎸囩殑鏄杞粨鐨勫悗涓€涓疆寤擄紝鍜屽墠涓€涓疆寤撱€備粬浠兘鏄悓涓€涓樁绾х殑杞粨銆傝繖涓墠鍚庨『搴忓彲鑳芥槸寰堥殢鎰忕殑锛岃嚦浠婅繕娌℃壘鍒拌寰嬨€?br />
鎵€浠ワ紝hierarchy[i][3]== -1 鏉′欢涓嬬殑缁撴灉锛?鏄€変腑浜嗘病鏈夌埗姣嶇殑杞粨锛屽嵆浠栫殑澶栧洿娌℃湁鍖呭洿浠栫殑杞粨銆?br />
5.4 鎸夐暱搴︾瓫閫夎疆寤?br />灏界涓婁竴涓楠ゅ凡缁忓墧闄や簡鑳屾櫙杞粨锛屼絾浠嶇劧娈嬬暀杩欎竴浜涚粏灏忕殑杞粨銆備笅涓€姝ュ彲浠ヤ娇鐢ㄨ疆寤撻暱搴ontours[i].size()鏉ユ护闄よ繃灏忔垨杩囧ぇ鐨勮疆寤擄細
// 5.2浣跨敤杞粨闀垮害婊ゆ尝
int min_size = 20;
int max_size = 500;
// 閽堝鎵€鏈夎疆寤?br /> itc = contours.begin();
itc_hierarchy = hierarchy.begin();
while (itc != contours.end())
銆€銆€//楠岃瘉杞粨澶у皬
銆€銆€if (itc->size() < min_size || itc->size() > max_size)
銆€銆€
銆€銆€銆€銆€itc = contours.erase(itc);
銆€銆€銆€銆€itc_hierarchy = hierarchy.erase(itc_hierarchy);
銆€銆€
銆€銆€else
銆€銆€
銆€銆€銆€銆€++itc;
銆€銆€銆€銆€++itc_hierarchy;
銆€銆€
printf("%d contours remaining after length filtering", contours.size());
// 缁樺埗闀垮害绛涢€夊悗鐨勮疆寤?br /> drawMyContours("contours after length filtering", image, contours, true);
6.鑱旈€氬煙鍒嗘瀽
杩為€氬尯鍩熼€氬父浠h〃浜嗗満鏅腑鐨勬煇涓墿浣撱€備负浜嗚瘑鍒鐗╀綋锛屾垨灏嗗畠涓庡叾浠栧浘鍍忓厓绱犳瘮杈冿紝闇€瑕佸姝ゅ尯鍩熻繘琛屾祴閲忥紝浠ユ彁鍙栭儴鍒嗙壒寰併€傛湰鑺備粙缁峯pencv鐨勫舰鐘舵弿杩板瓙锛岀敤浜庢弿杩拌繛閫氬尯鍩熺殑褰㈢姸銆侽penCV涓敤浜庡舰鐘舵弿杩扮殑鍑芥暟鏈夊緢澶氥€傛垜浠妸鍏朵腑鍑犱釜鐢ㄥ埌涓婅妭鎻愬彇鍒扮殑鍖哄煙銆?/p>
锛?锛夌煩褰㈡cv::Rect r0 = cv::boundingRect()
鍦ㄨ〃绀哄拰瀹氫綅鍥惧儚涓殑鍖哄煙鏂规硶涓紝杈圭晫妗嗗彲鑳芥槸鏈€绠€娲佺殑銆傚畠鐨勫畾涔夋槸锛氳兘瀹屾暣鍖呭惈璇ュ舰鐘剁殑鏈€灏忓瀭鐩寸煩褰€傛瘮杈冭竟鐣屾鐨勯珮搴﹀拰瀹藉害锛屽彲浠ヨ幏寰楃墿浣撳湪鍨傜洿鍜屾按骞虫柟鍚戜笂鐨勮寖鍥淬€?/p>
锛?锛夋渶灏忚鐩栧渾cv::minEnclosingCircle()
鏈€灏忚鐩栧渾閫氬父鐢ㄥ湪鍙渶瑕佸尯鍩熷昂瀵稿拰浣嶇疆鐨勮繎浼煎€肩殑鎯呭喌銆?/p>
锛?锛夊杈瑰舰閫艰繎cv::approxPolyDP()
濡傛灉瑕佹洿绱у噾鍦拌〃绀哄尯鍩熺殑褰㈢姸锛屽彲浠ラ噰鐢ㄥ杈瑰舰閫艰繎銆傚湪鍒涘缓鏃堕渶瑕佽缃簿搴﹀弬鏁帮紝琛ㄧず褰㈢姸涓庡搴旂殑绠€鍖栧杈瑰舰涔嬮棿鑳芥帴鍙楃殑鏈€澶ц窛绂汇€傚畠鏄痗v::approxPolyDP(contours[1],poly,5,true)鍑芥暟鐨勭鍥涗釜鍙傛暟銆傝繑鍥炵粨鏋滄槸cv::Point绫诲瀷鐨勫悜閲忥紝琛ㄧず澶氳竟褰㈤《鐐逛釜鏁般€傚湪鐢昏繖涓杈瑰舰鏃讹紝瑕佽凯浠i亶鍘嗘暣涓悜閲忥紝骞跺湪椤剁偣涔嬮棿鐢荤洿绾匡紝鎶婂畠浠€愪釜杩炴帴璧锋潵銆?/p>
锛?锛夊嚫鍖卌v::convexHull()
鍑稿寘鏄寘鍚褰㈢姸鐨勬渶灏忓嚫澶氳竟褰€傚彲浠ユ妸瀹冪湅浣滀竴鏉$粫鍦ㄥ尯鍩熷懆鍥寸殑姗$毊绛嬨€傚湪褰㈢姸杞粨涓嚬杩涘幓鐨勪綅缃紝鍑稿寘杞粨浼氫笌鍘熷杞粨鍙戠敓鍋忕銆?/p>
涓嬮潰浣跨敤涓婅堪鍒嗘瀽鏂规硶涓殑鍑稿寘涓庢渶灏忚鐩栫煩褰袱绉嶆柟娉曞垎鍒2鏋剁伀绠殑鎻愬彇杞粨杩涜鍒嗘瀽銆?/p>
// 6.褰㈢姸鎻忚堪瀛?br /> // 鏈€灏忚鐩栫煩褰?br /> cv::Mat result =image.clone();
cv::Rect rect = cv::boundingRect(contours[0]);//杞粨1
cv::rectangle(result, rect, cv::Scalar(0,255,255), 1);//鐢荤煩褰?br /> // 鍑稿寘
std::vector<cv::Point> hull;
cv::convexHull(contours[1], hull);//杞粨2
cv::polylines(result, hull, true, cv::Scalar(0, 255, 0), 1);//鐢诲杈瑰舰
cv::imshow("bounding", result);
cv::waitKey();
寰楀埌鐨勭粨鏋滃涓嬪浘鎵€绀猴細杞粨1涓烘渶灏忚鐩栫煩褰紙榛勮壊绾挎潯锛夛紝杞粨2涓哄嚫鍖咃紙缁胯壊绾挎潯锛?/p>
7.鏍囨敞杞粨閲嶅績
缁堜簬鍒版渶鍚庝竴姝ヤ簡銆備笅闈㈠厛姹?涓疆寤撶殑閲嶅績锛岀劧鍚庝娇鐢╟v::Circle()涓巆v::putText()鍑芥暟灏嗛噸蹇冧綅缃笌鍧愭爣鏍囨敞鍒扮敾闈笂銆?/p>
// 7.璁$畻杞粨鐭╋紝鐢婚噸蹇?/p>
以上是关于[opencv]findcoutours鍑芥暟浣跨敤的主要内容,如果未能解决你的问题,请参考以下文章