Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Why a contour can't always be drawn filled?

Hello,

Sometimes when I find the contours of an image, I ask for it to be drawn with CV_FILLED, sometimes (most of the time), it doesn't fill properly the contour, but sometimes it does, why can explain this and how could I make sure it is filled everytime? In blue I plotted the first and the last Points of the contour.

Many thanks

Heberger image

Heberger image

Why a contour can't always be drawn filled?

Hello,

Sometimes when I find the contours of an image, I ask for it to be drawn with CV_FILLED, sometimes (most of the time), it doesn't fill properly the contour, but sometimes it does, why can explain this and how could I make sure it is filled everytime? In blue I plotted the first and the last Points of the contour.

Many thanks

Heberger image

Heberger image

Edit:

A minimal code would be the following:

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;

int main(int argc, char** argv)
{
    int lowThreshold = 50;
    int max_lowThreshold = 100;
    int ratio = 3;
    int kernel_size = 3;
    Mat src1 = imread("/path/to/img1.jpg");
    Mat src2 = imread("/path/to/img2.jpg");
    Mat src_gray1;
    Mat src_gray2;
    cvtColor(src1, src_gray1, CV_BGR2GRAY);
    cvtColor(src2, src_gray2, CV_BGR2GRAY);
    Mat edge1;
    Mat edge2;
    Mat dst1;
    Mat dst2;
    Mat detected_edges1;
    Mat detected_edges2;
    std::vector<std::vector<Point> > contours1, contours2;
    std::vector<Vec4i> hierarchy1, hierarchy2;
    edge1.create(src1.size(), src1.type());
    edge2.create(src2.size(), src2.type());
    dst1.create(src1.size(), src1.type());
    dst2.create(src2.size(), src2.type());
    blur(src_gray1, detected_edges1, Size(3,3));
    blur(src_gray2, detected_edges2, Size(3,3));
    Canny(detected_edges1, detected_edges1, lowThreshold, lowThreshold * ratio, kernel_size);
    Canny(detected_edges2, detected_edges2, lowThreshold, lowThreshold * ratio, kernel_size);
    findContours(detected_edges1, contours1, hierarchy1, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
    findContours(detected_edges2, contours2, hierarchy2, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
    drawContours(edge1, contours1, 0, Scalar(0, 0, 255), CV_FILLED, 8, hierarchy1);
    drawContours(edge2, contours2, 0, Scalar(0, 0, 255), CV_FILLED, 8, hierarchy2);
    imwrite("result1.jpg", edge1);
    imwrite("result2.jpg", edge2);
    return 0;
}

where the files "img1.jpg" and "img2.jpg" can be downloaded from here: http://hpics.li/ecb8b7a http://hpics.li/f1672f0