Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Shape detection, why try several threshold levels?

I would like simply to know why in most of the shape detection algorythm the approach is search the contours by applying several threshold levels of binarization.

I'm referring in particular to this snippet of code extacted by the Squares.cpp sample:

...
// try several threshold levels
    for( int l = 0; l < N; l++ )
    {
        // hack: use Canny instead of zero threshold level.
        // Canny helps to catch squares with gradient shading
        if( l == 0 )
        {
            // apply Canny. Take the upper threshold from slider
            // and set the lower to 0 (which forces edges merging)
            Canny(gray0, gray, 0, thresh, 5);
            // dilate canny output to remove potential
            // holes between edge segments
            dilate(gray, gray, Mat(), Point(-1,-1));
        }
        else
        {
            // apply threshold if l!=0:
            //     tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0
            gray = gray0 >= (l+1)*255/N;
        }
        ...
 }

I understood the reason to use Canny when N is 0, but I can't say the same for the several threshold attempts. Any help will be very appreciated. Thanks.