分享

区域生长

 mscdj 2016-06-26
Mat RegionGrow(Mat src, Point2i pt, int th)
{
Point2i ptGrowing;                      //待生长点位置  
int nGrowLable = 0;                             //标记是否生长过  
int nSrcValue = 0;                              //生长起点灰度值  
int nCurValue = 0;                              //当前生长点灰度值  
Mat matDst = 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 } };
Vector<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();
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约