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
44 #include <limits>
45 #include <utility>
46 #include <algorithm>
47
48 #include <math.h>
49
50 namespace cv {
51
is_smaller(const std::pair<int,float> & p1,const std::pair<int,float> & p2)52 inline bool is_smaller(const std::pair<int, float>& p1, const std::pair<int, float>& p2)
53 {
54 return p1.second < p2.second;
55 }
56
orderContours(const std::vector<std::vector<Point>> & contours,Point2f point,std::vector<std::pair<int,float>> & order)57 static void orderContours(const std::vector<std::vector<Point> >& contours, Point2f point, std::vector<std::pair<int, float> >& order)
58 {
59 order.clear();
60 size_t i, j, n = contours.size();
61 for(i = 0; i < n; i++)
62 {
63 size_t ni = contours[i].size();
64 double min_dist = std::numeric_limits<double>::max();
65 for(j = 0; j < ni; j++)
66 {
67 double dist = norm(Point2f((float)contours[i][j].x, (float)contours[i][j].y) - point);
68 min_dist = MIN(min_dist, dist);
69 }
70 order.push_back(std::pair<int, float>((int)i, (float)min_dist));
71 }
72
73 std::sort(order.begin(), order.end(), is_smaller);
74 }
75
76 // fit second order curve to a set of 2D points
fitCurve2Order(const std::vector<Point2f> &,std::vector<float> &)77 inline void fitCurve2Order(const std::vector<Point2f>& /*points*/, std::vector<float>& /*curve*/)
78 {
79 // TBD
80 }
81
findCurvesCross(const std::vector<float> &,const std::vector<float> &,Point2f &)82 inline void findCurvesCross(const std::vector<float>& /*curve1*/, const std::vector<float>& /*curve2*/, Point2f& /*cross_point*/)
83 {
84 }
85
findLinesCrossPoint(Point2f origin1,Point2f dir1,Point2f origin2,Point2f dir2,Point2f & cross_point)86 static void findLinesCrossPoint(Point2f origin1, Point2f dir1, Point2f origin2, Point2f dir2, Point2f& cross_point)
87 {
88 float det = dir2.x*dir1.y - dir2.y*dir1.x;
89 Point2f offset = origin2 - origin1;
90
91 float alpha = (dir2.x*offset.y - dir2.y*offset.x)/det;
92 cross_point = origin1 + dir1*alpha;
93 }
94
findCorner(const std::vector<Point2f> & contour,Point2f point,Point2f & corner)95 static void findCorner(const std::vector<Point2f>& contour, Point2f point, Point2f& corner)
96 {
97 // find the nearest point
98 double min_dist = std::numeric_limits<double>::max();
99 int min_idx = -1;
100
101 // find corner idx
102 for(size_t i = 0; i < contour.size(); i++)
103 {
104 double dist = norm(contour[i] - point);
105 if(dist < min_dist)
106 {
107 min_dist = dist;
108 min_idx = (int)i;
109 }
110 }
111 CV_Assert(min_idx >= 0);
112
113 // temporary solution, have to make something more precise
114 corner = contour[min_idx];
115 return;
116 }
117
segment_hist_max(const Mat & hist,int & low_thresh,int & high_thresh)118 static int segment_hist_max(const Mat& hist, int& low_thresh, int& high_thresh)
119 {
120 Mat bw;
121 double total_sum = sum(hist).val[0];
122
123 double quantile_sum = 0.0;
124 //double min_quantile = 0.2;
125 double low_sum = 0;
126 double max_segment_length = 0;
127 int max_start_x = -1;
128 int max_end_x = -1;
129 int start_x = 0;
130 const double out_of_bells_fraction = 0.1;
131 for(int x = 0; x < hist.size[0]; x++)
132 {
133 quantile_sum += hist.at<float>(x);
134 if(quantile_sum < 0.2*total_sum) continue;
135
136 if(quantile_sum - low_sum > out_of_bells_fraction*total_sum)
137 {
138 if(max_segment_length < x - start_x)
139 {
140 max_segment_length = x - start_x;
141 max_start_x = start_x;
142 max_end_x = x;
143 }
144
145 low_sum = quantile_sum;
146 start_x = x;
147 }
148 }
149
150 if(start_x == -1)
151 {
152 return 0;
153 }
154 else
155 {
156 low_thresh = cvRound(max_start_x + 0.25*(max_end_x - max_start_x));
157 high_thresh = cvRound(max_start_x + 0.75*(max_end_x - max_start_x));
158 return 1;
159 }
160 }
161
162 }
163
find4QuadCornerSubpix(InputArray _img,InputOutputArray _corners,Size region_size)164 bool cv::find4QuadCornerSubpix(InputArray _img, InputOutputArray _corners, Size region_size)
165 {
166 Mat img = _img.getMat(), cornersM = _corners.getMat();
167 int ncorners = cornersM.checkVector(2, CV_32F);
168 CV_Assert( ncorners >= 0 );
169 Point2f* corners = cornersM.ptr<Point2f>();
170 const int nbins = 256;
171 float ranges[] = {0, 256};
172 const float* _ranges = ranges;
173 Mat hist;
174
175 Mat black_comp, white_comp;
176 for(int i = 0; i < ncorners; i++)
177 {
178 int channels = 0;
179 Rect roi(cvRound(corners[i].x - region_size.width), cvRound(corners[i].y - region_size.height),
180 region_size.width*2 + 1, region_size.height*2 + 1);
181 Mat img_roi = img(roi);
182 calcHist(&img_roi, 1, &channels, Mat(), hist, 1, &nbins, &_ranges);
183
184 int black_thresh = 0, white_thresh = 0;
185 segment_hist_max(hist, black_thresh, white_thresh);
186
187 threshold(img, black_comp, black_thresh, 255.0, THRESH_BINARY_INV);
188 threshold(img, white_comp, white_thresh, 255.0, THRESH_BINARY);
189
190 const int erode_count = 1;
191 erode(black_comp, black_comp, Mat(), Point(-1, -1), erode_count);
192 erode(white_comp, white_comp, Mat(), Point(-1, -1), erode_count);
193
194 std::vector<std::vector<Point> > white_contours, black_contours;
195 std::vector<Vec4i> white_hierarchy, black_hierarchy;
196 findContours(black_comp, black_contours, black_hierarchy, RETR_LIST, CHAIN_APPROX_SIMPLE);
197 findContours(white_comp, white_contours, white_hierarchy, RETR_LIST, CHAIN_APPROX_SIMPLE);
198
199 if(black_contours.size() < 5 || white_contours.size() < 5) continue;
200
201 // find two white and black blobs that are close to the input point
202 std::vector<std::pair<int, float> > white_order, black_order;
203 orderContours(black_contours, corners[i], black_order);
204 orderContours(white_contours, corners[i], white_order);
205
206 const float max_dist = 10.0f;
207 if(black_order[0].second > max_dist || black_order[1].second > max_dist ||
208 white_order[0].second > max_dist || white_order[1].second > max_dist)
209 {
210 continue; // there will be no improvement in this corner position
211 }
212
213 const std::vector<Point>* quads[4] = {&black_contours[black_order[0].first], &black_contours[black_order[1].first],
214 &white_contours[white_order[0].first], &white_contours[white_order[1].first]};
215 std::vector<Point2f> quads_approx[4];
216 Point2f quad_corners[4];
217 for(int k = 0; k < 4; k++)
218 {
219 std::vector<Point2f> temp;
220 for(size_t j = 0; j < quads[k]->size(); j++) temp.push_back((*quads[k])[j]);
221 approxPolyDP(Mat(temp), quads_approx[k], 0.5, true);
222
223 findCorner(quads_approx[k], corners[i], quad_corners[k]);
224 quad_corners[k] += Point2f(0.5f, 0.5f);
225 }
226
227 // cross two lines
228 Point2f origin1 = quad_corners[0];
229 Point2f dir1 = quad_corners[1] - quad_corners[0];
230 Point2f origin2 = quad_corners[2];
231 Point2f dir2 = quad_corners[3] - quad_corners[2];
232 double angle = acos(dir1.dot(dir2)/(norm(dir1)*norm(dir2)));
233 if(cvIsNaN(angle) || cvIsInf(angle) || angle < 0.5 || angle > CV_PI - 0.5) continue;
234
235 findLinesCrossPoint(origin1, dir1, origin2, dir2, corners[i]);
236 }
237
238 return true;
239 }
240