小編給大家分享一下用OpenCV去除面積較小連通域的方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
效果圖

源代碼
//測試
void CCutImageVS2013Dlg::OnBnClickedTestButton1()
{
vector<vector<Point> > contours; //輪廓數組
vector<Point2d> centers; //輪廓質心坐標
vector<vector<Point> >::iterator itr; //輪廓迭代器
vector<Point2d>::iterator itrc; //質心坐標迭代器
vector<vector<Point> > con; //當前輪廓
double area;
double minarea = 1000;
double maxarea = 0;
Moments mom; // 輪廓矩
Mat image, gray, edge, dst;
image = imread("D:\\66.png");
cvtColor(image, gray, COLOR_BGR2GRAY);
Mat rgbImg(gray.size(), CV_8UC3); //創建三通道圖
blur(gray, edge, Size(3, 3)); //模糊去噪
threshold(edge, edge, 200, 255, THRESH_BINARY_INV); //二值化處理,黑底白字
//--------去除較小輪廓,并尋找最大輪廓--------------------------
findContours(edge, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); //尋找輪廓
itr = contours.begin(); //使用迭代器去除噪聲輪廓
while (itr != contours.end())
{
area = contourArea(*itr); //獲得輪廓面積
if (area<minarea) //刪除較小面積的輪廓
{
itr = contours.erase(itr); //itr一旦erase,需要重新賦值
}
else
{
itr++;
}
if (area>maxarea) //尋找最大輪廓
{
maxarea = area;
}
}
dst = Mat::zeros(image.rows, image.cols, CV_8UC3);
/*繪制連通區域輪廓,計算質心坐標*/
Point2d center;
itr = contours.begin();
while (itr != contours.end())
{
area = contourArea(*itr);
con.push_back(*itr); //獲取當前輪廓
if (area == maxarea)
{
vector<Rect> boundRect(1); //定義外接矩形集合
boundRect[0] = boundingRect(Mat(*itr));
cvtColor(gray, rgbImg, COLOR_GRAY2BGR);
Rect select;
select.x = boundRect[0].x;
select.y = boundRect[0].y;
select.width = boundRect[0].width;
select.height = boundRect[0].height;
rectangle(rgbImg, select, Scalar(0, 255, 0), 3, 2); //用矩形畫矩形窗
drawContours(dst, con, -1, Scalar(0, 0, 255), 2); //最大面積紅色繪制
}
else
drawContours(dst, con, -1, Scalar(255, 0, 0), 2); //其它面積藍色繪制
con.pop_back();
//計算質心
mom = moments(*itr);
center.x = (int)(mom.m10 / mom.m00);
center.y = (int)(mom.m01 / mom.m00);
centers.push_back(center);
itr++;
}
imshow("rgbImg", rgbImg);
//imshow("gray", gray);
//imshow("edge", edge);
imshow("origin", image);
imshow("connected_region", dst);
waitKey(0);
return;
}前期做的,方法可能不太一樣
一,先看效果圖
原圖

處理前后圖

二,實現源代碼
//=======函數實現=====================================================================
void RemoveSmallRegion(Mat &Src, Mat &Dst, int AreaLimit, int CheckMode, int NeihborMode)
{
int RemoveCount = 0;
//新建一幅標簽圖像初始化為0像素點,為了記錄每個像素點檢驗狀態的標簽,0代表未檢查,1代表正在檢查,2代表檢查不合格(需要反轉顏色),3代表檢查合格或不需檢查
//初始化的圖像全部為0,未檢查
Mat PointLabel = Mat::zeros(Src.size(), CV_8UC1);
if (CheckMode == 1)//去除小連通區域的白色點
{
//cout << "去除小連通域.";
for (int i = 0; i < Src.rows; i++)
{
for (int j = 0; j < Src.cols; j++)
{
if (Src.at<uchar>(i, j) < 10)
{
PointLabel.at<uchar>(i, j) = 3;//將背景黑色點標記為合格,像素為3
}
}
}
}
else//去除孔洞,黑色點像素
{
//cout << "去除孔洞";
for (int i = 0; i < Src.rows; i++)
{
for (int j = 0; j < Src.cols; j++)
{
if (Src.at<uchar>(i, j) > 10)
{
PointLabel.at<uchar>(i, j) = 3;//如果原圖是白色區域,標記為合格,像素為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 int a = 0;//cout << "Neighbor mode: 4鄰域." << endl;
int NeihborCount = 4 + 4 * NeihborMode;
int CurrX = 0, CurrY = 0;
//開始檢測
for (int i = 0; i < Src.rows; i++)
{
for (int j = 0; j < Src.cols; j++)
{
if (PointLabel.at<uchar>(i, j) == 0)//標簽圖像像素點為0,表示還未檢查的不合格點
{ //開始檢查
vector<Point2i>GrowBuffer;//記錄檢查像素點的個數
GrowBuffer.push_back(Point2i(j, i));
PointLabel.at<uchar>(i, j) = 1;//標記為正在檢查
int CheckResult = 0;
for (int z = 0; z < GrowBuffer.size(); z++)
{
for (int q = 0; q < NeihborCount; q++)
{
CurrX = GrowBuffer.at(z).x + NeihborPos.at(q).x;
CurrY = GrowBuffer.at(z).y + NeihborPos.at(q).y;
if (CurrX >= 0 && CurrX<Src.cols&&CurrY >= 0 && CurrY<Src.rows) //防止越界
{
if (PointLabel.at<uchar>(CurrY, CurrX) == 0)
{
GrowBuffer.push_back(Point2i(CurrX, CurrY)); //鄰域點加入buffer
PointLabel.at<uchar>(CurrY, CurrX) = 1; //更新鄰域點的檢查標簽,避免重復檢查
}
}
}
}
if (GrowBuffer.size()>AreaLimit) //判斷結果(是否超出限定的大?。?,1為未超出,2為超出
CheckResult = 2;
else
{
CheckResult = 1;
RemoveCount++;//記錄有多少區域被去除
}
for (int z = 0; z < GrowBuffer.size(); z++)
{
CurrX = GrowBuffer.at(z).x;
CurrY = GrowBuffer.at(z).y;
PointLabel.at<uchar>(CurrY, CurrX) += CheckResult;//標記不合格的像素點,像素值為2
}
//********結束該點處的檢查**********
}
}
}
CheckMode = 255 * (1 - CheckMode);
//開始反轉面積過小的區域
for (int i = 0; i < Src.rows; ++i)
{
for (int j = 0; j < Src.cols; ++j)
{
if (PointLabel.at<uchar>(i, j) == 2)
{
Dst.at<uchar>(i, j) = CheckMode;
}
else if (PointLabel.at<uchar>(i, j) == 3)
{
Dst.at<uchar>(i, j) = Src.at<uchar>(i, j);
}
}
}
//cout << RemoveCount << " objects removed." << endl;
}
//=======函數實現=====================================================================
//=======調用函數=====================================================================
Mat img;
img = imread("D:\\1_1.jpg", 0);//讀取圖片
threshold(img, img, 128, 255, CV_THRESH_BINARY_INV);
imshow("去除前", img);
Mat img1;
RemoveSmallRegion(img, img, 200, 0, 1);
imshow("去除后", img);
waitKey(0);
//=======調用函數=====================================================================看完了這篇文章,相信你對用OpenCV去除面積較小連通域的方法有了一定的了解,想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。