简体   繁体   English

OpenCV 3.0已连接组件,无法使其正常工作

[英]OpenCV 3.0 Connected components, can't get it to work properly

I have the following code, it's just a simple test program to learn how to use the connected components functionality in openCV 3.0 我有以下代码,它只是一个简单的测试程序,用于学习如何在openCV 3.0中使用连接组件功能

int main(int argc, char** argv) {

char* line = argv[1];

Mat image;

image = imread(line,CV_LOAD_IMAGE_GRAYSCALE);   

cv::Mat label=Mat(image.size(),CV_16U);

int la=connectedComponents(image,label, 8,CV_16U);

    //tried also: label.convertTo(label,CV_8U,255);   
   // and label.convertTo(label,CV_16U,255);

namedWindow( "input", CV_WINDOW_AUTOSIZE );
imshow( "input", image);

    namedWindow( "ouput", CV_WINDOW_AUTOSIZE );
    imshow("output", label);
    cout<<la<<"\n";

    imwrite("output.png", label);
    waitKey(0);
    return 0;
    }

The input image is a color image with two red squares on a white background. 输入图像是在白色背景上具有两个红色正方形的彩色图像。 The image is correctly loaded and displayed as a grayscale image. 图像被正确加载并显示为灰度图像。 程序outoput

The thing is no matter what I do, the output is always a blank image, either black or white, depending on the convertTo paremeters. 无论我做什么,输出总是一个空白图像,黑色或白色,取决于convertTo参数。

However, the value returned by connectedComponents is 2. 但是, connectedComponents返回的值为2。

I tried the full code proposed by Miki, I get this: 我尝试了Miki提出的完整代码,我得到了这个:

在此输入图像描述

I thought the issue could be that connected Components is not working properly. 我认为问题可能是连接的组件无法正常工作。

Tried with a picture I had on my desktop and finally got something: 尝试了我在桌面上的图片,最后得到了一些东西:

在此输入图像描述

However, this time the source image is a regular picture with people, buildings, cars...and most of the output is still blank. 然而,这次源图像是人,建筑物,汽车的常规图像......并且大部分输出仍然是空白的。 Does anybody know why? 有人知道为什么吗?

After adding image = image < 200; 添加image = image < 200;

在此输入图像描述

With applyColorMap(seeMyLabels, seeMyLabels, COLORMAP_JET); 使用applyColorMap(seeMyLabels, seeMyLabels, COLORMAP_JET); , the labels image goes from almost black grayscale to blue shades ,标签图像从几乎黑色灰度变为蓝色

在此输入图像描述

Results are there, you just can't see them! 结果在那里,你只是看不到它们!


label contains the index of each label. label包含每个标签的索引。 This means that, in this case, in label you have pixel with values: 这意味着,在这种情况下,在标签中,您有像素值:

0 : background
1 : first connected component
2 : second connected component

so your image is quite dark. 所以你的形象很暗。 Just for visualization purposes, you can add this line: 仅出于可视化目的,您可以添加以下行:

Mat seeMyLabels;
normalize(label, seeMyLabels, 0, 255, NORM_MINMAX, CV_8U);
imshow("Labels", seeMyLabels);

that will scale the values to a visible range. 这会将值缩放到可见范围。

Note that the image should be a binary image, with black background and white foreground. 请注意,图像应为二进制图像,黑色背景和白色前景。

Full code: 完整代码:

#include <opencv2\opencv.hpp>
using namespace cv;

int main(int argc, char** argv)
{
    if (argc != 2) return -1;

    Mat image = imread(argv[1], IMREAD_GRAYSCALE);

    if (image.empty()) return -1;

    Mat label;
    int la = connectedComponents(image, label, 8, CV_16U);

    Mat seeMyLabels;
    normalize(label, seeMyLabels, 0, 255, NORM_MINMAX, CV_8U);

    // You can apply a colormap for better visualization
    //applyColorMap(seeMyLabels, seeMyLabels, COLORMAP_JET);

    imshow("Labels", seeMyLabels);
    imshow("input", image);
    imshow("output", label);
    waitKey(0);

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM