前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >opencv 查找连通区域 最大面积实例

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

作者头像
砸漏
发布2020-10-29 16:50:19
1.5K0
发布2020-10-29 16:50:19
举报
文章被收录于专栏:恩蓝脚本

今天在弄一个查找连通的最大面积的问题。

要把图像弄成黑底,白字,这样才可以正确找到。

然后调用下边的方法:

RETR_CCOMP:提取所有轮廓,并将轮廓组织成双层结构(two-level hierarchy),顶层为连通域的外围边界,次层位内层边界

代码语言:javascript
复制
#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); //这里先变反转颜色
 
  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;
}

方法二: connectedComponentsWithStats

代码语言:javascript
复制
std::pair< int , int   MaxAreaFromSource(Mat srcImage, Mat &dstImage, int index)
{
/*
vector<vector<cv::Point    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;
//连通域计算
int nccomps = cv::connectedComponentsWithStats (
srcImage, //二值图像
labels,   //和原图一样大的标记图
stats, //nccomps×5的矩阵 表示每个连通区域的外接矩形和面积(pixel)
centroids //nccomps×2的矩阵 表示每个连通区域的质心
);
//cv::imshow("labels", labels);
//cv::waitKey();
vector<cv::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<int (label,cv::CC_STAT_LEFT) << std::endl;
std::cout << "CC_STAT_TOP  = " << stats.at<int (label,cv::CC_STAT_TOP) << std::endl;
std::cout << "CC_STAT_WIDTH = " << stats.at<int (label,cv::CC_STAT_WIDTH) << std::endl;
std::cout << "CC_STAT_HEIGHT = " << stats.at<int (label,cv::CC_STAT_HEIGHT) << std::endl;
std::cout << "CC_STAT_AREA  = " << stats.at<int (label,cv::CC_STAT_AREA) << std::endl;
std::cout << "CENTER  = (" << centroids.at<double (label, 0) <<","<< centroids.at<double (label, 1) << ")"<< std::endl << std::endl;
int area = stats.at<int (label,cv::CC_STAT_AREA);
int left = stats.at<int (label,cv::CC_STAT_LEFT);
int top = stats.at<int (label,cv::CC_STAT_TOP);
int width = stats.at<int (label,cv::CC_STAT_WIDTH);
int height = stats.at<int (label,cv::CC_STAT_HEIGHT);
vec_area.push_back(area);
vec_width.push_back(width);
vec_height.push_back(height);
}
vector<int ::iterator bigwidth = std::max_element(std::begin(vec_width), std::end(vec_width));
vector<int ::iterator bigheight = std::max_element(std::begin(vec_height), std::end(vec_height));
vector<int ::iterator bigarea = std::max_element(std::begin(vec_area), std::end(vec_area));
//printf( "area:%d------------width:%d height:%d \n", *bigarea, *bigwidth, *bigheight );
//按照label值,对不同的连通域进行着色
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<int (y, x);
CV_Assert(0 <= label && label <= nccomps);
img_color.at<cv::Vec3b (y, x) = colors[label];
}
cv::imshow("color", img_color);
cv::waitKey();
return make_pair( *bigarea , index );
}

我先用这个函数实现了一下,效果正确,还是opencv demo 是正确的,网上找了个例子,害死我了。

说明一下:方法一 比 第二种方法 运行速度快很多哦! 这一点很重要。

以上这篇opencv 查找连通区域 最大面积实例就是小编分享给大家的全部内容了,希望能给大家一个参考。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档