How to Prevent drawContours() to draw strange wrong line?
I have found some rule about this problem, this wrong line connects two point and do not cross any others contour points.
It seams that drawContours prefer convex shape, but I am not very sure about it.
std::vector<std::vector<Point>> collision_for_mask2_contours;
test for this variable, I have put it in bmp image in accessory file , you can get points from it for drawContours. Thank you very much!
int main(int argc, char* argv[]){
// test code for drawContours
std::vector<std::vector<Point>> collision_for_mask2_contours;
std::vector<Point> constours;
Mat_<uchar> testbmp = imread("G:/test.bmp", WINDOW_AUTOSIZE);
namedWindow("testbmp");
imshow("testbmp", testbmp);
waitKey(0); // need to press any key to continue
for (int y = 0; y < testbmp.rows; ++y) {
for (int x = 0; x < testbmp.cols; ++x) {
if (testbmp(y, x)) {
constours.push_back(Point(x, y));
}
}
}
collision_for_mask2_contours.push_back(constours);
Mat_<uchar> testForDrawContours = Mat_<uchar>::zeros(testbmp.size());
// one case, can not get concave shape
drawContours(testForDrawContours, collision_for_mask2_contours, -1, Scalar(255));
namedWindow("testForDrawContours", WINDOW_AUTOSIZE);
imshow("testForDrawContours", testForDrawContours);
waitKey(0);
// one another case, also can not get concave shape
drawContours(testForDrawContours, collision_for_mask2_contours, -1, Scalar(255), -1, 8);
namedWindow("testForDrawContours2", WINDOW_AUTOSIZE);
imshow("testForDrawContours2", testForDrawContours);
waitKey(0);}
I cannot reproduce your issue. Give full code and check if you can reproduce using image http://answers.opencv.org/upfiles/153...
HI @LBerger, I have add test code for test.bmp, and showed the problem, you can test it, thank you. drawContours(testForDrawContours, collision_for_mask2_contours, -1, Scalar(255)); It seams that drawContours treat concave shape as one line.
WINDOW_AUTOSIZE == 1 , wrong flag for imread() anyway. but now you have a 3 channel img,but assign it to a
Mat_<uchar>
also, please use findContours() ,and avoid writing per-pixel loops.
again, there is no problem in drawContours(), it's the rest of your code, which is broken
a contour is not only pixel with non zero value, it is too a sorted list of point : you must follow contour :
imread("G:/test.bmp", 1); yes, 1 is right to get gray image.
I find drawContours must be used along with findContours, maybe I have not understand contours and its data structure.
no, IMREAD_GRAYSCALE (0) would be the correct flag here !
yes, you are right, I find Image file reading and writing