-
Notifications
You must be signed in to change notification settings - Fork 64
/
chessgen.cpp
144 lines (119 loc) · 4.32 KB
/
chessgen.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "opencv2/core/core.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
//#include <opencv2/imgcodecs.hpp>
#include <iostream>
#include <vector>
#include <cstdio>
using namespace cv;
using namespace std;
// specify board width and height
// specify output image size
// specify (optional) pixel size of squares
static void help(){
printf("This is a chessboard pattern generator.\n"
"Usage: chessgen <option> ..\n"
" -w <image_width> # the width in pixels of the output pattern\n"
" -h <image_width> # the height in pixels of the output pattern\n"
" -x <board_width> # the number of inner corners on the horizontal dimension\n"
" -y <board_width> # the number of inner corners on the vertical dimension\n"
" -s <square_size> # the size in pixels to make each square\n"
" -save # enable saving of the pattern\n"
" -show # enable display of the pattern\n"
"\n" );
}
int main(int argc, char** argv){
Size boardSize,imageSize;
int sqrSize = -1;
bool save_image = false;
bool show_image = false;
imageSize = Size(1024,768);
// parse arguments
if( argc < 2 )
{
help();
return 0;
}
for(int i=1;i<argc;i++)
{
const char* s = argv[i];
if( strcmp( s, "-w" ) == 0 )
{
if( sscanf( argv[++i], "%u", &imageSize.width ) != 1 || imageSize.width <= 0 )
return fprintf( stderr, "Invalid image width\n" ), -1;
}
else if( strcmp( s, "-h" ) == 0 )
{
if( sscanf( argv[++i], "%u", &imageSize.height ) != 1 || imageSize.height <= 0 )
return fprintf( stderr, "Invalid image height\n" ), -1;
}
else if( strcmp( s, "-x" ) == 0 )
{
if( sscanf( argv[++i], "%u", &boardSize.width ) != 1 || boardSize.width <= 0 )
return fprintf( stderr, "Invalid board height\n" ), -1;
}
else if( strcmp( s, "-y" ) == 0 )
{
if( sscanf( argv[++i], "%u", &boardSize.height ) != 1 || boardSize.height <= 0 )
return fprintf( stderr, "Invalid board height\n" ), -1;
}
else if( strcmp( s, "-s" ) == 0 )
{
if( sscanf( argv[++i], "%u", &sqrSize ) != 1 || sqrSize <= 0 )
return fprintf( stderr, "Invalid square size\n" ), -1;
}
else if(strcmp("-save",s)==0)
{
save_image = true;
}
else if(strcmp("-show",s)==0)
{
show_image = true;
}
else
return fprintf( stderr, "unknown option %s", s), -1;
}
boardSize.width++;
boardSize.height++;
float scl = 0.75;
if( sqrSize <= 0 ){
// auto calculate square size
float x_sz = imageSize.width / (float)boardSize.width;
float y_sz = imageSize.height / (float)boardSize.height;
x_sz *= scl;
y_sz *= scl;
sqrSize = min(x_sz, y_sz);
cout << "square size = " << sqrSize << endl;
}
Size2i chess_img_size = boardSize * sqrSize;
Point2i roi_vertex( (imageSize.width - chess_img_size.width)/2,
(imageSize.height - chess_img_size.height)/2 );
Mat chessboard(imageSize,CV_8UC3,Scalar::all(255));
Mat chessboard_roi( chessboard,Rect(roi_vertex.x,roi_vertex.y,chess_img_size.width,chess_img_size.height ) );
cout << "Initializing chessboard..";
for (int r=0; r<boardSize.height; r++)
{
for (int c=0; c<boardSize.width; c++)
{
Mat sub_roi(chessboard_roi,Rect(sqrSize*c,sqrSize*r,sqrSize,sqrSize));
if( (r+c)%2 == 1 )
sub_roi.setTo(Scalar::all(255));
else
sub_roi.setTo(Scalar::all(0));
}
}
cout << "done" << endl;
// generate filename
char buf[50];
sprintf(buf,"chessboard_%dx%d.png",boardSize.width-1,boardSize.height-1);
string fname(buf);
if( save_image ){
imwrite(fname, chessboard);
}
if( show_image ){
imshow(fname, chessboard);
waitKey(0);
}
return 0;
}