Opencv的区域增长法
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();
}