今天在弄一个查找连通的最大面积的问题。
要把图像弄成黑底,白字,这样才可以正确找到。
然后调用下边的方法:
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.
{
double area = contourArea( contours[i] ); // Fi

本文介绍如何使用OpenCV找到图像中的最大连通域并计算其面积。通过`findContours`和`connectedComponentsWithStats`两种方法实现,重点展示了如何获取最大面积的连通区域,并对这两种方法的运行速度进行了比较。
最低0.47元/天 解锁文章
1236

被折叠的 条评论
为什么被折叠?



