OpenCV連通域數(shù)量統(tǒng)計學(xué)習(xí)示例
學(xué)習(xí)目標(biāo):
1.輸入圖像為分割結(jié)果圖像
2.根據(jù)種子填充法思路,遍歷圖像,得到每個連通域外接矩形坐標(biāo)信息、面積信息
核心代碼
/*
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;
}
代碼執(zhí)行說明
<font color=#999AAA >在此不進行實例演示
1、 輸入圖像為分割后圖像
2、 執(zhí)行結(jié)果可根據(jù)featherList信息自行繪制矩形框
<hr style=" border:solid; width:100px; height:1px;" color=#000000 size=1">
以上就是OpenCV連通域數(shù)量統(tǒng)計學(xué)習(xí)示例的詳細內(nèi)容,更多關(guān)于OpenCV連通域數(shù)量統(tǒng)計的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python 用matplotlib畫以時間日期為x軸的圖像
這篇文章主要介紹了Python 用matplotlib畫以時間日期為x軸的圖像,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Python?Pandas中DataFrame.drop_duplicates()刪除重復(fù)值詳解
在實際處理數(shù)據(jù)中,數(shù)據(jù)預(yù)處理操作中,常常需要去除掉重復(fù)的數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于Python?Pandas中DataFrame.drop_duplicates()刪除重復(fù)值的相關(guān)資料,需要的朋友可以參考下2022-07-07
Python根據(jù)當(dāng)前日期取去年同星期日期
最近做項目,遇到這樣的業(yè)務(wù)開發(fā)需求,需要對比當(dāng)前時間段和去年同星期的時間段的數(shù)據(jù),下面小編通過實例代碼給大家分享Python根據(jù)當(dāng)前日期取去年同星期日期,需要的朋友參考下2019-04-04
python求列表對應(yīng)元素的乘積和的實現(xiàn)
這篇文章主要介紹了python求列表對應(yīng)元素的乘積和的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04

