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
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
Could it be because the contour is not always surrounding the area, but its contour?
Thanks for the reply. Actually, I'm not sure I understand what you suggested. I don't really know how drawContours works internally.
What I try to say s that in your firs image, the contour is just around the red area, that is your contour is just around a region that is a contour, like a gradient, so the white is just a line around the object, while in the second one the contour is around the whole object
could you show a real example image and your code.
I edited the question with more details. I should have done that earlier, thanks for the suggestion.
The reason of failure is due to the fact that there is no closed contour of the object and thus the edge gets a closed loop around the edge itself from inside to outside. You should have a closed blob for the contours to work like you are expecting. This behaviour is perfectly normal.