OpenCV连通域怎么对数量统计,代码是什么
Admin 2022-08-12 群英技术资讯 815 次浏览
很多朋友都对“OpenCV连通域怎么对数量统计,代码是什么”的内容比较感兴趣,对此小编整理了相关的知识分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获,那么感兴趣的朋友就继续往下看吧!1.输入图像为分割结果图像
2.根据种子填充法思路,遍历图像,得到每个连通域外接矩形坐标信息、面积信息
/*
Input:
src: 待检测连通域的二值化图像
Output:
dst: 标记后的图像
featherList: 连通域特征的清单(可自行查阅文档)
return:
连通域数量。
*/
int connectionDetect(Mat &src, Mat &dst, vector<Feather> &featherList)
{
int rows = src.rows;
int cols = src.cols;
int labelValue = 0;
Point seed, neighbor;
stack<Point> pointStack;
// 用于计算连通域的面积
int area = 0;
// 连通域的左边界,即外接最小矩形的左边框,横坐标值,依此类推
int leftBoundary = 0;
int rightBoundary = 0;
int topBoundary = 0;
int bottomBoundary = 0;
// 外接矩形框
Rect box;
Feather feather;
vector<stack<Point>> points;
featherList.clear();
dst.release();
dst = src.clone();
for (int i = 0; i < rows; i++)
{
uchar *pRow = dst.ptr<uchar>(i);
for (int j = 0; j < cols; j++)
{
if (pRow[j] == 255)
{
area = 0;
// labelValue最大为254,最小为1.
labelValue++;
// Point(横坐标,纵坐标)
seed = Point(j, i);
dst.at<uchar>(seed) = labelValue;
pointStack.push(seed);
area++;
leftBoundary = seed.x;
rightBoundary = seed.x;
topBoundary = seed.y;
bottomBoundary = seed.y;
while (!pointStack.empty())
{
neighbor = Point(seed.x + 1, seed.y);
if ((seed.x != (cols - 1)) && (dst.at<uchar>(neighbor) == 255))
{
dst.at<uchar>(neighbor) = labelValue;
pointStack.push(neighbor);
area++;
if (rightBoundary < neighbor.x)
rightBoundary = neighbor.x;
}
neighbor = Point(seed.x, seed.y + 1);
if ((seed.y != (rows - 1)) && (dst.at<uchar>(neighbor) == 255))
{
dst.at<uchar>(neighbor) = labelValue;
pointStack.push(neighbor);
area++;
if (bottomBoundary < neighbor.y)
bottomBoundary = neighbor.y;
}
neighbor = Point(seed.x - 1, seed.y);
if ((seed.x != 0) && (dst.at<uchar>(neighbor) == 255))
{
dst.at<uchar>(neighbor) = labelValue;
pointStack.push(neighbor);
area++;
if (leftBoundary > neighbor.x)
leftBoundary = neighbor.x;
}
neighbor = Point(seed.x, seed.y - 1);
if ((seed.y != 0) && (dst.at<uchar>(neighbor) == 255))
{
dst.at<uchar>(neighbor) = labelValue;
pointStack.push(neighbor);
area++;
if (topBoundary > neighbor.y)
topBoundary = neighbor.y;
}
seed = pointStack.top();
pointStack.pop();
}
box = Rect(leftBoundary, topBoundary, rightBoundary - leftBoundary, bottomBoundary - topBoundary);
feather.area = area;
feather.boundingbox = box;
feather.label = labelValue;
featherList.push_back(feather);
}
}
}
return labelValue;
}
<font color=#999AAA >在此不进行实例演示
1、 输入图像为分割后图像
2、 执行结果可根据featherList信息自行绘制矩形框
<hr style=" border:solid; width:100px; height:1px;" color=#000000 size=1">
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了python如何删除字符串最后一个字符,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
数据可视化是指用图形或表格的方式来呈现数据。图表能够清楚地呈现数据性质, 以及数据间或属性间的关系。本文为大家分享了几个Python数据可视化绘图的实例,感兴趣的可以了解一下
内容介绍开始之前查看屏幕大小获取当前鼠标的位置检查指定坐标是否在屏幕上设置暂停时间鼠标功能移动鼠标鼠标点击鼠标拖动鼠标滚动屏幕功能截屏功能定位功能消息框功能alert()函数confirm()函数pr
阈值分割法是一种基于区域的图像分割技术,原理是把图像像素点分为若干类。本文将利用Python+OpenCV实现阈值分割,感兴趣的可以了解一下
这篇文章是一篇入门篇,主要给大家介绍了关于python开发中的Docker安装部署,图文方式展示了安装的过程步骤,有需要的朋友可以参考下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008