使用OpenCV去除面積較小的連通域
這是后期補充的部分,和前期的代碼不太一樣
效果圖
源代碼
//測試 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ū)域輪廓,計算質(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(); //計算質(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; }
前期做的,方法可能不太一樣
一,先看效果圖
原圖
處理前后圖
二,實現(xiàn)源代碼
//=======函數(shù)實現(xiàn)===================================================================== void RemoveSmallRegion(Mat &Src, Mat &Dst, int AreaLimit, int CheckMode, int NeihborMode) { int RemoveCount = 0; //新建一幅標(biāo)簽圖像初始化為0像素點,為了記錄每個像素點檢驗狀態(tài)的標(biāo)簽,0代表未檢查,1代表正在檢查,2代表檢查不合格(需要反轉(zhuǎn)顏色),3代表檢查合格或不需檢查 //初始化的圖像全部為0,未檢查 Mat PointLabel = Mat::zeros(Src.size(), CV_8UC1); if (CheckMode == 1)//去除小連通區(qū)域的白色點 { //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;//將背景黑色點標(biāo)記為合格,像素為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;//如果原圖是白色區(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; //開始檢測 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)簽圖像像素點為0,表示還未檢查的不合格點 { //開始檢查 vector<Point2i>GrowBuffer;//記錄檢查像素點的個數(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)); //鄰域點加入buffer PointLabel.at<uchar>(CurrY, CurrX) = 1; //更新鄰域點的檢查標(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)記不合格的像素點,像素值為2 } //********結(jié)束該點處的檢查********** } } } CheckMode = 255 * (1 - CheckMode); //開始反轉(zhuǎn)面積過小的區(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ù)實現(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去除面積較小的連通域就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
分析Python中解析構(gòu)建數(shù)據(jù)知識
本篇文章給大家講述一下Python中解析構(gòu)建數(shù)據(jù)知識的相關(guān)內(nèi)容,有需要的朋友跟著學(xué)習(xí)下。2018-01-01Python標(biāo)準(zhǔn)庫:內(nèi)置函數(shù)max(iterable, *[, key, default])說明
這篇文章主要介紹了Python標(biāo)準(zhǔn)庫:內(nèi)置函數(shù)max(iterable, *[, key, default])說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04python中pip安裝庫時出現(xiàn)Read?timed?out解決辦法
最近需要使用pip庫,安裝的時候出現(xiàn)問題,本文就詳細(xì)的介紹一下python中pip安裝庫時出現(xiàn)Read?timed?out解決辦法,具有一定的參考價值,感興趣的可以了解一下2022-03-03python使用psutil模塊獲取系統(tǒng)狀態(tài)
作為程序猿,大家可能都熟悉linux系統(tǒng)的基礎(chǔ)信息獲取方法都是通過shell來獲取,但是在python中,我們還可以使用psutil模塊來獲取系統(tǒng)信息。psutil模塊把shell查看系統(tǒng)基礎(chǔ)信息的功能都包裝了下,使用更加簡單,功能豐富。2016-08-08pytorch中節(jié)約顯卡內(nèi)存的方法和技巧
顯存不足是很多人感到頭疼的問題,畢竟能擁有大量顯存的實驗室還是少數(shù),而現(xiàn)在的模型已經(jīng)越跑越大,模型參數(shù)量和數(shù)據(jù)集也越來越大,所以這篇文章給大家總結(jié)了一些pytorch中節(jié)約顯卡內(nèi)存的方法和技巧,需要的朋友可以參考下2023-11-11pycharm下配置pyqt5的教程(anaconda虛擬環(huán)境下+tensorflow)
這篇文章主要介紹了pycharm下配置pyqt5的教程(anaconda虛擬環(huán)境下+tensorflow),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03