[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鍑芥暟浣跨敤的主要内容,如果未能解决你的问题,请参考以下文章

PHP鍐呯疆鐨勫瓧绗︿覆澶勭悊鍑芥暟

LN鍑芥暟璁$畻鎸囧畾鏁板€肩殑鑷劧瀵规暟

鍗侊細楂橀樁鍑芥暟鍜岄€掑綊鍑芥暟

python閫掑綊鍑芥暟