Path: blob/master/modules/calib3d/src/checkchessboard.cpp
16354 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"42#include "opencv2/imgproc/imgproc_c.h"43#include "opencv2/calib3d/calib3d_c.h"4445#include <vector>46#include <algorithm>4748using namespace cv;49using namespace std;5051static 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)52{53const float min_aspect_ratio = 0.3f;54const float max_aspect_ratio = 3.0f;55const float min_box_size = 10.0f;5657typedef std::vector< std::vector< cv::Point > >::const_iterator iter_t;58iter_t i;59for (i = contours.begin(); i != contours.end(); ++i)60{61const iter_t::difference_type idx = i - contours.begin();62if (hierarchy.at(idx)[3] != -1)63continue; // skip holes6465const std::vector< cv::Point > & c = *i;66cv::RotatedRect box = cv::minAreaRect(c);6768float box_size = MAX(box.size.width, box.size.height);69if(box_size < min_box_size)70{71continue;72}7374float aspect_ratio = box.size.width/MAX(box.size.height, 1);75if(aspect_ratio < min_aspect_ratio || aspect_ratio > max_aspect_ratio)76{77continue;78}7980quads.push_back(std::pair<float, int>(box_size, class_id));81}82}8384static void countClasses(const std::vector<std::pair<float, int> >& pairs, size_t idx1, size_t idx2, std::vector<int>& counts)85{86counts.assign(2, 0);87for(size_t i = idx1; i != idx2; i++)88{89counts[pairs[i].second]++;90}91}9293inline bool less_pred(const std::pair<float, int>& p1, const std::pair<float, int>& p2)94{95return p1.first < p2.first;96}9798static void fillQuads(Mat & white, Mat & black, double white_thresh, double black_thresh, vector<pair<float, int> > & quads)99{100Mat thresh;101{102vector< vector<Point> > contours;103vector< Vec4i > hierarchy;104threshold(white, thresh, white_thresh, 255, THRESH_BINARY);105findContours(thresh, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE);106icvGetQuadrangleHypotheses(contours, hierarchy, quads, 1);107}108109{110vector< vector<Point> > contours;111vector< Vec4i > hierarchy;112threshold(black, thresh, black_thresh, 255, THRESH_BINARY_INV);113findContours(thresh, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE);114icvGetQuadrangleHypotheses(contours, hierarchy, quads, 0);115}116}117118static bool checkQuads(vector<pair<float, int> > & quads, const cv::Size & size)119{120const size_t min_quads_count = size.width*size.height/2;121std::sort(quads.begin(), quads.end(), less_pred);122123// now check if there are many hypotheses with similar sizes124// do this by floodfill-style algorithm125const float size_rel_dev = 0.4f;126127for(size_t i = 0; i < quads.size(); i++)128{129size_t j = i + 1;130for(; j < quads.size(); j++)131{132if(quads[j].first/quads[i].first > 1.0f + size_rel_dev)133{134break;135}136}137138if(j + 1 > min_quads_count + i)139{140// check the number of black and white squares141std::vector<int> counts;142countClasses(quads, i, j, counts);143const int black_count = cvRound(ceil(size.width/2.0)*ceil(size.height/2.0));144const int white_count = cvRound(floor(size.width/2.0)*floor(size.height/2.0));145if(counts[0] < black_count*0.75 ||146counts[1] < white_count*0.75)147{148continue;149}150return true;151}152}153return false;154}155156// does a fast check if a chessboard is in the input image. This is a workaround to157// a problem of cvFindChessboardCorners being slow on images with no chessboard158// - src: input image159// - size: chessboard size160// Returns 1 if a chessboard can be in this image and findChessboardCorners should be called,161// 0 if there is no chessboard, -1 in case of error162int cvCheckChessboard(IplImage* src, CvSize size)163{164cv::Mat img = cv::cvarrToMat(src);165return checkChessboard(img, size);166}167168int checkChessboard(const cv::Mat & img, const cv::Size & size)169{170CV_Assert(img.channels() == 1 && img.depth() == CV_8U);171172const int erosion_count = 1;173const float black_level = 20.f;174const float white_level = 130.f;175const float black_white_gap = 70.f;176177Mat white;178Mat black;179erode(img, white, Mat(), Point(-1, -1), erosion_count);180dilate(img, black, Mat(), Point(-1, -1), erosion_count);181182int result = 0;183for(float thresh_level = black_level; thresh_level < white_level && !result; thresh_level += 20.0f)184{185vector<pair<float, int> > quads;186fillQuads(white, black, thresh_level + black_white_gap, thresh_level, quads);187if (checkQuads(quads, size))188result = 1;189}190return result;191}192193// does a fast check if a chessboard is in the input image. This is a workaround to194// a problem of cvFindChessboardCorners being slow on images with no chessboard195// - src: input binary image196// - size: chessboard size197// Returns 1 if a chessboard can be in this image and findChessboardCorners should be called,198// 0 if there is no chessboard, -1 in case of error199int checkChessboardBinary(const cv::Mat & img, const cv::Size & size)200{201CV_Assert(img.channels() == 1 && img.depth() == CV_8U);202203Mat white = img.clone();204Mat black = img.clone();205206int result = 0;207for ( int erosion_count = 0; erosion_count <= 3; erosion_count++ )208{209if ( 1 == result )210break;211212if ( 0 != erosion_count ) // first iteration keeps original images213{214erode(white, white, Mat(), Point(-1, -1), 1);215dilate(black, black, Mat(), Point(-1, -1), 1);216}217218vector<pair<float, int> > quads;219fillQuads(white, black, 128, 128, quads);220if (checkQuads(quads, size))221result = 1;222}223return result;224}225226227