• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

使用OpenCV去除面积较小的连通域

python 搞代码 4年前 (2022-01-07) 19次浏览 已收录 0个评论

这篇文章主要介绍了使用OpenCV去除面积较小的连通域,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

这是后期补充的部分,和前期的代码不太一样

效果图

源代码

 //测试 void CCutImageVS2013Dlg::OnBnClickedTestButton1() { vector<vector > contours;  //轮廓数组 vector centers;    //轮廓质心坐标 vector<vector >::iterator itr; //轮廓迭代器 vector::iterator itrc;  //质心坐标迭代器 vector<vector > con;   //当前轮廓 double area; double minarea = 1000; double maxarea = 0; Moments mom;       // 轮廓矩 Mat image, gray, edge, dst; image = imread("D:\\66.png-600"); cvtColor(image, gray, COLOR_BGR2GRAY); Mat rgbImg(gray.size(), CV_8UC3); //创建三通道图 blur(gray, edge, Size(3, 3));       //模糊去噪 threshold(edge, edge, 200, 255, THRESH_BINARY_INV); //二值化处理,黑底白字 //--------去除较小轮廓,并寻找最大轮廓-------------------------- findContours(edge, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); //寻找轮廓 itr = contours.begin();    //使用迭代器去除噪声轮廓 while (itr != contours.end()) { area = contourArea(*itr);  //获得轮廓面积 if (areamaxarea)    //寻找最大轮廓 { maxarea = area; } } dst = Mat::zeros(image.rows, image.cols, CV_8UC3); /*绘制连通区域轮廓,计算质心坐标*/ Point2d center; itr = contours.begin(); while (itr != contours.end()) { area = contourArea(*itr); con.push_back(*itr);   //获取当前轮廓 if (area == maxarea) { vector boundRect(1); //定义外接矩形集合 boundRect[0] = boundingRect(Mat(*itr)); cvtColor(gray, rgbImg, COLOR_GRAY2BGR); Rect select; select.x = boundRect[0].x; select.y = boundRect[0].y; select.width = boundRect[0].width; select.height = boundRect[0].height; rectangle(rgbImg, select, Scalar(0, 255, 0), 3, 2); //用矩形画矩形窗 drawContours(dst, con, -1, Scalar(0, 0, 255), 2); //最大面积红色绘制 } else drawContours(dst, con, -1, Scalar(255, 0, 0), 2); //其它面积蓝色绘制 con.pop_back(); //计算质心 mom = moments(*itr); center.x = (int)(mom.m10 / mom.m00); center.y = (int)(mom.m01 / mom.m00); centers.push_back(center); itr++; } imshow("rgbImg", rgbImg); //imshow("gray", gray); //imshow("edge", edge); imshow("origin", image); imshow("connected_region", dst); waitKey(0); return; } 

前期做的,方法可能不太一样

一,先看效果图

原图

处理前后图

二,实现源代码

 //=======函数实现===================================================================== void RemoveSmallRegion(Mat &Src, Mat &Dst, int AreaLimit, int CheckMode, int NeihborMode) { int RemoveCount = 0; //新建一幅标签图像初始化为0像素点,为了记录每个像素点检验状态的标签,0代表未检查,1代表正在检查,2代表检查不合格(需要反转颜色),3代表检查合格或不需检查 //初始化的图像全部为0,未检查 Mat PointLabel = Mat::zeros(Src.size(), CV_8UC1); if (CheckMode == 1)//去除小连通区域的白色点 { //cout << "去除小连通域."; for (int i = 0; i <Src.rows; i++) { for (int j = 0; j <Src.cols; j++) { if (Src.at(i, j) <10) { PointLabel.at(i, j) = 3;//将背景黑色点标记为合格,像素为3 } } } } else//去除孔洞,黑色点像素 { //cout << "去除孔洞"; for (int i = 0; i <Src.rows; i++) { for (int j = 0; j <Src.cols; j++) { if (Src.at(i, j) > 10) { PointLabel.at(i, j) = 3;//如果原图是白色区域,标记为合格,像素为3 } } } } vectorNeihborPos;//将邻域压进容器 NeihborPos.push_back(Point2i(-1, 0)); NeihborPos.push_back(Point2i(1, 0)); NeihborPos.push_back(Point2i(0, -1)); NeihborPos.push_back(Point2i(0, 1)); if (NeihborMode == 1) { //cout << "Neighbor mode: 8邻域." << endl; NeihborPos.push_back(Point2i(-1, -1)); NeihborPos.push_back(Point2i(-1, 1)); NeihborPos.push_back(Point2i(1, -1)); NeihborPos.push_back(Point2i(1, 1)); } else int a = 0;//cout << "Neighbor mode: 4邻域." << endl; int NeihborCount = 4 + 4 * NeihborMode; int CurrX = 0, CurrY = 0; //开始检测 for (int i = 0; i <Src.rows; i++) { for (int j = 0; j <Src.cols; j++) { if (PointLabel.at(i, j) == 0)//标签图像像素点为0,表示还未检查的不合格点 { //开始检查 vectorGrowBuffer;//记录检查像素点的个数 GrowBuffer.push_back(Point2i(j, i)); PointLabel.at(i, j) = 1;//标记为正在检查 int CheckResult = 0; for (int z = 0; z <GrowBuffer.size(); z++) { for (int q = 0; q = 0 && CurrX= 0 && CurrY<Src.rows) //防止越界 { if (PointLabel.at(CurrY, CurrX) == 0) { GrowBuffer.push_back(Point2i(CurrX, CurrY)); //邻域点加入buffer PointLabel.at(CurrY, CurrX) = 1;   //更新邻域点的检查标签,避免重复检查 } } } } if (GrowBuffer.size()>AreaLimit) //判断结果(是否超出限定的大小),1为未超出,2为超出 CheckResult = 2; else { CheckResult = 1; RemoveCount++;//记录有多少区域被去除 } for (int z = 0; z <GrowBuffer.size(); z++) { CurrX = GrowBuffer.at(z).x; CurrY =<mark style="color:transparent">来源gaodaimacom搞#代%码网</mark> GrowBuffer.at(z).y; PointLabel.at(CurrY, CurrX) += CheckResult;//标记不合格的像素点,像素值为2 } //********结束该点处的检查********** } } } CheckMode = 255 * (1 - CheckMode); //开始反转面积过小的区域 for (int i = 0; i <Src.rows; ++i) { for (int j = 0; j <Src.cols; ++j) { if (PointLabel.at(i, j) == 2) { Dst.at(i, j) = CheckMode; } else if (PointLabel.at(i, j) == 3) { Dst.at(i, j) = Src.at(i, j); } } } //cout << RemoveCount << " objects removed." << endl; } //=======函数实现===================================================================== //=======调用函数===================================================================== Mat img; img = imread("D:\\1_1.jpg-600", 0);//读取图片 threshold(img, img, 128, 255, CV_THRESH_BINARY_INV); imshow("去除前", img); Mat img1; RemoveSmallRegion(img, img, 200, 0, 1); imshow("去除后", img); waitKey(0); //=======调用函数===================================================================== 

以上这篇使用OpenCV去除面积较小的连通域就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持gaodaima搞代码网

以上就是使用OpenCV去除面积较小的连通域的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:使用OpenCV去除面积较小的连通域

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址