OpenCV 您所在的位置:网站首页 最大连通域是什么 OpenCV

OpenCV

2023-09-08 22:12| 来源: 网络整理| 查看: 265

下文主要内容来自《Learning OpenCV 3》page417-419和官方文档 

在OpenCV 3中提供了两个很好的函数,在OpenCV 2中没有。 

(1)cv::connectedComponents()

(2)cv::connectedComponentsWithStats()

对应的官方文档为:https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga107a78bf7cd25dec05fb4dfc5c9e765f

int cv::connectedComponents ( cv::InputArrayn image, // input 8-bit single-channel (binary) cv::OutputArray labels, // output label map int connectivity = 8, // 4- or 8-connected components int ltype = CV_32S // Output label type (CV_32S or CV_16U) ); int cv::connectedComponentsWithStats ( cv::InputArrayn image, // input 8-bit single-channel (binary) cv::OutputArray labels, // output label map cv::OutputArray stats, // Nx5 matrix (CV_32S) of statistics: // [x0, y0, width0, height0, area0; // ... ; x(N-1), y(N-1), width(N-1), // height(N-1), area(N-1)] cv::OutputArray centroids, // Nx2 CV_64F matrix of centroids: // [ cx0, cy0; ... ; cx(N-1), cy(N-1)] int connectivity = 8, // 4- or 8-connected components int ltype = CV_32S // Output label type (CV_32S or CV_16U) ); 其中 connectedComponents()仅仅创建了一个标记图(图中不同连通域使用不同的标记,和原图宽高一致), connectedComponentsWith Stats()也可以完成上面任务,除此之外,还可以返回每个连通区域的重要信息-- bounding box, area, and center of mass( centroid). 如果不需要连通域的质心,将参数centroids设置为 cv::noArray() ,这句话在我的版本中运行会出错。

函数返回值为连通区域的总数N,范围为[0,N-1],其中0代表背景。

下面是一个简单的示例,画出了带标记的连通区域,同时去除很小的连通域(相当于无损降噪)。

#include #include #include using namespace std; int main() { cv::Mat src_img, img_bool, labels, stats, centroids, img_color, img_gray; if( (src_img = cv::imread("2.bmp",0)).empty()) { cout


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有