小編這次要給大家分享的是用實例分析opencv查找連通區(qū)域大面積,文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。
今天在弄一個查找連通的大面積的問題。
要把圖像弄成黑底,白字,這樣才可以正確找到。
然后調(diào)用下邊的方法:
RETR_CCOMP:提取所有輪廓,并將輪廓組織成雙層結(jié)構(gòu)(two-level hierarchy),頂層為連通域的外圍邊界,次層位內(nèi)層邊界
#include <opencv2/imgproc.hpp> #include <opencv2/highgui.hpp> using namespace cv; using namespace std; int main( int argc, char** argv ) { Mat src = imread( argv[1] ); int largest_area=0; int largest_contour_index=0; Rect bounding_rect; Mat thr; cvtColor( src, thr, COLOR_BGR2GRAY ); //Convert to gray threshold( thr, thr, 125, 255, THRESH_BINARY ); //Threshold the gray bitwise_not(thr,thr); //這里先變反轉(zhuǎn)顏色 vector<vector<Point> > contours; // Vector for storing contours findContours( thr, contours, RETR_CCOMP, CHAIN_APPROX_SIMPLE ); // Find the contours in the image for( size_t i = 0; i< contours.size(); i++ ) // iterate through each contour. { double area = contourArea( contours[i] ); // Find the area of contour if( area > largest_area ) { largest_area = area; largest_contour_index = i; //Store the index of largest contour bounding_rect = boundingRect( contours[i] ); // Find the bounding rectangle for biggest contour } } drawContours( src, contours,largest_contour_index, Scalar( 0, 255, 0 ), 2 ); // Draw the largest contour using previously stored index. imshow( "result", src ); waitKey(); return 0; }
文章題目:用實例分析opencv查找連通區(qū)域最大面積-創(chuàng)新互聯(lián)
本文地址:http://aaarwkj.com/article0/ideoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗、做網(wǎng)站、建站公司、商城網(wǎng)站、微信公眾號、網(wǎng)站設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容