• opencv [c++] 连通域分析connectedComponentsWithStats() 和 connectedComponents()


    1. API相关参数介绍:

    labels :对原始图中的每一个像素都打上标签,背景为0,连通域打上1,2,3。。。的标签,同一个连通域的像素打上同样的标签。相当与对每一个像素进行了分类(分割)

    1. int cv::connectedComponents (
    2. cv::InputArrayn image, // input 8-bit single-channel (binary)
    3. cv::OutputArray labels, // output label map
    4. int connectivity = 8, // 4- or 8-connected components
    5. int ltype = CV_32S // Output label type (CV_32S or CV_16U)
    6. );
    7. int cv::connectedComponentsWithStats (
    8. cv::InputArrayn image, // input 8-bit single-channel (binary)
    9. cv::OutputArray labels, // output label map
    10. cv::OutputArray stats, // Nx5 matrix (CV_32S) of statistics:
    11. // [x0, y0, width0, height0, area0;
    12. // ... ; x(N-1), y(N-1), width(N-1),
    13. // height(N-1), area(N-1)]
    14. cv::OutputArray centroids, // Nx2 CV_64F matrix of centroids:
    15. // [ cx0, cy0; ... ; cx(N-1), cy(N-1)]
    16. int connectivity = 8, // 4- or 8-connected components
    17. int ltype = CV_32S // Output label type (CV_32S or CV_16U)
    18. );

     参数介绍如下:
    image:也就是输入图像,必须是二值图,即8位单通道图像。(因此输入图像必须先进行二值化处理才能被这个函数接受)
    connectivity:可选值为4或8,也就是使用4连通还是8连通。
    ltype:输出图像标记的类型,目前支持CV_32S 和 CV_16U。 返回值:
    返回值:
    num_labels:所有连通域的数目
    labels:图像上每一像素的标记,用数字1、2、3…表示(不同的数字表示不同的连通域)
    stats:每一个标记的统计信息,是一个5列的矩阵,每一行对应每个连通区域的外接矩形的x、y、width、height和面积,示例如下: 0 0 720 720 291805
    centroids:连通域的中心点

    2.设置连通域 connectedComponentsWithStats()

    1. cv::Mat src_img, img_bool, labels, stats, centroids, img_color, img_gray;
    2. int main()
    3. {
    4. Mat src_img = imread("1.png", 0);
    5. threshold(src_img, img_bool, 75, 255, THRESH_BINARY);
    6. //连通域计算
    7. int n = cv::connectedComponentsWithStats(
    8. img_bool, //二值图像
    9. labels, //和原图一样大的标记图
    10. stats, //nccomps×5的矩阵 表示每个连通区域的外接矩形和面积(pixel)
    11. centroids //nccomps×2的矩阵 表示每个连通区域的质心
    12. );
    13. //显示原图统计结果
    14. char title[1024];
    15. sprintf_s(title, "原图中连通区域数:%d\n", n);
    16. cv::String num_connect(title);
    17. imshow(num_connect, img_bool);
    18. //去除过小区域,初始化颜色表
    19. vector<cv::Vec3b> colors(n);
    20. colors[0] = cv::Vec3b(0, 0, 0); // background pixels remain black.
    21. for (int i = 1; i < n; i++) {
    22. colors[i] = cv::Vec3b(rand() % 256, rand() % 256, rand() % 256);
    23. //去除面积小于100的连通域
    24. if (stats.at<int>(i, cv::CC_STAT_AREA) < 800)
    25. colors[i] = cv::Vec3b(0, 0, 0); // small regions are painted with black too.
    26. }
    27. //按照label值,对不同的连通域进行着色
    28. img_color = cv::Mat::zeros(src_img.size(), CV_8UC3);
    29. for (int y = 0; y < img_color.rows; y++)
    30. for (int x = 0; x < img_color.cols; x++)
    31. {
    32. int label = labels.at<int>(y, x);
    33. CV_Assert(0 <= label && label <= n);
    34. img_color.at<cv::Vec3b>(y, x) = colors[label];
    35. }
    36. //统计降噪后的连通区域
    37. cvtColor(img_color, img_gray, cv::COLOR_BGR2GRAY);
    38. threshold(img_gray, img_gray, 1, 255, cv::THRESH_BINARY);
    39. n = cv::connectedComponentsWithStats(img_gray, labels, stats, centroids);
    40. sprintf_s(title, "过滤小目标后的连通区域数量:%d\n", n);
    41. num_connect = title;
    42. imshow(num_connect, img_color);
    43. waitKey(0);
    44. }
    45. }


     精简版【参考网上资料】:

    1. #include<opencv2\opencv.hpp>
    2. #include<algorithm>
    3. #include<iostream>
    4. using namespace std;
    5. using namespace cv;
    6. Mat src, src_color,g_src, labels, stats, centroids;
    7. int g_threshold = 30;
    8. void trackbar(int, void*);
    9. int main() {
    10. src = imread("133.png", 0);
    11. namedWindow("src", 1);
    12. createTrackbar("threshold", "src", &g_threshold, 255, trackbar);
    13. imshow("src", src);
    14. threshold(src, g_src, 170, 255, THRESH_BINARY);
    15. imshow("d", g_src);
    16. int num = connectedComponentsWithStats(g_src, labels, stats, centroids);
    17. cout <<"轮廓数" << num << endl;
    18. vector<Vec3b> color(num + 1);
    19. color[0] = Vec3b(0, 0, 0);//背景色
    20. for (int m = 1; m <=num ; m++) {
    21. color[m] = Vec3b(rand() % 256, rand() % 256, rand() % 256);
    22. if (stats.at<int>(m - 1, CC_STAT_AREA) < 100)//连通域面积小于100的区域,将其当作背景
    23. color[m] = Vec3b(0, 0, 0);
    24. }
    25. src_color=Mat::zeros(src.size(), CV_8UC3);
    26. for (int x = 0; x < src.rows; x++)
    27. for (int y = 0; y < src.cols; y++)
    28. {
    29. int label = labels.at<int>(x, y);//注意labels是int型,不是uchar.
    30. src_color.at<Vec3b>(x, y) = color[label];
    31. }
    32. imshow("labelMap", src_color);
    33. waitKey(0);
    34. }
    35. void trackbar(int, void*) {
    36. threshold(src, g_src, g_threshold, 255, THRESH_BINARY_INV);
    37. imshow("d", g_src);
    38. int num = connectedComponentsWithStats(g_src, labels, stats, centroids);
    39. cout << "轮廓数" << num << endl;
    40. vector<Vec3b> color(num + 1);
    41. color[0] = Vec3b(0, 0, 0);//背景色
    42. for (int m = 1; m <= num; m++) {
    43. color[m] = Vec3b(rand() % 256, rand() % 256, rand() % 256);
    44. //if (stats.at<int>(m - 1, CC_STAT_AREA) < 30)
    45. //color[m] = Vec3b(0, 0, 0);
    46. }
    47. src_color = Mat::zeros(src.size(), CV_8UC3);
    48. for (int x = 0; x < src.rows; x++)
    49. for (int y = 0; y < src.cols; y++)
    50. {
    51. int label = labels.at<int>(x, y);//注意labels是int型,不是uchar.
    52. src_color.at<Vec3b>(x, y) = color[label];
    53. }
    54. imshow("labelMap", src_color);
    55. }

    connectedComponents()用的较少,和上述用法大致相同。

  • 相关阅读:
    初级项目经理 如何快速提升能力?
    Gartner最新报告:低代码应用开发平台在国内的发展
    如何在一台电脑上安装两个mysql
    oracle创建数据库,导入dmp操作全家桶
    Vue.js+Node.js全栈开发教程:Vue.js指令参数详解
    HTML旅游景点网页作业制作——旅游中国11个页面(HTML+CSS+JavaScript)
    Java项目:ssm赛事打分系统
    11/20总结报告
    深度学习——线性神经网络二
    精准突击!GitHub星标103k,2023年整理1658页JAVA秋招面试题
  • 原文地址:https://blog.csdn.net/weixin_50016546/article/details/125435749