OpenCV连通域数量统计学习示例

 更新时间:2022年06月07日 09:05:19   作者:忘·月  
这篇文章主要为大家介绍了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">

以上就是OpenCV连通域数量统计学习示例的详细内容,更多关于OpenCV连通域数量统计的资料请关注脚本之家其它相关文章!

相关文章

  • Python 用matplotlib画以时间日期为x轴的图像

    Python 用matplotlib画以时间日期为x轴的图像

    这篇文章主要介绍了Python 用matplotlib画以时间日期为x轴的图像,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08
  • 深入解析Python中的多进程

    深入解析Python中的多进程

    这篇文章主要介绍了深入解析Python中的多进程,“Python中的多进程是通过multiprocessing包来实现的,和多线程的threading.Thread差不多,它可以利用multiprocessing.Process对象来创建一个进程对象
    2022-06-06
  • Python Pandas中DataFrame.drop_duplicates()删除重复值详解

    Python Pandas中DataFrame.drop_duplicates()删除重复值详解

    在实际处理数据中,数据预处理操作中,常常需要去除掉重复的数据,这篇文章主要给大家介绍了关于Python Pandas中DataFrame.drop_duplicates()删除重复值的相关资料,需要的朋友可以参考下
    2022-07-07
  • Python 解析xml文件的示例

    Python 解析xml文件的示例

    这篇文章主要介绍了Python 解析xml文件的示例,帮助大家更好的利用python处理文件,感兴趣的朋友可以了解下
    2020-09-09
  • Python文件及目录处理的方法

    Python文件及目录处理的方法

    这篇文章主要介绍了Python文件及目录处理的方法,Python可以用于处理文本文件和二进制文件,比如创建文件、读写文件等操作。本文介绍Python处理目录以及文件的相关资料,需要的朋友可以参考一下
    2021-12-12
  • Python根据当前日期取去年同星期日期

    Python根据当前日期取去年同星期日期

    最近做项目,遇到这样的业务开发需求,需要对比当前时间段和去年同星期的时间段的数据,下面小编通过实例代码给大家分享Python根据当前日期取去年同星期日期,需要的朋友参考下
    2019-04-04
  • 一起来了解python的基本输入和输出

    一起来了解python的基本输入和输出

    这篇文章主要为大家详细介绍了python的基本输入和输出,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-02-02
  • Python使用Kubernetes API访问集群

    Python使用Kubernetes API访问集群

    本文主要介绍了Python使用Kubernetes API访问集群,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-05-05
  • python求列表对应元素的乘积和的实现

    python求列表对应元素的乘积和的实现

    这篇文章主要介绍了python求列表对应元素的乘积和的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • python环境的报错解决方法

    python环境的报错解决方法

    这篇文章主要为大家介绍了python环境的报错解决方法,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08

最新评论