opencv区域生长/区域扩散

区域生长:就是以某个像素值进行扩散,查找颜色相近的范围区域。

这里主要介绍四种方法: 固定灰度值、动态灰度值、固定RGB值、动态RGB值。这四种方法对应不同图片。经测试效果可以

1.固定灰度值区域生长

//固定灰度值区域生长
cv::Mat RegionGrow(cv::Mat src, cv::Point2i pt, int th)
{
	cv::Point2i ptGrowing;						//待生长点位置
	int nGrowLable = 0;								//标记是否生长过
	int nSrcValue = 0;								//生长起点灰度值
	int nCurValue = 0;								//当前生长点灰度值
	cv::Mat matDst = cv::Mat::zeros(src.size(), CV_8UC1);	//创建一个空白区域,填充为黑色
															//生长方向顺序数据
	int DIR[8][2] = { { -1, -1 },{ 0, -1 },{ 1, -1 },{ 1, 0 },{ 1, 1 },{ 0, 1 },{ -1, 1 },{ -1, 0 } };
	std::vector<cv::Point2i> vcGrowPt;						//生长点栈
	vcGrowPt.push_back(pt);							//将生长点压入栈中
	matDst.at<uchar>(pt.y, pt.x) = 255;				//标记生长点
	nSrcValue = src.at<uchar>(pt.y, pt.x);			//记录生长点的灰度值

	while (!vcGrowPt.empty())						//生长栈不为空则生长
	{
		pt = vcGrowPt.back();						//取出一个生长点
		vcGrowPt.pop_back();

		//分别对八个方向上的点进行生长
		for (int i = 0; i<9; ++i)
		{
			ptGrowing.x = pt.x + DIR[i][0];
			ptGrowing.y = pt.y + DIR[i][1];
			//检查是否是边缘点
			if (ptGrowing.x < 0 || ptGrowing.y < 0 || ptGrowing.x >(src.cols - 1) || (ptGrowing.y > src.rows - 1))
				continue;

			nGrowLable = matDst.at<uchar>(ptGrowing.y, ptGrowing.x);		//当前待生长点的灰度值

			if (nGrowLable == 0)					//如果标记点还没有被生长
			{
				nCurValue = src.at<uchar>(ptGrowing.y, ptGrowing.x);
				if (abs(nSrcValue - nCurValue) < th)					//在阈值范围内则生长
				{
					matDst.at<uchar>(ptGrowing.y, ptGrowing.x) = 255;		//标记为白色
					vcGrowPt.push_back(ptGrowing);					//将下一个生长点压入栈中
				}
			}
		}
	}
	return matDst.clone();
}

2.动态灰度值区域生长

//动态灰度值区域生长
cv::Mat RegionGrow_gray_real(cv::Mat src, cv::Point2i pt, int th)
{
	cv::Point2i ptGrowing;						//待生长点位置
	int nGrowLable = 0;								//标记是否生长过
	int nSrcValue = 0;								//生长起点灰度值
	int nCurValue = 0;								//当前生长点灰度值
	cv::Mat matDst = cv::Mat::zeros(src.size(), CV_8UC1);	//创建一个空白区域,填充为黑色
															//生长方向顺序数据
	int DIR[8][2] = { { -1, -1 },{ 0, -1 },{ 1, -1 },{ 1, 0 },{ 1, 1 },{ 0, 1 },{ -1, 1 },{ -1, 0 } };
	std::vector<cv::Point2i> vcGrowPt;						//生长点栈
	vcGrowPt.push_back(pt);							//将生长点压入栈中
	matDst.at<uchar>(pt.y, pt.x) = 255;				//标记生长点
	

	while (!vcGrowPt.empty())						//生长栈不为空则生长
	{
		pt = vcGrowPt.back();						//取出一个生长点
		vcGrowPt.pop_back();

		//分别对八个方向上的点进行生长
		for (int i = 0; i<9; ++i)
		{
			ptGrowing.x = pt.x + DIR[i][0];
			ptGrowing.y = pt.y + DIR[i][1];
			//检查是否是边缘点
			if (ptGrowing.x < 0 || ptGrowing.y < 0 || ptGrowing.x >(src.cols - 1) || (ptGrowing.y > src.rows - 1))
				continue;

			nGrowLable = matDst.at<uchar>(ptGrowing.y, ptGrowing.x);		//当前待生长点的灰度值
			nSrcValue = src.at<uchar>(pt.y, pt.x);			//记录生长点的灰度值
			if (nGrowLable == 0)					//如果标记点还没有被生长
			{
				nCurValue = src.at<uchar>(ptGrowing.y, ptGrowing.x);
				if (abs(nSrcValue - nCurValue) < th)					//在阈值范围内则生长
				{
					matDst.at<uchar>(ptGrowing.y, ptGrowing.x) = 255;		//标记为白色
					vcGrowPt.push_back(ptGrowing);					//将下一个生长点压入栈中
				}
			}
		}
	}
	return matDst.clone();
}

 3.固定RGB值区域生长

//固定RGB值区域生长
cv::Mat RegionGrow_rgb(cv::Mat src, cv::Point2i pt, int th)
{
	cv::Point2i ptGrowing;						//待生长点位置
	int nGrowLable = 0;								//标记是否生长过
	int nSrcValue[3] = { 0 };								//生长起点灰度值
	int nCurValue[3] = { 0 };								//当前生长点灰度值
	cv::Mat matDst = cv::Mat::zeros(src.size(), CV_8UC3);	//创建一个空白区域,填充为黑色
															//生长方向顺序数据
	int DIR[8][2] = { { -1, -1 },{ 0, -1 },{ 1, -1 },{ 1, 0 },{ 1, 1 },{ 0, 1 },{ -1, 1 },{ -1, 0 } };
	std::vector<cv::Point2i> vcGrowPt;						//生长点栈
	vcGrowPt.push_back(pt);							//将生长点压入栈中
	matDst.at<cv::Vec3b>(pt.y, pt.x)[0] = 255;				//标记生长点
	matDst.at<cv::Vec3b>(pt.y, pt.x)[1] = 255;				//标记生长点
	matDst.at<cv::Vec3b>(pt.y, pt.x)[2] = 255;				//标记生长点
	nSrcValue[0] = src.at<cv::Vec3b>(pt.y, pt.x)[0];			//记录生长点的灰度值
	nSrcValue[1] = src.at<cv::Vec3b>(pt.y, pt.x)[1];			//记录生长点的灰度值
	nSrcValue[2] = src.at<cv::Vec3b>(pt.y, pt.x)[2];			//记录生长点的灰度值


	while (!vcGrowPt.empty())						//生长栈不为空则生长
	{
		pt = vcGrowPt.back();						//取出一个生长点
		vcGrowPt.pop_back();

		//分别对八个方向上的点进行生长
		for (int i = 0; i < 9; ++i)
		{
			ptGrowing.x = pt.x + DIR[i][0];
			ptGrowing.y = pt.y + DIR[i][1];
			//检查是否是边缘点
			if (ptGrowing.x < 0 || ptGrowing.y < 0 || ptGrowing.x >(src.cols - 1) || (ptGrowing.y > src.rows - 1))
				continue;

			nGrowLable = matDst.at<cv::Vec3b>(ptGrowing.y, ptGrowing.x)[0] + matDst.at<cv::Vec3b>(ptGrowing.y, ptGrowing.x)[1] + matDst.at<cv::Vec3b>(ptGrowing.y, ptGrowing.x)[2];		//当前待生长点的灰度值

			if (nGrowLable == 0)					//如果标记点还没有被生长
			{
				nCurValue[0] = src.at<cv::Vec3b>(ptGrowing.y, ptGrowing.x)[0];
				nCurValue[1] = src.at<cv::Vec3b>(ptGrowing.y, ptGrowing.x)[1];
				nCurValue[2] = src.at<cv::Vec3b>(ptGrowing.y, ptGrowing.x)[2];

				if (abs(sqrt(nSrcValue[0] * nSrcValue[0] + nSrcValue[1] * nSrcValue[1] + nSrcValue[2] * nSrcValue[2]) - sqrt(nCurValue[0] * nCurValue[0] + nCurValue[1] * nCurValue[1] + nCurValue[2] * nCurValue[2])) < th)					//在阈值范围内则生长
				//if (abs((nSrcValue[0] - nCurValue[0])*(nSrcValue[0] - nCurValue[0]) + (nSrcValue[1] - nCurValue[1])*(nSrcValue[1] - nCurValue[1]) + (nSrcValue[2] - nCurValue[2])*(nSrcValue[2] - nCurValue[2])) < th)
				{
					matDst.at<cv::Vec3b>(ptGrowing.y, ptGrowing.x)[0] = 255;		//标记为白色
					matDst.at<cv::Vec3b>(ptGrowing.y, ptGrowing.x)[1] = 255;		//标记为白色
					matDst.at<cv::Vec3b>(ptGrowing.y, ptGrowing.x)[2] = 255;		//标记为白色

					vcGrowPt.push_back(ptGrowing);					//将下一个生长点压入栈中
				}
			}
		}
	}
	return matDst.clone();
}

4.动态RGB值区域生长

//动态RGB值区域生长
cv::Mat RegionGrow_rgb_real(cv::Mat src, cv::Point2i pt, int th)
{
	cv::Point2i ptGrowing;						//待生长点位置
	int nGrowLable = 0;								//标记是否生长过
	int nSrcValue[3] = { 0 };								//生长起点灰度值
	int nCurValue[3] = { 0 };								//当前生长点灰度值
	cv::Mat matDst = cv::Mat::zeros(src.size(), CV_8UC3);	//创建一个空白区域,填充为黑色
															//生长方向顺序数据
	int DIR[8][2] = { { -1, -1 },{ 0, -1 },{ 1, -1 },{ 1, 0 },{ 1, 1 },{ 0, 1 },{ -1, 1 },{ -1, 0 } };
	std::vector<cv::Point2i> vcGrowPt;						//生长点栈
	vcGrowPt.push_back(pt);							//将生长点压入栈中
	matDst.at<cv::Vec3b>(pt.y, pt.x)[0] = 255;				//标记生长点
	matDst.at<cv::Vec3b>(pt.y, pt.x)[1] = 255;				//标记生长点
	matDst.at<cv::Vec3b>(pt.y, pt.x)[2] = 255;				//标记生长点


	while (!vcGrowPt.empty())						//生长栈不为空则生长
	{
		pt = vcGrowPt.back();						//取出一个生长点
		vcGrowPt.pop_back();

		//分别对八个方向上的点进行生长
		for (int i = 0; i < 9; ++i)
		{
			ptGrowing.x = pt.x + DIR[i][0];
			ptGrowing.y = pt.y + DIR[i][1];
			//检查是否是边缘点
			if (ptGrowing.x < 0 || ptGrowing.y < 0 || ptGrowing.x >(src.cols - 1) || (ptGrowing.y > src.rows - 1))
				continue;

			nGrowLable = matDst.at<cv::Vec3b>(ptGrowing.y, ptGrowing.x)[0] + matDst.at<cv::Vec3b>(ptGrowing.y, ptGrowing.x)[1] + matDst.at<cv::Vec3b>(ptGrowing.y, ptGrowing.x)[2];		//当前待生长点的灰度值
			nSrcValue[0] = src.at<cv::Vec3b>(pt.y, pt.x)[0];			//记录生长点的灰度值
			nSrcValue[1] = src.at<cv::Vec3b>(pt.y, pt.x)[1];			//记录生长点的灰度值
			nSrcValue[2] = src.at<cv::Vec3b>(pt.y, pt.x)[2];			//记录生长点的灰度值

			if (nGrowLable == 0)					//如果标记点还没有被生长
			{
				nCurValue[0] = src.at<cv::Vec3b>(ptGrowing.y, ptGrowing.x)[0];
				nCurValue[1] = src.at<cv::Vec3b>(ptGrowing.y, ptGrowing.x)[1];
				nCurValue[2] = src.at<cv::Vec3b>(ptGrowing.y, ptGrowing.x)[2];

				if (abs(sqrt(nSrcValue[0] * nSrcValue[0] + nSrcValue[1] * nSrcValue[1] + nSrcValue[2] * nSrcValue[2]) - sqrt(nCurValue[0] * nCurValue[0] + nCurValue[1] * nCurValue[1] + nCurValue[2] * nCurValue[2])) < th)					//在阈值范围内则生长
				{
					matDst.at<cv::Vec3b>(ptGrowing.y, ptGrowing.x)[0] = 255;		//标记为白色
					matDst.at<cv::Vec3b>(ptGrowing.y, ptGrowing.x)[1] = 255;		//标记为白色
					matDst.at<cv::Vec3b>(ptGrowing.y, ptGrowing.x)[2] = 255;		//标记为白色

					vcGrowPt.push_back(ptGrowing);					//将下一个生长点压入栈中
				}
			}
		}
	}
	return matDst.clone();
}

如果满意欢迎点赞。

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大臉喵愛吃魚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值