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

1. API相关参数介绍:

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

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)
);

 参数介绍如下:
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()

cv::Mat src_img, img_bool, labels, stats, centroids, img_color, img_gray;
   
int main()
{

 Mat src_img = imread("1.png", 0);
	threshold(src_img, img_bool, 75, 255, THRESH_BINARY);
    //连通域计算
    int n = cv::connectedComponentsWithStats(
        img_bool, //二值图像
        labels,     //和原图一样大的标记图
        stats, //nccomps×5的矩阵 表示每个连通区域的外接矩形和面积(pixel)
        centroids //nccomps×2的矩阵 表示每个连通区域的质心
    );
    //显示原图统计结果
    char title[1024];
    sprintf_s(title, "原图中连通区域数:%d\n", n);
    cv::String num_connect(title);
    imshow(num_connect, img_bool);

    //去除过小区域,初始化颜色表
    vector<cv::Vec3b> colors(n);
    colors[0] = cv::Vec3b(0, 0, 0); // background pixels remain black.
    for (int i = 1; i < n; i++) {
        colors[i] = cv::Vec3b(rand() % 256, rand() % 256, rand() % 256);
        //去除面积小于100的连通域
        if (stats.at<int>(i, cv::CC_STAT_AREA) < 800)
            colors[i] = cv::Vec3b(0, 0, 0); // small regions are painted with black too.
    }
    //按照label值,对不同的连通域进行着色
    img_color = cv::Mat::zeros(src_img.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 <= n);
            img_color.at<cv::Vec3b>(y, x) = colors[label];
        }

    //统计降噪后的连通区域
    cvtColor(img_color, img_gray, cv::COLOR_BGR2GRAY);
    threshold(img_gray, img_gray, 1, 255, cv::THRESH_BINARY);
    n = cv::connectedComponentsWithStats(img_gray, labels, stats, centroids);
    sprintf_s(title, "过滤小目标后的连通区域数量:%d\n", n);
    num_connect = title;
    imshow(num_connect, img_color);
    waitKey(0);

}
}


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

#include<opencv2\opencv.hpp>
#include<algorithm>
#include<iostream>
using namespace std;
using namespace cv;
Mat src, src_color,g_src, labels, stats, centroids;
int g_threshold = 30;
void trackbar(int, void*);
int main() {
	src = imread("133.png", 0);
	namedWindow("src", 1);
	createTrackbar("threshold", "src", &g_threshold, 255, trackbar);
	imshow("src", src);
 
	threshold(src, g_src, 170, 255, THRESH_BINARY);
	imshow("d", g_src);
	int num = connectedComponentsWithStats(g_src, labels, stats, centroids);
	cout <<"轮廓数" << num << endl;
	vector<Vec3b> color(num + 1);
	color[0] = Vec3b(0, 0, 0);//背景色
	for (int m = 1; m <=num ; m++) {
		color[m] = Vec3b(rand() % 256, rand() % 256, rand() % 256);
		if (stats.at<int>(m - 1, CC_STAT_AREA) < 100)//连通域面积小于100的区域,将其当作背景
			color[m] = Vec3b(0, 0, 0);
	}
	 src_color=Mat::zeros(src.size(), CV_8UC3);
	for (int x = 0; x < src.rows; x++)
		for (int y = 0; y < src.cols; y++)
		{
			int label = labels.at<int>(x, y);//注意labels是int型,不是uchar.
			src_color.at<Vec3b>(x, y) = color[label];
		}
	imshow("labelMap", src_color);
 
	waitKey(0);
}
void trackbar(int, void*) {
	threshold(src, g_src, g_threshold, 255, THRESH_BINARY_INV);
	imshow("d", g_src);
	int num = connectedComponentsWithStats(g_src, labels, stats, centroids);
	cout << "轮廓数" << num << endl;
	vector<Vec3b> color(num + 1);
	color[0] = Vec3b(0, 0, 0);//背景色
	for (int m = 1; m <= num; m++) {
		color[m] = Vec3b(rand() % 256, rand() % 256, rand() % 256);
		//if (stats.at<int>(m - 1, CC_STAT_AREA) < 30)
			//color[m] = Vec3b(0, 0, 0);
	}
	src_color = Mat::zeros(src.size(), CV_8UC3);
	for (int x = 0; x < src.rows; x++)
		for (int y = 0; y < src.cols; y++)
		{
			int label = labels.at<int>(x, y);//注意labels是int型,不是uchar.
			src_color.at<Vec3b>(x, y) = color[label];
		}
	imshow("labelMap", src_color);
	
}

 😃带统计信息的API实现 【centroid  and  stats】


int main(int argc, char** argv)
{
    RNG rng(12345);
    Mat src, src_binary, dst;
    src = imread("维生素片检测和计数.png");
    imshow("原图片", src);
    Mat kernel = getStructuringElement(MORPH_RECT, Size(18, 18), Point(-1, -1));    
    morphologyEx(src, dst, MORPH_OPEN, kernel);
    imwrite("D:/111.png", dst);
    imshow("形态学", dst);
    cvtColor(dst, dst, COLOR_RGB2GRAY);
    threshold(dst, src_binary, 100, 255, THRESH_OTSU);
    imshow("二值化", src_binary);
    Mat labels = Mat::zeros(src.size(), CV_32S);
    //连通域分析
    Mat stats, centroids;//统计信息存放
    int num_labels = connectedComponentsWithStats(src_binary, labels, stats, centroids, 8, 4);
    vector<Vec3b> colors(num_labels);
    //背景颜色(黑色)
    colors[0] = Vec3b(0, 0, 0);
    // 区域颜色(随机)
    for (int i = 1; i < num_labels; i++) {
        colors[i] = Vec3b(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256));
    }
    //显示
    Mat dst1 = Mat::zeros(src.size(), src.type());
    int w = src.cols;
    int h = src.rows;
    for (int row = 0; row < h; row++) {
        for (int col = 0; col < w; col++) {
            int label = labels.at<int>(row, col);
            if (label == 0) continue;
            dst1.at<Vec3b>(row, col) = colors[label];
        }
    }
    for (int i = 1; i < num_labels; i++) {
        Vec2d pt = centroids.at<Vec2d>(i, 0);
        int x = stats.at<int>(i, CC_STAT_LEFT);
        int y = stats.at<int>(i, CC_STAT_TOP);
        int width = stats.at<int>(i, CC_STAT_WIDTH);
        int height = stats.at<int>(i, CC_STAT_HEIGHT);
        int area = stats.at<int>(i, CC_STAT_AREA);
        printf("area : %d, center point(%.2f, %.2f)\n", area, pt[0], pt[1]);//面积信息
        circle(dst1, Point(pt[0], pt[1]), 2, Scalar(0, 0, 255), -1, 8, 0);//中心点坐标
        rectangle(dst1, Rect(x, y, width, height), Scalar(255, 0, 255), 1, 8, 0);//外接矩形
    }

    imshow("带统计的连通域", dst1);
    waitKey(0);
    return 0;
}

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NCUTer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值