Update to 2.0.0 tree from current Fremantle build
[opencv] / samples / c / squares.c
index fe9f363..79f2b7b 100644 (file)
@@ -7,6 +7,8 @@
 #pragma package <opencv>
 #endif
 
+#define CV_NO_BACKWARD_COMPATIBILITY
+
 #include "cv.h"
 #include "highgui.h"
 #include <stdio.h>
@@ -21,7 +23,7 @@ const char* wndname = "Square Detection Demo";
 
 // helper function:
 // finds a cosine of angle between vectors
-// from pt0->pt1 and from pt0->pt2 
+// from pt0->pt1 and from pt0->pt2
 double angle( CvPoint* pt1, CvPoint* pt2, CvPoint* pt0 )
 {
     double dx1 = pt1->x - pt0->x;
@@ -39,7 +41,7 @@ CvSeq* findSquares4( IplImage* img, CvMemStorage* storage )
     int i, c, l, N = 11;
     CvSize sz = cvSize( img->width & -2, img->height & -2 );
     IplImage* timg = cvCloneImage( img ); // make a copy of input image
-    IplImage* gray = cvCreateImage( sz, 8, 1 ); 
+    IplImage* gray = cvCreateImage( sz, 8, 1 );
     IplImage* pyr = cvCreateImage( cvSize(sz.width/2, sz.height/2), 8, 3 );
     IplImage* tgray;
     CvSeq* result;
@@ -47,35 +49,35 @@ CvSeq* findSquares4( IplImage* img, CvMemStorage* storage )
     // create empty sequence that will contain points -
     // 4 points per square (the square's vertices)
     CvSeq* squares = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvPoint), storage );
-    
+
     // select the maximum ROI in the image
     // with the width and height divisible by 2
     cvSetImageROI( timg, cvRect( 0, 0, sz.width, sz.height ));
-    
+
     // down-scale and upscale the image to filter out the noise
     cvPyrDown( timg, pyr, 7 );
     cvPyrUp( pyr, timg, 7 );
     tgray = cvCreateImage( sz, 8, 1 );
-    
+
     // find squares in every color plane of the image
     for( c = 0; c < 3; c++ )
     {
         // extract the c-th color plane
         cvSetImageCOI( timg, c+1 );
         cvCopy( timg, tgray, 0 );
-        
+
         // try several threshold levels
         for( l = 0; l < N; l++ )
         {
             // hack: use Canny instead of zero threshold level.
-            // Canny helps to catch squares with gradient shading   
+            // 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) 
+                // and set the lower to 0 (which forces edges merging)
                 cvCanny( tgray, gray, 0, thresh, 5 );
                 // dilate canny output to remove potential
-                // holes between edge segments 
+                // holes between edge segments
                 cvDilate( gray, gray, 0, 1 );
             }
             else
@@ -84,11 +86,11 @@ CvSeq* findSquares4( IplImage* img, CvMemStorage* storage )
                 //     tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0
                 cvThreshold( tgray, gray, (l+1)*255/N, 255, CV_THRESH_BINARY );
             }
-            
+
             // find contours and store them all as a list
             cvFindContours( gray, storage, &contours, sizeof(CvContour),
                 CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
-            
+
             // test each contour
             while( contours )
             {
@@ -107,7 +109,7 @@ CvSeq* findSquares4( IplImage* img, CvMemStorage* storage )
                     cvCheckContourConvexity(result) )
                 {
                     s = 0;
-                    
+
                     for( i = 0; i < 5; i++ )
                     {
                         // find minimum angle between joint
@@ -121,28 +123,28 @@ CvSeq* findSquares4( IplImage* img, CvMemStorage* storage )
                             s = s > t ? s : t;
                         }
                     }
-                    
+
                     // if cosines of all angles are small
                     // (all angles are ~90 degree) then write quandrange
-                    // vertices to resultant sequence 
+                    // vertices to resultant sequence
                     if( s < 0.3 )
                         for( i = 0; i < 4; i++ )
                             cvSeqPush( squares,
                                 (CvPoint*)cvGetSeqElem( result, i ));
                 }
-                
+
                 // take the next contour
                 contours = contours->h_next;
             }
         }
     }
-    
+
     // release all the temporary images
     cvReleaseImage( &gray );
     cvReleaseImage( &pyr );
     cvReleaseImage( &tgray );
     cvReleaseImage( &timg );
-    
+
     return squares;
 }
 
@@ -153,26 +155,26 @@ void drawSquares( IplImage* img, CvSeq* squares )
     CvSeqReader reader;
     IplImage* cpy = cvCloneImage( img );
     int i;
-    
+
     // initialize reader of the sequence
     cvStartReadSeq( squares, &reader, 0 );
-    
+
     // read 4 sequence elements at a time (all vertices of a square)
     for( i = 0; i < squares->total; i += 4 )
     {
         CvPoint pt[4], *rect = pt;
         int count = 4;
-        
+
         // read 4 vertices
         CV_READ_SEQ_ELEM( pt[0], reader );
         CV_READ_SEQ_ELEM( pt[1], reader );
         CV_READ_SEQ_ELEM( pt[2], reader );
         CV_READ_SEQ_ELEM( pt[3], reader );
-        
-        // draw the square as a closed polyline 
+
+        // draw the square as a closed polyline
         cvPolyLine( cpy, &rect, &count, 1, 1, CV_RGB(0,255,0), 3, CV_AA, 0 );
     }
-    
+
     // show the resultant image
     cvShowImage( wndname, cpy );
     cvReleaseImage( &cpy );
@@ -198,14 +200,14 @@ int main(int argc, char** argv)
             continue;
         }
         img = cvCloneImage( img0 );
-        
+
         // create window and a trackbar (slider) with parent "image" and set callback
-        // (the slider regulates upper threshold, passed to Canny edge detector) 
+        // (the slider regulates upper threshold, passed to Canny edge detector)
         cvNamedWindow( wndname, 1 );
-        
+
         // find and draw the squares
         drawSquares( img, findSquares4( img, storage ) );
-        
+
         // wait for key.
         // Also the function cvWaitKey takes care of event processing
         c = cvWaitKey(0);
@@ -217,8 +219,8 @@ int main(int argc, char** argv)
         if( (char)c == 27 )
             break;
     }
-    
+
     cvDestroyWindow( wndname );
-    
+
     return 0;
 }