findcontours发现太多轮廓

时间:2015-01-31 15:57:22

标签: opencv

找到轮廓找到很多轮廓,显然只有3个轮廓,这是正常的吗?

原始图像和绘图后发现的轮廓如下所示: enter image description here

enter image description here

代码是:

Mat image;
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    image = imread(argv[1], 0);   // Read the file
    findContours(image, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
    cout << contours.size();


    RNG rng(12345);
    Mat drawing = Mat::zeros(image.size(), CV_8UC3);
    for (int i = 0; i< contours.size(); i++)
    {
        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
        drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point());
    }
    /// Show in a window
    namedWindow("Contours", CV_WINDOW_AUTOSIZE);
    imshow("Contours", drawing);
namedWindow("Display window", WINDOW_AUTOSIZE);// Create a window for display.
    imshow("Display window", image);                   // Show our image inside it.

1 个答案:

答案 0 :(得分:3)

我认为您正在阅读Jpeg或实际输入图像的任何其他压缩版本,这会增加这些噪音。查找轮廓需要二进制图像。如果要在查找轮廓之前处理此图像,请简单应用阈值!请注意,您的源图像对我来说工作正常! enter image description here