CLAHE Python bindings
[profile/ivi/opencv.git] / samples / ocl / squares.cpp
1 // The "Square Detector" program.
2 // It loads several images sequentially and tries to find squares in
3 // each image
4
5 #include "opencv2/core/core.hpp"
6 #include "opencv2/imgproc/imgproc.hpp"
7 #include "opencv2/highgui/highgui.hpp"
8 #include "opencv2/ocl/ocl.hpp"
9
10 #include <iostream>
11 #include <math.h>
12 #include <string.h>
13
14 using namespace cv;
15 using namespace std;
16
17 static void help()
18 {
19     cout <<
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"
25         "Call:\n"
26         "./squares\n"
27         "Using OpenCV version %s\n" << CV_VERSION << "\n" << endl;
28 }
29
30
31 int thresh = 50, N = 11;
32 const char* wndname = "OpenCL Square Detection Demo";
33
34 // helper function:
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 )
38 {
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);
44 }
45
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 )
49 {
50     squares.clear();
51
52     Mat gray;
53     cv::ocl::oclMat pyr_ocl, timg_ocl, gray0_ocl, gray_ocl;
54
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);
58
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++ )
64     {
65         gray0_ocl = gray0s[c];
66         // try several threshold levels
67         for( int l = 0; l < N; l++ )
68         {
69             // hack: use Canny instead of zero threshold level.
70             // Canny helps to catch squares with gradient shading
71             if( l == 0 )
72             {
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));
80                 gray = Mat(gray_ocl);
81             }
82             else
83             {
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);
87                 gray = gray_ocl;
88             }
89
90             // find contours and store them all as a list
91             findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
92
93             vector<Point> approx;
94
95             // test each contour
96             for( size_t i = 0; i < contours.size(); i++ )
97             {
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);
101
102                 // square contours should have 4 vertices after approximation
103                 // relatively large area (to filter out noisy contours)
104                 // and be convex.
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)) )
111                 {
112                     double maxCosine = 0;
113
114                     for( int j = 2; j < 5; j++ )
115                     {
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);
119                     }
120
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);
126                 }
127             }
128         }
129     }
130 }
131
132
133 // the function draws all the squares in the image
134 static void drawSquares( Mat& image, const vector<vector<Point> >& squares )
135 {
136     for( size_t i = 0; i < squares.size(); i++ )
137     {
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);
141     }
142
143     imshow(wndname, image);
144 }
145
146
147 int main(int /*argc*/, char** /*argv*/)
148 {
149
150     //ocl::setBinpath("F:/kernel_bin");
151     vector<ocl::Info> info;
152     CV_Assert(ocl::getDevice(info));
153
154     static const char* names[] = { "pic1.png", "pic2.png", "pic3.png",
155         "pic4.png", "pic5.png", "pic6.png", 0 };
156     help();
157     namedWindow( wndname, 1 );
158     vector<vector<Point> > squares;
159
160     for( int i = 0; names[i] != 0; i++ )
161     {
162         Mat image = imread(names[i], 1);
163         if( image.empty() )
164         {
165             cout << "Couldn't load " << names[i] << endl;
166             continue;
167         }
168
169         findSquares(image, squares);
170         drawSquares(image, squares);
171
172         int c = waitKey();
173         if( (char)c == 27 )
174             break;
175     }
176
177     return 0;
178 }