Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/calib3d/src/circlesgrid.hpp
16354 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
26
//
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
29
//
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
40
//
41
//M*/
42
43
#ifndef CIRCLESGRID_HPP_
44
#define CIRCLESGRID_HPP_
45
46
#include <fstream>
47
#include <set>
48
#include <list>
49
#include <numeric>
50
#include <map>
51
52
class CirclesGridClusterFinder
53
{
54
CirclesGridClusterFinder& operator=(const CirclesGridClusterFinder&);
55
CirclesGridClusterFinder(const CirclesGridClusterFinder&);
56
public:
57
CirclesGridClusterFinder(const cv::CirclesGridFinderParameters &parameters)
58
{
59
isAsymmetricGrid = parameters.gridType == cv::CirclesGridFinderParameters::ASYMMETRIC_GRID;
60
squareSize = parameters.squareSize;
61
maxRectifiedDistance = parameters.maxRectifiedDistance;
62
}
63
void findGrid(const std::vector<cv::Point2f> &points, cv::Size patternSize, std::vector<cv::Point2f>& centers);
64
65
//cluster 2d points by geometric coordinates
66
void hierarchicalClustering(const std::vector<cv::Point2f> &points, const cv::Size &patternSize, std::vector<cv::Point2f> &patternPoints);
67
private:
68
void findCorners(const std::vector<cv::Point2f> &hull2f, std::vector<cv::Point2f> &corners);
69
void findOutsideCorners(const std::vector<cv::Point2f> &corners, std::vector<cv::Point2f> &outsideCorners);
70
void getSortedCorners(const std::vector<cv::Point2f> &hull2f, const std::vector<cv::Point2f> &corners, const std::vector<cv::Point2f> &outsideCorners, std::vector<cv::Point2f> &sortedCorners);
71
void rectifyPatternPoints(const std::vector<cv::Point2f> &patternPoints, const std::vector<cv::Point2f> &sortedCorners, std::vector<cv::Point2f> &rectifiedPatternPoints);
72
void parsePatternPoints(const std::vector<cv::Point2f> &patternPoints, const std::vector<cv::Point2f> &rectifiedPatternPoints, std::vector<cv::Point2f> &centers);
73
74
float squareSize, maxRectifiedDistance;
75
bool isAsymmetricGrid;
76
77
cv::Size patternSize;
78
};
79
80
class Graph
81
{
82
public:
83
typedef std::set<size_t> Neighbors;
84
struct Vertex
85
{
86
Neighbors neighbors;
87
};
88
typedef std::map<size_t, Vertex> Vertices;
89
90
Graph(size_t n);
91
void addVertex(size_t id);
92
void addEdge(size_t id1, size_t id2);
93
void removeEdge(size_t id1, size_t id2);
94
bool doesVertexExist(size_t id) const;
95
bool areVerticesAdjacent(size_t id1, size_t id2) const;
96
size_t getVerticesCount() const;
97
size_t getDegree(size_t id) const;
98
const Neighbors& getNeighbors(size_t id) const;
99
void floydWarshall(cv::Mat &distanceMatrix, int infinity = -1) const;
100
private:
101
Vertices vertices;
102
};
103
104
struct Path
105
{
106
int firstVertex;
107
int lastVertex;
108
int length;
109
110
std::vector<size_t> vertices;
111
112
Path(int first = -1, int last = -1, int len = -1)
113
{
114
firstVertex = first;
115
lastVertex = last;
116
length = len;
117
}
118
};
119
120
class CirclesGridFinder
121
{
122
public:
123
CirclesGridFinder(cv::Size patternSize, const std::vector<cv::Point2f> &testKeypoints,
124
const cv::CirclesGridFinderParameters &parameters = cv::CirclesGridFinderParameters());
125
bool findHoles();
126
static cv::Mat rectifyGrid(cv::Size detectedGridSize, const std::vector<cv::Point2f>& centers, const std::vector<
127
cv::Point2f> &keypoint, std::vector<cv::Point2f> &warpedKeypoints);
128
129
void getHoles(std::vector<cv::Point2f> &holes) const;
130
void getAsymmetricHoles(std::vector<cv::Point2f> &holes) const;
131
cv::Size getDetectedGridSize() const;
132
133
void drawBasis(const std::vector<cv::Point2f> &basis, cv::Point2f origin, cv::Mat &drawImg) const;
134
void drawBasisGraphs(const std::vector<Graph> &basisGraphs, cv::Mat &drawImg, bool drawEdges = true,
135
bool drawVertices = true) const;
136
void drawHoles(const cv::Mat &srcImage, cv::Mat &drawImage) const;
137
private:
138
void computeRNG(Graph &rng, std::vector<cv::Point2f> &vectors, cv::Mat *drawImage = 0) const;
139
void rng2gridGraph(Graph &rng, std::vector<cv::Point2f> &vectors) const;
140
void eraseUsedGraph(std::vector<Graph> &basisGraphs) const;
141
void filterOutliersByDensity(const std::vector<cv::Point2f> &samples, std::vector<cv::Point2f> &filteredSamples);
142
void findBasis(const std::vector<cv::Point2f> &samples, std::vector<cv::Point2f> &basis,
143
std::vector<Graph> &basisGraphs);
144
void findMCS(const std::vector<cv::Point2f> &basis, std::vector<Graph> &basisGraphs);
145
size_t findLongestPath(std::vector<Graph> &basisGraphs, Path &bestPath);
146
float computeGraphConfidence(const std::vector<Graph> &basisGraphs, bool addRow, const std::vector<size_t> &points,
147
const std::vector<size_t> &seeds);
148
void addHolesByGraph(const std::vector<Graph> &basisGraphs, bool addRow, cv::Point2f basisVec);
149
150
size_t findNearestKeypoint(cv::Point2f pt) const;
151
void addPoint(cv::Point2f pt, std::vector<size_t> &points);
152
void findCandidateLine(std::vector<size_t> &line, size_t seedLineIdx, bool addRow, cv::Point2f basisVec, std::vector<
153
size_t> &seeds);
154
void findCandidateHoles(std::vector<size_t> &above, std::vector<size_t> &below, bool addRow, cv::Point2f basisVec,
155
std::vector<size_t> &aboveSeeds, std::vector<size_t> &belowSeeds);
156
static bool areCentersNew(const std::vector<size_t> &newCenters, const std::vector<std::vector<size_t> > &holes);
157
bool isDetectionCorrect();
158
159
static void insertWinner(float aboveConfidence, float belowConfidence, float minConfidence, bool addRow,
160
const std::vector<size_t> &above, const std::vector<size_t> &below, std::vector<std::vector<
161
size_t> > &holes);
162
163
struct Segment
164
{
165
cv::Point2f s;
166
cv::Point2f e;
167
Segment(cv::Point2f _s, cv::Point2f _e);
168
};
169
170
//if endpoint is on a segment then function return false
171
static bool areSegmentsIntersecting(Segment seg1, Segment seg2);
172
static bool doesIntersectionExist(const std::vector<Segment> &corner, const std::vector<std::vector<Segment> > &segments);
173
void getCornerSegments(const std::vector<std::vector<size_t> > &points, std::vector<std::vector<Segment> > &segments,
174
std::vector<cv::Point> &cornerIndices, std::vector<cv::Point> &firstSteps,
175
std::vector<cv::Point> &secondSteps) const;
176
size_t getFirstCorner(std::vector<cv::Point> &largeCornerIndices, std::vector<cv::Point> &smallCornerIndices,
177
std::vector<cv::Point> &firstSteps, std::vector<cv::Point> &secondSteps) const;
178
static double getDirection(cv::Point2f p1, cv::Point2f p2, cv::Point2f p3);
179
180
std::vector<cv::Point2f> keypoints;
181
182
std::vector<std::vector<size_t> > holes;
183
std::vector<std::vector<size_t> > holes2;
184
std::vector<std::vector<size_t> > *largeHoles;
185
std::vector<std::vector<size_t> > *smallHoles;
186
187
const cv::Size_<size_t> patternSize;
188
cv::CirclesGridFinderParameters parameters;
189
190
CirclesGridFinder& operator=(const CirclesGridFinder&);
191
CirclesGridFinder(const CirclesGridFinder&);
192
};
193
194
#endif /* CIRCLESGRID_HPP_ */
195
196