OpenCV連通域數(shù)量統(tǒng)計(jì)學(xué)習(xí)示例
學(xué)習(xí)目標(biāo):
1.輸入圖像為分割結(jié)果圖像
2.根據(jù)種子填充法思路,遍歷圖像,得到每個(gè)連通域外接矩形坐標(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;
// 用于計(jì)算連通域的面積
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 >在此不進(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ì)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
pytorch finetuning 自己的圖片進(jìn)行訓(xùn)練操作
這篇文章主要介紹了pytorch finetuning 自己的圖片進(jìn)行訓(xùn)練操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Python中pandas庫sort_values()方法的使用
最后去看了有關(guān)于 sort_values 的文檔,成功解決先把單詞出現(xiàn)頻次由高往低依次排序,再把頻次相同的情況下的單詞按照 MD5 值排序這個(gè)問題,下面通過本文講解下Python中pandas庫sort_values()方法的使用,感興趣的朋友一起看看吧2023-07-07
利用Python+阿里云實(shí)現(xiàn)DDNS動(dòng)態(tài)域名解析的方法
這篇文章主要介紹了利用Python+阿里云實(shí)現(xiàn)DDNS動(dòng)態(tài)域名解析的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-04-04
從零開始安裝Conda并搭建Python環(huán)境的全過程
conda是一個(gè)開源的包、環(huán)境管理器,可以用于在同一個(gè)機(jī)器上創(chuàng)建不同的虛擬環(huán)境,這篇文章主要介紹了安裝Conda并搭建Python環(huán)境的相關(guān)資料,需要的朋友可以參考下2025-04-04
Python實(shí)現(xiàn)控制臺中的進(jìn)度條功能代碼
下面小編就為大家分享一篇Python實(shí)現(xiàn)控制臺中的進(jìn)度條功能代碼,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12
Python?Ruby?等語言棄用自增運(yùn)算符原因剖析
這篇文章主要為大家介紹了Python?Ruby?等語言棄用自增運(yùn)算符原因剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08

