当前位置: 首页 > news >正文

C++实现Matlab imregionalmax函数——查找区域最大值

C++实现imregionalmax函数

  • 1 说明
  • 2 示例
  • 3 C++实现

1 说明

Matlab imregionalmax

BW = imregionalmax(I)  

返回标识灰度图像 I 中区域最大值的二值图像BW。
区域最大值点是具有同一强度值的连通像素分量,其周围的像素强度值比该值低。
BW — 逻辑数组
区域最大值的位置,以与 I 大小相同的逻辑数组形式返回。值为 1 的像素表示区域最大值;所有其他像素设置为 0。

2 示例

在简单示例图像中寻找区域最大值

创建一个具有若干区域最大值的简单示例图像。

A = 10*ones(10,10);
A(2:4,2:4) = 22; 
A(6:8,6:8) = 33; 
A(2,7) = 44;
A(3,8) = 45;
A(4,9) = 44
A = 10×1010    10    10    10    10    10    10    10    10    1010    22    22    22    10    10    44    10    10    1010    22    22    22    10    10    10    45    10    1010    22    22    22    10    10    10    10    44    1010    10    10    10    10    10    10    10    10    1010    10    10    10    10    33    33    33    10    1010    10    10    10    10    33    33    33    10    1010    10    10    10    10    33    33    33    10    1010    10    10    10    10    10    10    10    10    1010    10    10    10    10    10    10    10    10    10

查找区域最大值。请注意,结果包括在 (3,8) 处的区域最大值。

regmax = imregionalmax(A)
regmax = 10x10 logical array0   0   0   0   0   0   0   0   0   00   1   1   1   0   0   0   0   0   00   1   1   1   0   0   0   1   0   00   1   1   1   0   0   0   0   0   00   0   0   0   0   0   0   0   0   00   0   0   0   0   1   1   1   0   00   0   0   0   0   1   1   1   0   00   0   0   0   0   1   1   1   0   00   0   0   0   0   0   0   0   0   00   0   0   0   0   0   0   0   0   0

3 C++实现

namespace MatlabCppCode {bool isPeak(cv::Mat mx[], std::vector<cv::Point>& conn_points){cv::Point poi_point = conn_points.back();int row = poi_point.y;int col = poi_point.x;float poi_val = mx[0].at<float>(poi_point);bool isPeakEle = true;for (int mask_row = row - 1; mask_row >= 0 && mask_row <= row + 1 && mask_row < mx[0].rows; mask_row++) {for (int mask_col = col - 1; mask_col >= 0 && mask_col <= col + 1 && mask_col < mx[0].cols; mask_col++) {if (mask_row == row && mask_col == col) {continue;}float conn_pt_val = mx[0].at<float>(mask_row, mask_col);if (poi_val < conn_pt_val) {isPeakEle = false;break;}if (poi_val == conn_pt_val) {int Peak_status = mx[1].at<uchar>(mask_row, mask_col);if (Peak_status == 0) {isPeakEle = false;break;}else if (Peak_status == 1) {isPeakEle = true;break;}else {cv::Point p(mask_col, mask_row);std::vector<cv::Point>::iterator it;it = std::find(conn_points.begin(), conn_points.end(), p);if (it == conn_points.end()) {conn_points.push_back(p);isPeakEle = isPeakEle && isPeak(mx, conn_points);}}}}if (isPeakEle == false) {break;}}return isPeakEle;}/*** @brief This is equivalent to imregionalmax(img, conn = 8) of Matlab* It takes floating point matrix, finds all local maximas and put them in 8UC1 matrix* pls refer: https://in.mathworks.com/help/images/ref/imregionalmax.html for imregionalmax* eg: I/P*      10    10    10    10    10    10    10    10    10    1010    22    22    22    10    10    44    10    10    1010    22    22    22    10    10    10    45    10    1010    22    22    22    10    10    10    10    44    1010    10    10    10    10    10    10    10    10    1010    10    10    10    10    33    33    33    10    1010    10    10    10    10    33    33    33    10    1010    10    10    10    10    33    33    33    10    1010    10    10    10    10    10    10    10    10    1010    10    10    10    10    10    10    10    10    10* O/P0   0   0   0   0   0   0   0   0   00   1   1   1   0   0   0   0   0   00   1   1   1   0   0   0   1   0   00   1   1   1   0   0   0   0   0   00   0   0   0   0   0   0   0   0   00   0   0   0   0   1   1   1   0   00   0   0   0   0   1   1   1   0   00   0   0   0   0   1   1   1   0   00   0   0   0   0   0   0   0   0   00   0   0   0   0   0   0   0   0   0* @param src* @param conn* @return cv::Mat*/cv::Mat imregionalmax(cv::Mat& src){//cv::Mat padded;//cv::copyMakeBorder(src, padded, 1, 1, 1, 1, cv::BORDER_CONSTANT, cv::Scalar::all(-1));cv::Mat padded;cv::copyMakeBorder(src, padded, 1, 1, 1, 1, cv::BORDER_REPLICATE);cv::Mat mx_ch1(padded.rows, padded.cols, CV_8UC1, cv::Scalar(2)); //Peak elements will be represented by 1, others by 0, initializing Mat with 2 for differentiation cv::Mat mx[2] = { padded, mx_ch1 }; //mx[0] is padded image, mx[1] is regional maxima matrixint mx_rows = mx[0].rows;int mx_cols = mx[0].cols;cv::Mat dest(mx[0].size(), CV_8UC1);//Check each pixel for local maxfor (int row = 1; row < mx_rows - 1; row++) {for (int col = 1; col < mx_cols - 1; col++) {std::vector<cv::Point> conn_points; //this vector holds all connected points for candidate pixelcv::Point p(col, row);conn_points.push_back(p);bool isPartOfPeak = isPeak(mx, conn_points);if (isPartOfPeak) {mx[1].at<uchar>(row, col) = 1;}else {mx[1].at<uchar>(row, col) = 0;}}}dest = mx[1](cv::Rect(1, 1, src.cols, src.rows));return dest;}
}
void test() {cv::Mat A = (cv::Mat_<float>(10, 10) <<10, 10, 10, 10, 10, 10, 10, 10, 10, 10,10, 22, 22, 22, 10, 10, 44, 10, 10, 10,10, 22, 22, 22, 10, 10, 10, 45, 10, 10,10, 22, 22, 22, 10, 10, 10, 10, 44, 10,10, 10, 10, 10, 10, 10, 10, 10, 10, 10,10, 10, 10, 10, 10, 33, 33, 33, 10, 10,10, 10,  9, 10, 10, 33, 33, 33, 10, 10,10,  9,  9,  9, 10, 33, 33, 33, 10, 10,10, 10,  9, 10, 10, 10, 10, 10, 10, 10,10, 10, 10, 10, 10, 10, 10, 10, 10, 10);//-- 查找区域最大值auto outFlags = MatlabCppCode::imregionalmax(A);std::cout <<"imregionalmax:\n"<< outFlags << std::endl;//-- 查找区域最小值cv::Mat revDataMat(10, 10, CV_32F);cv::normalize(A, revDataMat, 0, 1, cv::NORM_MINMAX);revDataMat = 1 - revDataMat;outFlags = MatlabCppCode::imregionalmax(revDataMat);std::cout << "imregionalmin:\n" << outFlags << std::endl;
}

在这里插入图片描述


http://www.mrgr.cn/news/5622.html

相关文章:

  • 游戏开发设计模式之策略模式
  • 容器篇(JavaSE - 双列集合)(持续更新迭代)
  • U盘车载专用音乐合集 3068首 24G
  • 校门外的树2贪心
  • D3js——数据绑定 datum
  • SQL注入(原理、分类、union、POST注入)
  • wangeditor报错Error: Cannot find a descendant at path [3] in node: {“children“:
  • 【开发工具】Git教程
  • in silico cloning 方法的具体步骤是什么?
  • 基于Modbus的MFC智能控制
  • 前端CSS选择器
  • Redis 详细介绍及安装使用教程(含 C# 示例)
  • 宠物空气净化器是智商税吗?性价比宠物空气净化器测评
  • 介绍 Apache Spark 的基本概念和在大数据分析中的应用
  • 剑指offer 30. 包含min函数的栈
  • Redis7基础篇(六)
  • MySQL支持的数据类型
  • nginx: [emerg] the “ssl“ parameter requires ngx_http_ssl_module in nginx.conf
  • 主机字节序和网络字节序
  • golang每日一库——casbin开源的访问控制框架