src/ios/CVSquares.cpp
// returns sequence of squares detected on the image.
// the sequence is stored in the specified memory storage
//static void findSquares( const Mat& image, vector<vector<Point> >& squares )
returns sequence of squares detected on the image.
the sequence is stored in the specified memory storage
static void findSquares( const Mat& image, vector >& squares )
returns sequence of squares detected on the image . the sequence is stored in the specified memory storage static void findSquares ( const Mat& image vector > & squares )
static void findSquares(  const Mat& image,   vector<vector<Point> >& squares )
{
    squares.clear();
    Mat pyr, timg, gray0(image.size(), CV_8U), gray;
    pyrDown(image, pyr, Size(image.cols/2, image.rows/2));
    pyrUp(pyr, timg, image.size());
    vector<vector<Point> > contours;
    int planes = 1;
    int canny = 0;
    if (accuracy) {
        planes = 4;
        canny = 1;
    }
    for( int c = 0; c < planes; c++ )
        {
        int ch[] = {c, 0};
        mixChannels(&timg, 1, &gray0, 1, ch, 1);
        for( int l = 0; l < N; l++ )
            {
            if( l == 0 && canny == 1 )
                {
                Canny(gray0, gray, 0, thresh, 5);
                dilate(gray, gray, Mat(), Point(-1,-1));
                }
            else
                {
                gray = gray0 >= (l+1)*255/N;
                }
            findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
            vector<Point> approx;
            for( size_t i = 0; i < contours.size(); i++ )
                {
                approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);
                if( approx.size() == 4 &&
                   fabs(contourArea(Mat(approx))) > 1000 &&
                   isContourConvex(Mat(approx)) )
                    {
                    double maxCosine = 0;
                    for( int j = 2; j < 5; j++ )
                        {
                        double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
                        maxCosine = MAX(maxCosine, cosine);
                        }
                    if( maxCosine < tolerance )
                        squares.push_back(approx);
                    }
                }
            }
        }
}
static void findSquares ( const Mat & image , vector < vector < Point > > & squares ) { squares . clear ( ) ; Mat pyr , timg , gray0 ( image . size ( ) , CV_8U ) , gray ; pyrDown ( image , pyr , Size ( image . cols / 2 , image . rows / 2 ) ) ; pyrUp ( pyr , timg , image . size ( ) ) ; vector < vector < Point > > contours ; int planes = 1 ; int canny = 0 ; if ( accuracy ) { planes = 4 ; canny = 1 ; } for ( int c = 0 ; c < planes ; c ++ ) { int ch [ ] = { c , 0 } ; mixChannels ( & timg , 1 , & gray0 , 1 , ch , 1 ) ; for ( int l = 0 ; l < N ; l ++ ) { if ( l == 0 && canny == 1 ) { Canny ( gray0 , gray , 0 , thresh , 5 ) ; dilate ( gray , gray , Mat ( ) , Point ( -1 , -1 ) ) ; } else { gray = gray0 >= ( l + 1 ) * 255 / N ; } findContours ( gray , contours , CV_RETR_LIST , CV_CHAIN_APPROX_SIMPLE ) ; vector < Point > approx ; for ( size_t i = 0 ; i < contours . size ( ) ; i ++ ) { approxPolyDP ( Mat ( contours [ i ] ) , approx , arcLength ( Mat ( contours [ i ] ) , true ) * 0.02 , true ) ; if ( approx . size ( ) == 4 && fabs ( contourArea ( Mat ( approx ) ) ) > 1000 && isContourConvex ( Mat ( approx ) ) ) { double maxCosine = 0 ; for ( int j = 2 ; j < 5 ; j ++ ) { double cosine = fabs ( angle ( approx [ j % 4 ] , approx [ j - 2 ] , approx [ j - 1 ] ) ) ; maxCosine = MAX ( maxCosine , cosine ) ; } if ( maxCosine < tolerance ) squares . push_back ( approx ) ; } } } } }
returns sequence of squares detected on the image.
returns sequence of squares detected on the image .
// down-scale and upscale the image to filter out the noise // find squares in every color plane of the image // try several threshold levels // hack: use Canny instead of zero threshold level. // Canny helps to catch squares with gradient shading // apply Canny. Take the upper threshold from slider // and set the lower to 0 (which forces edges merging) // dilate canny output to remove potent5ial // holes between edge segments // apply threshold if l!=0: //     tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0 // find contours and store them all as a list // test each contour // approximate contour with accuracy proportional // to the contour perimeter // square contours should have 4 vertices after approximation // relatively large area (to filter out noisy contours) // and be convex. // Note: absolute value of an area is used because // area may be positive or negative - in accordance with the // contour orientation // find the maximum cosine of the angle between joint edges // if cosines of all angles are small // (all angles are ~90 degree) then write quandrange // vertices to resultant sequence