-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathimageFrame.cpp
More file actions
233 lines (202 loc) · 9.05 KB
/
imageFrame.cpp
File metadata and controls
233 lines (202 loc) · 9.05 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "include/imageFrame.hpp"
#include "include/confirmation.hpp"
#include "include/camera.hpp"
#include "include/cornerDetector.hpp"
#include <opencv/cv.h>
namespace iwb {
ImageFrame::ImageFrame(Capture* cpt, Presentation* prs, Analysis* analysis) : Drawable("res/bg.jpg", prs, cvPoint(100, 300), cvPoint(924, 768)) {
this->cpt = cpt;
this->analysis = analysis;
ulImage = cvLoadImage("res/CleftSmaller.jpg", 0);
brImage = cvLoadImage("res/CrightSmaller.jpg", 0);
startTime = -1;
currentProcess = DRAWING;
captureState = IDLE;
// initializing camera coordinates
// FIXME: replace with actual camera coordinates
cameraUL = cvPoint(0, 0);
cameraBR = cvPoint(1024, 768);
// refreshCornerCoords(time);
// IplImage* currentFrame = cpt->getCapture();
// p1 = Analysis::getLocation(Analysis::getDiff(cornerFrame, currentFrame), tmp1, true);
// p2 = Analysis::getLocation(Analysis::getDiff(cornerFrame, currentFrame), tmp2, false);
}
void ImageFrame::refreshCornerCoords(IplImage* currentFrame) {
// bool calculate = true;
// while (firstTime || calculate) {
// CvPoint ul = Analysis::getLocation(analysis->getCornerDiff(), ulImage, true);
// CvPoint br = Analysis::getLocation(analysis->getCornerDiff(), brImage, false);
//
// if (ul.x < br.x && ul.y < br.y) {
// projectorUL = ul;
// projectorBR = br;
//
// break;
// }
//
// calculate = false;
// }
// Second time capturing still frame
CornerDetector cd;
IplImage* diff = analysis->getCornerDiff(capturedFrame, currentFrame);
// if (!cd.detect(diff, &cameraUL, &cameraBR)) {
// printf("DEBUG: IT'S FAAAAAAAAAAAALSE YOU KNOW\n");
// cvSaveImage("cornercornerCAPTURED.jpg", capturedFrame);
// cvSaveImage("cornercornerCURRENT.jpg", currentFrame);
// cvSaveImage("cornercornercorner.jpg", diff);
// return;
// }
cameraUL = analysis->getLocation(diff, ulImage, true);
cameraBR = analysis->getLocation(diff, brImage, false);
Camera *camera = Camera::getInstance();
CvPoint po = camera->getProjectorOrigin();
int pw = camera->getProjectorWidth(),
ph = camera->getProjectorHeight(),
w = this->prs->screenWidth,
h = this->prs->screenHeight,
x = cameraUL.x,
y = cameraUL.y;
//printf("\n\nx: po.x(%d) w(%d) pw(%d) p.x(%d)\ny: po.y(%d) h(%d) ph(%d) p.y(%d)\n", po.x, w, pw, p.x, po.y, h, ph, p.y);
// int x = po.x+((float)pw/w)*p.x,
// y = po.y+((float)ph/h)*p.y;
projectorUL.x = (int)((float)x - po.x)* w / pw;
projectorUL.y = (int)((float)y - po.y)* h / ph;
x = cameraBR.x,
y = cameraBR.y;
projectorBR.x = (int)((float)x - po.x)* w / pw;
projectorBR.y = (int)((float)y - po.y)* h / ph;
//printf("(%d, %d)", x,y);
// return cvPoint(x,y);
// cvRectangle(diff, projectorUL, projectorBR, cvScalar(0, 0, 255, 0), 1, 0, 0);
cpt->saveFrame("cornerDiff.jpg", diff);
printf("if ul.x: %d, ul.y: %d\n", projectorUL.x, projectorUL.y);
printf("if br.x: %d, br.y: %d\n", projectorBR.x, projectorBR.y);
// cpt->saveFrame("diff.jpg", Analysis::getDiff(capturedFrame, currentFrame));
// ]]] Save KF & CI
//------------------------------------------------------------------------------
}
void ImageFrame::captureFrame(IplImage* currentFrame) {
// cvRectangle(currentFrame, cameraUL, cameraBR, cvScalar(0, 0, 255, 0), 1, 0, 0);
//------------------------------------------------------------------------------
// [[[ Save KF & CI
//Counting coordinates for making CI
int width = cameraBR.x - cameraUL.x,
height = cameraBR.y - cameraUL.y;
try {
cvSetImageROI(currentFrame, cvRect(cameraUL.x, cameraUL.y, width, height));
IplImage* Ci = cvCreateImage(cvGetSize(currentFrame), currentFrame->depth, currentFrame->nChannels);
cvCopyImage(currentFrame, Ci);
capturedFrame = cvCloneImage(Ci);
// if (currentFrame != NULL)
// cvReleaseImage(¤tFrame);
cvResetImageROI(currentFrame);
// // Saving Images
// // @todo save function here
// cpt->saveFrame("KeyFrame.jpg", currentFrame);
// //currentKfSaved = true;
//
// isSaved = true;
cvReleaseImage(&Ci);
//cvReleaseImage(&capturedFrame);
} catch (cv::Exception& e) {
}
}
void ImageFrame::setImagePath(const char* imagePath) {
if (captureState == IDLE && currentProcess == DRAWING) {
cvReleaseImage(&image);
printf(">>>>>>>>>>>> %s\n", imagePath);
image = cvLoadImage(imagePath, CV_LOAD_IMAGE_UNCHANGED);
currentProcess = CHANGING_IMAGE;
captureState = GETTING_CAPTURE;
}
}
void ImageFrame::saveFrame() {
if (captureState == IDLE && currentProcess == DRAWING) {
currentProcess = SAVING_IMAGE;
captureState = GETTING_CAPTURE;
}
}
void ImageFrame::checkForCapture() {
refreshCornerCoords(NULL);
}
void ImageFrame::checkForMovement() {
if (captureState == IDLE) {
return;
}
IplImage* currentFrame = cvQueryFrame(cpt->getCapture());
if (cpt->getPreviousFrame() == NULL) {
cpt->setPreviousFrame(cvCloneImage(currentFrame));
return;
}
IplImage* previousFrame = cpt->getPreviousFrame();
IplImage* diff = analysis->getCornerDiff(previousFrame, currentFrame);
cpt->setPreviousFrame(cvCloneImage(currentFrame));
// int startTime = -1;
if (!analysis->isMoving(diff) && (captureState == GETTING_CAPTURE || captureState == WAITING_FOR_CORNERS)) {
printf("DEBUG: not moving\n");
if (startTime == -1) {
printf("DEBUG: clock started\n");
startTime = clock() / CLOCKS_PER_SEC;
}
int timeDifference;
timeDifference = clock() / CLOCKS_PER_SEC - startTime;
if (timeDifference >= 2) {
printf("DEBUG: clock triggered\n");
switch (captureState) {
case GETTING_CAPTURE:
capturedFrame = cvCloneImage(currentFrame);
puts("first");
startTime = -1;
captureState = WAITING_FOR_CORNERS;
break;
case WAITING_FOR_CORNERS:
refreshCornerCoords(currentFrame);
puts("second");
// TODO: pass the saveFrame function as a callback to the confirmation dialog for touched yes event
captureState = CAPTURED;
break;
}
}
} else {
startTime = -1;
}
cvReleaseImage(&diff);
}
void ImageFrame::update() {
// checkForMovement();
printf("DEBUG: imageFrame current process: %d\n", currentProcess);
printf("DEBUG: imageFrame capture state: %d\n", captureState);
switch (currentProcess) {
case DRAWING:
break;
case CHANGING_IMAGE:
checkForMovement();
if (captureState == CAPTURED) {
captureState = IDLE;
currentProcess = DRAWING;
}
break;
case SAVING_IMAGE:
checkForMovement();
if (captureState == CAPTURED) {
captureFrame(cpt->getPreviousFrame());
cpt->saveFrame("CapturedImage.jpg", capturedFrame);
captureState = IDLE;
currentProcess = DRAWING;
}
break;
}
if (captureState == CAPTURED) {
startTime = -1;
cvReleaseImage(&capturedFrame);
capturedFrame = NULL;
}
}
void ImageFrame::draw(Presentation* prs) {
if (currentProcess == DRAWING || currentProcess == SAVING_IMAGE) {
// prs->putImage(projectorUL, projectorBR, image);
prs->putImage(projectorUL, projectorBR, NULL, NULL, image);
prs->applyBuffer();
}
}
}