• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2  //
3  //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4  //
5  //  By downloading, copying, installing or using the software you agree to this license.
6  //  If you do not agree to this license, do not download, install,
7  //  copy or use the software.
8  //
9  //
10  //                        Intel License Agreement
11  //                For Open Source Computer Vision Library
12  //
13  // Copyright (C) 2000, Intel Corporation, all rights reserved.
14  // Third party copyrights are property of their respective owners.
15  //
16  // Redistribution and use in source and binary forms, with or without modification,
17  // are permitted provided that the following conditions are met:
18  //
19  //   * Redistribution's of source code must retain the above copyright notice,
20  //     this list of conditions and the following disclaimer.
21  //
22  //   * Redistribution's in binary form must reproduce the above copyright notice,
23  //     this list of conditions and the following disclaimer in the documentation
24  //     and/or other materials provided with the distribution.
25  //
26  //   * The name of Intel Corporation may not be used to endorse or promote products
27  //     derived from this software without specific prior written permission.
28  //
29  // This software is provided by the copyright holders and contributors "as is" and
30  // any express or implied warranties, including, but not limited to, the implied
31  // warranties of merchantability and fitness for a particular purpose are disclaimed.
32  // In no event shall the Intel Corporation or contributors be liable for any direct,
33  // indirect, incidental, special, exemplary, or consequential damages
34  // (including, but not limited to, procurement of substitute goods or services;
35  // loss of use, data, or profits; or business interruption) however caused
36  // and on any theory of liability, whether in contract, strict liability,
37  // or tort (including negligence or otherwise) arising in any way out of
38  // the use of this software, even if advised of the possibility of such damage.
39  //
40  //M*/
41 
42 #include "precomp.hpp"
43 #include "opencv2/imgproc/imgproc_c.h"
44 #include "opencv2/calib3d/calib3d_c.h"
45 
46 #include <vector>
47 #include <algorithm>
48 
49 using namespace cv;
50 using namespace std;
51 
icvGetQuadrangleHypotheses(const std::vector<std::vector<cv::Point>> & contours,const std::vector<cv::Vec4i> & hierarchy,std::vector<std::pair<float,int>> & quads,int class_id)52 static void icvGetQuadrangleHypotheses(const std::vector<std::vector< cv::Point > > & contours, const std::vector< cv::Vec4i > & hierarchy, std::vector<std::pair<float, int> >& quads, int class_id)
53 {
54     const float min_aspect_ratio = 0.3f;
55     const float max_aspect_ratio = 3.0f;
56     const float min_box_size = 10.0f;
57 
58     typedef std::vector< std::vector< cv::Point > >::const_iterator iter_t;
59     iter_t i;
60     for (i = contours.begin(); i != contours.end(); ++i)
61     {
62         const iter_t::difference_type idx = i - contours.begin();
63         if (hierarchy.at(idx)[3] != -1)
64             continue; // skip holes
65 
66         const std::vector< cv::Point > & c = *i;
67         cv::RotatedRect box = cv::minAreaRect(c);
68 
69         float box_size = MAX(box.size.width, box.size.height);
70         if(box_size < min_box_size)
71         {
72             continue;
73         }
74 
75         float aspect_ratio = box.size.width/MAX(box.size.height, 1);
76         if(aspect_ratio < min_aspect_ratio || aspect_ratio > max_aspect_ratio)
77         {
78             continue;
79         }
80 
81         quads.push_back(std::pair<float, int>(box_size, class_id));
82     }
83 }
84 
countClasses(const std::vector<std::pair<float,int>> & pairs,size_t idx1,size_t idx2,std::vector<int> & counts)85 static void countClasses(const std::vector<std::pair<float, int> >& pairs, size_t idx1, size_t idx2, std::vector<int>& counts)
86 {
87     counts.assign(2, 0);
88     for(size_t i = idx1; i != idx2; i++)
89     {
90         counts[pairs[i].second]++;
91     }
92 }
93 
less_pred(const std::pair<float,int> & p1,const std::pair<float,int> & p2)94 inline bool less_pred(const std::pair<float, int>& p1, const std::pair<float, int>& p2)
95 {
96     return p1.first < p2.first;
97 }
98 
fillQuads(Mat & white,Mat & black,double white_thresh,double black_thresh,vector<pair<float,int>> & quads)99 static void fillQuads(Mat & white, Mat & black, double white_thresh, double black_thresh, vector<pair<float, int> > & quads)
100 {
101     Mat thresh;
102     {
103         vector< vector<Point> > contours;
104         vector< Vec4i > hierarchy;
105         threshold(white, thresh, white_thresh, 255, THRESH_BINARY);
106         findContours(thresh, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE);
107         icvGetQuadrangleHypotheses(contours, hierarchy, quads, 1);
108     }
109 
110     {
111         vector< vector<Point> > contours;
112         vector< Vec4i > hierarchy;
113         threshold(black, thresh, black_thresh, 255, THRESH_BINARY_INV);
114         findContours(thresh, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE);
115         icvGetQuadrangleHypotheses(contours, hierarchy, quads, 0);
116     }
117 }
118 
checkQuads(vector<pair<float,int>> & quads,const cv::Size & size)119 static bool checkQuads(vector<pair<float, int> > & quads, const cv::Size & size)
120 {
121     const size_t min_quads_count = size.width*size.height/2;
122     std::sort(quads.begin(), quads.end(), less_pred);
123 
124     // now check if there are many hypotheses with similar sizes
125     // do this by floodfill-style algorithm
126     const float size_rel_dev = 0.4f;
127 
128     for(size_t i = 0; i < quads.size(); i++)
129     {
130         size_t j = i + 1;
131         for(; j < quads.size(); j++)
132         {
133             if(quads[j].first/quads[i].first > 1.0f + size_rel_dev)
134             {
135                 break;
136             }
137         }
138 
139         if(j + 1 > min_quads_count + i)
140         {
141             // check the number of black and white squares
142             std::vector<int> counts;
143             countClasses(quads, i, j, counts);
144             const int black_count = cvRound(ceil(size.width/2.0)*ceil(size.height/2.0));
145             const int white_count = cvRound(floor(size.width/2.0)*floor(size.height/2.0));
146             if(counts[0] < black_count*0.75 ||
147                counts[1] < white_count*0.75)
148             {
149                 continue;
150             }
151             return true;
152         }
153     }
154     return false;
155 }
156 
157 // does a fast check if a chessboard is in the input image. This is a workaround to
158 // a problem of cvFindChessboardCorners being slow on images with no chessboard
159 // - src: input image
160 // - size: chessboard size
161 // Returns 1 if a chessboard can be in this image and findChessboardCorners should be called,
162 // 0 if there is no chessboard, -1 in case of error
cvCheckChessboard(IplImage * src,CvSize size)163 int cvCheckChessboard(IplImage* src, CvSize size)
164 {
165     cv::Mat img = cv::cvarrToMat(src);
166     return checkChessboard(img, size);
167 }
168 
checkChessboard(const cv::Mat & img,const cv::Size & size)169 int checkChessboard(const cv::Mat & img, const cv::Size & size)
170 {
171     CV_Assert(img.channels() == 1 && img.depth() == CV_8U);
172 
173     const int erosion_count = 1;
174     const float black_level = 20.f;
175     const float white_level = 130.f;
176     const float black_white_gap = 70.f;
177 
178     Mat white;
179     Mat black;
180     erode(img, white, Mat(), Point(-1, -1), erosion_count);
181     dilate(img, black, Mat(), Point(-1, -1), erosion_count);
182 
183     int result = 0;
184     for(float thresh_level = black_level; thresh_level < white_level && !result; thresh_level += 20.0f)
185     {
186         vector<pair<float, int> > quads;
187         fillQuads(white, black, thresh_level + black_white_gap, thresh_level, quads);
188         if (checkQuads(quads, size))
189             result = 1;
190     }
191     return result;
192 }
193 
194 // does a fast check if a chessboard is in the input image. This is a workaround to
195 // a problem of cvFindChessboardCorners being slow on images with no chessboard
196 // - src: input binary image
197 // - size: chessboard size
198 // Returns 1 if a chessboard can be in this image and findChessboardCorners should be called,
199 // 0 if there is no chessboard, -1 in case of error
checkChessboardBinary(const cv::Mat & img,const cv::Size & size)200 int checkChessboardBinary(const cv::Mat & img, const cv::Size & size)
201 {
202     CV_Assert(img.channels() == 1 && img.depth() == CV_8U);
203 
204     Mat white = img.clone();
205     Mat black = img.clone();
206 
207     int result = 0;
208     for ( int erosion_count = 0; erosion_count <= 3; erosion_count++ )
209     {
210         if ( 1 == result )
211             break;
212 
213         if ( 0 != erosion_count ) // first iteration keeps original images
214         {
215             erode(white, white, Mat(), Point(-1, -1), 1);
216             dilate(black, black, Mat(), Point(-1, -1), 1);
217         }
218 
219         vector<pair<float, int> > quads;
220         fillQuads(white, black, 128, 128, quads);
221         if (checkQuads(quads, size))
222             result = 1;
223     }
224     return result;
225 }
226