-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathManualScanning(RealTime).cpp
More file actions
521 lines (482 loc) · 12.4 KB
/
ManualScanning(RealTime).cpp
File metadata and controls
521 lines (482 loc) · 12.4 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include <opencv2\opencv.hpp>
#include <opencv2\core.hpp>
#include <opencv2\highgui.hpp>
#include <opencv2\imgproc.hpp>
#include <wtypes.h>
#include <math.h>
#include <boost\filesystem.hpp>
#include <opencv2\features2d.hpp>
//#include <boost/thread.hpp>
//#include <boost/bind.hpp>
//#include <boost/thread/future.hpp>
//#include <pthread.h>
//#include <opencv2\xfeatures2d.hpp>
using namespace std;
using namespace cv;
namespace bfs = boost::filesystem;
//using namespace boost::posix_time;
//namespace xf = xfeatures2d;
typedef struct relcord
{
int tx;
int ty;
Point bridge;
relcord()
{
tx = 0;
ty = 0;
bridge.x = 0;
bridge.y = 0;
}
}relcord;
int initCamera(VideoCapture &mscam)
{
//video stream parameters
mscam.set(CAP_PROP_FPS, 40);
mscam.set(CAP_PROP_FRAME_WIDTH, 640);
mscam.set(CAP_PROP_FRAME_HEIGHT, 480);
//using the default camera
mscam.open(0);
if (!mscam.isOpened())
{
return -1;
}
else
{
return 0;
}
}
Size getScreenRes()
{
Size temp(0, 0);
RECT screen;
const HWND hScreen = GetDesktopWindow();
GetWindowRect(hScreen, &screen);
temp.width = screen.right;
temp.height = screen.bottom;
return temp;
}
void getOffset(Size screenres, Size currentres, Point2d &vp2cur)
{
vp2cur.x = (screenres.width - currentres.width) / 2;
vp2cur.y = (screenres.height - currentres.height) / 2;
}
relcord displace(relcord origin, int xhor, int yver, int tilesize)
{
relcord temp;
if (origin.bridge.x + xhor < 0)
{
int rem = origin.bridge.x + xhor;
int mul = ceil((rem*-1.0) / tilesize);
temp.tx = origin.tx - mul;
temp.bridge.x = (mul*tilesize) + rem;
}
else if (origin.bridge.x + xhor > tilesize - 1)
{
int rem = origin.bridge.x + xhor;
int mul = floor((rem*1.0) / tilesize);
temp.tx = origin.tx + mul;
temp.bridge.x = rem - (mul*tilesize);
}
else
{
temp.tx = origin.tx;
temp.bridge.x = origin.bridge.x + xhor;
}
if (origin.bridge.y + yver < 0)
{
int rem = origin.bridge.y + yver;
int mul = ceil((rem*-1.0) / tilesize);
temp.ty = origin.ty - mul;
temp.bridge.y = (mul*tilesize) + rem;
}
else if (origin.bridge.y + yver > tilesize - 1)
{
int rem = origin.bridge.y + yver;
int mul = floor((rem*1.0) / tilesize);
temp.ty = origin.ty + mul;
temp.bridge.y = rem - (mul*tilesize);
}
else
{
temp.ty = origin.ty;
temp.bridge.y = origin.bridge.y + yver;
}
return temp;
}
Mat getCanvas(string tiledir, relcord canvastlc, int reqwidth, int reqheight, int tilesize, int gridspan[])
{
relcord br = displace(canvastlc, reqwidth - 1, reqheight - 1, tilesize);
if (canvastlc.tx < gridspan[0])
{
gridspan[0] = canvastlc.tx;
}
if (br.tx > gridspan[1])
{
gridspan[1] = br.tx;
}
if (canvastlc.ty < gridspan[2])
{
gridspan[2] = canvastlc.ty;
}
if (br.ty > gridspan[3])
{
gridspan[3] = br.ty;
}
Mat et = Mat::zeros(Size(tilesize, tilesize), CV_8UC3);
//build all rows
vector<Mat> crows;
for (int i = canvastlc.ty; i <= br.ty; i++)
{
vector<Mat> crowbuffer;
for (int j = canvastlc.tx; j <= br.tx; j++)
{
ostringstream ss;
ss << "\\" << j;
if (!bfs::exists(tiledir + ss.str()))
{
bfs::create_directory(tiledir + ss.str());
}
ss << "\\" << i << ".bmp";
Mat temp = imread(tiledir + ss.str());
if (temp.empty())
{
imwrite(tiledir + ss.str(), et);
temp = imread(tiledir + ss.str());
}
crowbuffer.push_back(temp);
}
if (crowbuffer.size() == 1)
{
crows.push_back(crowbuffer[0]);
}
else
{
Mat hcres;
hconcat(crowbuffer, hcres);
crows.push_back(hcres);
}
}
//assemble all rows
if (crows.size() == 1)
{
return crows[0];
}
else
{
Mat canvas;
vconcat(crows, canvas);
return canvas;
}
}
bool getFrame(VideoCapture mscam, Mat ¤t)
{
bool flag = false;
Mat frame;
flag = mscam.read(frame);
Mat result = Mat::zeros(current.size(), current.type());
if (flag && frame.cols >= current.cols && frame.rows >= current.rows)
{
Rect roi((frame.cols - current.cols) / 2, (frame.rows - current.rows) / 2, current.cols, current.rows);
result = frame(roi);
result.copyTo(current);
return true;
}
else
{
result.copyTo(current);
return false;
}
}
void updateCanvas(Mat canvas, string tiledir, int tilesize, relcord canvastlc)
{
int rc = canvas.rows / tilesize;
int cc = canvas.cols / tilesize;
int fy = canvastlc.ty;
for (int i = 0; i < rc; i++)
{
int fx = canvastlc.tx;
for (int j = 0; j < cc; j++)
{
Mat temp = canvas(Rect
(j*tilesize, i*tilesize, tilesize, tilesize));
ostringstream ss;
ss << "\\" << fx << "\\" << fy << ".bmp";
imwrite(tiledir + ss.str(), temp);
fx++;
}
fy++;
}
}
void saveScan(string tiledir, int gridspan[], int tilesize)
{
Mat blank = Mat::zeros(Size(tilesize, tilesize), CV_8UC3);
vector<Mat> rows;
for (int i = gridspan[2]; i <= gridspan[3]; i++)
{
vector<Mat> buffer;
for (int j = gridspan[0]; j <= gridspan[1]; j++)
{
ostringstream ss;
ss << "\\" << j << "\\" << i << ".bmp";
Mat temp = imread(tiledir + ss.str());
if (temp.empty())
{
buffer.push_back(blank);
}
else
{
buffer.push_back(temp);
}
}
Mat crow;
hconcat(buffer, crow);
rows.push_back(crow);
buffer.clear();
}
Mat output;
vconcat(rows, output);
imwrite("result.bmp", output);
bfs::remove_all(tiledir);
}
bool placeTM(Mat current, Mat &window, relcord wtlc, Size microtile, Point2d &tvec, int tilesize)
{
Mat c;
cvtColor(current, c, CV_BGR2GRAY);
Canny(c, c, 100, 200, 5);
Mat w;
cvtColor(window, w, CV_BGR2GRAY);
Mat mask;
threshold(w, mask, 0, 255, CV_THRESH_BINARY);
erode(mask, mask, Mat());
Canny(w, w, 100, 200, 5);
Mat scene = Mat::zeros(window.size(), CV_8UC1);
w.copyTo(scene, mask);
Mat MM;
matchTemplate(scene, c, MM, CV_TM_CCORR_NORMED);
Point match;
double mv = 0.0;
minMaxLoc(MM, NULL, &mv, NULL, &match);
if (mv >= 0.5)
{
relcord ctlc = displace(wtlc, match.x, match.y, tilesize);
Point topleft;
topleft.x = microtile.width - (ctlc.bridge.x % microtile.width);
topleft.y = microtile.height - (ctlc.bridge.y % microtile.height);
int xmul = (current.cols - topleft.x) / microtile.width;
int ymul = (current.rows - topleft.y) / microtile.height;
Point botright;
botright.x = (xmul*microtile.width) + topleft.x;
botright.y = (ymul*microtile.height) + topleft.y;
Mat mask = Mat::zeros(current.size(), CV_8UC1);
rectangle(mask, topleft, botright, Scalar(255), -1, 8);
Rect X = Rect(match.x, match.y, current.cols, current.rows);
current.copyTo(window(X), mask);
tvec.x = match.x - current.cols;
tvec.y = match.y - current.rows;
return true;
}
else
{
return false;
}
}
int getSD(Mat image)
{
cvtColor(image, image, CV_BGR2GRAY);
Laplacian(image, image, CV_8UC1, 1);
Scalar m, sd;
meanStdDev(image, m, sd, noArray());
return int(sd[0]);
}
int main()
{
//Set tile buffer directory
string exeprojpath = "E:\\MANUAL SLIDE SCANNING SYSTEM BY PRANJAL DT. 6.11.2017";
string tiledir = exeprojpath + "\\Tiles";
if (bfs::exists(tiledir))
{
bfs::remove_all(tiledir);
}
bfs::create_directory(tiledir);
//Initialize gridspan
int gridspan[4];
for (int i = 0; i < 4; i++)
{
gridspan[i] = 0;
}
//Set current's resolution
const Size currentres(400, 300);
const Size microtile(40, 30);
//Initialize the camera
VideoCapture mscam;
int index = initCamera(mscam);
if (index < 0)
{
bfs::remove_all(tiledir);
cout << "Camera not found\n\n";
getchar();
return 1;
}
else
{
cout << "Using the default camera\n\n";
} //wait for a frame to be read
//Obtain the screen display resolution
Size screenres = getScreenRes();
if (screenres.width < 800 || screenres.height < 600)
{
bfs::remove_all(tiledir);
cout << "Use a display resolution of [800 x 600] or higher\n\n";
getchar();
return 1;
}
//Set tile size
const int tilesize = 256;
//Calculate requisite offsets
Point2d vp2cur;
getOffset(screenres, currentres, vp2cur);
//Setup the canvas
relcord canvastlc;
canvastlc.bridge.x = 40;
canvastlc.bridge.y = 30;
/**/int reqwidth = canvastlc.bridge.x + screenres.width;
/**/int reqheight = canvastlc.bridge.y + screenres.height;
/**/Mat canvas = getCanvas(tiledir, canvastlc, reqwidth, reqheight, tilesize, gridspan);
//Initializations
const string win = "MWSI";
namedWindow(win, CV_WINDOW_AUTOSIZE);
Mat current = Mat::zeros(currentres, CV_8UC3);
Rect curonvp(vp2cur.x, vp2cur.y, currentres.width, currentres.height);
Mat vp, viewport;
/**/Rect vpfromcanvas(canvastlc.bridge.x, canvastlc.bridge.y, screenres.width, screenres.height);
vp = canvas(vpfromcanvas);
vp.copyTo(viewport);
//Loop 1
bool cameraFlag = false;
Mat temp = Mat::zeros(currentres, CV_8UC3);
int fscore = 0;
while (1)
{
cameraFlag = getFrame(mscam, current);
if (!cameraFlag)
{
break;
}
fscore = getSD(current);
ostringstream ss;
ss << fscore;
current.copyTo(temp);
rectangle(temp, Point(0, 0), Point(temp.cols - 1, temp.rows - 1), Scalar(0, 0, 255), 1, 8);
putText(temp, ss.str(), Point(10, 30), CV_FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0), 2, 8);
temp.copyTo(viewport(curonvp));
imshow(win, viewport);
int key;
key = waitKey(1);
if ((char)key == 's')
{
current.copyTo(canvas(Rect(canvastlc.bridge.x + vp2cur.x, canvastlc.bridge.y + vp2cur.y, currentres.width, currentres.height)));
updateCanvas(canvas, tiledir, tilesize, canvastlc);
break;
}
else if ((char)key == 'q')
{
cout << "\nUser action: [Q] Exit program.";
destroyWindow(win);
bfs::remove_all(tiledir);
getchar();
return 0;
//break;
}
}
if (!cameraFlag)
{
destroyWindow(win);
bfs::remove_all(tiledir);
cout << "Camera disconnected. Program will terminate.\n\n";
getchar();
return 1;
}
//Loop 2
cameraFlag = false;
Point shift;
relcord wtlc;
Mat minicanvas;
Mat window = Mat::zeros(Size(currentres.width * 3, currentres.height * 3), current.type());
bool sflag = false;
Point2d tvec;
fscore = 0;
while (1)
{
cameraFlag = getFrame(mscam, current);
if (!cameraFlag)
{
break;
}
fscore = getSD(current);
ostringstream ss;
ss << fscore;
shift.x = vp2cur.x - currentres.width;
shift.y = vp2cur.y - currentres.height;
wtlc = displace(canvastlc, shift.x, shift.y, tilesize);
//wtlc = tgroup.create_thread(&displace, canvastlc, shift.x, shift.y, tilesize);
minicanvas = getCanvas(tiledir, wtlc, window.cols, window.rows, tilesize, gridspan);
window = minicanvas(Rect(wtlc.bridge.x, wtlc.bridge.y, window.cols, window.rows));
sflag = placeTM(current, window, wtlc, microtile, tvec, tilesize);
if (sflag)
{
updateCanvas(minicanvas, tiledir, tilesize, wtlc);
canvastlc = displace(canvastlc, tvec.x, tvec.y, tilesize);
reqwidth = canvastlc.bridge.x + screenres.width;
reqheight = canvastlc.bridge.y + screenres.height;
canvas = getCanvas(tiledir, canvastlc, reqwidth, reqheight, tilesize, gridspan);
vpfromcanvas = Rect(canvastlc.bridge.x, canvastlc.bridge.y, screenres.width, screenres.height);
}
vp = canvas(vpfromcanvas);
vp.copyTo(viewport);
putText(current, ss.str(), Point(10, 30), CV_FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0), 2, 8);
if (sflag)
{
rectangle(current, Point(0, 0), Point(current.cols - 1, current.rows - 1), Scalar(0, 255, 0), 1, 8);
}
else
{
rectangle(current, Point(0, 0), Point(current.cols - 1, current.rows - 1), Scalar(0, 0, 255), 1, 8);
}
current.copyTo(viewport(curonvp));
imshow(win, viewport);
int key;
key = waitKey(1);
if ((char)key == 'f')
{
updateCanvas(canvas, tiledir, tilesize, canvastlc);
break;
}
else if ((char)key == 'q')
{
cout << "\nUser action: [Q] Exit program.";
destroyWindow(win);
//bfs::remove_all(tiledir);
getchar();
//return 0;
break;
}
}
if (!cameraFlag)
{
destroyWindow(win);
bfs::remove_all(tiledir);
cout << "Camera disconnected. Program will terminate.\n\n";
getchar();
return 1;
}
destroyAllWindows();
saveScan(tiledir, gridspan, tilesize);
cout << "\nProgram run successful. Next step -> Assembly to TIFF."; getchar();
return 0;
}