Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
dde91811
Commit
dde91811
authored
May 06, 2011
by
Ilya Lysenkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added a fast algorithm for the symmetric circles grid detection
parent
58b7c344
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
68 additions
and
19 deletions
+68
-19
calibinit.cpp
modules/calib3d/src/calibinit.cpp
+6
-2
circlesgrid.cpp
modules/calib3d/src/circlesgrid.cpp
+55
-14
circlesgrid.hpp
modules/calib3d/src/circlesgrid.hpp
+7
-3
No files found.
modules/calib3d/src/calibinit.cpp
View file @
dde91811
...
@@ -1929,6 +1929,10 @@ void cv::drawChessboardCorners( InputOutputArray _image, Size patternSize,
...
@@ -1929,6 +1929,10 @@ void cv::drawChessboardCorners( InputOutputArray _image, Size patternSize,
bool
cv
::
findCirclesGrid
(
const
InputArray
&
_image
,
Size
patternSize
,
bool
cv
::
findCirclesGrid
(
const
InputArray
&
_image
,
Size
patternSize
,
OutputArray
_centers
,
int
flags
)
OutputArray
_centers
,
int
flags
)
{
{
bool
isAsymmetricGrid
=
(
flags
&
CALIB_CB_ASYMMETRIC_GRID
);
bool
isSymmetricGrid
=
(
flags
&
CALIB_CB_SYMMETRIC_GRID
);
CV_Assert
(
isAsymmetricGrid
^
isSymmetricGrid
);
Mat
image
=
_image
.
getMat
();
Mat
image
=
_image
.
getMat
();
vector
<
Point2f
>
centers
;
vector
<
Point2f
>
centers
;
SimpleBlobDetector
::
Params
params
;
SimpleBlobDetector
::
Params
params
;
...
@@ -1947,9 +1951,9 @@ bool cv::findCirclesGrid( const InputArray& _image, Size patternSize,
...
@@ -1947,9 +1951,9 @@ bool cv::findCirclesGrid( const InputArray& _image, Size patternSize,
points
.
push_back
(
keypoints
[
i
].
pt
);
points
.
push_back
(
keypoints
[
i
].
pt
);
}
}
if
(
(
flags
&
CALIB_CB_CLUSTERING
)
&&
(
flags
&
CALIB_CB_ASYMMETRIC_GRID
)
)
if
(
flags
&
CALIB_CB_CLUSTERING
)
{
{
CirclesGridClusterFinder
circlesGridClusterFinder
;
CirclesGridClusterFinder
circlesGridClusterFinder
(
isAsymmetricGrid
)
;
circlesGridClusterFinder
.
findGrid
(
points
,
patternSize
,
centers
);
circlesGridClusterFinder
.
findGrid
(
points
,
patternSize
,
centers
);
Mat
(
centers
).
copyTo
(
_centers
);
Mat
(
centers
).
copyTo
(
_centers
);
return
!
centers
.
empty
();
return
!
centers
.
empty
();
...
...
modules/calib3d/src/circlesgrid.cpp
View file @
dde91811
...
@@ -73,7 +73,7 @@ void CirclesGridClusterFinder::hierarchicalClustering(const vector<Point2f> poin
...
@@ -73,7 +73,7 @@ void CirclesGridClusterFinder::hierarchicalClustering(const vector<Point2f> poin
}
}
int
patternClusterIdx
=
0
;
int
patternClusterIdx
=
0
;
while
(
(
int
)
clusters
[
patternClusterIdx
].
size
()
<
patternSize
.
area
(
)
&&
countNonZero
(
distsMask
==
255
)
>
0
)
while
(
clusters
[
patternClusterIdx
].
size
()
<
static_cast
<
size_t
>
(
patternSize
.
area
()
)
&&
countNonZero
(
distsMask
==
255
)
>
0
)
{
{
Point
minLoc
;
Point
minLoc
;
minMaxLoc
(
dists
,
0
,
0
,
&
minLoc
,
0
,
distsMask
);
minMaxLoc
(
dists
,
0
,
0
,
&
minLoc
,
0
,
distsMask
);
...
@@ -93,7 +93,8 @@ void CirclesGridClusterFinder::hierarchicalClustering(const vector<Point2f> poin
...
@@ -93,7 +93,8 @@ void CirclesGridClusterFinder::hierarchicalClustering(const vector<Point2f> poin
}
}
patternPoints
.
clear
();
patternPoints
.
clear
();
if
((
int
)
clusters
[
patternClusterIdx
].
size
()
!=
patternSize
.
area
())
if
(
clusters
[
patternClusterIdx
].
size
()
!=
static_cast
<
size_t
>
(
patternSize
.
area
()))
{
{
return
;
return
;
}
}
...
@@ -104,8 +105,9 @@ void CirclesGridClusterFinder::hierarchicalClustering(const vector<Point2f> poin
...
@@ -104,8 +105,9 @@ void CirclesGridClusterFinder::hierarchicalClustering(const vector<Point2f> poin
}
}
}
}
void
CirclesGridClusterFinder
::
findGrid
(
const
std
::
vector
<
cv
::
Point2f
>
points
,
cv
::
Size
patternSize
,
vector
<
Point2f
>&
centers
)
void
CirclesGridClusterFinder
::
findGrid
(
const
std
::
vector
<
cv
::
Point2f
>
points
,
cv
::
Size
_
patternSize
,
vector
<
Point2f
>&
centers
)
{
{
patternSize
=
_patternSize
;
centers
.
clear
();
centers
.
clear
();
if
(
points
.
empty
())
if
(
points
.
empty
())
{
{
...
@@ -121,7 +123,7 @@ void CirclesGridClusterFinder::findGrid(const std::vector<cv::Point2f> points, c
...
@@ -121,7 +123,7 @@ void CirclesGridClusterFinder::findGrid(const std::vector<cv::Point2f> points, c
vector
<
Point2f
>
hull2f
;
vector
<
Point2f
>
hull2f
;
convexHull
(
Mat
(
patternPoints
),
hull2f
,
false
);
convexHull
(
Mat
(
patternPoints
),
hull2f
,
false
);
const
size_t
cornersCount
=
6
;
const
size_t
cornersCount
=
isAsymmetricGrid
?
6
:
4
;
if
(
hull2f
.
size
()
<
cornersCount
)
if
(
hull2f
.
size
()
<
cornersCount
)
return
;
return
;
...
@@ -130,23 +132,24 @@ void CirclesGridClusterFinder::findGrid(const std::vector<cv::Point2f> points, c
...
@@ -130,23 +132,24 @@ void CirclesGridClusterFinder::findGrid(const std::vector<cv::Point2f> points, c
if
(
corners
.
size
()
!=
cornersCount
)
if
(
corners
.
size
()
!=
cornersCount
)
return
;
return
;
vector
<
Point2f
>
outsideCorners
;
vector
<
Point2f
>
outsideCorners
,
sortedCorners
;
if
(
isAsymmetricGrid
)
{
findOutsideCorners
(
corners
,
outsideCorners
);
findOutsideCorners
(
corners
,
outsideCorners
);
const
size_t
outsideCornersCount
=
2
;
const
size_t
outsideCornersCount
=
2
;
if
(
outsideCorners
.
size
()
!=
outsideCornersCount
)
if
(
outsideCorners
.
size
()
!=
outsideCornersCount
)
return
;
return
;
}
vector
<
Point2f
>
sortedCorners
;
getSortedCorners
(
hull2f
,
corners
,
outsideCorners
,
sortedCorners
);
getSortedCorners
(
hull2f
,
corners
,
outsideCorners
,
sortedCorners
);
if
(
sortedCorners
.
size
()
!=
cornersCount
)
if
(
sortedCorners
.
size
()
!=
cornersCount
)
return
;
return
;
vector
<
Point2f
>
rectifiedPatternPoints
;
vector
<
Point2f
>
rectifiedPatternPoints
;
rectifyPatternPoints
(
pattern
Size
,
pattern
Points
,
sortedCorners
,
rectifiedPatternPoints
);
rectifyPatternPoints
(
patternPoints
,
sortedCorners
,
rectifiedPatternPoints
);
if
(
patternPoints
.
size
()
!=
rectifiedPatternPoints
.
size
())
if
(
patternPoints
.
size
()
!=
rectifiedPatternPoints
.
size
())
return
;
return
;
parsePatternPoints
(
pattern
Size
,
pattern
Points
,
rectifiedPatternPoints
,
centers
);
parsePatternPoints
(
patternPoints
,
rectifiedPatternPoints
,
centers
);
}
}
void
CirclesGridClusterFinder
::
findCorners
(
const
std
::
vector
<
cv
::
Point2f
>
&
hull2f
,
std
::
vector
<
cv
::
Point2f
>
&
corners
)
void
CirclesGridClusterFinder
::
findCorners
(
const
std
::
vector
<
cv
::
Point2f
>
&
hull2f
,
std
::
vector
<
cv
::
Point2f
>
&
corners
)
...
@@ -167,7 +170,7 @@ void CirclesGridClusterFinder::findCorners(const std::vector<cv::Point2f> &hull2
...
@@ -167,7 +170,7 @@ void CirclesGridClusterFinder::findCorners(const std::vector<cv::Point2f> &hull2
Mat
sortedIndices
;
Mat
sortedIndices
;
sortIdx
(
anglesMat
,
sortedIndices
,
CV_SORT_EVERY_COLUMN
+
CV_SORT_DESCENDING
);
sortIdx
(
anglesMat
,
sortedIndices
,
CV_SORT_EVERY_COLUMN
+
CV_SORT_DESCENDING
);
CV_Assert
(
sortedIndices
.
type
()
==
CV_32SC1
);
CV_Assert
(
sortedIndices
.
type
()
==
CV_32SC1
);
const
int
cornersCount
=
6
;
const
int
cornersCount
=
isAsymmetricGrid
?
6
:
4
;
corners
.
clear
();
corners
.
clear
();
for
(
int
i
=
0
;
i
<
cornersCount
;
i
++
)
for
(
int
i
=
0
;
i
<
cornersCount
;
i
++
)
{
{
...
@@ -224,6 +227,9 @@ void CirclesGridClusterFinder::findOutsideCorners(const std::vector<cv::Point2f>
...
@@ -224,6 +227,9 @@ void CirclesGridClusterFinder::findOutsideCorners(const std::vector<cv::Point2f>
void
CirclesGridClusterFinder
::
getSortedCorners
(
const
std
::
vector
<
cv
::
Point2f
>
&
hull2f
,
const
std
::
vector
<
cv
::
Point2f
>
&
corners
,
const
std
::
vector
<
cv
::
Point2f
>
&
outsideCorners
,
std
::
vector
<
cv
::
Point2f
>
&
sortedCorners
)
void
CirclesGridClusterFinder
::
getSortedCorners
(
const
std
::
vector
<
cv
::
Point2f
>
&
hull2f
,
const
std
::
vector
<
cv
::
Point2f
>
&
corners
,
const
std
::
vector
<
cv
::
Point2f
>
&
outsideCorners
,
std
::
vector
<
cv
::
Point2f
>
&
sortedCorners
)
{
{
Point2f
firstCorner
;
if
(
isAsymmetricGrid
)
{
Point2f
center
=
std
::
accumulate
(
corners
.
begin
(),
corners
.
end
(),
Point2f
(
0.0
f
,
0.0
f
));
Point2f
center
=
std
::
accumulate
(
corners
.
begin
(),
corners
.
end
(),
Point2f
(
0.0
f
,
0.0
f
));
center
*=
1.0
/
corners
.
size
();
center
*=
1.0
/
corners
.
size
();
...
@@ -237,7 +243,12 @@ void CirclesGridClusterFinder::getSortedCorners(const std::vector<cv::Point2f> &
...
@@ -237,7 +243,12 @@ void CirclesGridClusterFinder::getSortedCorners(const std::vector<cv::Point2f> &
float
crossProduct
=
centerToCorners
[
0
].
x
*
centerToCorners
[
1
].
y
-
centerToCorners
[
0
].
y
*
centerToCorners
[
1
].
x
;
float
crossProduct
=
centerToCorners
[
0
].
x
*
centerToCorners
[
1
].
y
-
centerToCorners
[
0
].
y
*
centerToCorners
[
1
].
x
;
//y axis is inverted in computer vision so we check > 0
//y axis is inverted in computer vision so we check > 0
bool
isClockwise
=
crossProduct
>
0
;
bool
isClockwise
=
crossProduct
>
0
;
Point2f
firstCorner
=
isClockwise
?
outsideCorners
[
1
]
:
outsideCorners
[
0
];
firstCorner
=
isClockwise
?
outsideCorners
[
1
]
:
outsideCorners
[
0
];
}
else
{
firstCorner
=
corners
[
0
];
}
std
::
vector
<
Point2f
>::
const_iterator
firstCornerIterator
=
std
::
find
(
hull2f
.
begin
(),
hull2f
.
end
(),
firstCorner
);
std
::
vector
<
Point2f
>::
const_iterator
firstCornerIterator
=
std
::
find
(
hull2f
.
begin
(),
hull2f
.
end
(),
firstCorner
);
sortedCorners
.
clear
();
sortedCorners
.
clear
();
...
@@ -257,16 +268,34 @@ void CirclesGridClusterFinder::getSortedCorners(const std::vector<cv::Point2f> &
...
@@ -257,16 +268,34 @@ void CirclesGridClusterFinder::getSortedCorners(const std::vector<cv::Point2f> &
sortedCorners
.
push_back
(
*
it
);
sortedCorners
.
push_back
(
*
it
);
}
}
}
}
if
(
!
isAsymmetricGrid
)
{
double
dist1
=
norm
(
sortedCorners
[
0
]
-
sortedCorners
[
1
]);
double
dist2
=
norm
(
sortedCorners
[
1
]
-
sortedCorners
[
2
]);
if
((
dist1
>
dist2
&&
patternSize
.
height
>
patternSize
.
width
)
||
(
dist1
<
dist2
&&
patternSize
.
height
<
patternSize
.
width
))
{
for
(
size_t
i
=
0
;
i
<
sortedCorners
.
size
()
-
1
;
i
++
)
{
sortedCorners
[
i
]
=
sortedCorners
[
i
+
1
];
}
sortedCorners
[
sortedCorners
.
size
()
-
1
]
=
firstCorner
;
}
}
}
}
void
CirclesGridClusterFinder
::
rectifyPatternPoints
(
const
cv
::
Size
&
patternSize
,
const
std
::
vector
<
cv
::
Point2f
>
&
patternPoints
,
const
std
::
vector
<
cv
::
Point2f
>
&
sortedCorners
,
std
::
vector
<
cv
::
Point2f
>
&
rectifiedPatternPoints
)
void
CirclesGridClusterFinder
::
rectifyPatternPoints
(
const
std
::
vector
<
cv
::
Point2f
>
&
patternPoints
,
const
std
::
vector
<
cv
::
Point2f
>
&
sortedCorners
,
std
::
vector
<
cv
::
Point2f
>
&
rectifiedPatternPoints
)
{
{
//indices of corner points in pattern
//indices of corner points in pattern
vector
<
Point
>
trueIndices
;
vector
<
Point
>
trueIndices
;
trueIndices
.
push_back
(
Point
(
0
,
0
));
trueIndices
.
push_back
(
Point
(
0
,
0
));
trueIndices
.
push_back
(
Point
(
patternSize
.
width
-
1
,
0
));
trueIndices
.
push_back
(
Point
(
patternSize
.
width
-
1
,
0
));
if
(
isAsymmetricGrid
)
{
trueIndices
.
push_back
(
Point
(
patternSize
.
width
-
1
,
1
));
trueIndices
.
push_back
(
Point
(
patternSize
.
width
-
1
,
1
));
trueIndices
.
push_back
(
Point
(
patternSize
.
width
-
1
,
patternSize
.
height
-
2
));
trueIndices
.
push_back
(
Point
(
patternSize
.
width
-
1
,
patternSize
.
height
-
2
));
}
trueIndices
.
push_back
(
Point
(
patternSize
.
width
-
1
,
patternSize
.
height
-
1
));
trueIndices
.
push_back
(
Point
(
patternSize
.
width
-
1
,
patternSize
.
height
-
1
));
trueIndices
.
push_back
(
Point
(
0
,
patternSize
.
height
-
1
));
trueIndices
.
push_back
(
Point
(
0
,
patternSize
.
height
-
1
));
...
@@ -275,8 +304,15 @@ void CirclesGridClusterFinder::rectifyPatternPoints(const cv::Size &patternSize,
...
@@ -275,8 +304,15 @@ void CirclesGridClusterFinder::rectifyPatternPoints(const cv::Size &patternSize,
{
{
int
i
=
trueIndices
[
idx
].
y
;
int
i
=
trueIndices
[
idx
].
y
;
int
j
=
trueIndices
[
idx
].
x
;
int
j
=
trueIndices
[
idx
].
x
;
if
(
isAsymmetricGrid
)
{
idealPoints
.
push_back
(
Point2f
((
2
*
j
+
i
%
2
)
*
squareSize
,
i
*
squareSize
));
idealPoints
.
push_back
(
Point2f
((
2
*
j
+
i
%
2
)
*
squareSize
,
i
*
squareSize
));
}
}
else
{
idealPoints
.
push_back
(
Point2f
(
j
*
squareSize
,
i
*
squareSize
));
}
}
Mat
homography
=
findHomography
(
Mat
(
sortedCorners
),
Mat
(
idealPoints
),
0
);
Mat
homography
=
findHomography
(
Mat
(
sortedCorners
),
Mat
(
idealPoints
),
0
);
Mat
rectifiedPointsMat
;
Mat
rectifiedPointsMat
;
...
@@ -285,7 +321,7 @@ void CirclesGridClusterFinder::rectifyPatternPoints(const cv::Size &patternSize,
...
@@ -285,7 +321,7 @@ void CirclesGridClusterFinder::rectifyPatternPoints(const cv::Size &patternSize,
convertPointsFromHomogeneous
(
rectifiedPointsMat
,
rectifiedPatternPoints
);
convertPointsFromHomogeneous
(
rectifiedPointsMat
,
rectifiedPatternPoints
);
}
}
void
CirclesGridClusterFinder
::
parsePatternPoints
(
const
cv
::
Size
&
patternSize
,
const
std
::
vector
<
cv
::
Point2f
>
&
patternPoints
,
const
std
::
vector
<
cv
::
Point2f
>
&
rectifiedPatternPoints
,
std
::
vector
<
cv
::
Point2f
>
&
centers
)
void
CirclesGridClusterFinder
::
parsePatternPoints
(
const
std
::
vector
<
cv
::
Point2f
>
&
patternPoints
,
const
std
::
vector
<
cv
::
Point2f
>
&
rectifiedPatternPoints
,
std
::
vector
<
cv
::
Point2f
>
&
centers
)
{
{
flann
::
LinearIndexParams
flannIndexParams
;
flann
::
LinearIndexParams
flannIndexParams
;
flann
::
Index
flannIndex
(
Mat
(
rectifiedPatternPoints
).
reshape
(
1
),
flannIndexParams
);
flann
::
Index
flannIndex
(
Mat
(
rectifiedPatternPoints
).
reshape
(
1
),
flannIndexParams
);
...
@@ -295,7 +331,12 @@ void CirclesGridClusterFinder::parsePatternPoints(const cv::Size &patternSize, c
...
@@ -295,7 +331,12 @@ void CirclesGridClusterFinder::parsePatternPoints(const cv::Size &patternSize, c
{
{
for
(
int
j
=
0
;
j
<
patternSize
.
width
;
j
++
)
for
(
int
j
=
0
;
j
<
patternSize
.
width
;
j
++
)
{
{
Point2f
idealPt
((
2
*
j
+
i
%
2
)
*
squareSize
,
i
*
squareSize
);
Point2f
idealPt
;
if
(
isAsymmetricGrid
)
idealPt
=
Point2f
((
2
*
j
+
i
%
2
)
*
squareSize
,
i
*
squareSize
);
else
idealPt
=
Point2f
(
j
*
squareSize
,
i
*
squareSize
);
vector
<
float
>
query
=
Mat
(
idealPt
);
vector
<
float
>
query
=
Mat
(
idealPt
);
int
knn
=
1
;
int
knn
=
1
;
vector
<
int
>
indices
(
knn
);
vector
<
int
>
indices
(
knn
);
...
...
modules/calib3d/src/circlesgrid.hpp
View file @
dde91811
...
@@ -53,8 +53,9 @@
...
@@ -53,8 +53,9 @@
class
CirclesGridClusterFinder
class
CirclesGridClusterFinder
{
{
public
:
public
:
CirclesGridClusterFinder
()
CirclesGridClusterFinder
(
bool
_isAsymmetricGrid
)
{
{
isAsymmetricGrid
=
_isAsymmetricGrid
;
squareSize
=
1.0
f
;
squareSize
=
1.0
f
;
maxRectifiedDistance
=
squareSize
/
2.0
;
maxRectifiedDistance
=
squareSize
/
2.0
;
}
}
...
@@ -66,10 +67,13 @@ private:
...
@@ -66,10 +67,13 @@ private:
void
findCorners
(
const
std
::
vector
<
cv
::
Point2f
>
&
hull2f
,
std
::
vector
<
cv
::
Point2f
>
&
corners
);
void
findCorners
(
const
std
::
vector
<
cv
::
Point2f
>
&
hull2f
,
std
::
vector
<
cv
::
Point2f
>
&
corners
);
void
findOutsideCorners
(
const
std
::
vector
<
cv
::
Point2f
>
&
corners
,
std
::
vector
<
cv
::
Point2f
>
&
outsideCorners
);
void
findOutsideCorners
(
const
std
::
vector
<
cv
::
Point2f
>
&
corners
,
std
::
vector
<
cv
::
Point2f
>
&
outsideCorners
);
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
);
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
);
void
rectifyPatternPoints
(
const
cv
::
Size
&
patternSize
,
const
std
::
vector
<
cv
::
Point2f
>
&
patternPoints
,
const
std
::
vector
<
cv
::
Point2f
>
&
sortedCorners
,
std
::
vector
<
cv
::
Point2f
>
&
rectifiedPatternPoints
);
void
rectifyPatternPoints
(
const
std
::
vector
<
cv
::
Point2f
>
&
patternPoints
,
const
std
::
vector
<
cv
::
Point2f
>
&
sortedCorners
,
std
::
vector
<
cv
::
Point2f
>
&
rectifiedPatternPoints
);
void
parsePatternPoints
(
const
cv
::
Size
&
patternSize
,
const
std
::
vector
<
cv
::
Point2f
>
&
patternPoints
,
const
std
::
vector
<
cv
::
Point2f
>
&
rectifiedPatternPoints
,
std
::
vector
<
cv
::
Point2f
>
&
centers
);
void
parsePatternPoints
(
const
std
::
vector
<
cv
::
Point2f
>
&
patternPoints
,
const
std
::
vector
<
cv
::
Point2f
>
&
rectifiedPatternPoints
,
std
::
vector
<
cv
::
Point2f
>
&
centers
);
float
squareSize
,
maxRectifiedDistance
;
float
squareSize
,
maxRectifiedDistance
;
bool
isAsymmetricGrid
;
cv
::
Size
patternSize
;
};
};
class
Graph
class
Graph
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment