2010-05-12 01:44:00 +08:00
|
|
|
//M*//////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Intel License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000, Intel Corporation, all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of Intel Corporation may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
|
|
|
/************************************************************************************\
|
|
|
|
This is improved variant of chessboard corner detection algorithm that
|
|
|
|
uses a graph of connected quads. It is based on the code contributed
|
|
|
|
by Vladimir Vezhnevets and Philip Gruebele.
|
|
|
|
Here is the copyright notice from the original Vladimir's code:
|
|
|
|
===============================================================
|
|
|
|
|
|
|
|
The algorithms developed and implemented by Vezhnevets Vldimir
|
|
|
|
aka Dead Moroz (vvp@graphics.cs.msu.ru)
|
|
|
|
See http://graphics.cs.msu.su/en/research/calibration/opencv.html
|
|
|
|
for detailed information.
|
|
|
|
|
|
|
|
Reliability additions and modifications made by Philip Gruebele.
|
|
|
|
<a href="mailto:pgruebele@cox.net">pgruebele@cox.net</a>
|
|
|
|
|
|
|
|
Some further improvements for detection of partially ocluded boards at non-ideal
|
|
|
|
lighting conditions have been made by Alex Bovyrin and Kurt Kolonige
|
|
|
|
|
|
|
|
\************************************************************************************/
|
|
|
|
|
2015-05-22 16:39:01 +08:00
|
|
|
/************************************************************************************\
|
|
|
|
This version adds a new and improved variant of chessboard corner detection
|
|
|
|
that works better in poor lighting condition. It is based on work from
|
|
|
|
Oliver Schreer and Stefano Masneri. This method works faster than the previous
|
|
|
|
one and reverts back to the older method in case no chessboard detection is
|
|
|
|
possible. Overall performance improves also because now the method avoids
|
|
|
|
performing the same computation multiple times when not necessary.
|
|
|
|
|
|
|
|
\************************************************************************************/
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
#include "precomp.hpp"
|
2010-12-21 17:24:36 +08:00
|
|
|
#include "circlesgrid.hpp"
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
#include <stack>
|
2016-07-19 22:24:55 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
//#define ENABLE_TRIM_COL_ROW
|
|
|
|
|
|
|
|
//#define DEBUG_CHESSBOARD
|
2018-05-23 22:44:12 +08:00
|
|
|
#define DEBUG_CHESSBOARD_TIMEOUT 0 // 0 - wait for 'q'
|
2016-07-13 20:45:48 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
#include <opencv2/core/utils/logger.defines.hpp>
|
2018-05-28 17:19:06 +08:00
|
|
|
//#undef CV_LOG_STRIP_LEVEL
|
|
|
|
//#define CV_LOG_STRIP_LEVEL CV_LOG_LEVEL_VERBOSE + 1
|
|
|
|
#include <opencv2/core/utils/logger.hpp>
|
|
|
|
|
2012-03-13 20:33:16 +08:00
|
|
|
#ifdef DEBUG_CHESSBOARD
|
2018-05-23 22:44:12 +08:00
|
|
|
#include "opencv2/highgui.hpp"
|
|
|
|
#include "opencv2/imgproc.hpp"
|
|
|
|
#define DPRINTF(...) CV_LOG_INFO(NULL, cv::format("calib3d: " __VA_ARGS__))
|
2010-05-12 01:44:00 +08:00
|
|
|
#else
|
2018-05-23 22:44:12 +08:00
|
|
|
#define DPRINTF(...)
|
2010-05-12 01:44:00 +08:00
|
|
|
#endif
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
namespace cv {
|
2018-05-28 17:19:06 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
//=====================================================================================
|
|
|
|
// Implementation for the enhanced calibration object detection
|
|
|
|
//=====================================================================================
|
|
|
|
|
|
|
|
#define MAX_CONTOUR_APPROX 7
|
|
|
|
|
2018-05-28 17:19:06 +08:00
|
|
|
#define USE_CV_FINDCONTOURS // switch between cv::findContours() and legacy C API
|
|
|
|
#ifdef USE_CV_FINDCONTOURS
|
|
|
|
struct QuadCountour {
|
|
|
|
Point pt[4];
|
|
|
|
int parent_contour;
|
|
|
|
|
|
|
|
QuadCountour(const Point pt_[4], int parent_contour_) :
|
|
|
|
parent_contour(parent_contour_)
|
|
|
|
{
|
|
|
|
pt[0] = pt_[0]; pt[1] = pt_[1]; pt[2] = pt_[2]; pt[3] = pt_[3];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#else
|
2018-05-23 22:44:12 +08:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
#include "opencv2/imgproc/imgproc_c.h"
|
|
|
|
namespace cv {
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
struct CvContourEx
|
|
|
|
{
|
|
|
|
CV_CONTOUR_FIELDS()
|
|
|
|
int counter;
|
|
|
|
};
|
2018-05-28 17:19:06 +08:00
|
|
|
#endif
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
/** This structure stores information about the chessboard corner.*/
|
2018-05-23 22:44:12 +08:00
|
|
|
struct ChessBoardCorner
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
cv::Point2f pt; // Coordinates of the corner
|
2010-05-12 01:44:00 +08:00
|
|
|
int row; // Board row index
|
|
|
|
int count; // Number of neighbor corners
|
2018-05-23 22:44:12 +08:00
|
|
|
struct ChessBoardCorner* neighbors[4]; // Neighbor corners
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardCorner(const cv::Point2f& pt_ = cv::Point2f()) :
|
|
|
|
pt(pt_), row(0), count(0)
|
|
|
|
{
|
|
|
|
neighbors[0] = neighbors[1] = neighbors[2] = neighbors[3] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
float sumDist(int& n_) const
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
float sum = 0;
|
|
|
|
int n = 0;
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int i = 0; i < 4; ++i)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if (neighbors[i])
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
sum += sqrt(normL2Sqr<float>(neighbors[i]->pt - pt));
|
2010-05-12 01:44:00 +08:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
n_ = n;
|
|
|
|
return sum;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
|
|
|
|
/** This structure stores information about the chessboard quadrangle.*/
|
|
|
|
struct ChessBoardQuad
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
int count; // Number of quad neighbors
|
|
|
|
int group_idx; // quad group ID
|
|
|
|
int row, col; // row and column of this quad
|
|
|
|
bool ordered; // true if corners/neighbors are ordered counter-clockwise
|
|
|
|
float edge_len; // quad edge len, in pix^2
|
|
|
|
// neighbors and corners are synced, i.e., neighbor 0 shares corner 0
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardCorner *corners[4]; // Coordinates of quad corners
|
|
|
|
struct ChessBoardQuad *neighbors[4]; // Pointers of quad neighbors
|
|
|
|
|
|
|
|
ChessBoardQuad(int group_idx_ = -1) :
|
|
|
|
count(0),
|
|
|
|
group_idx(group_idx_),
|
|
|
|
row(0), col(0),
|
|
|
|
ordered(0),
|
|
|
|
edge_len(0)
|
|
|
|
{
|
|
|
|
corners[0] = corners[1] = corners[2] = corners[3] = NULL;
|
|
|
|
neighbors[0] = neighbors[1] = neighbors[2] = neighbors[3] = NULL;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
};
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2016-07-19 22:24:55 +08:00
|
|
|
#ifdef DEBUG_CHESSBOARD
|
|
|
|
static void SHOW(const std::string & name, Mat & img)
|
|
|
|
{
|
|
|
|
imshow(name, img);
|
2018-05-23 22:44:12 +08:00
|
|
|
#if DEBUG_CHESSBOARD_TIMEOUT
|
|
|
|
waitKey(DEBUG_CHESSBOARD_TIMEOUT);
|
|
|
|
#else
|
2016-07-19 22:24:55 +08:00
|
|
|
while ((uchar)waitKey(0) != 'q') {}
|
2018-05-23 22:44:12 +08:00
|
|
|
#endif
|
2016-07-19 22:24:55 +08:00
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
static void SHOW_QUADS(const std::string & name, const Mat & img_, ChessBoardQuad * quads, int quads_count)
|
2016-07-19 22:24:55 +08:00
|
|
|
{
|
|
|
|
Mat img = img_.clone();
|
|
|
|
if (img.channels() == 1)
|
|
|
|
cvtColor(img, img, COLOR_GRAY2BGR);
|
|
|
|
for (int i = 0; i < quads_count; ++i)
|
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardQuad & quad = quads[i];
|
2016-07-19 22:24:55 +08:00
|
|
|
for (int j = 0; j < 4; ++j)
|
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
line(img, quad.corners[j]->pt, quad.corners[(j + 1) & 3]->pt, Scalar(0, 240, 0), 1, LINE_AA);
|
2016-07-19 22:24:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
imshow(name, img);
|
2018-05-23 22:44:12 +08:00
|
|
|
#if DEBUG_CHESSBOARD_TIMEOUT
|
|
|
|
waitKey(DEBUG_CHESSBOARD_TIMEOUT);
|
|
|
|
#else
|
2016-07-19 22:24:55 +08:00
|
|
|
while ((uchar)waitKey(0) != 'q') {}
|
2018-05-23 22:44:12 +08:00
|
|
|
#endif
|
2016-07-19 22:24:55 +08:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define SHOW(...)
|
|
|
|
#define SHOW_QUADS(...)
|
|
|
|
#endif
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2016-07-19 22:24:55 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
class ChessBoardDetector
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
cv::Mat binarized_image;
|
|
|
|
Size pattern_size;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
cv::AutoBuffer<ChessBoardQuad> all_quads;
|
|
|
|
cv::AutoBuffer<ChessBoardCorner> all_corners;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
int all_quads_count;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardDetector(const Size& pattern_size_) :
|
|
|
|
pattern_size(pattern_size_),
|
|
|
|
all_quads_count(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
all_quads.deallocate();
|
|
|
|
all_corners.deallocate();
|
|
|
|
all_quads_count = 0;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
void generateQuads(const cv::Mat& image_, int flags);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
bool processQuads(std::vector<cv::Point2f>& out_corners, int &prev_sqr_size);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
void findQuadNeighbors();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
void findConnectedQuads(std::vector<ChessBoardQuad*>& out_group, int group_idx);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
int checkQuadGroup(std::vector<ChessBoardQuad*>& quad_group, std::vector<ChessBoardCorner*>& out_corners);
|
|
|
|
|
|
|
|
int cleanFoundConnectedQuads(std::vector<ChessBoardQuad*>& quad_group);
|
|
|
|
|
|
|
|
int orderFoundConnectedQuads(std::vector<ChessBoardQuad*>& quads);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
void orderQuad(ChessBoardQuad& quad, ChessBoardCorner& corner, int common);
|
|
|
|
|
|
|
|
#ifdef ENABLE_TRIM_COL_ROW
|
|
|
|
void trimCol(std::vector<ChessBoardQuad*>& quads, int col, int dir);
|
|
|
|
void trimRow(std::vector<ChessBoardQuad*>& quads, int row, int dir);
|
2010-05-12 01:44:00 +08:00
|
|
|
#endif
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
int addOuterQuad(ChessBoardQuad& quad, std::vector<ChessBoardQuad*>& quads);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
void removeQuadFromGroup(std::vector<ChessBoardQuad*>& quads, ChessBoardQuad& q0);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
bool checkBoardMonotony(const std::vector<cv::Point2f>& corners);
|
|
|
|
};
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2015-05-22 16:39:01 +08:00
|
|
|
/***************************************************************************************************/
|
|
|
|
//COMPUTE INTENSITY HISTOGRAM OF INPUT IMAGE
|
2018-05-23 22:44:12 +08:00
|
|
|
template<typename ArrayContainer>
|
|
|
|
static void icvGetIntensityHistogram256(const Mat& img, ArrayContainer& piHist)
|
2015-05-22 16:39:01 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int i = 0; i < 256; i++)
|
|
|
|
piHist[i] = 0;
|
2016-07-19 22:24:55 +08:00
|
|
|
// sum up all pixel in row direction and divide by number of columns
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int j = 0; j < img.rows; ++j)
|
2015-05-22 16:39:01 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
const uchar* row = img.ptr<uchar>(j);
|
|
|
|
for (int i = 0; i < img.cols; i++)
|
2016-07-19 22:24:55 +08:00
|
|
|
{
|
|
|
|
piHist[row[i]]++;
|
|
|
|
}
|
2015-05-22 16:39:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/***************************************************************************************************/
|
2016-07-19 22:24:55 +08:00
|
|
|
//SMOOTH HISTOGRAM USING WINDOW OF SIZE 2*iWidth+1
|
2018-05-23 22:44:12 +08:00
|
|
|
template<int iWidth_, typename ArrayContainer>
|
|
|
|
static void icvSmoothHistogram256(const ArrayContainer& piHist, ArrayContainer& piHistSmooth, int iWidth = 0)
|
2015-05-22 16:39:01 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
CV_DbgAssert(iWidth_ == 0 || (iWidth == iWidth_ || iWidth == 0));
|
|
|
|
iWidth = (iWidth_ != 0) ? iWidth_ : iWidth;
|
|
|
|
CV_Assert(iWidth > 0);
|
|
|
|
CV_DbgAssert(piHist.size() == 256);
|
|
|
|
CV_DbgAssert(piHistSmooth.size() == 256);
|
|
|
|
for (int i = 0; i < 256; ++i)
|
|
|
|
{
|
|
|
|
int iIdx_min = std::max(0, i - iWidth);
|
|
|
|
int iIdx_max = std::min(255, i + iWidth);
|
2016-07-19 22:24:55 +08:00
|
|
|
int iSmooth = 0;
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int iIdx = iIdx_min; iIdx <= iIdx_max; ++iIdx)
|
2016-07-19 22:24:55 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
CV_DbgAssert(iIdx >= 0 && iIdx < 256);
|
|
|
|
iSmooth += piHist[iIdx];
|
2016-07-19 22:24:55 +08:00
|
|
|
}
|
|
|
|
piHistSmooth[i] = iSmooth/(2*iWidth+1);
|
2015-05-22 16:39:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/***************************************************************************************************/
|
2016-07-19 22:24:55 +08:00
|
|
|
//COMPUTE FAST HISTOGRAM GRADIENT
|
2018-05-23 22:44:12 +08:00
|
|
|
template<typename ArrayContainer>
|
|
|
|
static void icvGradientOfHistogram256(const ArrayContainer& piHist, ArrayContainer& piHistGrad)
|
2015-05-22 16:39:01 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
CV_DbgAssert(piHist.size() == 256);
|
|
|
|
CV_DbgAssert(piHistGrad.size() == 256);
|
2016-07-19 22:24:55 +08:00
|
|
|
piHistGrad[0] = 0;
|
2018-05-23 22:44:12 +08:00
|
|
|
int prev_grad = 0;
|
|
|
|
for (int i = 1; i < 255; ++i)
|
2015-05-22 16:39:01 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
int grad = piHist[i-1] - piHist[i+1];
|
|
|
|
if (std::abs(grad) < 100)
|
2016-07-19 22:24:55 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if (prev_grad == 0)
|
|
|
|
grad = -100;
|
2016-07-19 22:24:55 +08:00
|
|
|
else
|
2018-05-23 22:44:12 +08:00
|
|
|
grad = prev_grad;
|
2016-07-19 22:24:55 +08:00
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
piHistGrad[i] = grad;
|
|
|
|
prev_grad = grad;
|
2015-05-22 16:39:01 +08:00
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
piHistGrad[255] = 0;
|
2015-05-22 16:39:01 +08:00
|
|
|
}
|
|
|
|
/***************************************************************************************************/
|
2016-07-19 22:24:55 +08:00
|
|
|
//PERFORM SMART IMAGE THRESHOLDING BASED ON ANALYSIS OF INTENSTY HISTOGRAM
|
2018-05-23 22:44:12 +08:00
|
|
|
static void icvBinarizationHistogramBased(Mat & img)
|
2015-05-22 16:39:01 +08:00
|
|
|
{
|
2016-07-19 22:24:55 +08:00
|
|
|
CV_Assert(img.channels() == 1 && img.depth() == CV_8U);
|
|
|
|
int iCols = img.cols;
|
|
|
|
int iRows = img.rows;
|
|
|
|
int iMaxPix = iCols*iRows;
|
|
|
|
int iMaxPix1 = iMaxPix/100;
|
|
|
|
const int iNumBins = 256;
|
2018-05-23 22:44:12 +08:00
|
|
|
const int iMaxPos = 20;
|
|
|
|
cv::AutoBuffer<int, 256> piHistIntensity(iNumBins);
|
|
|
|
cv::AutoBuffer<int, 256> piHistSmooth(iNumBins);
|
|
|
|
cv::AutoBuffer<int, 256> piHistGrad(iNumBins);
|
|
|
|
cv::AutoBuffer<int> piMaxPos(iMaxPos);
|
2016-07-19 22:24:55 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
icvGetIntensityHistogram256(img, piHistIntensity);
|
2016-07-19 22:24:55 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
#if 0
|
2016-07-19 22:24:55 +08:00
|
|
|
// get accumulated sum starting from bright
|
2018-05-23 22:44:12 +08:00
|
|
|
cv::AutoBuffer<int, 256> piAccumSum(iNumBins);
|
2016-07-19 22:24:55 +08:00
|
|
|
piAccumSum[iNumBins-1] = piHistIntensity[iNumBins-1];
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int i = iNumBins - 2; i >= 0; --i)
|
2015-05-22 16:39:01 +08:00
|
|
|
{
|
2016-07-19 22:24:55 +08:00
|
|
|
piAccumSum[i] = piHistIntensity[i] + piAccumSum[i+1];
|
2015-05-22 16:39:01 +08:00
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
#endif
|
2016-07-19 22:24:55 +08:00
|
|
|
|
|
|
|
// first smooth the distribution
|
2018-05-23 22:44:12 +08:00
|
|
|
//const int iWidth = 1;
|
|
|
|
icvSmoothHistogram256<1>(piHistIntensity, piHistSmooth);
|
2016-07-19 22:24:55 +08:00
|
|
|
|
|
|
|
// compute gradient
|
2018-05-23 22:44:12 +08:00
|
|
|
icvGradientOfHistogram256(piHistSmooth, piHistGrad);
|
2016-07-19 22:24:55 +08:00
|
|
|
|
|
|
|
// check for zeros
|
2018-05-23 22:44:12 +08:00
|
|
|
unsigned iCntMaxima = 0;
|
|
|
|
for (int i = iNumBins-2; (i > 2) && (iCntMaxima < iMaxPos); --i)
|
2015-05-22 16:39:01 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if ((piHistGrad[i-1] < 0) && (piHistGrad[i] > 0))
|
2016-07-19 22:24:55 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
int iSumAroundMax = piHistSmooth[i-1] + piHistSmooth[i] + piHistSmooth[i+1];
|
|
|
|
if (!(iSumAroundMax < iMaxPix1 && i < 64))
|
|
|
|
{
|
|
|
|
piMaxPos[iCntMaxima++] = i;
|
|
|
|
}
|
2016-07-19 22:24:55 +08:00
|
|
|
}
|
2015-05-22 16:39:01 +08:00
|
|
|
}
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
DPRINTF("HIST: MAXIMA COUNT: %d (%d, %d, %d, ...)", iCntMaxima,
|
|
|
|
iCntMaxima > 0 ? piMaxPos[0] : -1,
|
|
|
|
iCntMaxima > 1 ? piMaxPos[1] : -1,
|
|
|
|
iCntMaxima > 2 ? piMaxPos[2] : -1);
|
2018-05-25 20:09:36 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
int iThresh = 0;
|
2018-05-25 20:09:36 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
CV_Assert((size_t)iCntMaxima <= piMaxPos.size());
|
|
|
|
|
|
|
|
DPRINTF("HIST: MAXIMA COUNT: %d (%d, %d, %d, ...)", iCntMaxima,
|
2018-05-25 20:09:36 +08:00
|
|
|
iCntMaxima > 0 ? piMaxPos[0] : -1,
|
|
|
|
iCntMaxima > 1 ? piMaxPos[1] : -1,
|
|
|
|
iCntMaxima > 2 ? piMaxPos[2] : -1);
|
|
|
|
|
|
|
|
if (iCntMaxima == 0)
|
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
// no any maxima inside (only 0 and 255 which are not counted above)
|
2018-05-25 20:09:36 +08:00
|
|
|
// Does image black-write already?
|
|
|
|
const int iMaxPix2 = iMaxPix / 2;
|
|
|
|
for (int sum = 0, i = 0; i < 256; ++i) // select mean intensity
|
|
|
|
{
|
|
|
|
sum += piHistIntensity[i];
|
|
|
|
if (sum > iMaxPix2)
|
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
iThresh = i;
|
|
|
|
break;
|
2018-05-25 20:09:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (iCntMaxima == 1)
|
2015-05-22 16:39:01 +08:00
|
|
|
{
|
2016-07-19 22:24:55 +08:00
|
|
|
iThresh = piMaxPos[0]/2;
|
2015-05-22 16:39:01 +08:00
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
else if (iCntMaxima == 2)
|
2015-05-22 16:39:01 +08:00
|
|
|
{
|
2016-07-19 22:24:55 +08:00
|
|
|
iThresh = (piMaxPos[0] + piMaxPos[1])/2;
|
2015-05-22 16:39:01 +08:00
|
|
|
}
|
2016-07-19 22:24:55 +08:00
|
|
|
else // iCntMaxima >= 3
|
|
|
|
{
|
|
|
|
// CHECKING THRESHOLD FOR WHITE
|
|
|
|
int iIdxAccSum = 0, iAccum = 0;
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int i = iNumBins - 1; i > 0; --i)
|
2016-07-19 22:24:55 +08:00
|
|
|
{
|
|
|
|
iAccum += piHistIntensity[i];
|
|
|
|
// iMaxPix/18 is about 5,5%, minimum required number of pixels required for white part of chessboard
|
|
|
|
if ( iAccum > (iMaxPix/18) )
|
|
|
|
{
|
|
|
|
iIdxAccSum = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-22 16:39:01 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
unsigned iIdxBGMax = 0;
|
2016-07-19 22:24:55 +08:00
|
|
|
int iBrightMax = piMaxPos[0];
|
|
|
|
// printf("iBrightMax = %d\n", iBrightMax);
|
2018-05-23 22:44:12 +08:00
|
|
|
for (unsigned n = 0; n < iCntMaxima - 1; ++n)
|
2016-07-19 22:24:55 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
iIdxBGMax = n + 1;
|
2016-07-19 22:24:55 +08:00
|
|
|
if ( piMaxPos[n] < iIdxAccSum )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
iBrightMax = piMaxPos[n];
|
|
|
|
}
|
2015-05-22 16:39:01 +08:00
|
|
|
|
2016-07-19 22:24:55 +08:00
|
|
|
// CHECKING THRESHOLD FOR BLACK
|
|
|
|
int iMaxVal = piHistIntensity[piMaxPos[iIdxBGMax]];
|
2015-05-22 16:39:01 +08:00
|
|
|
|
2016-07-19 22:24:55 +08:00
|
|
|
//IF TOO CLOSE TO 255, jump to next maximum
|
2018-05-23 22:44:12 +08:00
|
|
|
if (piMaxPos[iIdxBGMax] >= 250 && iIdxBGMax + 1 < iCntMaxima)
|
2016-07-19 22:24:55 +08:00
|
|
|
{
|
|
|
|
iIdxBGMax++;
|
|
|
|
iMaxVal = piHistIntensity[piMaxPos[iIdxBGMax]];
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
for (unsigned n = iIdxBGMax + 1; n < iCntMaxima; n++)
|
2016-07-19 22:24:55 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if (piHistIntensity[piMaxPos[n]] >= iMaxVal)
|
2016-07-19 22:24:55 +08:00
|
|
|
{
|
|
|
|
iMaxVal = piHistIntensity[piMaxPos[n]];
|
|
|
|
iIdxBGMax = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//SETTING THRESHOLD FOR BINARIZATION
|
|
|
|
int iDist2 = (iBrightMax - piMaxPos[iIdxBGMax])/2;
|
|
|
|
iThresh = iBrightMax - iDist2;
|
2018-05-23 22:44:12 +08:00
|
|
|
DPRINTF("THRESHOLD SELECTED = %d, BRIGHTMAX = %d, DARKMAX = %d", iThresh, iBrightMax, piMaxPos[iIdxBGMax]);
|
2016-07-19 22:24:55 +08:00
|
|
|
}
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
if (iThresh > 0)
|
2015-05-22 16:39:01 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
img = (img >= iThresh);
|
2015-05-22 16:39:01 +08:00
|
|
|
}
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
bool findChessboardCorners(InputArray image_, Size pattern_size,
|
|
|
|
OutputArray corners_, int flags)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
CV_INSTRUMENT_REGION()
|
2016-07-19 22:24:55 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
DPRINTF("==== findChessboardCorners(img=%dx%d, pattern=%dx%d, flags=%d)",
|
|
|
|
image_.cols(), image_.rows(), pattern_size.width, pattern_size.height, flags);
|
|
|
|
|
|
|
|
bool found = false;
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
const int min_dilations = 0;
|
|
|
|
const int max_dilations = 7;
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
int type = image_.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
|
|
|
|
Mat img = image_.getMat();
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
CV_CheckType(type, depth == CV_8U && (cn == 1 || cn == 3 || cn == 4),
|
|
|
|
"Only 8-bit grayscale or color images are supported");
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
if (pattern_size.width <= 2 || pattern_size.height <= 2)
|
|
|
|
CV_Error(Error::StsOutOfRange, "Both width and height of the pattern should have bigger than 2");
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
if (!corners_.needed())
|
|
|
|
CV_Error(Error::StsNullPtr, "Null pointer to corners");
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
std::vector<cv::Point2f> out_corners;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2016-07-19 22:24:55 +08:00
|
|
|
if (img.channels() != 1)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2016-07-19 22:24:55 +08:00
|
|
|
cvtColor(img, img, COLOR_BGR2GRAY);
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
int prev_sqr_size = 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2016-07-19 22:24:55 +08:00
|
|
|
Mat thresh_img_new = img.clone();
|
2018-05-23 22:44:12 +08:00
|
|
|
icvBinarizationHistogramBased(thresh_img_new); // process image in-place
|
2016-07-19 22:24:55 +08:00
|
|
|
SHOW("New binarization", thresh_img_new);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
if (flags & CALIB_CB_FAST_CHECK)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2015-05-22 16:39:01 +08:00
|
|
|
//perform new method for checking chessboard using a binary image.
|
|
|
|
//image is binarised using a threshold dependent on the image histogram
|
2016-07-19 22:24:55 +08:00
|
|
|
if (checkChessboardBinary(thresh_img_new, pattern_size) <= 0) //fall back to the old method
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2016-07-19 22:24:55 +08:00
|
|
|
if (checkChessboard(img, pattern_size) <= 0)
|
2015-05-22 16:39:01 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
corners_.release();
|
|
|
|
return false;
|
2015-05-22 16:39:01 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardDetector detector(pattern_size);
|
2016-07-19 22:24:55 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
// Try our standard "1" dilation, but if the pattern is not found, iterate the whole procedure with higher dilations.
|
|
|
|
// This is necessary because some squares simply do not separate properly with a single dilation. However,
|
|
|
|
// we want to use the minimum number of dilations possible since dilations cause the squares to become smaller,
|
|
|
|
// making it difficult to detect smaller squares.
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int dilations = min_dilations; dilations <= max_dilations; dilations++)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2016-07-19 22:24:55 +08:00
|
|
|
//USE BINARY IMAGE COMPUTED USING icvBinarizationHistogramBased METHOD
|
|
|
|
dilate( thresh_img_new, thresh_img_new, Mat(), Point(-1, -1), 1 );
|
2015-05-22 16:39:01 +08:00
|
|
|
|
2016-07-19 22:24:55 +08:00
|
|
|
// So we can find rectangles that go to the edge, we draw a white line around the image edge.
|
|
|
|
// Otherwise FindContours will miss those clipped rectangle contours.
|
|
|
|
// The border color will be the image mean, because otherwise we risk screwing up filters like cvSmooth()...
|
|
|
|
rectangle( thresh_img_new, Point(0,0), Point(thresh_img_new.cols-1, thresh_img_new.rows-1), Scalar(255,255,255), 3, LINE_8);
|
2018-05-23 22:44:12 +08:00
|
|
|
|
|
|
|
detector.reset();
|
|
|
|
|
2018-06-01 00:08:00 +08:00
|
|
|
#ifdef USE_CV_FINDCONTOURS
|
|
|
|
Mat binarized_img = thresh_img_new;
|
|
|
|
#else
|
2018-05-25 20:01:34 +08:00
|
|
|
Mat binarized_img = thresh_img_new.clone(); // make clone because cvFindContours modifies the source image
|
2018-06-01 00:08:00 +08:00
|
|
|
#endif
|
2018-05-23 22:44:12 +08:00
|
|
|
detector.generateQuads(binarized_img, flags);
|
|
|
|
DPRINTF("Quad count: %d/%d", detector.all_quads_count, (pattern_size.width/2+1)*(pattern_size.height/2+1));
|
|
|
|
SHOW_QUADS("New quads", thresh_img_new, &detector.all_quads[0], detector.all_quads_count);
|
|
|
|
if (detector.processQuads(out_corners, prev_sqr_size))
|
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
2016-07-19 22:24:55 +08:00
|
|
|
}
|
2015-05-22 16:39:01 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
DPRINTF("Chessboard detection result 0: %d", (int)found);
|
2016-07-13 20:45:48 +08:00
|
|
|
|
2015-05-22 16:39:01 +08:00
|
|
|
// revert to old, slower, method if detection failed
|
|
|
|
if (!found)
|
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if (flags & CALIB_CB_NORMALIZE_IMAGE)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-07-14 16:14:35 +08:00
|
|
|
img = img.clone();
|
2018-05-23 22:44:12 +08:00
|
|
|
equalizeHist(img, img);
|
2016-07-19 22:24:55 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2016-07-19 22:24:55 +08:00
|
|
|
Mat thresh_img;
|
|
|
|
prev_sqr_size = 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
DPRINTF("Fallback to old algorithm");
|
|
|
|
const bool useAdaptive = flags & CALIB_CB_ADAPTIVE_THRESH;
|
2016-07-19 22:24:55 +08:00
|
|
|
if (!useAdaptive)
|
|
|
|
{
|
|
|
|
// empiric threshold level
|
|
|
|
// thresholding performed here and not inside the cycle to save processing time
|
|
|
|
double mean = cv::mean(img).val[0];
|
2018-05-23 22:44:12 +08:00
|
|
|
int thresh_level = std::max(cvRound(mean - 10), 10);
|
|
|
|
threshold(img, thresh_img, thresh_level, 255, THRESH_BINARY);
|
2016-07-19 22:24:55 +08:00
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
//if flag CALIB_CB_ADAPTIVE_THRESH is not set it doesn't make sense to iterate over k
|
2016-07-19 22:24:55 +08:00
|
|
|
int max_k = useAdaptive ? 6 : 1;
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int k = 0; k < max_k && !found; k++)
|
2016-07-19 22:24:55 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int dilations = min_dilations; dilations <= max_dilations; dilations++)
|
2015-05-22 16:39:01 +08:00
|
|
|
{
|
2016-07-19 22:24:55 +08:00
|
|
|
// convert the input grayscale image to binary (black-n-white)
|
|
|
|
if (useAdaptive)
|
|
|
|
{
|
|
|
|
int block_size = cvRound(prev_sqr_size == 0
|
2018-05-23 22:44:12 +08:00
|
|
|
? std::min(img.cols, img.rows) * (k % 2 == 0 ? 0.2 : 0.1)
|
2016-07-19 22:24:55 +08:00
|
|
|
: prev_sqr_size * 2);
|
|
|
|
block_size = block_size | 1;
|
|
|
|
// convert to binary
|
|
|
|
adaptiveThreshold( img, thresh_img, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, block_size, (k/2)*5 );
|
|
|
|
if (dilations > 0)
|
|
|
|
dilate( thresh_img, thresh_img, Mat(), Point(-1, -1), dilations-1 );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2016-07-19 22:24:55 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dilate( thresh_img, thresh_img, Mat(), Point(-1, -1), 1 );
|
|
|
|
}
|
|
|
|
SHOW("Old binarization", thresh_img);
|
|
|
|
|
|
|
|
// So we can find rectangles that go to the edge, we draw a white line around the image edge.
|
|
|
|
// Otherwise FindContours will miss those clipped rectangle contours.
|
|
|
|
// The border color will be the image mean, because otherwise we risk screwing up filters like cvSmooth()...
|
|
|
|
rectangle( thresh_img, Point(0,0), Point(thresh_img.cols-1, thresh_img.rows-1), Scalar(255,255,255), 3, LINE_8);
|
2018-05-23 22:44:12 +08:00
|
|
|
|
|
|
|
detector.reset();
|
|
|
|
|
2018-06-01 00:08:00 +08:00
|
|
|
#ifdef USE_CV_FINDCONTOURS
|
|
|
|
Mat binarized_img = thresh_img;
|
|
|
|
#else
|
2018-05-25 20:01:34 +08:00
|
|
|
Mat binarized_img = (useAdaptive) ? thresh_img : thresh_img.clone(); // make clone because cvFindContours modifies the source image
|
2018-06-01 00:08:00 +08:00
|
|
|
#endif
|
2018-05-23 22:44:12 +08:00
|
|
|
detector.generateQuads(binarized_img, flags);
|
|
|
|
DPRINTF("Quad count: %d/%d", detector.all_quads_count, (pattern_size.width/2+1)*(pattern_size.height/2+1));
|
|
|
|
SHOW_QUADS("Old quads", thresh_img, &detector.all_quads[0], detector.all_quads_count);
|
|
|
|
if (detector.processQuads(out_corners, prev_sqr_size))
|
|
|
|
{
|
2016-07-19 22:24:55 +08:00
|
|
|
found = 1;
|
2018-05-23 22:44:12 +08:00
|
|
|
break;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2016-07-19 22:24:55 +08:00
|
|
|
}
|
2015-05-22 16:39:01 +08:00
|
|
|
}
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
DPRINTF("Chessboard detection result 1: %d", (int)found);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
if (found)
|
|
|
|
found = detector.checkBoardMonotony(out_corners);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
DPRINTF("Chessboard detection result 2: %d", (int)found);
|
2016-07-13 20:45:48 +08:00
|
|
|
|
2012-06-09 23:00:04 +08:00
|
|
|
// check that none of the found corners is too close to the image boundary
|
2018-05-23 22:44:12 +08:00
|
|
|
if (found)
|
2012-06-09 23:00:04 +08:00
|
|
|
{
|
|
|
|
const int BORDER = 8;
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int k = 0; k < pattern_size.width*pattern_size.height; ++k)
|
2012-06-09 23:00:04 +08:00
|
|
|
{
|
2016-07-19 22:24:55 +08:00
|
|
|
if( out_corners[k].x <= BORDER || out_corners[k].x > img.cols - BORDER ||
|
|
|
|
out_corners[k].y <= BORDER || out_corners[k].y > img.rows - BORDER )
|
2018-05-23 22:44:12 +08:00
|
|
|
{
|
|
|
|
found = false;
|
2012-06-09 23:00:04 +08:00
|
|
|
break;
|
2018-05-23 22:44:12 +08:00
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
}
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
DPRINTF("Chessboard detection result 3: %d", (int)found);
|
2016-07-13 20:45:48 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
if (found)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if ((pattern_size.height & 1) == 0 && (pattern_size.width & 1) == 0 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2016-07-19 22:24:55 +08:00
|
|
|
int last_row = (pattern_size.height-1)*pattern_size.width;
|
|
|
|
double dy0 = out_corners[last_row].y - out_corners[0].y;
|
2018-05-23 22:44:12 +08:00
|
|
|
if (dy0 < 0)
|
2016-07-19 22:24:55 +08:00
|
|
|
{
|
|
|
|
int n = pattern_size.width*pattern_size.height;
|
|
|
|
for(int i = 0; i < n/2; i++ )
|
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
std::swap(out_corners[i], out_corners[n-i-1]);
|
2016-07-19 22:24:55 +08:00
|
|
|
}
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
cv::cornerSubPix(img, out_corners, Size(2, 2), Size(-1,-1),
|
|
|
|
cv::TermCriteria(TermCriteria::EPS + TermCriteria::MAX_ITER, 15, 0.1));
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
|
|
|
|
Mat(out_corners).copyTo(corners_);
|
2010-05-12 01:44:00 +08:00
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
//
|
|
|
|
// Checks that each board row and column is pretty much monotonous curve:
|
|
|
|
// It analyzes each row and each column of the chessboard as following:
|
|
|
|
// for each corner c lying between end points in the same row/column it checks that
|
|
|
|
// the point projection to the line segment (a,b) is lying between projections
|
|
|
|
// of the neighbor corners in the same row/column.
|
|
|
|
//
|
|
|
|
// This function has been created as temporary workaround for the bug in current implementation
|
|
|
|
// of cvFindChessboardCornes that produces absolutely unordered sets of corners.
|
|
|
|
//
|
2018-05-23 22:44:12 +08:00
|
|
|
bool ChessBoardDetector::checkBoardMonotony(const std::vector<cv::Point2f>& corners)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int k = 0; k < 2; ++k)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
int max_i = (k == 0 ? pattern_size.height : pattern_size.width);
|
|
|
|
int max_j = (k == 0 ? pattern_size.width: pattern_size.height) - 1;
|
|
|
|
for (int i = 0; i < max_i; ++i)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
cv::Point2f a = k == 0 ? corners[i*pattern_size.width] : corners[i];
|
|
|
|
cv::Point2f b = k == 0 ? corners[(i+1)*pattern_size.width-1]
|
|
|
|
: corners[(pattern_size.height-1)*pattern_size.width + i];
|
|
|
|
float dx0 = b.x - a.x, dy0 = b.y - a.y;
|
|
|
|
if (fabs(dx0) + fabs(dy0) < FLT_EPSILON)
|
|
|
|
return false;
|
|
|
|
float prevt = 0;
|
|
|
|
for (int j = 1; j < max_j; ++j)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
cv::Point2f c = k == 0 ? corners[i*pattern_size.width + j]
|
|
|
|
: corners[j*pattern_size.width + i];
|
2010-05-12 01:44:00 +08:00
|
|
|
float t = ((c.x - a.x)*dx0 + (c.y - a.y)*dy0)/(dx0*dx0 + dy0*dy0);
|
2018-05-23 22:44:12 +08:00
|
|
|
if (t < prevt || t > 1)
|
|
|
|
return false;
|
2010-05-12 01:44:00 +08:00
|
|
|
prevt = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
return true;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// order a group of connected quads
|
|
|
|
// order of corners:
|
|
|
|
// 0 is top left
|
|
|
|
// clockwise from there
|
|
|
|
// note: "top left" is nominal, depends on initial ordering of starting quad
|
|
|
|
// but all other quads are ordered consistently
|
|
|
|
//
|
|
|
|
// can change the number of quads in the group
|
|
|
|
// can add quads, so we need to have quad/corner arrays passed in
|
|
|
|
//
|
2018-05-23 22:44:12 +08:00
|
|
|
int ChessBoardDetector::orderFoundConnectedQuads(std::vector<ChessBoardQuad*>& quads)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
const int max_quad_buf_size = (int)all_quads.size();
|
|
|
|
int quad_count = (int)quads.size();
|
|
|
|
|
|
|
|
std::stack<ChessBoardQuad*> stack;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// first find an interior quad
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardQuad *start = NULL;
|
|
|
|
for (int i = 0; i < quad_count; i++)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
if (quads[i]->count == 4)
|
|
|
|
{
|
|
|
|
start = quads[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (start == NULL)
|
|
|
|
return 0; // no 4-connected quad
|
|
|
|
|
|
|
|
// start with first one, assign rows/cols
|
|
|
|
int row_min = 0, col_min = 0, row_max=0, col_max = 0;
|
|
|
|
|
|
|
|
std::map<int, int> col_hist;
|
|
|
|
std::map<int, int> row_hist;
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
stack.push(start);
|
2010-05-12 01:44:00 +08:00
|
|
|
start->row = 0;
|
|
|
|
start->col = 0;
|
|
|
|
start->ordered = true;
|
|
|
|
|
|
|
|
// Recursively order the quads so that all position numbers (e.g.,
|
|
|
|
// 0,1,2,3) are in the at the same relative corner (e.g., lower right).
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
while (!stack.empty())
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardQuad* q = stack.top(); stack.pop(); CV_Assert(q);
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
int col = q->col;
|
|
|
|
int row = q->row;
|
|
|
|
col_hist[col]++;
|
|
|
|
row_hist[row]++;
|
|
|
|
|
|
|
|
// check min/max
|
|
|
|
if (row > row_max) row_max = row;
|
|
|
|
if (row < row_min) row_min = row;
|
|
|
|
if (col > col_max) col_max = col;
|
|
|
|
if (col < col_min) col_min = col;
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int i = 0; i < 4; i++)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardQuad *neighbor = q->neighbors[i];
|
2010-05-12 01:44:00 +08:00
|
|
|
switch(i) // adjust col, row for this quad
|
|
|
|
{ // start at top left, go clockwise
|
|
|
|
case 0:
|
|
|
|
row--; col--; break;
|
|
|
|
case 1:
|
|
|
|
col += 2; break;
|
|
|
|
case 2:
|
2012-06-09 23:00:04 +08:00
|
|
|
row += 2; break;
|
2010-05-12 01:44:00 +08:00
|
|
|
case 3:
|
|
|
|
col -= 2; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// just do inside quads
|
|
|
|
if (neighbor && neighbor->ordered == false && neighbor->count == 4)
|
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
DPRINTF("col: %d row: %d", col, row);
|
|
|
|
CV_Assert(q->corners[i]);
|
|
|
|
orderQuad(*neighbor, *(q->corners[i]), (i+2)&3); // set in order
|
2010-05-12 01:44:00 +08:00
|
|
|
neighbor->ordered = true;
|
|
|
|
neighbor->row = row;
|
|
|
|
neighbor->col = col;
|
2018-05-23 22:44:12 +08:00
|
|
|
stack.push(neighbor);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
#ifdef DEBUG_CHESSBOARD
|
|
|
|
for (int i = col_min; i <= col_max; i++)
|
|
|
|
DPRINTF("HIST[%d] = %d", i, col_hist[i]);
|
|
|
|
#endif
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// analyze inner quad structure
|
|
|
|
int w = pattern_size.width - 1;
|
|
|
|
int h = pattern_size.height - 1;
|
|
|
|
int drow = row_max - row_min + 1;
|
|
|
|
int dcol = col_max - col_min + 1;
|
|
|
|
|
|
|
|
// normalize pattern and found quad indices
|
|
|
|
if ((w > h && dcol < drow) ||
|
|
|
|
(w < h && drow < dcol))
|
|
|
|
{
|
|
|
|
h = pattern_size.width - 1;
|
|
|
|
w = pattern_size.height - 1;
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
DPRINTF("Size: %dx%d Pattern: %dx%d", dcol, drow, w, h);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// check if there are enough inner quads
|
|
|
|
if (dcol < w || drow < h) // found enough inner quads?
|
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
DPRINTF("Too few inner quad rows/cols");
|
2010-05-12 01:44:00 +08:00
|
|
|
return 0; // no, return
|
|
|
|
}
|
|
|
|
#ifdef ENABLE_TRIM_COL_ROW
|
|
|
|
// too many columns, not very common
|
|
|
|
if (dcol == w+1) // too many, trim
|
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
DPRINTF("Trimming cols");
|
2010-05-12 01:44:00 +08:00
|
|
|
if (col_hist[col_max] > col_hist[col_min])
|
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
DPRINTF("Trimming left col");
|
|
|
|
trimCol(quads, col_min, -1);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
DPRINTF("Trimming right col");
|
|
|
|
trimCol(quads, col_max, +1);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// too many rows, not very common
|
|
|
|
if (drow == h+1) // too many, trim
|
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
DPRINTF("Trimming rows");
|
2010-05-12 01:44:00 +08:00
|
|
|
if (row_hist[row_max] > row_hist[row_min])
|
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
DPRINTF("Trimming top row");
|
|
|
|
trimRow(quads, row_min, -1);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
DPRINTF("Trimming bottom row");
|
|
|
|
trimRow(quads, row_max, +1);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
|
|
|
|
quad_count = (int)quads.size(); // update after icvTrimCol/icvTrimRow
|
2010-05-12 01:44:00 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// check edges of inner quads
|
|
|
|
// if there is an outer quad missing, fill it in
|
|
|
|
// first order all inner quads
|
|
|
|
int found = 0;
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int i=0; i < quad_count; ++i)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardQuad& q = *quads[i];
|
|
|
|
if (q.count != 4)
|
|
|
|
continue;
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
{ // ok, look at neighbors
|
2018-05-23 22:44:12 +08:00
|
|
|
int col = q.col;
|
|
|
|
int row = q.row;
|
|
|
|
for (int j = 0; j < 4; j++)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
switch(j) // adjust col, row for this quad
|
2018-05-23 22:44:12 +08:00
|
|
|
{ // start at top left, go clockwise
|
2010-05-12 01:44:00 +08:00
|
|
|
case 0:
|
|
|
|
row--; col--; break;
|
|
|
|
case 1:
|
|
|
|
col += 2; break;
|
|
|
|
case 2:
|
2012-06-09 23:00:04 +08:00
|
|
|
row += 2; break;
|
2010-05-12 01:44:00 +08:00
|
|
|
case 3:
|
|
|
|
col -= 2; break;
|
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardQuad *neighbor = q.neighbors[j];
|
2010-05-12 01:44:00 +08:00
|
|
|
if (neighbor && !neighbor->ordered && // is it an inner quad?
|
|
|
|
col <= col_max && col >= col_min &&
|
|
|
|
row <= row_max && row >= row_min)
|
|
|
|
{
|
|
|
|
// if so, set in order
|
2018-05-23 22:44:12 +08:00
|
|
|
DPRINTF("Adding inner: col: %d row: %d", col, row);
|
2010-05-12 01:44:00 +08:00
|
|
|
found++;
|
2018-05-23 22:44:12 +08:00
|
|
|
CV_Assert(q.corners[j]);
|
|
|
|
orderQuad(*neighbor, *q.corners[j], (j+2)&3);
|
2010-05-12 01:44:00 +08:00
|
|
|
neighbor->ordered = true;
|
|
|
|
neighbor->row = row;
|
|
|
|
neighbor->col = col;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we have found inner quads, add corresponding outer quads,
|
|
|
|
// which are missing
|
|
|
|
if (found > 0)
|
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
DPRINTF("Found %d inner quads not connected to outer quads, repairing", found);
|
|
|
|
for (int i = 0; i < quad_count && all_quads_count < max_quad_buf_size; i++)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardQuad& q = *quads[i];
|
|
|
|
if (q.count < 4 && q.ordered)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
int added = addOuterQuad(q, quads);
|
2010-05-12 01:44:00 +08:00
|
|
|
quad_count += added;
|
|
|
|
}
|
|
|
|
}
|
2015-08-28 09:14:08 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
if (all_quads_count >= max_quad_buf_size)
|
2015-08-28 09:14:08 +08:00
|
|
|
return 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// final trimming of outer quads
|
2012-06-09 23:00:04 +08:00
|
|
|
if (dcol == w && drow == h) // found correct inner quads
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
DPRINTF("Inner bounds ok, check outer quads");
|
|
|
|
for (int i = quad_count - 1; i >= 0; i--) // eliminate any quad not connected to an ordered quad
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardQuad& q = *quads[i];
|
|
|
|
if (q.ordered == false)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
bool outer = false;
|
|
|
|
for (int j=0; j<4; j++) // any neighbors that are ordered?
|
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if (q.neighbors[j] && q.neighbors[j]->ordered)
|
2010-05-12 01:44:00 +08:00
|
|
|
outer = true;
|
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
if (!outer) // not an outer quad, eliminate
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
DPRINTF("Removing quad %d", i);
|
|
|
|
removeQuadFromGroup(quads, q);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
return (int)quads.size();
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// add an outer quad
|
|
|
|
// looks for the neighbor of <quad> that isn't present,
|
|
|
|
// tries to add it in.
|
|
|
|
// <quad> is ordered
|
2018-05-23 22:44:12 +08:00
|
|
|
int ChessBoardDetector::addOuterQuad(ChessBoardQuad& quad, std::vector<ChessBoardQuad*>& quads)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
int added = 0;
|
2018-05-23 22:44:12 +08:00
|
|
|
int max_quad_buf_size = (int)all_quads.size();
|
|
|
|
|
|
|
|
for (int i = 0; i < 4 && all_quads_count < max_quad_buf_size; i++) // find no-neighbor corners
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if (!quad.neighbors[i]) // ok, create and add neighbor
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
int j = (i+2)&3;
|
|
|
|
DPRINTF("Adding quad as neighbor 2");
|
|
|
|
int q_index = all_quads_count++;
|
|
|
|
ChessBoardQuad& q = all_quads[q_index];
|
|
|
|
q = ChessBoardQuad(0);
|
2010-05-12 01:44:00 +08:00
|
|
|
added++;
|
2018-05-23 22:44:12 +08:00
|
|
|
quads.push_back(&q);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// set neighbor and group id
|
2018-05-23 22:44:12 +08:00
|
|
|
quad.neighbors[i] = &q;
|
|
|
|
quad.count += 1;
|
|
|
|
q.neighbors[j] = &quad;
|
|
|
|
q.group_idx = quad.group_idx;
|
|
|
|
q.count = 1; // number of neighbors
|
|
|
|
q.ordered = false;
|
|
|
|
q.edge_len = quad.edge_len;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// make corners of new quad
|
|
|
|
// same as neighbor quad, but offset
|
2018-05-23 22:44:12 +08:00
|
|
|
const cv::Point2f pt_offset = quad.corners[i]->pt - quad.corners[j]->pt;
|
|
|
|
for (int k = 0; k < 4; k++)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardCorner& corner = (ChessBoardCorner&)all_corners[q_index * 4 + k];
|
|
|
|
const cv::Point2f& pt = quad.corners[k]->pt;
|
|
|
|
corner = ChessBoardCorner(pt);
|
|
|
|
q.corners[k] = &corner;
|
|
|
|
corner.pt += pt_offset;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
// have to set exact corner
|
2018-05-23 22:44:12 +08:00
|
|
|
q.corners[j] = quad.corners[i];
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// now find other neighbor and add it, if possible
|
2018-05-23 22:44:12 +08:00
|
|
|
int next_i = (i + 1) & 3;
|
|
|
|
int prev_i = (i + 3) & 3; // equal to (j + 1) & 3
|
|
|
|
ChessBoardQuad* quad_prev = quad.neighbors[prev_i];
|
|
|
|
if (quad_prev &&
|
|
|
|
quad_prev->ordered &&
|
|
|
|
quad_prev->neighbors[i] &&
|
|
|
|
quad_prev->neighbors[i]->ordered )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardQuad* qn = quad_prev->neighbors[i];
|
|
|
|
q.count = 2;
|
|
|
|
q.neighbors[prev_i] = qn;
|
|
|
|
qn->neighbors[next_i] = &q;
|
2010-05-12 01:44:00 +08:00
|
|
|
qn->count += 1;
|
|
|
|
// have to set exact corner
|
2018-05-23 22:44:12 +08:00
|
|
|
q.corners[prev_i] = qn->corners[next_i];
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return added;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// trimming routines
|
|
|
|
#ifdef ENABLE_TRIM_COL_ROW
|
2018-05-23 22:44:12 +08:00
|
|
|
void ChessBoardDetector::trimCol(std::vector<ChessBoardQuad*>& quads, int col, int dir)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
std::vector<ChessBoardQuad*> quads_(quads);
|
2010-05-12 01:44:00 +08:00
|
|
|
// find the right quad(s)
|
2018-05-23 22:44:12 +08:00
|
|
|
for (size_t i = 0; i < quads_.size(); ++i)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardQuad& q = *quads_[i];
|
2010-05-12 01:44:00 +08:00
|
|
|
#ifdef DEBUG_CHESSBOARD
|
2018-05-23 22:44:12 +08:00
|
|
|
if (q.ordered)
|
|
|
|
DPRINTF("i: %d index: %d cur: %d", (int)i, col, q.col);
|
2010-05-12 01:44:00 +08:00
|
|
|
#endif
|
2018-05-23 22:44:12 +08:00
|
|
|
if (q.ordered && q.col == col)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
if (dir == 1)
|
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if (q.neighbors[1])
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
removeQuadFromGroup(quads, *q.neighbors[1]);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
if (q.neighbors[2])
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
removeQuadFromGroup(quads, *q.neighbors[2]);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if (q.neighbors[0])
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
removeQuadFromGroup(quads, *q.neighbors[0]);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
if (q.neighbors[3])
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
removeQuadFromGroup(quads, *q.neighbors[3]);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
void ChessBoardDetector::trimRow(std::vector<ChessBoardQuad*>& quads, int row, int dir)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
std::vector<ChessBoardQuad*> quads_(quads);
|
2010-05-12 01:44:00 +08:00
|
|
|
// find the right quad(s)
|
2018-05-23 22:44:12 +08:00
|
|
|
for (size_t i = 0; i < quads_.size(); ++i)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardQuad& q = *quads_[i];
|
2010-05-12 01:44:00 +08:00
|
|
|
#ifdef DEBUG_CHESSBOARD
|
2018-05-23 22:44:12 +08:00
|
|
|
if (q.ordered)
|
|
|
|
DPRINTF("i: %d index: %d cur: %d", (int)i, row, q.row);
|
2010-05-12 01:44:00 +08:00
|
|
|
#endif
|
2018-05-23 22:44:12 +08:00
|
|
|
if (q.ordered && q.row == row)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
if (dir == 1) // remove from bottom
|
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if (q.neighbors[2])
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
removeQuadFromGroup(quads, *q.neighbors[2]);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
if (q.neighbors[3])
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
removeQuadFromGroup(quads, *q.neighbors[3]);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else // remove from top
|
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if (q.neighbors[0])
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
removeQuadFromGroup(quads, *q.neighbors[0]);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
if (q.neighbors[1])
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
removeQuadFromGroup(quads, *q.neighbors[1]);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//
|
|
|
|
// remove quad from quad group
|
|
|
|
//
|
2018-05-23 22:44:12 +08:00
|
|
|
void ChessBoardDetector::removeQuadFromGroup(std::vector<ChessBoardQuad*>& quads, ChessBoardQuad& q0)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
const int count = (int)quads.size();
|
|
|
|
|
|
|
|
int self_idx = -1;
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
// remove any references to this quad as a neighbor
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int i = 0; i < count; ++i)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardQuad* q = quads[i];
|
|
|
|
if (q == &q0)
|
|
|
|
self_idx = i;
|
|
|
|
for (int j = 0; j < 4; j++)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if (q->neighbors[j] == &q0)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
q->neighbors[j] = NULL;
|
2010-05-12 01:44:00 +08:00
|
|
|
q->count--;
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int k = 0; k < 4; ++k)
|
|
|
|
{
|
|
|
|
if (q0.neighbors[k] == q)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
q0.neighbors[k] = 0;
|
|
|
|
q0.count--;
|
|
|
|
#ifndef _DEBUG
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
2018-05-23 22:44:12 +08:00
|
|
|
#endif
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
}
|
2016-03-07 19:36:16 +08:00
|
|
|
break;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
CV_Assert(self_idx >= 0); // item itself should be found
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// remove the quad
|
2018-05-23 22:44:12 +08:00
|
|
|
if (self_idx != count-1)
|
|
|
|
quads[self_idx] = quads[count-1];
|
|
|
|
quads.resize(count - 1);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// put quad into correct order, where <corner> has value <common>
|
|
|
|
//
|
2018-05-23 22:44:12 +08:00
|
|
|
void ChessBoardDetector::orderQuad(ChessBoardQuad& quad, ChessBoardCorner& corner, int common)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
CV_DbgAssert(common >= 0 && common <= 3);
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
// find the corner
|
2018-05-23 22:44:12 +08:00
|
|
|
int tc = 0;;
|
|
|
|
for (; tc < 4; ++tc)
|
|
|
|
if (quad.corners[tc]->pt == corner.pt)
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
// set corner order
|
|
|
|
// shift
|
|
|
|
while (tc != common)
|
|
|
|
{
|
|
|
|
// shift by one
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardCorner *tempc = quad.corners[3];
|
|
|
|
ChessBoardQuad *tempq = quad.neighbors[3];
|
|
|
|
for (int i = 3; i > 0; --i)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
quad.corners[i] = quad.corners[i-1];
|
|
|
|
quad.neighbors[i] = quad.neighbors[i-1];
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
quad.corners[0] = tempc;
|
|
|
|
quad.neighbors[0] = tempq;
|
|
|
|
tc = (tc + 1) & 3;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// if we found too many connect quads, remove those which probably do not belong.
|
2018-05-23 22:44:12 +08:00
|
|
|
int ChessBoardDetector::cleanFoundConnectedQuads(std::vector<ChessBoardQuad*>& quad_group)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
// number of quads this pattern should contain
|
|
|
|
int count = ((pattern_size.width + 1)*(pattern_size.height + 1) + 1)/2;
|
|
|
|
|
|
|
|
// if we have more quadrangles than we should,
|
|
|
|
// try to eliminate duplicates or ones which don't belong to the pattern rectangle...
|
2018-05-23 22:44:12 +08:00
|
|
|
int quad_count = (int)quad_group.size();
|
|
|
|
if (quad_count <= count)
|
2010-05-12 01:44:00 +08:00
|
|
|
return quad_count;
|
2018-05-23 22:44:12 +08:00
|
|
|
CV_DbgAssert(quad_count > 0);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// create an array of quadrangle centers
|
2018-05-23 22:44:12 +08:00
|
|
|
cv::AutoBuffer<cv::Point2f> centers(quad_count);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
cv::Point2f center;
|
|
|
|
for (int i = 0; i < quad_count; ++i)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardQuad* q = quad_group[i];
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
const cv::Point2f ci = (
|
|
|
|
q->corners[0]->pt +
|
|
|
|
q->corners[1]->pt +
|
|
|
|
q->corners[2]->pt +
|
|
|
|
q->corners[3]->pt
|
|
|
|
) * 0.25f;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
centers[i] = ci;
|
2018-05-23 22:44:12 +08:00
|
|
|
center += ci;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
center.x *= (1.0f / quad_count);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// If we still have more quadrangles than we should,
|
|
|
|
// we try to eliminate bad ones based on minimizing the bounding box.
|
|
|
|
// We iteratively remove the point which reduces the size of
|
|
|
|
// the bounding box of the blobs the most
|
|
|
|
// (since we want the rectangle to be as small as possible)
|
|
|
|
// remove the quadrange that causes the biggest reduction
|
|
|
|
// in pattern size until we have the correct number
|
2018-05-23 22:44:12 +08:00
|
|
|
for (; quad_count > count; quad_count--)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
double min_box_area = DBL_MAX;
|
2018-05-23 22:44:12 +08:00
|
|
|
int min_box_area_index = -1;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// For each point, calculate box area without that point
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int skip = 0; skip < quad_count; ++skip)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
// get bounding rectangle
|
2018-05-23 22:44:12 +08:00
|
|
|
cv::Point2f temp = centers[skip]; // temporarily make index 'skip' the same as
|
2010-05-12 01:44:00 +08:00
|
|
|
centers[skip] = center; // pattern center (so it is not counted for convex hull)
|
2018-05-23 22:44:12 +08:00
|
|
|
std::vector<Point2f> hull;
|
|
|
|
Mat points(1, quad_count, CV_32FC2, ¢ers[0]);
|
|
|
|
cv::convexHull(points, hull, true);
|
2010-05-12 01:44:00 +08:00
|
|
|
centers[skip] = temp;
|
2018-05-23 22:44:12 +08:00
|
|
|
double hull_area = contourArea(hull, true);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// remember smallest box area
|
2018-05-23 22:44:12 +08:00
|
|
|
if (hull_area < min_box_area)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
min_box_area = hull_area;
|
|
|
|
min_box_area_index = skip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardQuad *q0 = quad_group[min_box_area_index];
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// remove any references to this quad as a neighbor
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int i = 0; i < quad_count; ++i)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardQuad *q = quad_group[i];
|
|
|
|
for (int j = 0; j < 4; ++j)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if (q->neighbors[j] == q0)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
q->neighbors[j] = 0;
|
|
|
|
q->count--;
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int k = 0; k < 4; ++k)
|
|
|
|
{
|
|
|
|
if (q0->neighbors[k] == q)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
q0->neighbors[k] = 0;
|
|
|
|
q0->count--;
|
|
|
|
break;
|
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove the quad
|
|
|
|
quad_count--;
|
|
|
|
quad_group[min_box_area_index] = quad_group[quad_count];
|
|
|
|
centers[min_box_area_index] = centers[quad_count];
|
|
|
|
}
|
|
|
|
|
|
|
|
return quad_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
|
|
|
|
void ChessBoardDetector::findConnectedQuads(std::vector<ChessBoardQuad*>& out_group, int group_idx)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
out_group.clear();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
std::stack<ChessBoardQuad*> stack;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
int i = 0;
|
|
|
|
for (; i < all_quads_count; i++)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardQuad* q = (ChessBoardQuad*)&all_quads[i];
|
|
|
|
|
|
|
|
// Scan the array for a first unlabeled quad
|
|
|
|
if (q->count <= 0 || q->group_idx >= 0) continue;
|
|
|
|
|
|
|
|
// Recursively find a group of connected quads starting from the seed all_quads[i]
|
|
|
|
stack.push(q);
|
|
|
|
out_group.push_back(q);
|
2010-05-12 01:44:00 +08:00
|
|
|
q->group_idx = group_idx;
|
|
|
|
q->ordered = false;
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
while (!stack.empty())
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
q = stack.top(); CV_Assert(q);
|
|
|
|
stack.pop();
|
|
|
|
for (int k = 0; k < 4; k++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardQuad *neighbor = q->neighbors[k];
|
|
|
|
if (neighbor && neighbor->count > 0 && neighbor->group_idx < 0 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
stack.push(neighbor);
|
|
|
|
out_group.push_back(neighbor);
|
2010-05-12 01:44:00 +08:00
|
|
|
neighbor->group_idx = group_idx;
|
|
|
|
neighbor->ordered = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
break;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
int ChessBoardDetector::checkQuadGroup(std::vector<ChessBoardQuad*>& quad_group, std::vector<ChessBoardCorner*>& out_corners)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
const int ROW1 = 1000000;
|
|
|
|
const int ROW2 = 2000000;
|
|
|
|
const int ROW_ = 3000000;
|
2018-05-23 22:44:12 +08:00
|
|
|
|
|
|
|
int quad_count = (int)quad_group.size();
|
|
|
|
|
|
|
|
std::vector<ChessBoardCorner*> corners(quad_count*4);
|
|
|
|
int corner_count = 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
int width = 0, height = 0;
|
|
|
|
int hist[5] = {0,0,0,0,0};
|
2018-05-23 22:44:12 +08:00
|
|
|
//ChessBoardCorner* first = 0, *first2 = 0, *right, *cur, *below, *c;
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
// build dual graph, which vertices are internal quad corners
|
|
|
|
// and two vertices are connected iff they lie on the same quad edge
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int i = 0; i < quad_count; ++i)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardQuad* q = quad_group[i];
|
2010-05-12 01:44:00 +08:00
|
|
|
/*CvScalar color = q->count == 0 ? cvScalar(0,255,255) :
|
|
|
|
q->count == 1 ? cvScalar(0,0,255) :
|
|
|
|
q->count == 2 ? cvScalar(0,255,0) :
|
|
|
|
q->count == 3 ? cvScalar(255,255,0) :
|
|
|
|
cvScalar(255,0,0);*/
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int j = 0; j < 4; ++j)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
//cvLine( debug_img, cvPointFrom32f(q->corners[j]->pt), cvPointFrom32f(q->corners[(j+1)&3]->pt), color, 1, CV_AA, 0 );
|
2018-05-23 22:44:12 +08:00
|
|
|
if (q->neighbors[j])
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
int next_j = (j + 1) & 3;
|
|
|
|
ChessBoardCorner *a = q->corners[j], *b = q->corners[next_j];
|
2010-05-12 01:44:00 +08:00
|
|
|
// mark internal corners that belong to:
|
|
|
|
// - a quad with a single neighbor - with ROW1,
|
|
|
|
// - a quad with two neighbors - with ROW2
|
|
|
|
// make the rest of internal corners with ROW_
|
|
|
|
int row_flag = q->count == 1 ? ROW1 : q->count == 2 ? ROW2 : ROW_;
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
if (a->row == 0)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
corners[corner_count++] = a;
|
|
|
|
a->row = row_flag;
|
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
else if (a->row > row_flag)
|
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
a->row = row_flag;
|
2018-05-23 22:44:12 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
if (q->neighbors[next_j])
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if (a->count >= 4 || b->count >= 4)
|
2010-05-12 01:44:00 +08:00
|
|
|
goto finalize;
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int k = 0; k < 4; ++k)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if (a->neighbors[k] == b)
|
2010-05-12 01:44:00 +08:00
|
|
|
goto finalize;
|
2018-05-23 22:44:12 +08:00
|
|
|
if (b->neighbors[k] == a)
|
2010-05-12 01:44:00 +08:00
|
|
|
goto finalize;
|
|
|
|
}
|
|
|
|
a->neighbors[a->count++] = b;
|
|
|
|
b->neighbors[b->count++] = a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
if (corner_count != pattern_size.width*pattern_size.height)
|
2010-05-12 01:44:00 +08:00
|
|
|
goto finalize;
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
{
|
|
|
|
ChessBoardCorner* first = NULL, *first2 = NULL;
|
|
|
|
for (int i = 0; i < corner_count; ++i)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
int n = corners[i]->count;
|
2018-05-23 22:44:12 +08:00
|
|
|
CV_DbgAssert(0 <= n && n <= 4);
|
2010-05-12 01:44:00 +08:00
|
|
|
hist[n]++;
|
2018-05-23 22:44:12 +08:00
|
|
|
if (!first && n == 2)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if (corners[i]->row == ROW1)
|
2010-05-12 01:44:00 +08:00
|
|
|
first = corners[i];
|
2018-05-23 22:44:12 +08:00
|
|
|
else if (!first2 && corners[i]->row == ROW2)
|
2010-05-12 01:44:00 +08:00
|
|
|
first2 = corners[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 23:15:36 +08:00
|
|
|
// start with a corner that belongs to a quad with a single neighbor.
|
2010-05-12 01:44:00 +08:00
|
|
|
// if we do not have such, start with a corner of a quad with two neighbors.
|
|
|
|
if( !first )
|
|
|
|
first = first2;
|
|
|
|
|
|
|
|
if( !first || hist[0] != 0 || hist[1] != 0 || hist[2] != 4 ||
|
|
|
|
hist[3] != (pattern_size.width + pattern_size.height)*2 - 8 )
|
|
|
|
goto finalize;
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardCorner* cur = first;
|
|
|
|
ChessBoardCorner* right = NULL;
|
|
|
|
ChessBoardCorner* below = NULL;
|
|
|
|
out_corners.push_back(cur);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int k = 0; k < 4; ++k)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardCorner* c = cur->neighbors[k];
|
|
|
|
if (c)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if (!right)
|
2010-05-12 01:44:00 +08:00
|
|
|
right = c;
|
2018-05-23 22:44:12 +08:00
|
|
|
else if (!below)
|
2010-05-12 01:44:00 +08:00
|
|
|
below = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !right || (right->count != 2 && right->count != 3) ||
|
|
|
|
!below || (below->count != 2 && below->count != 3) )
|
|
|
|
goto finalize;
|
|
|
|
|
|
|
|
cur->row = 0;
|
|
|
|
//cvCircle( debug_img, cvPointFrom32f(cur->pt), 3, cvScalar(0,255,0), -1, 8, 0 );
|
|
|
|
|
|
|
|
first = below; // remember the first corner in the next row
|
2018-05-23 22:44:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
// find and store the first row (or column)
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int j = 1; ; ++j)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
right->row = 0;
|
2018-05-23 22:44:12 +08:00
|
|
|
out_corners.push_back(right);
|
2010-05-12 01:44:00 +08:00
|
|
|
//cvCircle( debug_img, cvPointFrom32f(right->pt), 3, cvScalar(0,255-j*10,0), -1, 8, 0 );
|
|
|
|
if( right->count == 2 )
|
|
|
|
break;
|
2018-05-23 22:44:12 +08:00
|
|
|
if( right->count != 3 || (int)out_corners.size() >= std::max(pattern_size.width,pattern_size.height) )
|
2010-05-12 01:44:00 +08:00
|
|
|
goto finalize;
|
|
|
|
cur = right;
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int k = 0; k < 4; ++k)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardCorner* c = cur->neighbors[k];
|
|
|
|
if (c && c->row > 0)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
int kk = 0;
|
|
|
|
for (; kk < 4; ++kk)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if (c->neighbors[kk] == below)
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
if (kk < 4)
|
2010-05-12 01:44:00 +08:00
|
|
|
below = c;
|
|
|
|
else
|
|
|
|
right = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
width = (int)out_corners.size();
|
|
|
|
if (width == pattern_size.width)
|
2010-05-12 01:44:00 +08:00
|
|
|
height = pattern_size.height;
|
2018-05-23 22:44:12 +08:00
|
|
|
else if (width == pattern_size.height)
|
2010-05-12 01:44:00 +08:00
|
|
|
height = pattern_size.width;
|
|
|
|
else
|
|
|
|
goto finalize;
|
|
|
|
|
|
|
|
// find and store all the other rows
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int i = 1; ; ++i)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
if( !first )
|
|
|
|
break;
|
|
|
|
cur = first;
|
|
|
|
first = 0;
|
2018-05-23 22:44:12 +08:00
|
|
|
int j = 0;
|
|
|
|
for (; ; ++j)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
cur->row = i;
|
2018-05-23 22:44:12 +08:00
|
|
|
out_corners.push_back(cur);
|
2010-05-12 01:44:00 +08:00
|
|
|
//cvCircle( debug_img, cvPointFrom32f(cur->pt), 3, cvScalar(0,0,255-j*10), -1, 8, 0 );
|
2018-05-23 22:44:12 +08:00
|
|
|
if (cur->count == 2 + (i < height-1) && j > 0)
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
right = 0;
|
|
|
|
|
|
|
|
// find a neighbor that has not been processed yet
|
|
|
|
// and that has a neighbor from the previous row
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int k = 0; k < 4; ++k)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardCorner* c = cur->neighbors[k];
|
|
|
|
if (c && c->row > i)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
int kk = 0;
|
|
|
|
for (; kk < 4; ++kk)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if (c->neighbors[kk] && c->neighbors[kk]->row == i-1)
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
if(kk < 4)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
right = c;
|
2018-05-23 22:44:12 +08:00
|
|
|
if (j > 0)
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
else if (j == 0)
|
2010-05-12 01:44:00 +08:00
|
|
|
first = c;
|
|
|
|
}
|
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
if (!right)
|
2010-05-12 01:44:00 +08:00
|
|
|
goto finalize;
|
|
|
|
cur = right;
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
if (j != width - 1)
|
2010-05-12 01:44:00 +08:00
|
|
|
goto finalize;
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
if ((int)out_corners.size() != corner_count)
|
2010-05-12 01:44:00 +08:00
|
|
|
goto finalize;
|
|
|
|
|
|
|
|
// check if we need to transpose the board
|
2018-05-23 22:44:12 +08:00
|
|
|
if (width != pattern_size.width)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
std::swap(width, height);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
std::vector<ChessBoardCorner*> tmp(out_corners);
|
|
|
|
for (int i = 0; i < height; ++i)
|
|
|
|
for (int j = 0; j < width; ++j)
|
|
|
|
out_corners[i*width + j] = tmp[j*height + i];
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if we need to revert the order in each row
|
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
cv::Point2f p0 = out_corners[0]->pt,
|
|
|
|
p1 = out_corners[pattern_size.width-1]->pt,
|
|
|
|
p2 = out_corners[pattern_size.width]->pt;
|
2010-05-12 01:44:00 +08:00
|
|
|
if( (p1.x - p0.x)*(p2.y - p1.y) - (p1.y - p0.y)*(p2.x - p1.x) < 0 )
|
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if (width % 2 == 0)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int i = 0; i < height; ++i)
|
|
|
|
for (int j = 0; j < width/2; ++j)
|
|
|
|
std::swap(out_corners[i*width+j], out_corners[i*width+width-j-1]);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int j = 0; j < width; ++j)
|
|
|
|
for (int i = 0; i < height/2; ++i)
|
|
|
|
std::swap(out_corners[i*width+j], out_corners[(height - i - 1)*width+j]);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result = corner_count;
|
2018-05-23 22:44:12 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
finalize:
|
2018-05-23 22:44:12 +08:00
|
|
|
if (result <= 0)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
corner_count = std::min(corner_count, pattern_size.area());
|
|
|
|
out_corners.resize(corner_count);
|
|
|
|
for (int i = 0; i < corner_count; i++)
|
2010-05-12 01:44:00 +08:00
|
|
|
out_corners[i] = corners[i];
|
2018-05-23 22:44:12 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
result = -corner_count;
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
if (result == -pattern_size.area())
|
2010-05-12 01:44:00 +08:00
|
|
|
result = -result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
void ChessBoardDetector::findQuadNeighbors()
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
const float thresh_scale = 1.f;
|
|
|
|
// find quad neighbors
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int idx = 0; idx < all_quads_count; idx++)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardQuad& cur_quad = (ChessBoardQuad&)all_quads[idx];
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// choose the points of the current quadrangle that are close to
|
|
|
|
// some points of the other quadrangles
|
|
|
|
// (it can happen for split corners (due to dilation) of the
|
|
|
|
// checker board). Search only in other quadrangles!
|
|
|
|
|
|
|
|
// for each corner of this quadrangle
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int i = 0; i < 4; i++)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if (cur_quad.neighbors[i])
|
|
|
|
continue;
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
float min_dist = FLT_MAX;
|
|
|
|
int closest_corner_idx = -1;
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardQuad *closest_quad = 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
cv::Point2f pt = cur_quad.corners[i]->pt;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// find the closest corner in all other quadrangles
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int k = 0; k < all_quads_count; k++)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if (k == idx)
|
2010-05-12 01:44:00 +08:00
|
|
|
continue;
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardQuad& q_k = all_quads[k];
|
|
|
|
|
|
|
|
for (int j = 0; j < 4; j++)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if (q_k.neighbors[j])
|
2010-05-12 01:44:00 +08:00
|
|
|
continue;
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
float dist = normL2Sqr<float>(pt - q_k.corners[j]->pt);
|
|
|
|
if (dist < min_dist &&
|
|
|
|
dist <= cur_quad.edge_len*thresh_scale &&
|
|
|
|
dist <= q_k.edge_len*thresh_scale )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
// check edge lengths, make sure they're compatible
|
|
|
|
// edges that are different by more than 1:4 are rejected
|
2018-05-23 22:44:12 +08:00
|
|
|
float ediff = cur_quad.edge_len - q_k.edge_len;
|
|
|
|
if (ediff > 32*cur_quad.edge_len ||
|
|
|
|
ediff > 32*q_k.edge_len)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
DPRINTF("Incompatible edge lengths");
|
2010-05-12 01:44:00 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
closest_corner_idx = j;
|
2018-05-23 22:44:12 +08:00
|
|
|
closest_quad = &q_k;
|
2010-05-12 01:44:00 +08:00
|
|
|
min_dist = dist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we found a matching corner point?
|
2018-05-23 22:44:12 +08:00
|
|
|
if (closest_corner_idx >= 0 && min_dist < FLT_MAX)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
CV_Assert(closest_quad);
|
|
|
|
|
|
|
|
if (cur_quad.count >= 4 || closest_quad->count >= 4)
|
|
|
|
continue;
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
// If another point from our current quad is closer to the found corner
|
|
|
|
// than the current one, then we don't count this one after all.
|
|
|
|
// This is necessary to support small squares where otherwise the wrong
|
|
|
|
// corner will get matched to closest_quad;
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardCorner& closest_corner = *closest_quad->corners[closest_corner_idx];
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
int j = 0;
|
|
|
|
for (; j < 4; j++)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if (cur_quad.neighbors[j] == closest_quad)
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
if (normL2Sqr<float>(closest_corner.pt - cur_quad.corners[j]->pt) < min_dist)
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
if (j < 4)
|
2010-05-12 01:44:00 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check that each corner is a neighbor of different quads
|
2018-05-23 22:44:12 +08:00
|
|
|
for(j = 0; j < closest_quad->count; j++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if (closest_quad->neighbors[j] == &cur_quad)
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
if (j < closest_quad->count)
|
2010-05-12 01:44:00 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// check whether the closest corner to closest_corner
|
|
|
|
// is different from cur_quad->corners[i]->pt
|
2018-05-23 22:44:12 +08:00
|
|
|
for (j = 0; j < all_quads_count; j++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
ChessBoardQuad* q = &const_cast<ChessBoardQuad&>(all_quads[j]);
|
|
|
|
if (j == idx || q == closest_quad)
|
2010-05-12 01:44:00 +08:00
|
|
|
continue;
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
int k = 0;
|
|
|
|
for (; k < 4; k++ )
|
|
|
|
{
|
|
|
|
if (!q->neighbors[k])
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
if (normL2Sqr<float>(closest_corner.pt - q->corners[k]->pt) < min_dist)
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
}
|
|
|
|
if (k < 4)
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
if (j < all_quads_count)
|
2010-05-12 01:44:00 +08:00
|
|
|
continue;
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
closest_corner.pt = (pt + closest_corner.pt) * 0.5f;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// We've found one more corner - remember it
|
2018-05-23 22:44:12 +08:00
|
|
|
cur_quad.count++;
|
|
|
|
cur_quad.neighbors[i] = closest_quad;
|
|
|
|
cur_quad.corners[i] = &closest_corner;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
closest_quad->count++;
|
2018-05-23 22:44:12 +08:00
|
|
|
closest_quad->neighbors[closest_corner_idx] = &cur_quad;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// returns corners in clockwise order
|
|
|
|
// corners don't necessarily start at same position on quad (e.g.,
|
|
|
|
// top left corner)
|
2018-05-23 22:44:12 +08:00
|
|
|
void ChessBoardDetector::generateQuads(const cv::Mat& image_, int flags)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
binarized_image = image_; // save for debug purposes
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
int quad_count = 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
all_quads.deallocate();
|
|
|
|
all_corners.deallocate();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-28 17:19:06 +08:00
|
|
|
// empiric bound for minimal allowed perimeter for squares
|
|
|
|
int min_size = 25; //cvRound( image->cols * image->rows * .03 * 0.01 * 0.92 );
|
|
|
|
|
|
|
|
bool filterQuads = (flags & CALIB_CB_FILTER_QUADS) != 0;
|
|
|
|
#ifdef USE_CV_FINDCONTOURS // use cv::findContours
|
|
|
|
|
|
|
|
std::vector<std::vector<Point> > contours;
|
|
|
|
std::vector<Vec4i> hierarchy;
|
|
|
|
|
|
|
|
cv::findContours(image_, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE);
|
|
|
|
|
|
|
|
if (contours.empty())
|
|
|
|
{
|
|
|
|
CV_LOG_DEBUG(NULL, "calib3d(chessboard): cv::findContours() returns no contours");
|
2018-05-23 22:44:12 +08:00
|
|
|
return;
|
2018-05-28 17:19:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<int> contour_child_counter(contours.size(), 0);
|
|
|
|
int boardIdx = -1;
|
|
|
|
|
|
|
|
std::vector<QuadCountour> contour_quads;
|
|
|
|
|
|
|
|
for (int idx = (int)(contours.size() - 1); idx >= 0; --idx)
|
|
|
|
{
|
|
|
|
int parentIdx = hierarchy[idx][3];
|
|
|
|
if (hierarchy[idx][2] != -1 || parentIdx == -1) // holes only (no child contours and with parent)
|
|
|
|
continue;
|
|
|
|
const std::vector<Point>& contour = contours[idx];
|
|
|
|
|
|
|
|
Rect contour_rect = boundingRect(contour);
|
|
|
|
if (contour_rect.area() < min_size)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::vector<Point> approx_contour;
|
|
|
|
|
|
|
|
const int min_approx_level = 1, max_approx_level = MAX_CONTOUR_APPROX;
|
|
|
|
for (int approx_level = min_approx_level; approx_level <= max_approx_level; approx_level++ )
|
|
|
|
{
|
|
|
|
approxPolyDP(contour, approx_contour, (float)approx_level, true);
|
|
|
|
if (approx_contour.size() == 4)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// we call this again on its own output, because sometimes
|
|
|
|
// approxPoly() does not simplify as much as it should.
|
|
|
|
std::vector<Point> approx_contour_tmp;
|
|
|
|
std::swap(approx_contour, approx_contour_tmp);
|
|
|
|
approxPolyDP(approx_contour_tmp, approx_contour, (float)approx_level, true);
|
|
|
|
if (approx_contour.size() == 4)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// reject non-quadrangles
|
|
|
|
if (approx_contour.size() != 4)
|
|
|
|
continue;
|
|
|
|
if (!cv::isContourConvex(approx_contour))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
cv::Point pt[4];
|
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
pt[i] = approx_contour[i];
|
|
|
|
CV_LOG_VERBOSE(NULL, 9, "... contours(" << contour_quads.size() << " added):" << pt[0] << " " << pt[1] << " " << pt[2] << " " << pt[3]);
|
|
|
|
|
|
|
|
if (filterQuads)
|
|
|
|
{
|
|
|
|
double p = cv::arcLength(approx_contour, true);
|
|
|
|
double area = cv::contourArea(approx_contour, false);
|
|
|
|
|
|
|
|
double d1 = sqrt(normL2Sqr<double>(pt[0] - pt[2]));
|
|
|
|
double d2 = sqrt(normL2Sqr<double>(pt[1] - pt[3]));
|
|
|
|
|
|
|
|
// philipg. Only accept those quadrangles which are more square
|
|
|
|
// than rectangular and which are big enough
|
|
|
|
double d3 = sqrt(normL2Sqr<double>(pt[0] - pt[1]));
|
|
|
|
double d4 = sqrt(normL2Sqr<double>(pt[1] - pt[2]));
|
|
|
|
if (!(d3*4 > d4 && d4*4 > d3 && d3*d4 < area*1.5 && area > min_size &&
|
|
|
|
d1 >= 0.15 * p && d2 >= 0.15 * p))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
contour_child_counter[parentIdx]++;
|
|
|
|
if (boardIdx != parentIdx && (boardIdx < 0 || contour_child_counter[boardIdx] < contour_child_counter[parentIdx]))
|
|
|
|
boardIdx = parentIdx;
|
|
|
|
|
|
|
|
contour_quads.push_back(QuadCountour(pt, parentIdx));
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t total = contour_quads.size();
|
2018-05-23 22:44:12 +08:00
|
|
|
size_t max_quad_buf_size = std::max((size_t)2, total * 3);
|
|
|
|
all_quads.allocate(max_quad_buf_size);
|
|
|
|
all_corners.allocate(max_quad_buf_size * 4);
|
2018-05-28 17:19:06 +08:00
|
|
|
|
|
|
|
// Create array of quads structures
|
2018-05-23 22:44:12 +08:00
|
|
|
for (size_t idx = 0; idx < total; ++idx)
|
2018-05-28 17:19:06 +08:00
|
|
|
{
|
|
|
|
QuadCountour& qc = contour_quads[idx];
|
|
|
|
if (filterQuads && qc.parent_contour != boardIdx)
|
|
|
|
continue;
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
int quad_idx = quad_count++;
|
|
|
|
ChessBoardQuad& q = all_quads[quad_idx];
|
|
|
|
|
2018-05-28 17:19:06 +08:00
|
|
|
// reset group ID
|
2018-05-23 22:44:12 +08:00
|
|
|
q = ChessBoardQuad();
|
2018-05-28 17:19:06 +08:00
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
Point2f pt(qc.pt[i]);
|
|
|
|
ChessBoardCorner& corner = all_corners[quad_idx * 4 + i];
|
2018-05-28 17:19:06 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
corner = ChessBoardCorner(pt);
|
|
|
|
q.corners[i] = &corner;
|
2018-05-28 17:19:06 +08:00
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
q.edge_len = FLT_MAX;
|
2018-05-28 17:19:06 +08:00
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
float d = normL2Sqr<float>(q.corners[i]->pt - q.corners[(i+1)&3]->pt);
|
|
|
|
q.edge_len = std::min(q.edge_len, d);
|
2018-05-28 17:19:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else // use legacy API: cvStartFindContours / cvFindNextContour / cvEndFindContours
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
CvMat image_old = cvMat(image_), *image = &image_old;
|
2018-05-28 17:19:06 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
CvContourEx* board = 0;
|
|
|
|
|
|
|
|
// create temporary storage for contours and the sequence of pointers to found quadrangles
|
2018-05-23 22:44:12 +08:00
|
|
|
cv::Ptr<CvMemStorage> temp_storage(cvCreateMemStorage(0));
|
|
|
|
CvSeq *root = cvCreateSeq(0, sizeof(CvSeq), sizeof(CvSeq*), temp_storage);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// initialize contour retrieving routine
|
2018-05-23 22:44:12 +08:00
|
|
|
CvContourScanner scanner = cvStartFindContours(image, temp_storage, sizeof(CvContourEx),
|
|
|
|
CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// get all the contours one by one
|
2018-05-23 22:44:12 +08:00
|
|
|
CvSeq* src_contour = NULL;
|
|
|
|
while ((src_contour = cvFindNextContour(scanner)) != NULL)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
CvSeq *dst_contour = 0;
|
|
|
|
CvRect rect = ((CvContour*)src_contour)->rect;
|
|
|
|
|
|
|
|
// reject contours with too small perimeter
|
|
|
|
if( CV_IS_SEQ_HOLE(src_contour) && rect.width*rect.height >= min_size )
|
|
|
|
{
|
2010-10-01 04:31:09 +08:00
|
|
|
const int min_approx_level = 1, max_approx_level = MAX_CONTOUR_APPROX;
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int approx_level = min_approx_level; approx_level <= max_approx_level; approx_level++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
dst_contour = cvApproxPoly( src_contour, sizeof(CvContour), temp_storage,
|
|
|
|
CV_POLY_APPROX_DP, (float)approx_level );
|
2010-10-01 04:31:09 +08:00
|
|
|
if( dst_contour->total == 4 )
|
|
|
|
break;
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
// we call this again on its own output, because sometimes
|
|
|
|
// cvApproxPoly() does not simplify as much as it should.
|
|
|
|
dst_contour = cvApproxPoly( dst_contour, sizeof(CvContour), temp_storage,
|
|
|
|
CV_POLY_APPROX_DP, (float)approx_level );
|
|
|
|
|
|
|
|
if( dst_contour->total == 4 )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// reject non-quadrangles
|
|
|
|
if( dst_contour->total == 4 && cvCheckContourConvexity(dst_contour) )
|
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
cv::Point2i pt[4];
|
|
|
|
double p = cvContourPerimeter(dst_contour);
|
2010-05-12 01:44:00 +08:00
|
|
|
double area = fabs(cvContourArea(dst_contour, CV_WHOLE_SEQ));
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int i = 0; i < 4; ++i)
|
2010-05-12 01:44:00 +08:00
|
|
|
pt[i] = *(CvPoint*)cvGetSeqElem(dst_contour, i);
|
2018-05-23 22:44:12 +08:00
|
|
|
CV_LOG_VERBOSE(NULL, 9, "... contours(" << root->total << " added):" << pt[0] << " " << pt[1] << " " << pt[2] << " " << pt[3]);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
double d1 = sqrt(normL2Sqr<double>(pt[0] - pt[2]));
|
|
|
|
double d2 = sqrt(normL2Sqr<double>(pt[1] - pt[3]));
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// philipg. Only accept those quadrangles which are more square
|
|
|
|
// than rectangular and which are big enough
|
2018-05-23 22:44:12 +08:00
|
|
|
double d3 = sqrt(normL2Sqr<double>(pt[0] - pt[1]));
|
|
|
|
double d4 = sqrt(normL2Sqr<double>(pt[1] - pt[2]));
|
2018-05-28 17:19:06 +08:00
|
|
|
if (!filterQuads ||
|
2010-05-12 01:44:00 +08:00
|
|
|
(d3*4 > d4 && d4*4 > d3 && d3*d4 < area*1.5 && area > min_size &&
|
2018-05-28 17:19:06 +08:00
|
|
|
d1 >= 0.15 * p && d2 >= 0.15 * p))
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
CvContourEx* parent = (CvContourEx*)(src_contour->v_prev);
|
|
|
|
parent->counter++;
|
|
|
|
if( !board || board->counter < parent->counter )
|
|
|
|
board = parent;
|
|
|
|
dst_contour->v_prev = (CvSeq*)parent;
|
|
|
|
//for( i = 0; i < 4; i++ ) cvLine( debug_img, pt[i], pt[(i+1)&3], cvScalar(200,255,255), 1, CV_AA, 0 );
|
|
|
|
cvSeqPush( root, &dst_contour );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// finish contour retrieving
|
|
|
|
cvEndFindContours( &scanner );
|
|
|
|
|
|
|
|
// allocate quad & corner buffers
|
2018-05-28 17:19:06 +08:00
|
|
|
int total = root->total;
|
2018-05-23 22:44:12 +08:00
|
|
|
size_t max_quad_buf_size = std::max((size_t)2, (size_t)total * 3);
|
|
|
|
all_quads.allocate(max_quad_buf_size);
|
|
|
|
all_corners.allocate(max_quad_buf_size * 4);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// Create array of quads structures
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int idx = 0; idx < total; ++idx)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
/* CvSeq* */src_contour = *(CvSeq**)cvGetSeqElem(root, idx);
|
2018-05-28 17:19:06 +08:00
|
|
|
if (filterQuads && src_contour->v_prev != (CvSeq*)board)
|
2010-05-12 01:44:00 +08:00
|
|
|
continue;
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
int quad_idx = quad_count++;
|
|
|
|
ChessBoardQuad& q = all_quads[quad_idx];
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
// reset group ID
|
2018-05-23 22:44:12 +08:00
|
|
|
q = ChessBoardQuad();
|
|
|
|
CV_Assert(src_contour->total == 4);
|
|
|
|
for (int i = 0; i < 4; i++)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
Point* onePoint = (Point*)cvGetSeqElem(src_contour, i);
|
2017-07-01 23:43:48 +08:00
|
|
|
CV_Assert(onePoint != NULL);
|
2018-05-23 22:44:12 +08:00
|
|
|
Point2f pt(*onePoint);
|
|
|
|
ChessBoardCorner& corner = all_corners[quad_idx*4 + i];
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
corner = ChessBoardCorner(pt);
|
|
|
|
q.corners[i] = &corner;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
q.edge_len = FLT_MAX;
|
|
|
|
for (int i = 0; i < 4; ++i)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
float d = normL2Sqr<float>(q.corners[i]->pt - q.corners[(i+1)&3]->pt);
|
|
|
|
q.edge_len = std::min(q.edge_len, d);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
2018-05-28 17:19:06 +08:00
|
|
|
#endif
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
all_quads_count = quad_count;
|
|
|
|
|
2018-05-28 17:19:06 +08:00
|
|
|
CV_LOG_VERBOSE(NULL, 3, "Total quad contours: " << total);
|
2018-05-23 22:44:12 +08:00
|
|
|
CV_LOG_VERBOSE(NULL, 3, "max_quad_buf_size=" << max_quad_buf_size);
|
2018-05-28 17:19:06 +08:00
|
|
|
CV_LOG_VERBOSE(NULL, 3, "filtered quad_count=" << quad_count);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
bool ChessBoardDetector::processQuads(std::vector<cv::Point2f>& out_corners, int &prev_sqr_size)
|
2016-07-19 22:24:55 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
out_corners.resize(0);
|
|
|
|
if (all_quads_count <= 0)
|
2016-07-19 22:24:55 +08:00
|
|
|
return false;
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
size_t max_quad_buf_size = all_quads.size();
|
2016-07-19 22:24:55 +08:00
|
|
|
|
|
|
|
// Find quad's neighbors
|
2018-05-23 22:44:12 +08:00
|
|
|
findQuadNeighbors();
|
2016-07-19 22:24:55 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
// allocate extra for adding in orderFoundQuads
|
|
|
|
std::vector<ChessBoardQuad*> quad_group;
|
|
|
|
std::vector<ChessBoardCorner*> corner_group; corner_group.reserve(max_quad_buf_size * 4);
|
2016-07-19 22:24:55 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
for (int group_idx = 0; ; group_idx++)
|
2016-07-19 22:24:55 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
findConnectedQuads(quad_group, group_idx);
|
|
|
|
if (quad_group.empty())
|
2016-07-19 22:24:55 +08:00
|
|
|
break;
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
int count = (int)quad_group.size();
|
|
|
|
|
2016-07-19 22:24:55 +08:00
|
|
|
// order the quad corners globally
|
|
|
|
// maybe delete or add some
|
2018-05-23 22:44:12 +08:00
|
|
|
DPRINTF("Starting ordering of inner quads (%d)", count);
|
|
|
|
count = orderFoundConnectedQuads(quad_group);
|
|
|
|
DPRINTF("Finished ordering of inner quads (%d)", count);
|
2016-07-19 22:24:55 +08:00
|
|
|
|
|
|
|
if (count == 0)
|
|
|
|
continue; // haven't found inner quads
|
|
|
|
|
|
|
|
// If count is more than it should be, this will remove those quads
|
|
|
|
// which cause maximum deviation from a nice square pattern.
|
2018-05-23 22:44:12 +08:00
|
|
|
count = cleanFoundConnectedQuads(quad_group);
|
|
|
|
DPRINTF("Connected group: %d, count: %d", group_idx, count);
|
2016-07-19 22:24:55 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
count = checkQuadGroup(quad_group, corner_group);
|
|
|
|
DPRINTF("Connected group: %d, count: %d", group_idx, count);
|
2016-07-19 22:24:55 +08:00
|
|
|
|
|
|
|
int n = count > 0 ? pattern_size.width * pattern_size.height : -count;
|
2018-05-23 22:44:12 +08:00
|
|
|
n = std::min(n, pattern_size.width * pattern_size.height);
|
2016-07-19 22:24:55 +08:00
|
|
|
float sum_dist = 0;
|
|
|
|
int total = 0;
|
|
|
|
|
|
|
|
for(int i = 0; i < n; i++ )
|
|
|
|
{
|
|
|
|
int ni = 0;
|
2018-05-23 22:44:12 +08:00
|
|
|
float sum = corner_group[i]->sumDist(ni);
|
|
|
|
sum_dist += sum;
|
2016-07-19 22:24:55 +08:00
|
|
|
total += ni;
|
|
|
|
}
|
2018-05-23 22:44:12 +08:00
|
|
|
prev_sqr_size = cvRound(sum_dist/std::max(total, 1));
|
2016-07-19 22:24:55 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
if (count > 0 || (-count > (int)out_corners.size()))
|
2016-07-19 22:24:55 +08:00
|
|
|
{
|
|
|
|
// copy corners to output array
|
2018-05-23 22:44:12 +08:00
|
|
|
out_corners.reserve(n);
|
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
out_corners.push_back(corner_group[i]->pt);
|
2016-07-19 22:24:55 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
if (count == pattern_size.width*pattern_size.height
|
|
|
|
&& checkBoardMonotony(out_corners))
|
2016-07-19 22:24:55 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
return true;
|
2016-07-19 22:24:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
return false;
|
2016-07-19 22:24:55 +08:00
|
|
|
}
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
|
|
|
|
void drawChessboardCorners( InputOutputArray image, Size patternSize,
|
|
|
|
InputArray _corners,
|
|
|
|
bool patternWasFound )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
CV_INSTRUMENT_REGION();
|
|
|
|
|
|
|
|
int type = image.type();
|
|
|
|
int cn = CV_MAT_CN(type);
|
|
|
|
CV_CheckType(type, cn == 1 || cn == 3 || cn == 4,
|
|
|
|
"Number of channels must be 1, 3 or 4" );
|
|
|
|
|
|
|
|
int depth = CV_MAT_DEPTH(type);
|
|
|
|
CV_CheckType(type, depth == CV_8U || depth == CV_16U || depth == CV_32F,
|
|
|
|
"Only 8-bit, 16-bit or floating-point 32-bit images are supported");
|
|
|
|
|
|
|
|
if (_corners.empty())
|
|
|
|
return;
|
|
|
|
Mat corners = _corners.getMat();
|
|
|
|
const Point2f* corners_data = corners.ptr<Point2f>(0);
|
|
|
|
int nelems = corners.checkVector(2, CV_32F, true);
|
|
|
|
CV_Assert(nelems >= 0);
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
const int shift = 0;
|
|
|
|
const int radius = 4;
|
|
|
|
const int r = radius*(1 << shift);
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
double scale = 1;
|
|
|
|
switch (depth)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
case CV_8U:
|
|
|
|
scale = 1;
|
|
|
|
break;
|
|
|
|
case CV_16U:
|
|
|
|
scale = 256;
|
|
|
|
break;
|
|
|
|
case CV_32F:
|
|
|
|
scale = 1./255;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
int line_type = (type == CV_8UC1 || type == CV_8UC3) ? LINE_AA : LINE_8;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
if (!patternWasFound)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
Scalar color(0,0,255,0);
|
|
|
|
if (cn == 1)
|
|
|
|
color = Scalar::all(200);
|
|
|
|
color *= scale;
|
|
|
|
|
|
|
|
for (int i = 0; i < nelems; i++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
cv::Point2i pt(
|
|
|
|
cvRound(corners_data[i].x*(1 << shift)),
|
|
|
|
cvRound(corners_data[i].y*(1 << shift))
|
|
|
|
);
|
|
|
|
line(image, Point(pt.x - r, pt.y - r), Point( pt.x + r, pt.y + r), color, 1, line_type, shift);
|
|
|
|
line(image, Point(pt.x - r, pt.y + r), Point( pt.x + r, pt.y - r), color, 1, line_type, shift);
|
|
|
|
circle(image, pt, r+(1<<shift), color, 1, line_type, shift);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const int line_max = 7;
|
2018-05-23 22:44:12 +08:00
|
|
|
static const int line_colors[line_max][4] =
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
{0,0,255,0},
|
|
|
|
{0,128,255,0},
|
|
|
|
{0,200,200,0},
|
|
|
|
{0,255,0,0},
|
|
|
|
{200,200,0,0},
|
|
|
|
{255,0,0,0},
|
|
|
|
{255,0,255,0}
|
2010-05-12 01:44:00 +08:00
|
|
|
};
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
cv::Point2i prev_pt;
|
|
|
|
for (int y = 0, i = 0; y < patternSize.height; y++)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
const int* line_color = &line_colors[y % line_max][0];
|
|
|
|
Scalar color(line_color[0], line_color[1], line_color[2], line_color[3]);
|
|
|
|
if (cn == 1)
|
|
|
|
color = Scalar::all(200);
|
|
|
|
color *= scale;
|
|
|
|
|
|
|
|
for (int x = 0; x < patternSize.width; x++, i++)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
cv::Point2i pt(
|
|
|
|
cvRound(corners_data[i].x*(1 << shift)),
|
|
|
|
cvRound(corners_data[i].y*(1 << shift))
|
|
|
|
);
|
|
|
|
|
|
|
|
if (i != 0)
|
|
|
|
line(image, prev_pt, pt, color, 1, line_type, shift);
|
|
|
|
|
|
|
|
line(image, Point(pt.x - r, pt.y - r), Point( pt.x + r, pt.y + r), color, 1, line_type, shift);
|
|
|
|
line(image, Point(pt.x - r, pt.y + r), Point( pt.x + r, pt.y - r), color, 1, line_type, shift);
|
|
|
|
circle(image, pt, r+(1<<shift), color, 1, line_type, shift);
|
2010-05-12 01:44:00 +08:00
|
|
|
prev_pt = pt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
static int quiet_error(int /*status*/, const char* /*func_name*/,
|
|
|
|
const char* /*err_msg*/, const char* /*file_name*/,
|
|
|
|
int /*line*/, void* /*userdata*/)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-23 22:44:12 +08:00
|
|
|
return 0;
|
2010-11-28 07:16:50 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
bool findCirclesGrid(InputArray image, Size patternSize,
|
|
|
|
OutputArray centers, int flags,
|
|
|
|
const Ptr<FeatureDetector> &blobDetector,
|
|
|
|
CirclesGridFinderParameters parameters)
|
2017-09-08 22:35:23 +08:00
|
|
|
{
|
|
|
|
CirclesGridFinderParameters2 parameters2;
|
|
|
|
*((CirclesGridFinderParameters*)¶meters2) = parameters;
|
|
|
|
return cv::findCirclesGrid2(image, patternSize, centers, flags, blobDetector, parameters2);
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
bool findCirclesGrid2(InputArray _image, Size patternSize,
|
|
|
|
OutputArray _centers, int flags, const Ptr<FeatureDetector> &blobDetector,
|
|
|
|
CirclesGridFinderParameters2 parameters)
|
2010-12-21 17:24:36 +08:00
|
|
|
{
|
2016-08-18 14:53:00 +08:00
|
|
|
CV_INSTRUMENT_REGION()
|
|
|
|
|
2011-07-13 21:05:41 +08:00
|
|
|
bool isAsymmetricGrid = (flags & CALIB_CB_ASYMMETRIC_GRID) ? true : false;
|
|
|
|
bool isSymmetricGrid = (flags & CALIB_CB_SYMMETRIC_GRID ) ? true : false;
|
2011-05-06 17:40:18 +08:00
|
|
|
CV_Assert(isAsymmetricGrid ^ isSymmetricGrid);
|
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
Mat image = _image.getMat();
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<Point2f> centers;
|
2011-05-06 22:02:07 +08:00
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<KeyPoint> keypoints;
|
2011-05-06 22:02:07 +08:00
|
|
|
blobDetector->detect(image, keypoints);
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<Point2f> points;
|
2010-12-27 17:15:08 +08:00
|
|
|
for (size_t i = 0; i < keypoints.size(); i++)
|
|
|
|
{
|
2011-02-07 15:57:32 +08:00
|
|
|
points.push_back (keypoints[i].pt);
|
2010-12-27 17:15:08 +08:00
|
|
|
}
|
2010-12-21 17:24:36 +08:00
|
|
|
|
2017-09-08 22:35:23 +08:00
|
|
|
if(flags & CALIB_CB_ASYMMETRIC_GRID)
|
|
|
|
parameters.gridType = CirclesGridFinderParameters::ASYMMETRIC_GRID;
|
|
|
|
if(flags & CALIB_CB_SYMMETRIC_GRID)
|
|
|
|
parameters.gridType = CirclesGridFinderParameters::SYMMETRIC_GRID;
|
|
|
|
|
2011-05-06 17:40:18 +08:00
|
|
|
if(flags & CALIB_CB_CLUSTERING)
|
2011-04-15 23:33:11 +08:00
|
|
|
{
|
2017-09-08 22:35:23 +08:00
|
|
|
CirclesGridClusterFinder circlesGridClusterFinder(parameters);
|
2011-04-15 23:33:11 +08:00
|
|
|
circlesGridClusterFinder.findGrid(points, patternSize, centers);
|
2011-04-17 21:14:45 +08:00
|
|
|
Mat(centers).copyTo(_centers);
|
2011-04-15 23:33:11 +08:00
|
|
|
return !centers.empty();
|
|
|
|
}
|
|
|
|
|
2010-12-21 17:24:36 +08:00
|
|
|
const int attempts = 2;
|
2011-02-07 15:57:32 +08:00
|
|
|
const size_t minHomographyPoints = 4;
|
2010-12-21 17:24:36 +08:00
|
|
|
Mat H;
|
|
|
|
for (int i = 0; i < attempts; i++)
|
|
|
|
{
|
|
|
|
centers.clear();
|
2010-12-27 17:15:08 +08:00
|
|
|
CirclesGridFinder boxFinder(patternSize, points, parameters);
|
2010-12-21 17:24:36 +08:00
|
|
|
bool isFound = false;
|
2011-06-02 05:49:08 +08:00
|
|
|
#define BE_QUIET 1
|
|
|
|
#if BE_QUIET
|
2012-04-30 22:33:52 +08:00
|
|
|
void* oldCbkData;
|
2018-05-23 22:44:12 +08:00
|
|
|
ErrorCallback oldCbk = redirectError(quiet_error, 0, &oldCbkData); // FIXIT not thread safe
|
2011-06-02 05:49:08 +08:00
|
|
|
#endif
|
2017-11-28 17:53:40 +08:00
|
|
|
CV_TRY
|
2010-12-21 17:24:36 +08:00
|
|
|
{
|
|
|
|
isFound = boxFinder.findHoles();
|
|
|
|
}
|
2017-11-28 17:53:40 +08:00
|
|
|
CV_CATCH(Exception, e)
|
2010-12-21 17:24:36 +08:00
|
|
|
{
|
2017-11-28 17:53:40 +08:00
|
|
|
CV_UNUSED(e);
|
2011-06-02 05:49:08 +08:00
|
|
|
}
|
|
|
|
#if BE_QUIET
|
2012-04-30 22:33:52 +08:00
|
|
|
redirectError(oldCbk, oldCbkData);
|
2011-06-02 05:49:08 +08:00
|
|
|
#endif
|
2010-12-21 17:24:36 +08:00
|
|
|
if (isFound)
|
|
|
|
{
|
2012-06-09 23:00:04 +08:00
|
|
|
switch(parameters.gridType)
|
|
|
|
{
|
2011-02-07 15:57:32 +08:00
|
|
|
case CirclesGridFinderParameters::SYMMETRIC_GRID:
|
|
|
|
boxFinder.getHoles(centers);
|
|
|
|
break;
|
|
|
|
case CirclesGridFinderParameters::ASYMMETRIC_GRID:
|
2012-06-09 23:00:04 +08:00
|
|
|
boxFinder.getAsymmetricHoles(centers);
|
|
|
|
break;
|
2011-02-07 15:57:32 +08:00
|
|
|
default:
|
2018-05-23 22:44:12 +08:00
|
|
|
CV_Error(Error::StsBadArg, "Unknown pattern type");
|
2012-06-09 23:00:04 +08:00
|
|
|
}
|
2011-02-07 15:57:32 +08:00
|
|
|
|
2010-12-21 17:24:36 +08:00
|
|
|
if (i != 0)
|
|
|
|
{
|
|
|
|
Mat orgPointsMat;
|
2011-04-17 21:14:45 +08:00
|
|
|
transform(centers, orgPointsMat, H.inv());
|
|
|
|
convertPointsFromHomogeneous(orgPointsMat, centers);
|
2010-12-21 17:24:36 +08:00
|
|
|
}
|
2011-04-17 21:14:45 +08:00
|
|
|
Mat(centers).copyTo(_centers);
|
2010-12-21 17:24:36 +08:00
|
|
|
return true;
|
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2011-02-07 15:57:32 +08:00
|
|
|
boxFinder.getHoles(centers);
|
2010-12-21 17:24:36 +08:00
|
|
|
if (i != attempts - 1)
|
|
|
|
{
|
|
|
|
if (centers.size() < minHomographyPoints)
|
|
|
|
break;
|
2010-12-27 17:15:08 +08:00
|
|
|
H = CirclesGridFinder::rectifyGrid(boxFinder.getDetectedGridSize(), centers, points, points);
|
2010-12-21 17:24:36 +08:00
|
|
|
}
|
|
|
|
}
|
2011-04-17 21:14:45 +08:00
|
|
|
Mat(centers).copyTo(_centers);
|
2010-12-21 17:24:36 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
bool findCirclesGrid(InputArray _image, Size patternSize,
|
|
|
|
OutputArray _centers, int flags, const Ptr<FeatureDetector> &blobDetector)
|
2017-03-01 18:52:25 +08:00
|
|
|
{
|
2017-09-08 22:35:23 +08:00
|
|
|
return cv::findCirclesGrid2(_image, patternSize, _centers, flags, blobDetector, CirclesGridFinderParameters2());
|
2017-03-01 18:52:25 +08:00
|
|
|
}
|
|
|
|
|
2018-05-23 22:44:12 +08:00
|
|
|
} // namespace
|
2010-05-12 01:44:00 +08:00
|
|
|
/* End of file. */
|