opencv 图像连通域分析

一、简介

图像的连通域是指图像中具有相同像素值并且位置相邻的像素组成的区域,连通域分析是指在图像中寻找出彼此互相独立的连通域并将其标记出来。提取图像中不同的连通域是图像处理中较为常用的方法,例如在车牌识别、文字识别、目标检测等领域对感兴趣区域分割与识别。一般情况下,一个连通域内只包含一个像素值,因此为了防止像素值波动对提取不同连通域的影响,连通域分析常处理的是二值化后的图像。 

 二、connectedComponents()函数

int cv::connectedComponents(
InputArray  image,//待标记不同连通域的单通道图像,数据类型必须为CV_8U。
OutputArray  labels,//标记不同连通域后的输出图像,与输入图像具有相同的尺寸。
nt  connectivity,//标记连通域时使用的邻域种类,4表示4-邻域,8表示8-邻域。
int  ltype,//输出图像的数据类型,目前支持CV_32S和CV_16U两种数据类型。
int  ccltype //标记连通域时使用的算法类型标志
)

#if  1// 连通域
int main()
{
	//对图像进行距离变换
	Mat img = imread("C:\\Users\\19473\\Desktop\\opencv_images\\618.png");
	if (img.empty())
	{
		printf("could not  load  image....\n");
		return -1;
	}
	Mat rice, riceBW;

	//将图像转成二值图像,用于统计连通域
	cvtColor(img, rice, COLOR_BGR2GRAY);
	threshold(rice, riceBW, 50, 255, THRESH_BINARY);


	//生成随机颜色,用于区分不同连通域
	RNG rng(12345);
	Mat out;
	//统计图像中连通域的个数
	int number = connectedComponents(riceBW, out, 8, CV_16U);
	
	vector<Vec3b> colors;
	for (int i = 0; i < number; i++)
	{
		//使用均匀分布的随机数确定颜色
		Vec3b vec3 = Vec3b(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256));
		colors.push_back(vec3);
	}

	//以不同颜色标记出不同的连通域
	Mat result = Mat::zeros(rice.size(), img.type());
	int w = result.cols;
	int h = result.rows;
	for (int row = 0; row < h; row++)
	{
		for (int col = 0; col < w; col++)
		{
			int label = out.at<uint16_t>(row, col);
			if (label == 0)  //背景的黑色不改变
			{
				continue;
			}
			result.at<Vec3b>(row, col) = colors[label];
		}
	}

	//显示结果
	imshow("原图", img);
	imshow("标记后的图像", result);

	waitKey(0);
	return 0;
}
#endif
int cv::connectedComponentsWithStats(
InputArray  image,//必须为CV_8U
 OutputArray  labels,//标记不同连通域后的输出图像,与输入图像具有相同的尺寸。
 OutputArray  stats,//不同连通域的统计信息矩阵,矩阵的数据类型为CV_32S。矩阵中第i行是标签为i的连通域的统计特性,
 OutputArray  centroids,//每个连通域的质心坐标,数据类型为CV_64F
 int  connectivity = 8,//标记连通域时使用的邻域种类,4表示4-邻域,8表示8-邻域,默认参数值为8
 int  ltype = CV_32S //输出图像的数据类型,目前只支持CV_32S和CV_16U这两种数据类型,默认参数值为CV_32S
)

 

#if  1 // 1.1连通域 connectedComponentsWithStats
int main()
{
	//对图像进行距离变换
	Mat img = imread("C:\\Users\\19473\\Desktop\\opencv_images\\618.png");
	if (img.empty())
	{
		printf("could not  load  image....\n");
		return -1;
	}
	imshow("原图", img);
	Mat rice, riceBW;

	//将图像转成二值图像,用于统计连通域
	cvtColor(img, rice, COLOR_BGR2GRAY);
	threshold(rice, riceBW, 50, 255, THRESH_BINARY);

	//生成随机颜色,用于区分不同连通域
	RNG rng(10086);
	Mat out, stats, centroids;
	//统计图像中连通域的个数
	int number = connectedComponentsWithStats(riceBW, out, stats, centroids, 8, CV_16U);
	vector<Vec3b> colors;
	for (int i = 0; i < number; i++)
	{
		//使用均匀分布的随机数确定颜色
		Vec3b vec3 = Vec3b(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256));
		colors.push_back(vec3);
	}

	//以不同颜色标记出不同的连通域
	Mat result = Mat::zeros(rice.size(), img.type());
	int w = result.cols;
	int h = result.rows;
	for (int i = 1; i < number; i++)
	{
		// 中心位置
		int center_x = centroids.at<double>(i, 0);
		int center_y = centroids.at<double>(i, 1);
		//矩形边框
		int x = stats.at<int>(i, CC_STAT_LEFT);
		int y = stats.at<int>(i, CC_STAT_TOP);
		int w = stats.at<int>(i, CC_STAT_WIDTH);
		int h = stats.at<int>(i, CC_STAT_HEIGHT);
		int area = stats.at<int>(i, CC_STAT_AREA);

		// 中心位置绘制
		circle(img, Point(center_x, center_y), 2, Scalar(0, 255, 0), 2, 8, 0);
		// 外接矩形
		Rect rect(x, y, w, h);
		rectangle(img, rect, colors[i], 1, 8, 0);
		putText(img, format("%d", i), Point(center_x, center_y),
			FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 255), 1);
		cout << "number: " << i << ",area: " << area << endl;
	}
	//显示结果
	imshow("标记后的图像", img);

	waitKey(0);
	return 0;
}
#endif

 

【OpenCV 4开发详解】图像连通域分析 - 知乎

这文章的所属全归上面知乎的作者所有,我个人只是做个学习记录 

猜你喜欢

转载自blog.csdn.net/weixin_39354845/article/details/123027194