day18:像素距离与连通域

在图像形态学运算中,常将不与其他区域连接的独立区域称为集合或者连通域,这个集合中的 元素就是包含在连通域内的每一个像素,可以用该像素在图像中的坐标来 描述,像素之 间的距离可以用来表示两个连通域之间的关系.在了解图像形态学运算之前,首先需要了解图像中两个像素之间的距离描述方式,以及如何从图像中分离出不同的连通域。
欧式距离:

街道距离:        d=|x1-x2|+|y1-y2|
棋盘距离:        d=max(|x1-x2|,|y1-y2|),两个像素点X方向,y方向距离的最大值

 

 

 代码:

void visionagin::Mydistancetransform()
{
	Mat data = (Mat_<uchar>(5, 5) << 1, 1, 1, 1, 1,
		1, 1, 1, 1, 1,
		1, 1, 0, 1, 1,
		1, 1, 1, 1, 1,
		1, 1, 1, 1, 1);
	Mat dststreet, dstouji, dstchess;
	distanceTransform(data, dststreet, 1, 3, CV_8U);//计算街道距离
	distanceTransform(data, dstouji, 2, 5, CV_8U);//计算欧几里得距离
	distanceTransform(data, dstchess, 3, 3, CV_8U);//计算棋盘距离
	cout << dststreet << endl;
	cout << dstouji << endl;
	cout << dstchess << endl;

	Mat src=imread("C:\\Users\\86176\\Downloads\\visionimage\\rice.jfif");
	if (src.empty())
	{
		cout << "open failed !" << endl;
	}
	imshow("原图", src);
	Mat temp1,temp2, gry;
	cvtColor(src, gry, COLOR_BGR2GRAY);
	threshold(gry, temp1, 50, 255, THRESH_BINARY);//黑底白图
	threshold(gry, temp2, 50, 255,THRESH_BINARY_INV);//白底黑图
	imshow("黑底白图", temp1);
	imshow("白底黑图", temp2);
	Mat res1, res2;
	distanceTransform(temp1, res1, 1, 3,CV_32F);//cv_32F显示更清晰
	distanceTransform(temp2, res2, 1, 5,CV_8U);
	imshow("黑底白图res1", res1);
	imshow("白底黑图res2", res2);

}
结果:

 原图及二值化后的图像:

 距离变换后的结果:

图像连通域分析:

代码:

void visionagin::Myconnectedcompents()
{
	Mat src= imread("C:\\Users\\86176\\Downloads\\visionimage\\rice.jfif");
	imshow("原图", src);
	Mat gry, res_thre, res_conect;
	cvtColor(src, gry, COLOR_BGR2GRAY);
	threshold(gry, res_thre, 50, 255, THRESH_BINARY);
	int nums=connectedComponents(res_thre, res_conect, 8, CV_16U);
	cout << "total is " << nums << endl;
	RNG rng(12345);
	vector<Vec3b>color;
	for (int i = 0; i < nums; ++i)
	{
		 Vec3b color_value = Vec3b(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256));//为每个连通域生成随机颜色
		 color.push_back(color_value);
	}
	Mat temp = Mat::zeros(src.size(),src.type());//与原图像大小,类型一致的画布
	int width = temp.cols;
	int height = temp.rows;
	for (int j = 0; j < height; j++)
	{
		for (int k = 0; k < width; ++k)
		{
			int label = res_conect.at<uint16_t>(j, k);//每个连通域的label

			if (label == 0)//黑色
			{
				continue;
			}
			else
			{
				temp.at<Vec3b>(j, k) = color[label];//除了黑色背景,都改成随机颜色
			}
		}
	}
	imshow("res_con_picture", temp);

虽然该 函数可以实现图像中多个连通域的统计, 但是只能通过标签将图像 中的不同连通域区分开,无法得到更多的统计信息.有时,我们希望得到每个连通域中心位置或者在图像中标记出连通城所在的矩形区域

 

 代码:

void visionagin::Myconnectedcompents2()
{
	Mat src = imread("C:\\Users\\86176\\Downloads\\visionimage\\rice.jfif");
	imshow("原图", src);
	Mat gry, res_thre, res_conect, stats, center;
	cvtColor(src, gry, COLOR_BGR2GRAY);
	threshold(gry, res_thre, 50, 255, THRESH_BINARY);
	int nums = connectedComponentsWithStats(res_thre, res_conect, stats, center, 8, CV_16U);
	cout << "total is " << nums << endl;
	RNG rng(12345);
	vector<Vec3b>color;
	for (int i = 0; i < nums; ++i)
	{
		Vec3b color_value = Vec3b(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256));//为每个连通域生成随机颜色
		color.push_back(color_value);
	}
	Mat temp = Mat::zeros(src.size(), src.type());//与原图像大小,类型一致的画布
	int width = temp.cols;
	int height = temp.rows;

	for (int i = 0; i < nums; ++i)
	{
		Point Center(center.at<double>(i, 0), center.at<double>(i, 1));
		circle(src, Center, 2, Scalar(color[i]), 1);//画质心

		int px(stats.at<int>(i, 0));
		int py(stats.at<int>(i, 1));
		int w = stats.at<int>(i, 2);
		int h = stats.at<int>(i, 3);
	
		rectangle(src, Rect(px, py, w, h), Scalar(color[i]), 2);
	}
	imshow("res_con_picture", src);
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值