使用OpenCV去除面積較小的連通域
這是后期補(bǔ)充的部分,和前期的代碼不太一樣
效果圖

源代碼
//測(cè)試
void CCutImageVS2013Dlg::OnBnClickedTestButton1()
{
vector<vector<Point> > contours; //輪廓數(shù)組
vector<Point2d> centers; //輪廓質(zhì)心坐標(biāo)
vector<vector<Point> >::iterator itr; //輪廓迭代器
vector<Point2d>::iterator itrc; //質(zhì)心坐標(biāo)迭代器
vector<vector<Point> > con; //當(dāng)前輪廓
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); //創(chuàng)建三通道圖
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);
/*繪制連通區(qū)域輪廓,計(jì)算質(zhì)心坐標(biāo)*/
Point2d center;
itr = contours.begin();
while (itr != contours.end())
{
area = contourArea(*itr);
con.push_back(*itr); //獲取當(dāng)前輪廓
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); //其它面積藍(lán)色繪制
con.pop_back();
//計(jì)算質(zhì)心
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;
}
前期做的,方法可能不太一樣
一,先看效果圖
原圖

處理前后圖

二,實(shí)現(xiàn)源代碼
//=======函數(shù)實(shí)現(xiàn)=====================================================================
void RemoveSmallRegion(Mat &Src, Mat &Dst, int AreaLimit, int CheckMode, int NeihborMode)
{
int RemoveCount = 0;
//新建一幅標(biāo)簽圖像初始化為0像素點(diǎn),為了記錄每個(gè)像素點(diǎn)檢驗(yàn)狀態(tài)的標(biāo)簽,0代表未檢查,1代表正在檢查,2代表檢查不合格(需要反轉(zhuǎn)顏色),3代表檢查合格或不需檢查
//初始化的圖像全部為0,未檢查
Mat PointLabel = Mat::zeros(Src.size(), CV_8UC1);
if (CheckMode == 1)//去除小連通區(qū)域的白色點(diǎn)
{
//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;//將背景黑色點(diǎn)標(biāo)記為合格,像素為3
}
}
}
}
else//去除孔洞,黑色點(diǎn)像素
{
//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;//如果原圖是白色區(qū)域,標(biāo)記為合格,像素為3
}
}
}
}
vector<Point2i>NeihborPos;//將鄰域壓進(jìn)容器
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;
//開(kāi)始檢測(cè)
for (int i = 0; i < Src.rows; i++)
{
for (int j = 0; j < Src.cols; j++)
{
if (PointLabel.at<uchar>(i, j) == 0)//標(biāo)簽圖像像素點(diǎn)為0,表示還未檢查的不合格點(diǎn)
{ //開(kāi)始檢查
vector<Point2i>GrowBuffer;//記錄檢查像素點(diǎn)的個(gè)數(shù)
GrowBuffer.push_back(Point2i(j, i));
PointLabel.at<uchar>(i, j) = 1;//標(biāo)記為正在檢查
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)); //鄰域點(diǎn)加入buffer
PointLabel.at<uchar>(CurrY, CurrX) = 1; //更新鄰域點(diǎn)的檢查標(biāo)簽,避免重復(fù)檢查
}
}
}
}
if (GrowBuffer.size()>AreaLimit) //判斷結(jié)果(是否超出限定的大?。?,1為未超出,2為超出
CheckResult = 2;
else
{
CheckResult = 1;
RemoveCount++;//記錄有多少區(qū)域被去除
}
for (int z = 0; z < GrowBuffer.size(); z++)
{
CurrX = GrowBuffer.at(z).x;
CurrY = GrowBuffer.at(z).y;
PointLabel.at<uchar>(CurrY, CurrX) += CheckResult;//標(biāo)記不合格的像素點(diǎn),像素值為2
}
//********結(jié)束該點(diǎn)處的檢查**********
}
}
}
CheckMode = 255 * (1 - CheckMode);
//開(kāi)始反轉(zhuǎn)面積過(guò)小的區(qū)域
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;
}
//=======函數(shù)實(shí)現(xiàn)=====================================================================
//=======調(diào)用函數(shù)=====================================================================
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);
//=======調(diào)用函數(shù)=====================================================================
以上這篇使用OpenCV去除面積較小的連通域就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
分析Python中解析構(gòu)建數(shù)據(jù)知識(shí)
本篇文章給大家講述一下Python中解析構(gòu)建數(shù)據(jù)知識(shí)的相關(guān)內(nèi)容,有需要的朋友跟著學(xué)習(xí)下。2018-01-01
Python標(biāo)準(zhǔn)庫(kù):內(nèi)置函數(shù)max(iterable, *[, key, default])說(shuō)明
對(duì)python制作自己的數(shù)據(jù)集實(shí)例講解
python中pip安裝庫(kù)時(shí)出現(xiàn)Read?timed?out解決辦法
python使用psutil模塊獲取系統(tǒng)狀態(tài)
pytorch中節(jié)約顯卡內(nèi)存的方法和技巧
Python腳本判斷 Linux 是否運(yùn)行在虛擬機(jī)上
pycharm下配置pyqt5的教程(anaconda虛擬環(huán)境下+tensorflow)

