您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“OpenCV連通域數(shù)量統(tǒng)計實(shí)例分析”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“OpenCV連通域數(shù)量統(tǒng)計實(shí)例分析”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。
/* Input: src: 待檢測連通域的二值化圖像 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; // 用于計算連通域的面積 int area = 0; // 連通域的左邊界,即外接最小矩形的左邊框,橫坐標(biāo)值,依此類推 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; }
<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)計實(shí)例分析”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點(diǎn)還需要大家自己動手實(shí)踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。