void RemoveSmallRegion(Mat &Src, Mat &Dst,int AreaLimit, int CheckMode, int NeihborMode)
{
int RemoveCount = 0;
Mat PointLabel = Mat::zeros((), CV_8UC1);
if (CheckMode == 1)
{
cout << "去除小连通域.";
for (int i = 0; i < ; i++)
{
for (int j = 0; j < ; j++)
{
if (<uchar>(i, j) < 10)
{
<uchar>(i, j) = 3;
}
}
}
}
else
{
cout << "去除孔洞";
for (int i = 0; i < ; i++)
{
for (int j = 0; j < ; j++)
{
if (<uchar>(i, j) > 10)
{
<uchar>(i, j) = 3;
}
}
}
}
vector<Point2i>NeihborPos;
NeihborPos.push_back(Point2i(-1, 0));
NeihborPos.push_back(Point2i(1, 0));
NeihborPos.push_back(Point2i(0, -1));
NeihborPos.push_back(Point2i(0, 1));
if (NeihborMode == 1)
{
cout << "Neighbor mode: 8邻域." << endl;
NeihborPos.push_back(Point2i(-1, -1));
NeihborPos.push_back(Point2i(-1, 1));
NeihborPos.push_back(Point2i(1, -1));
NeihborPos.push_back(Point2i(1, 1));
}
else cout << "Neighbor mode: 4邻域." << endl;
int NeihborCount = 4 + 4 * NeihborMode;
int CurrX = 0, CurrY = 0;
for (int i = 0; i < ; i++)
{
for (int j = 0; j < ; j++)
{
if (<uchar>(i, j) == 0)
{
vector<Point2i>GrowBuffer;
GrowBuffer.push_back(Point2i(j, i));
<uchar>(i, j) = 1;
int CheckResult = 0;
for (int z = 0; z < (); z++)
{
for (int q = 0; q < NeihborCount; q++)
{
CurrX = (z).x + (q).x;
CurrY = (z).y + (q).y;
if (CurrX >= 0 && CurrX<&&CurrY >= 0 && CurrY<)
{
if (<uchar>(CurrY, CurrX) == 0)
{
GrowBuffer.push_back(Point2i(CurrX, CurrY));
<uchar>(CurrY, CurrX) = 1;
}
}
}
}
if (()>AreaLimit)
CheckResult = 2;
else
{
CheckResult = 1;
RemoveCount++;
}
for (int z = 0; z < (); z++)
{
CurrX = (z).x;
CurrY = (z).y;
<uchar>(CurrY,CurrX)+=CheckResult;
}
}
}
}
CheckMode = 255 * (1 - CheckMode);
for (int i = 0; i < ; ++i)
{
for (int j = 0; j < ; ++j)
{
if (<uchar>(i,j)==2)
{
<uchar>(i, j) = CheckMode;
}
else if (<uchar>(i, j) == 3)
{
<uchar>(i, j) = <uchar>(i, j);
}
}
}
cout << RemoveCount << " objects removed." << endl;
}