-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathChessPatternDetector.cpp
60 lines (52 loc) · 1.38 KB
/
ChessPatternDetector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "ChessPatternDetector.h"
pair<vector<Point2f>, Mat> get_pattern_points( int cameraIndex, int num_h, int num_w )
{
VideoCapture cap(cameraIndex);
while(true)
{
Mat frame;
cap >> frame;
static vector<Point2f> cornerPoints;
cornerPoints.clear();
bool patternfound = false;
patternfound = findChessboardCorners( frame, Size(num_h, num_w), cornerPoints );
if( patternfound )
{
cout << " **Pattern Found** " << endl;
sortGridPoints( cornerPoints, num_h, num_w );
return make_pair( cornerPoints, frame );
}else
{
cout << " **Pattern NOT Found** " << endl;
}
}
}
void sortGridPoints( vector<Point2f> &points,int width, int height )
{
for( int i = 1; i < points.size(); ++i)
{
for( int j = 0; j < ( points.size()-i ); ++j )
if( points[j].y > points[j+1].y )
{
Point2f temp=points[j];
points[j]=points[j+1];
points[j+1]=temp;
}
}
for( int i = 0; i < height ; i++ )
{
for( int j = 1; j < width; j++ )
{
for( int k = 0; k < width-j; k++ )
{
int index = i * width + k;
if( points[index].x > points[index+1].x )
{
Point2f temp = points[index];
points[index] = points[index+1];
points[index+1] = temp;
}
}
}
}
}