Path: blob/master/modules/calib3d/src/quadsubpix.cpp
16339 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// Intel License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000, Intel Corporation, all rights reserved.13// Third party copyrights are property of their respective owners.14//15// Redistribution and use in source and binary forms, with or without modification,16// are permitted provided that the following conditions are met:17//18// * Redistribution's of source code must retain the above copyright notice,19// this list of conditions and the following disclaimer.20//21// * Redistribution's in binary form must reproduce the above copyright notice,22// this list of conditions and the following disclaimer in the documentation23// and/or other materials provided with the distribution.24//25// * The name of Intel Corporation may not be used to endorse or promote products26// derived from this software without specific prior written permission.27//28// This software is provided by the copyright holders and contributors "as is" and29// any express or implied warranties, including, but not limited to, the implied30// warranties of merchantability and fitness for a particular purpose are disclaimed.31// In no event shall the Intel Corporation or contributors be liable for any direct,32// indirect, incidental, special, exemplary, or consequential damages33// (including, but not limited to, procurement of substitute goods or services;34// loss of use, data, or profits; or business interruption) however caused35// and on any theory of liability, whether in contract, strict liability,36// or tort (including negligence or otherwise) arising in any way out of37// the use of this software, even if advised of the possibility of such damage.38//39//M*/4041#include "precomp.hpp"4243#include <limits>44#include <utility>45#include <algorithm>4647#include <math.h>4849namespace cv {5051inline bool is_smaller(const std::pair<int, float>& p1, const std::pair<int, float>& p2)52{53return p1.second < p2.second;54}5556static void orderContours(const std::vector<std::vector<Point> >& contours, Point2f point, std::vector<std::pair<int, float> >& order)57{58order.clear();59size_t i, j, n = contours.size();60for(i = 0; i < n; i++)61{62size_t ni = contours[i].size();63double min_dist = std::numeric_limits<double>::max();64for(j = 0; j < ni; j++)65{66double dist = norm(Point2f((float)contours[i][j].x, (float)contours[i][j].y) - point);67min_dist = MIN(min_dist, dist);68}69order.push_back(std::pair<int, float>((int)i, (float)min_dist));70}7172std::sort(order.begin(), order.end(), is_smaller);73}7475// fit second order curve to a set of 2D points76inline void fitCurve2Order(const std::vector<Point2f>& /*points*/, std::vector<float>& /*curve*/)77{78// TBD79}8081inline void findCurvesCross(const std::vector<float>& /*curve1*/, const std::vector<float>& /*curve2*/, Point2f& /*cross_point*/)82{83}8485static void findLinesCrossPoint(Point2f origin1, Point2f dir1, Point2f origin2, Point2f dir2, Point2f& cross_point)86{87float det = dir2.x*dir1.y - dir2.y*dir1.x;88Point2f offset = origin2 - origin1;8990float alpha = (dir2.x*offset.y - dir2.y*offset.x)/det;91cross_point = origin1 + dir1*alpha;92}9394static void findCorner(const std::vector<Point2f>& contour, Point2f point, Point2f& corner)95{96// find the nearest point97double min_dist = std::numeric_limits<double>::max();98int min_idx = -1;99100// find corner idx101for(size_t i = 0; i < contour.size(); i++)102{103double dist = norm(contour[i] - point);104if(dist < min_dist)105{106min_dist = dist;107min_idx = (int)i;108}109}110CV_Assert(min_idx >= 0);111112// temporary solution, have to make something more precise113corner = contour[min_idx];114return;115}116117static int segment_hist_max(const Mat& hist, int& low_thresh, int& high_thresh)118{119Mat bw;120double total_sum = sum(hist).val[0];121122double quantile_sum = 0.0;123//double min_quantile = 0.2;124double low_sum = 0;125double max_segment_length = 0;126int max_start_x = -1;127int max_end_x = -1;128int start_x = 0;129const double out_of_bells_fraction = 0.1;130for(int x = 0; x < hist.size[0]; x++)131{132quantile_sum += hist.at<float>(x);133if(quantile_sum < 0.2*total_sum) continue;134135if(quantile_sum - low_sum > out_of_bells_fraction*total_sum)136{137if(max_segment_length < x - start_x)138{139max_segment_length = x - start_x;140max_start_x = start_x;141max_end_x = x;142}143144low_sum = quantile_sum;145start_x = x;146}147}148149if(start_x == -1)150{151return 0;152}153else154{155low_thresh = cvRound(max_start_x + 0.25*(max_end_x - max_start_x));156high_thresh = cvRound(max_start_x + 0.75*(max_end_x - max_start_x));157return 1;158}159}160161}162163bool cv::find4QuadCornerSubpix(InputArray _img, InputOutputArray _corners, Size region_size)164{165CV_INSTRUMENT_REGION();166167Mat img = _img.getMat(), cornersM = _corners.getMat();168int ncorners = cornersM.checkVector(2, CV_32F);169CV_Assert( ncorners >= 0 );170Point2f* corners = cornersM.ptr<Point2f>();171const int nbins = 256;172float ranges[] = {0, 256};173const float* _ranges = ranges;174Mat hist;175176Mat black_comp, white_comp;177for(int i = 0; i < ncorners; i++)178{179int channels = 0;180Rect roi(cvRound(corners[i].x - region_size.width), cvRound(corners[i].y - region_size.height),181region_size.width*2 + 1, region_size.height*2 + 1);182Mat img_roi = img(roi);183calcHist(&img_roi, 1, &channels, Mat(), hist, 1, &nbins, &_ranges);184185int black_thresh = 0, white_thresh = 0;186segment_hist_max(hist, black_thresh, white_thresh);187188threshold(img, black_comp, black_thresh, 255.0, THRESH_BINARY_INV);189threshold(img, white_comp, white_thresh, 255.0, THRESH_BINARY);190191const int erode_count = 1;192erode(black_comp, black_comp, Mat(), Point(-1, -1), erode_count);193erode(white_comp, white_comp, Mat(), Point(-1, -1), erode_count);194195std::vector<std::vector<Point> > white_contours, black_contours;196std::vector<Vec4i> white_hierarchy, black_hierarchy;197findContours(black_comp, black_contours, black_hierarchy, RETR_LIST, CHAIN_APPROX_SIMPLE);198findContours(white_comp, white_contours, white_hierarchy, RETR_LIST, CHAIN_APPROX_SIMPLE);199200if(black_contours.size() < 5 || white_contours.size() < 5) continue;201202// find two white and black blobs that are close to the input point203std::vector<std::pair<int, float> > white_order, black_order;204orderContours(black_contours, corners[i], black_order);205orderContours(white_contours, corners[i], white_order);206207const float max_dist = 10.0f;208if(black_order[0].second > max_dist || black_order[1].second > max_dist ||209white_order[0].second > max_dist || white_order[1].second > max_dist)210{211continue; // there will be no improvement in this corner position212}213214const std::vector<Point>* quads[4] = {&black_contours[black_order[0].first], &black_contours[black_order[1].first],215&white_contours[white_order[0].first], &white_contours[white_order[1].first]};216std::vector<Point2f> quads_approx[4];217Point2f quad_corners[4];218for(int k = 0; k < 4; k++)219{220std::vector<Point2f> temp;221for(size_t j = 0; j < quads[k]->size(); j++) temp.push_back((*quads[k])[j]);222approxPolyDP(Mat(temp), quads_approx[k], 0.5, true);223224findCorner(quads_approx[k], corners[i], quad_corners[k]);225quad_corners[k] += Point2f(0.5f, 0.5f);226}227228// cross two lines229Point2f origin1 = quad_corners[0];230Point2f dir1 = quad_corners[1] - quad_corners[0];231Point2f origin2 = quad_corners[2];232Point2f dir2 = quad_corners[3] - quad_corners[2];233double angle = acos(dir1.dot(dir2)/(norm(dir1)*norm(dir2)));234if(cvIsNaN(angle) || cvIsInf(angle) || angle < 0.5 || angle > CV_PI - 0.5) continue;235236findLinesCrossPoint(origin1, dir1, origin2, dir2, corners[i]);237}238239return true;240}241242243