日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

opencv 最大连通域_opencv 查找连通区域 最大面积实例

發(fā)布時(shí)間:2023/12/10 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 opencv 最大连通域_opencv 查找连通区域 最大面积实例 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

今天在弄一個(gè)查找連通的最大面積的問(wèn)題。

要把圖像弄成黑底,白字,這樣才可以正確找到。

然后調(diào)用下邊的方法:

RETR_CCOMP:提取所有輪廓,并將輪廓組織成雙層結(jié)構(gòu)(two-level hierarchy),頂層為連通域的外圍邊界,次層位內(nèi)層邊界

#include

#include

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 > 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;

}

方法二: connectedComponentsWithStats

std::pair< int , int > MaxAreaFromSource(Mat srcImage, Mat &dstImage, int index)

{

/*

vector > contours; // Vector for storing contours

int largest_area=0;

size_t largest_contour_index=0;

Rect bounding_rect;

findContours( srcImage, 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

}

}

Mat dst;

cvtColor(srcImage, dst, CV_GRAY2RGB);

drawContours( dst, contours,largest_contour_index, Scalar( 0, 255, 0 ), 2 ); // Draw the largest contour using previously stored index.

imshow( "result", dst );

waitKey();

printf("%%%%%%%%%%%max area:%d\n", largest_area);

return make_pair( largest_area, index);

*/

cv::Mat img_bool, labels, stats, centroids, img_color, img_gray;

//連通域計(jì)算

int nccomps = cv::connectedComponentsWithStats (

srcImage, //二值圖像

labels, //和原圖一樣大的標(biāo)記圖

stats, //nccomps×5的矩陣 表示每個(gè)連通區(qū)域的外接矩形和面積(pixel)

centroids //nccomps×2的矩陣 表示每個(gè)連通區(qū)域的質(zhì)心

);

//cv::imshow("labels", labels);

//cv::waitKey();

vector<:vec3b> colors(nccomps);

colors[0] = cv::Vec3b(0,0,0); // background pixels remain black.

printf( "index:%d==================\n",index );

vector< int >vec_width,vec_area,vec_height;

for(int label = 1; label < nccomps; ++label)

{

colors[label] = cv::Vec3b( (std::rand()&255), (std::rand()&255), (std::rand()&255) );

std::cout << "Component "<< label << std::endl;

std::cout << "CC_STAT_LEFT = " << stats.at(label,cv::CC_STAT_LEFT) << std::endl;

std::cout << "CC_STAT_TOP = " << stats.at(label,cv::CC_STAT_TOP) << std::endl;

std::cout << "CC_STAT_WIDTH = " << stats.at(label,cv::CC_STAT_WIDTH) << std::endl;

std::cout << "CC_STAT_HEIGHT = " << stats.at(label,cv::CC_STAT_HEIGHT) << std::endl;

std::cout << "CC_STAT_AREA = " << stats.at(label,cv::CC_STAT_AREA) << std::endl;

std::cout << "CENTER = (" << centroids.at(label, 0) <(label, 1) << ")"<< std::endl << std::endl;

int area = stats.at(label,cv::CC_STAT_AREA);

int left = stats.at(label,cv::CC_STAT_LEFT);

int top = stats.at(label,cv::CC_STAT_TOP);

int width = stats.at(label,cv::CC_STAT_WIDTH);

int height = stats.at(label,cv::CC_STAT_HEIGHT);

vec_area.push_back(area);

vec_width.push_back(width);

vec_height.push_back(height);

}

vector::iterator bigwidth = std::max_element(std::begin(vec_width), std::end(vec_width));

vector::iterator bigheight = std::max_element(std::begin(vec_height), std::end(vec_height));

vector::iterator bigarea = std::max_element(std::begin(vec_area), std::end(vec_area));

//printf( "area:%d------------width:%d height:%d \n", *bigarea, *bigwidth, *bigheight );

//按照l(shuí)abel值,對(duì)不同的連通域進(jìn)行著色

img_color = cv::Mat::zeros(srcImage.size(), CV_8UC3);

for( int y = 0; y < img_color.rows; y++ )

for( int x = 0; x < img_color.cols; x++ )

{

int label = labels.at(y, x);

CV_Assert(0 <= label && label <= nccomps);

img_color.at<:vec3b>(y, x) = colors[label];

}

cv::imshow("color", img_color);

cv::waitKey();

return make_pair( *bigarea , index );

}

我先用這個(gè)函數(shù)實(shí)現(xiàn)了一下,效果正確,還是opencv demo 是正確的,網(wǎng)上找了個(gè)例子,害死我了。

說(shuō)明一下:方法一 比 第二種方法 運(yùn)行速度快很多哦! 這一點(diǎn)很重要。

以上這篇opencv 查找連通區(qū)域 最大面積實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持隨便開(kāi)發(fā)網(wǎng)。

總結(jié)

以上是生活随笔為你收集整理的opencv 最大连通域_opencv 查找连通区域 最大面积实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。