opencv connectedcomponentswithstats
时间: 2023-09-08 14:04:21 浏览: 69
OpenCV的connectedComponentsWithStats函数是一个用于图像分割和连通组件标记的函数。该函数可以将二值图像中的连通组件识别出来,并返回每个连通组件的标记、面积、边界框等统计信息。
函数的调用方法如下:
```
retval, labels, stats, centroids = cv2.connectedComponentsWithStats(image, connectivity, ltype)
```
其中,image是输入的二值图像,connectivity是连通性,可以取4或8,ltype是输出标记的数据类型,可以取cv2.CV_32S或cv2.CV_16SC1。
函数的返回值包括:
- retval:连通组件的个数,包括背景组件。
- labels:标记图像,与输入图像大小一致,每个像素点的值表示该像素点所属的连通组件的标记。
- stats:统计信息,包含连通组件的面积、边界框的左上角坐标、宽度和高度等信息。stats[i]中的元素对应第i个连通组件的信息。
- centroids:质心坐标,包含每个连通组件的质心坐标(x,y)。
通过connectedComponentsWithStats函数可以方便地对图像进行分割和连通组件标记,并且可以利用返回的统计信息提取感兴趣的连通组件,比如面积最大的连通组件、宽高比最大的连通组件等。函数的灵活调用方式和提供的统计信息使得它在图像处理和计算机视觉应用中具有广泛的应用场景。
相关问题
c++ opencv connectedcomponentswithstats
`connectedComponentsWithStats` 是一个 OpenCV 中的函数,用于图像处理中的连通组件分析。它能够将一个二值化图像中的连通区域提取出来,并给出每个连通区域的统计信息。
这个函数的使用方法非常简单。首先,我们需要对图像进行二值化处理,以便将图像分为前景和背景。然后,我们可以调用 `connectedComponentsWithStats` 函数,传入二值化图像作为参数。该函数会返回包含连通区域的标签图像,以及每个连通区域的相关统计信息。
统计信息包括了每个连通区域的左上角坐标 `(x, y)`、宽度 `width`、高度 `height`,以及连通区域内的像素数量 `area`。我们可以通过遍历每个连通区域的标签值,并访问统计信息数组,来获取每个连通区域的详细信息。
值得注意的是,函数返回的标签图像中,连通区域的标签值从 1 开始,因为背景像素的标签值为 0。如果我们只对前景区域感兴趣,可以通过创建一个和原来图像大小相同但像素值全部为 0 的新图像,然后将连通区域标签值大于 0 的像素设置为 255,以获得只包含前景区域的二值化图像。
总之,`connectedComponentsWithStats` 是一个非常有用的函数,能够方便地实现对图像中连通区域的分析和提取。在图像分割、物体检测和图像识别等领域,都可以广泛应用。
基于OpenCV的connectedComponentsWithStats查找图像连通域,使用C++并进行从左上角到右下角的坐标排序
以下是基于OpenCV的connectedComponentsWithStats函数查找图像连通域,并按从左上角到右下角的坐标进行排序的C代码示例:
```c
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv)
{
Mat image = imread(argv[1], IMREAD_GRAYSCALE);
if (image.empty())
{
printf("Cannot read image file: %s\n", argv[1]);
return -1;
}
// Find connected components with statistics
Mat labels, stats, centroids;
int num_labels = connectedComponentsWithStats(image, labels, stats, centroids);
// Sort by top-left corner coordinates
for (int i = 1; i < num_labels; i++)
{
for (int j = i + 1; j < num_labels; j++)
{
if (stats.at<int>(i, CC_STAT_LEFT) > stats.at<int>(j, CC_STAT_LEFT) ||
(stats.at<int>(i, CC_STAT_LEFT) == stats.at<int>(j, CC_STAT_LEFT) &&
stats.at<int>(i, CC_STAT_TOP) > stats.at<int>(j, CC_STAT_TOP)))
{
// Swap labels
int temp_label = labels.at<int>(0, i);
labels.at<int>(0, i) = labels.at<int>(0, j);
labels.at<int>(0, j) = temp_label;
// Swap statistics
for (int k = 0; k < CC_STAT_MAX; k++)
{
int temp_stat = stats.at<int>(i, k);
stats.at<int>(i, k) = stats.at<int>(j, k);
stats.at<int>(j, k) = temp_stat;
}
// Swap centroids
for (int k = 0; k < 2; k++)
{
double temp_centroid = centroids.at<double>(i, k);
centroids.at<double>(i, k) = centroids.at<double>(j, k);
centroids.at<double>(j, k) = temp_centroid;
}
}
}
}
// Print statistics for each component
for (int i = 1; i < num_labels; i++)
{
printf("Component %d:\n", i);
printf(" Left: %d\n", stats.at<int>(i, CC_STAT_LEFT));
printf(" Top: %d\n", stats.at<int>(i, CC_STAT_TOP));
printf(" Width: %d\n", stats.at<int>(i, CC_STAT_WIDTH));
printf(" Height: %d\n", stats.at<int>(i, CC_STAT_HEIGHT));
printf(" Area: %d\n", stats.at<int>(i, CC_STAT_AREA));
printf(" Centroid: (%f, %f)\n", centroids.at<double>(i, 0), centroids.at<double>(i, 1));
}
return 0;
}
```
该代码使用OpenCV的connectedComponentsWithStats函数找到图像中的连通域,并使用嵌套循环按从左上角到右下角的顺序对它们进行排序。然后,对于每个连通域,该代码输出其统计信息,包括左上角坐标、宽度、高度、面积和质心。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)