1 // The "Square Detector" program.
2 // It loads several images sequentially and tries to find squares in
5 #include "opencv2/core/core.hpp"
6 #include "opencv2/imgproc/imgproc.hpp"
7 #include "opencv2/highgui/highgui.hpp"
8 #include "opencv2/ocl/ocl.hpp"
20 "\nA program using OCL module pyramid scaling, Canny, dilate functions, threshold, split; cpu contours, contour simpification and\n"
21 "memory storage (it's got it all folks) to find\n"
22 "squares in a list of images pic1-6.png\n"
23 "Returns sequence of squares detected on the image.\n"
24 "the sequence is stored in the specified memory storage\n"
27 "Using OpenCV version %s\n" << CV_VERSION << "\n" << endl;
31 int thresh = 50, N = 11;
32 const char* wndname = "OpenCL Square Detection Demo";
35 // finds a cosine of angle between vectors
36 // from pt0->pt1 and from pt0->pt2
37 static double angle( Point pt1, Point pt2, Point pt0 )
39 double dx1 = pt1.x - pt0.x;
40 double dy1 = pt1.y - pt0.y;
41 double dx2 = pt2.x - pt0.x;
42 double dy2 = pt2.y - pt0.y;
43 return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
46 // returns sequence of squares detected on the image.
47 // the sequence is stored in the specified memory storage
48 static void findSquares( const Mat& image, vector<vector<Point> >& squares )
53 cv::ocl::oclMat pyr_ocl, timg_ocl, gray0_ocl, gray_ocl;
55 // down-scale and upscale the image to filter out the noise
56 ocl::pyrDown(ocl::oclMat(image), pyr_ocl);
57 ocl::pyrUp(pyr_ocl, timg_ocl);
59 vector<vector<Point> > contours;
60 vector<cv::ocl::oclMat> gray0s;
61 ocl::split(timg_ocl, gray0s); // split 3 channels into a vector of oclMat
62 // find squares in every color plane of the image
63 for( int c = 0; c < 3; c++ )
65 gray0_ocl = gray0s[c];
66 // try several threshold levels
67 for( int l = 0; l < N; l++ )
69 // hack: use Canny instead of zero threshold level.
70 // Canny helps to catch squares with gradient shading
73 // do canny on OpenCL device
74 // apply Canny. Take the upper threshold from slider
75 // and set the lower to 0 (which forces edges merging)
76 cv::ocl::Canny(gray0_ocl, gray_ocl, 0, thresh, 5);
77 // dilate canny output to remove potential
78 // holes between edge segments
79 ocl::dilate(gray_ocl, gray_ocl, Mat(), Point(-1,-1));
84 // apply threshold if l!=0:
85 // tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0
86 cv::ocl::threshold(gray0_ocl, gray_ocl, (l+1)*255/N, 255, THRESH_BINARY);
90 // find contours and store them all as a list
91 findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
96 for( size_t i = 0; i < contours.size(); i++ )
98 // approximate contour with accuracy proportional
99 // to the contour perimeter
100 approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);
102 // square contours should have 4 vertices after approximation
103 // relatively large area (to filter out noisy contours)
105 // Note: absolute value of an area is used because
106 // area may be positive or negative - in accordance with the
107 // contour orientation
108 if( approx.size() == 4 &&
109 fabs(contourArea(Mat(approx))) > 1000 &&
110 isContourConvex(Mat(approx)) )
112 double maxCosine = 0;
114 for( int j = 2; j < 5; j++ )
116 // find the maximum cosine of the angle between joint edges
117 double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
118 maxCosine = MAX(maxCosine, cosine);
121 // if cosines of all angles are small
122 // (all angles are ~90 degree) then write quandrange
123 // vertices to resultant sequence
124 if( maxCosine < 0.3 )
125 squares.push_back(approx);
133 // the function draws all the squares in the image
134 static void drawSquares( Mat& image, const vector<vector<Point> >& squares )
136 for( size_t i = 0; i < squares.size(); i++ )
138 const Point* p = &squares[i][0];
139 int n = (int)squares[i].size();
140 polylines(image, &p, &n, 1, true, Scalar(0,255,0), 3, CV_AA);
143 imshow(wndname, image);
147 int main(int /*argc*/, char** /*argv*/)
150 //ocl::setBinpath("F:/kernel_bin");
151 vector<ocl::Info> info;
152 CV_Assert(ocl::getDevice(info));
154 static const char* names[] = { "pic1.png", "pic2.png", "pic3.png",
155 "pic4.png", "pic5.png", "pic6.png", 0 };
157 namedWindow( wndname, 1 );
158 vector<vector<Point> > squares;
160 for( int i = 0; names[i] != 0; i++ )
162 Mat image = imread(names[i], 1);
165 cout << "Couldn't load " << names[i] << endl;
169 findSquares(image, squares);
170 drawSquares(image, squares);