Detecting squares in an image

asked 2014-04-14 05:55:22 -0600

john1991 gravatar image

I found this square detection code online and I'm trying to understand it, does anyone understand what the line at the end does? it states: "gray = gray0 >= (l+1)*255/N;"

Mat pyr, timg, gray0(image.size(), CV_8U), gray;

// down-scale and upscale the image to filter out the noise
pyrDown(image, pyr, Size(image.cols/2, image.rows/2));
pyrUp(pyr, timg, image.size());
vector<vector<Point> > contours;

// find squares in every color plane of the image
for( int c = 0; c < 3; c++ )
{
    int ch[] = {c, 0};
    mixChannels(&timg, 1, &gray0, 1, ch, 1);

    // 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;
        }
edit retag flag offensive close merge delete

Comments

4

// apply threshold

the c++ Mat api has overloaded operators for <, >, &, | , so you indeed can write:

m = m>128; // this will set anything below128 to 0, anything above to 255

berak gravatar imageberak ( 2014-04-14 06:06:23 -0600 )edit