網站首頁 編程語言 正文
學習目標:
對二值圖進行分析,設定最大最小面積區間
保留該面積區間內的區域
示例代碼
//src為二值圖,minArea、maxArea為面積閾值,dest為結果圖像
void connectionAreaSelect(Mat src, int minArea, int maxArea, Mat &dest)
{
Mat labels, stats, centroids, img_color;
//連通域計算
int nccomps = connectedComponentsWithStats(
src, //二值圖像
labels,
stats,
centroids
);
//去除過小區域,初始化顏色表
vector<Vec3b> colors(nccomps);
colors[0] = Vec3b(0, 0, 0); // background pixels remain black.
for (int i = 1; i < nccomps; i++)
{
colors[i] = Vec3b(rand() % 256, rand() % 256, rand() % 256);
//面積閾值篩選
int holeArea = stats.at<int>(i, CC_STAT_AREA);
if ((holeArea < minArea) || (holeArea > maxArea))
{
colors[i] = Vec3b(0, 0, 0);
}
}
//按照label值,對不同的連通域進行著色
img_color = Mat::zeros(src.size(), CV_8UC3);
for (int y = 0; y < img_color.rows; y++)
{
for (int x = 0; x < img_color.cols; x++)
{
int label = labels.at<int>(y, x);
CV_Assert(0 <= label && label <= nccomps);
img_color.at<Vec3b>(y, x) = colors[label];
}
}
//統計降噪后的連通區域
Mat grayImg;
cvtColor(img_color, grayImg, COLOR_BGR2GRAY);
threshold(grayImg, grayImg, 1, 255, THRESH_BINARY);
dest = grayImg.clone();
labels.release();
stats.release();
centroids.release();
img_color.release();
grayImg.release();
}
原文鏈接:https://blog.csdn.net/ETNthrough/article/details/117467093
相關推薦
- 2022-09-13 一文詳解C語言中文件相關函數的使用_C 語言
- 2022-07-09 python如何給內存和cpu使用量設置限制_python
- 2023-10-16 基于lodop實現web端打印分頁樣式自定義配置需求
- 2022-07-04 python如何輸入根號_python
- 2022-05-21 python實現會員信息管理系統(List)_python
- 2023-10-12 利用touch-action解決驗證碼滑塊滑動時,背景跟隨一起滑動的問題,以及詳解touch-act
- 2022-06-16 golang?gorm的預加載及軟刪硬刪的數據操作示例_Golang
- 2022-12-09 C++輸出問題:保留兩位小數_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支