在OpenCV 3中提供了两个很好的函数,在OpenCV 2中没有。
(1)connectedComponents()
(2)connectedComponentsWithStats()
对应的官方文档为:https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga107a78bf7cd25dec05fb4dfc5c9e765f
函数原型:
int cv::connectedComponents (
cv::InputArrayn image, // //8位单通道二值图像 (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;//nccomps×5的矩阵 表示每个连通区域的外接矩形和面积(pixel)
// ... ; x(N-1), y(N-1), width(N-1), height(N-1), area(N-1)]
cv::OutputArray centroids, //nccomps×2的矩阵 表示每个连通区域的质心(pixel)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()仅仅创建了一个标记图(图中不同连通域使用不同的标记,和原图宽高一致),connectedComponentsWithStats()可以完成上面任务,除此之外,还可以返回每个连通区域的重要信息--bounding box, area, andcentroid
参数说明:
该函数有返回值,返回一个int整型 nccomps,函数返回值为连通区域的总数N,范围为[0,N-1],其中0代表背景。
image:输入8位单通道二值图像;
label:输出,和原图image一样大的标记图,label对应于表示是当前像素是第几个轮廓,背景置0;
centroids:对应的是轮廓的中心点。nccomps×2的矩阵 表示每个连通区域的质心
stats:输出,nccomps×5的矩阵 ,表示每个连通区域的外接矩形和面积(pixel),例如下列:
分别对应各个轮廓的x,y,width,height和面积。注意0的区域标识的是background
例子:from:https://www.cnblogs.com/jsxyhelu/p/7439655.html
对上图进行连通域分析,看labels 和 stats,其中第1 2 6 个的面积小于200
而labels中
完全对的上号,结果为
实例:
对图像进行连通域分析,将面积小于100的区域作为背景,进行剔除,去除很小的连通域(相当于无损降噪)
#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);
}
结果:
from:https://blog.csdn.net/i_chaoren/article/details/78358297