/
camera.cpp
151 lines (120 loc) · 5.3 KB
/
camera.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
145
146
147
148
149
150
151
#include "include/camera.hpp"
#include "include/analysis.hpp"
#include <stdio.h>
#include <vector>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv/cv.h>
namespace iwb {
Camera* Camera::instance = NULL;
Camera* Camera::getInstance() {
if (instance == NULL) {
instance = new Camera();
}
return instance;
}
Camera::Camera() {
}
int Camera::getWidth() {
return this->width;
}
int Camera::getHeight() {
return this->height;
}
int Camera::getProjectorWidth() {
return this->projectorWidth;
}
int Camera::getProjectorHeight() {
return this->projectorHeight;
}
CvPoint Camera::getProjectorOrigin() {
return this->projectorOrigin;
}
void Camera::calibrate(Capture* cpt, Presentation* prs){
#ifdef NO_CALIBRATION
printf("DEBUG: camera calibration disabled!\n");
IplImage* cf = cvQueryFrame(cpt->getCapture());
this->width = cf->width;
this->height = cf->height;
this->projectorWidth = this->width;
this->projectorHeight = this->height;
this->projectorOrigin = cvPoint(0,0);
// cvReleaseImage (&cf);
return;
#endif
printf("DEBUG: calibrating camera\n");
CvSize nsquares = cvSize(6,4);
CvPoint2D32f* corners = new CvPoint2D32f[ 6*4 ];
IplImage *cb = cvLoadImage("res/chessboard.png",1);
prs->putImage(cvPoint(0,0), cvPoint(prs->getScreenWidth(), prs->getScreenHeight()), NULL, NULL, cb);
prs->applyBuffer();
//IplImage *fake = cvLoadImage("fake.jpg", 1);
//IplImage *src = cvCreateImage(cvGetSize(fake), IPL_DEPTH_8U, 1);
//cvCvtColor(fake, src, CV_RGB2GRAY);
IplImage *frame;
bool patternFound = false;
int cc;
while (!patternFound) {
printf("trying to find chessboard\n");
frame = cvQueryFrame(cpt->getCapture());
patternFound = cvFindChessboardCorners(frame, nsquares, corners, &cc,
CV_CALIB_CB_ADAPTIVE_THRESH |
CV_CALIB_CB_FILTER_QUADS |
CV_CALIB_CB_FAST_CHECK |
CV_CALIB_CB_NORMALIZE_IMAGE);
// prs->putImage(cvPoint(0,0), cvPoint(prs->getScreenWidth(), prs->getScreenHeight()), frame);
// prs->applyBuffer();
cvWaitKey(5);
}
this->width = frame->width;
this->height = frame->height;
printf("\n");
//float x = 2*corners[0].x-corners[1].x,
// y = 2*corners[0].y-corners[7].y;
float x = corners[0].x-1.5*(corners[1].x-corners[0].x),
y = corners[0].y-1.5*(corners[6].y-corners[0].y);
this->projectorOrigin = cvPoint((int)x, (int)y);
//x = 2*corners[34].x-corners[33].x;
//y = 2*corners[34].y-corners[27].y;
x = corners[23].x+1.5*(corners[23].x-corners[22].x);
y = corners[23].y+1.5*(corners[23].y-corners[17].y);
this->projectorWidth = (int)(x-this->projectorOrigin.x);
this->projectorHeight = (int)(y-this->projectorOrigin.y);
printf("Projector: (%d, %d): %dx%d)\n", this->projectorOrigin.x, this->projectorOrigin.y, this->projectorWidth,this->projectorHeight);
//cpt->saveFrame("cbfound.jpg", frame);
// improve result (thoug through testing, it seems it makes it worse - disabling)
// could use simple linear regression to get a "better" calibration.
// IplImage *frame_gray = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);
// cvFindCornerSubPix( frame_gray, corners, cc, cvSize( 11, 11 ),
// cvSize( -1, -1 ), cvTermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ));
// draw calibration result
cvDrawChessboardCorners( frame, nsquares , corners, cc, patternFound );
IplImage* bg;
bg = cvLoadImage("res/bg.jpg", CV_LOAD_IMAGE_UNCHANGED);
prs->putImage(cvPoint(0,0), cvPoint(prs->getScreenWidth(), prs->getScreenHeight()), NULL, NULL, bg);
prs->applyBuffer();
// display result longer
cvWaitKey(2000);
prs->clearArea(cvPoint(0,0), cvPoint(prs->getScreenWidth(), prs->getScreenHeight()));
prs->applyBuffer();
cvWaitKey(1000);
for (int i=0; i<100; i++)
cvQueryFrame(cpt->getCapture());
//cvNamedWindow("Foobar", CV_WINDOW_AUTOSIZE);
//cvShowImage("Foobar", cvQueryFrame(cpt->getCapture()));
//cvWaitKey(500);
// cvReleaseImage(&frame);
// generate chessboard programatically. (htf does cvFillPoly work?! - using image for now)
// IplImage *cb = cvCreateImage(cvSize(prs->getScreenWidth(), prs->getScreenHeight()), IPL_DEPTH_8U, 1);
// int x,y, dx = prs->getScreenWidth()/nsquares.width, dy = prs->getScreenHeight()/nsquares.height;
// CvPoint *corners[4];
// for (x=0; x<prs->getScreenWidth(); x+=dx) {
// for (y=0; y<prs->getScreenHeight(); y+=dy) {
// corners[0] = cvPoint(x,y);
// corners[1] = cvPoint(x+dx,y);
// corners[2] = cvPoint(x+dx,y+dy);
// corners[3] = cvPoint(x,y+dy);
// cvFillPoly(...)
// }
// }
}
}