算法步骤:
- 创建一个空白的图像(全黑);
- 将种子点存入vector中,vector中存储待生长的种子点;
- 依次弹出种子点并判断种子点如周围8领域的关系(生长规则),相似的点则作为下次生长的种子点;
- 设置结束条件;
- vector中不存在种子点后就停止生长。
用于实现区域内具有相同特征的区域检测。
代码:
Mat RegionGrow(Mat mat, vector<Point2i> ptt, int th)
{
Point2i ptGrowing; //待生长点位置
int nGrowLable = 0; //标记是否生长过
int nSrcValue = 0; //生长起点灰度值
int nCurValue = 0; //当前生长点灰度值
Mat matDst = Mat::zeros(mat.size(), CV_8UC1); //创建一个空白区域,填充为黑色
//生长方向顺序数据
int DIR[8][2] = { { -1, -1 },{ 0, -1 },{ 1, -1 },{ 1, 0 },{ 1, 1 },{ 0, 1 },{ -1, 1 },{ -1, 0 } };
vector<Point2i> vcGrowPt; //生长点栈
Point2i pt;
int mask = 0 ;
for (int i = 0; i < ptt.size(); i++) {
if (mat.at<uchar>(ptt[i].y, ptt[i].x) < 200)
continue;
pt = ptt[i];
vcGrowPt.push_back(pt); //将生长点压入栈中
matDst.at<uchar>(pt.y, pt.x) = 255; //标记生长点
nSrcValue = mat.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 >(mat.cols - 1) || (ptGrowing.y > mat.rows - 1))
continue;
nGrowLable = matDst.at<uchar>(ptGrowing.y, ptGrowing.x); //当前待生长点的灰度值
if (nGrowLable == 0) //如果标记点还没有被生长
{
nCurValue = mat.at<uchar>(ptGrowing.y, ptGrowing.x);
if (abs(nSrcValue - nCurValue) < th) //在阈值范围内则生长
{
matDst.at<uchar>(ptGrowing.y, ptGrowing.x) = 255; //标记为白色
mask +=1;
vcGrowPt.push_back(ptGrowing); //将下一个生长点压入栈中
}
}
if (mask > 600)
break;
}
}
}
return matDst.clone();
}
结果:
完成了多种子点的区域定位。其中有缺定位的,原因在于通过边缘计算的质心种子点不在理想区域内,待改进。