Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

if you apply inRange on a grayscale image, it will get thresholded in the [0..20] range, and probably give you a total black binary image. then, findContours won't find any, and there things start to break apart.

even with hsv as input, you have to take into account, that findContours might return an empty array, and adjust your code to that.

    findContours(bw, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
    if (contours.size() > 0)
    {
         ... do your drawing stuff
    }
    imshow("Hsv", drawing);
    if (waitKey(30) >= 0) break;

if you apply inRange on a grayscale image, it will get thresholded in the [0..20] range, range (the scalar values you're using there, are for hsv, and not really suitable for grayscale), and probably give you a total black binary image. then, findContours won't find any, and there things start to break apart.

even with hsv as input, you have to take into account, that findContours might return an empty array, and adjust your code to that.

    findContours(bw, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
    if (contours.size() > 0)
    {
         ... do your drawing stuff
    }
    imshow("Hsv", drawing);
    if (waitKey(30) >= 0) break;

if you apply inRange on a grayscale image, it will get thresholded in the [0..20] range (the scalar values you're using there, are for hsv, and not really suitable for grayscale), and probably give you a total black binary image. then, findContours won't find any, and there things start to break apart.

even with hsv as input, you have to take into account, that findContours might return an empty array, and adjust your code to that.

    findContours(bw, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

    Mat drawing = Mat::zeros(img.size(), CV_8UC1);
    if (contours.size() > 0)
    {
         ... do your drawing stuff
    }
    imshow("Hsv", drawing);
    if (waitKey(30) >= 0) break;