網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
學(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)值,依此類推
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">
原文鏈接:https://blog.csdn.net/ETNthrough/article/details/117444020
相關(guān)推薦
- 2023-12-02 vscode無(wú)法連接寶塔ftp排雷
- 2022-11-04 C++淺析內(nèi)存分區(qū)模型概念與示例_C 語(yǔ)言
- 2022-06-28 Go并發(fā)編程sync.Cond的具體使用_Golang
- 2022-10-16 docker保存鏡像到本地并加載本地鏡像文件詳解_docker
- 2022-12-27 刪除Helm使用時(shí)關(guān)于kubernetes文件的警告問(wèn)題_云其它
- 2022-03-11 部署.Net6項(xiàng)目到docker_基礎(chǔ)應(yīng)用
- 2022-04-20 .NET?Core使用Worker?Service創(chuàng)建服務(wù)_實(shí)用技巧
- 2022-03-19 Android使用DocumentFile讀寫(xiě)外置存儲(chǔ)的問(wèn)題_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支