区域生长:就是以某个像素值进行扩散,查找颜色相近的范围区域。
这里主要介绍四种方法: 固定灰度值、动态灰度值、固定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();
}
如果满意欢迎点赞。