OpenCV連通域數(shù)量統(tǒng)計(jì)學(xué)習(xí)示例
學(xué)習(xí)目標(biāo):
1.輸入圖像為分割結(jié)果圖像
2.根據(jù)種子填充法思路,遍歷圖像,得到每個(gè)連通域外接矩形坐標(biāo)信息、面積信息
核心代碼
/* Input: src: 待檢測(cè)連通域的二值化圖像 Output: dst: 標(biāo)記后的圖像 featherList: 連通域特征的清單(可自行查閱文檔) return: 連通域數(shù)量。 */ int connectionDetect(Mat &src, Mat &dst, vector<Feather> &featherList) { int rows = src.rows; int cols = src.cols; int labelValue = 0; Point seed, neighbor; stack<Point> pointStack; // 用于計(jì)算連通域的面積 int area = 0; // 連通域的左邊界,即外接最小矩形的左邊框,橫坐標(biāo)值,依此類(lèi)推 int leftBoundary = 0; int rightBoundary = 0; int topBoundary = 0; int bottomBoundary = 0; // 外接矩形框 Rect box; Feather feather; vector<stack<Point>> points; featherList.clear(); dst.release(); dst = src.clone(); for (int i = 0; i < rows; i++) { uchar *pRow = dst.ptr<uchar>(i); for (int j = 0; j < cols; j++) { if (pRow[j] == 255) { area = 0; // labelValue最大為254,最小為1. labelValue++; // Point(橫坐標(biāo),縱坐標(biāo)) seed = Point(j, i); dst.at<uchar>(seed) = labelValue; pointStack.push(seed); area++; leftBoundary = seed.x; rightBoundary = seed.x; topBoundary = seed.y; bottomBoundary = seed.y; while (!pointStack.empty()) { neighbor = Point(seed.x + 1, seed.y); if ((seed.x != (cols - 1)) && (dst.at<uchar>(neighbor) == 255)) { dst.at<uchar>(neighbor) = labelValue; pointStack.push(neighbor); area++; if (rightBoundary < neighbor.x) rightBoundary = neighbor.x; } neighbor = Point(seed.x, seed.y + 1); if ((seed.y != (rows - 1)) && (dst.at<uchar>(neighbor) == 255)) { dst.at<uchar>(neighbor) = labelValue; pointStack.push(neighbor); area++; if (bottomBoundary < neighbor.y) bottomBoundary = neighbor.y; } neighbor = Point(seed.x - 1, seed.y); if ((seed.x != 0) && (dst.at<uchar>(neighbor) == 255)) { dst.at<uchar>(neighbor) = labelValue; pointStack.push(neighbor); area++; if (leftBoundary > neighbor.x) leftBoundary = neighbor.x; } neighbor = Point(seed.x, seed.y - 1); if ((seed.y != 0) && (dst.at<uchar>(neighbor) == 255)) { dst.at<uchar>(neighbor) = labelValue; pointStack.push(neighbor); area++; if (topBoundary > neighbor.y) topBoundary = neighbor.y; } seed = pointStack.top(); pointStack.pop(); } box = Rect(leftBoundary, topBoundary, rightBoundary - leftBoundary, bottomBoundary - topBoundary); feather.area = area; feather.boundingbox = box; feather.label = labelValue; featherList.push_back(feather); } } } return labelValue; }
代碼執(zhí)行說(shuō)明
<font color=#999AAA >在此不進(jìn)行實(shí)例演示
1、 輸入圖像為分割后圖像
2、 執(zhí)行結(jié)果可根據(jù)featherList信息自行繪制矩形框
<hr style=" border:solid; width:100px; height:1px;" color=#000000 size=1">
以上就是OpenCV連通域數(shù)量統(tǒng)計(jì)學(xué)習(xí)示例的詳細(xì)內(nèi)容,更多關(guān)于OpenCV連通域數(shù)量統(tǒng)計(jì)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python 用matplotlib畫(huà)以時(shí)間日期為x軸的圖像
這篇文章主要介紹了Python 用matplotlib畫(huà)以時(shí)間日期為x軸的圖像,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Python?Pandas中DataFrame.drop_duplicates()刪除重復(fù)值詳解
在實(shí)際處理數(shù)據(jù)中,數(shù)據(jù)預(yù)處理操作中,常常需要去除掉重復(fù)的數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于Python?Pandas中DataFrame.drop_duplicates()刪除重復(fù)值的相關(guān)資料,需要的朋友可以參考下2022-07-07Python根據(jù)當(dāng)前日期取去年同星期日期
最近做項(xiàng)目,遇到這樣的業(yè)務(wù)開(kāi)發(fā)需求,需要對(duì)比當(dāng)前時(shí)間段和去年同星期的時(shí)間段的數(shù)據(jù),下面小編通過(guò)實(shí)例代碼給大家分享Python根據(jù)當(dāng)前日期取去年同星期日期,需要的朋友參考下2019-04-04Python使用Kubernetes API訪問(wèn)集群
本文主要介紹了Python使用Kubernetes API訪問(wèn)集群,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05python求列表對(duì)應(yīng)元素的乘積和的實(shí)現(xiàn)
這篇文章主要介紹了python求列表對(duì)應(yīng)元素的乘積和的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04python環(huán)境的報(bào)錯(cuò)解決方法
這篇文章主要為大家介紹了python環(huán)境的報(bào)錯(cuò)解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08