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

本文介绍如何使用OpenCV找到图像中的最大连通域并计算其面积。通过`findContours`和`connectedComponentsWithStats`两种方法实现,重点展示了如何获取最大面积的连通区域,并对这两种方法的运行速度进行了比较。
摘要由CSDN通过智能技术生成

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

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

然后调用下边的方法:

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

#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); //这里先变反转颜色

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值