網(wǎng)站首頁 編程語言 正文
學(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">
原文鏈接:https://blog.csdn.net/ETNthrough/article/details/117444020
相關(guān)推薦
- 2022-03-23 c++特殊構(gòu)造函數(shù)詳解_C 語言
- 2022-06-29 詳解Shell腳本中^M的問題和解決方案_linux shell
- 2022-03-09 軟件構(gòu)建工具makefile基礎(chǔ)講解_C 語言
- 2022-03-29 C語言的基本編寫規(guī)范你了解嗎_C 語言
- 2022-02-18 Ubuntu重啟后nvidia-smi命令報錯NVIDIA-SMI has failed becau
- 2022-03-23 QT實現(xiàn)定時關(guān)閉消息提示框_C 語言
- 2021-12-12 七大經(jīng)典排序算法圖解_C 語言
- 2022-10-24 React報錯之Parameter?event?implicitly?has?an?any?type
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支