OpenCV连通域面积的实现及示例代码是什么
Admin 2022-08-12 群英技术资讯 471 次浏览
对二值图进行分析,设定最大最小面积区间
保留该面积区间内的区域
//src为二值图,minArea、maxArea为面积阈值,dest为结果图像 void connectionAreaSelect(Mat src, int minArea, int maxArea, Mat &dest) { Mat labels, stats, centroids, img_color; //连通域计算 int nccomps = connectedComponentsWithStats( src, //二值图像 labels, stats, centroids ); //去除过小区域,初始化颜色表 vector<Vec3b> colors(nccomps); colors[0] = Vec3b(0, 0, 0); // background pixels remain black. for (int i = 1; i < nccomps; i++) { colors[i] = Vec3b(rand() % 256, rand() % 256, rand() % 256); //面积阈值筛选 int holeArea = stats.at<int>(i, CC_STAT_AREA); if ((holeArea < minArea) || (holeArea > maxArea)) { colors[i] = Vec3b(0, 0, 0); } } //按照label值,对不同的连通域进行着色 img_color = Mat::zeros(src.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<Vec3b>(y, x) = colors[label]; } } //统计降噪后的连通区域 Mat grayImg; cvtColor(img_color, grayImg, COLOR_BGR2GRAY); threshold(grayImg, grayImg, 1, 255, THRESH_BINARY); dest = grayImg.clone(); labels.release(); stats.release(); centroids.release(); img_color.release(); grayImg.release(); }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
Python内置函数-dict()函数。dict() 函数用于创建一个字典。
这篇文章主要介绍pytorch中常用损失函数以及用法,对新手学习pytorch中损失函数有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章能有所收获,接下来小编带着大家一起了解看看。
Python内置函数- __import__() 函数。__import__() 函数用于动态加载类和函数 。
Python网络编程实现TCP和UDP连接, 使用socket模块, 所有代码在python3下测试通过。实现TCP ! usr bin envpython3 -*-coding:utf-8-*-imp
这篇文章主要介绍了Django显示可视化图表的实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008