Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/cpp/cloning_gui.cpp
16337 views
1
/*
2
* cloning.cpp
3
*
4
* Author:
5
* Siddharth Kherada <siddharthkherada27[at]gmail[dot]com>
6
*
7
* This tutorial demonstrates how to use OpenCV seamless cloning
8
* module.
9
*
10
* 1- Normal Cloning
11
* 2- Mixed Cloning
12
* 3- Monochrome Transfer
13
* 4- Color Change
14
* 5- Illumination change
15
* 6- Texture Flattening
16
17
* The program takes as input a source and a destination image (for 1-3 methods)
18
* and outputs the cloned image.
19
20
* Step 1:
21
* -> In the source image, select the region of interest by left click mouse button. A Polygon ROI will be created by left clicking mouse button.
22
* -> To set the Polygon ROI, click the right mouse button or 'd' key.
23
* -> To reset the region selected, click the middle mouse button or 'r' key.
24
25
* Step 2:
26
* -> In the destination image, select the point where you want to place the ROI in the image by left clicking mouse button.
27
* -> To get the cloned result, click the right mouse button or 'c' key.
28
* -> To quit the program, use 'q' key.
29
*
30
* Result: The cloned image will be displayed.
31
*/
32
33
#include <signal.h>
34
#include "opencv2/photo.hpp"
35
#include "opencv2/imgproc.hpp"
36
#include "opencv2/imgcodecs.hpp"
37
#include "opencv2/highgui.hpp"
38
#include "opencv2/core.hpp"
39
#include <iostream>
40
#include <stdlib.h>
41
42
// we're NOT "using namespace std;" here, to avoid collisions between the beta variable and std::beta in c++17
43
using std::cin;
44
using std::cout;
45
using std::endl;
46
using std::string;
47
48
using namespace cv;
49
50
Mat img0, img1, img2, res, res1, final, final1, blend;
51
52
Point point;
53
int drag = 0;
54
int destx, desty;
55
56
int numpts = 100;
57
Point* pts = new Point[100];
58
Point* pts2 = new Point[100];
59
Point* pts_diff = new Point[100];
60
61
int var = 0;
62
int flag = 0, flag1 = 0, flag4 = 0;
63
64
int minx, miny, maxx, maxy, lenx, leny;
65
int minxd, minyd, maxxd, maxyd, lenxd, lenyd;
66
67
int channel, num, kernel_size;
68
69
float alpha,beta;
70
71
float red, green, blue;
72
73
float low_t, high_t;
74
75
void source(int, int, int, int, void*);
76
void destination(int, int, int, int, void*);
77
void checkfile(char*);
78
79
void source(int event, int x, int y, int, void*)
80
{
81
82
if (event == EVENT_LBUTTONDOWN && !drag)
83
{
84
if(flag1 == 0)
85
{
86
if(var==0)
87
img1 = img0.clone();
88
point = Point(x, y);
89
circle(img1,point,2,Scalar(0, 0, 255),-1, 8, 0);
90
pts[var] = point;
91
var++;
92
drag = 1;
93
if(var>1)
94
line(img1,pts[var-2], point, Scalar(0, 0, 255), 2, 8, 0);
95
96
imshow("Source", img1);
97
}
98
}
99
100
if (event == EVENT_LBUTTONUP && drag)
101
{
102
imshow("Source", img1);
103
104
drag = 0;
105
}
106
if (event == EVENT_RBUTTONDOWN)
107
{
108
flag1 = 1;
109
img1 = img0.clone();
110
for(int i = var; i < numpts ; i++)
111
pts[i] = point;
112
113
if(var!=0)
114
{
115
const Point* pts3[1] = {&pts[0]};
116
polylines( img1, pts3, &numpts,1, 1, Scalar(0,0,0), 2, 8, 0);
117
}
118
119
for(int i=0;i<var;i++)
120
{
121
minx = min(minx,pts[i].x);
122
maxx = max(maxx,pts[i].x);
123
miny = min(miny,pts[i].y);
124
maxy = max(maxy,pts[i].y);
125
}
126
lenx = maxx - minx;
127
leny = maxy - miny;
128
129
int mid_pointx = minx + lenx/2;
130
int mid_pointy = miny + leny/2;
131
132
for(int i=0;i<var;i++)
133
{
134
pts_diff[i].x = pts[i].x - mid_pointx;
135
pts_diff[i].y = pts[i].y - mid_pointy;
136
}
137
138
imshow("Source", img1);
139
}
140
141
if (event == EVENT_RBUTTONUP)
142
{
143
flag = var;
144
145
final = Mat::zeros(img0.size(),CV_8UC3);
146
res1 = Mat::zeros(img0.size(),CV_8UC1);
147
const Point* pts4[1] = {&pts[0]};
148
149
fillPoly(res1, pts4,&numpts, 1, Scalar(255, 255, 255), 8, 0);
150
bitwise_and(img0, img0, final,res1);
151
152
imshow("Source", img1);
153
154
if(num == 4)
155
{
156
colorChange(img0,res1,blend,red,green,blue);
157
imshow("Color Change Image", blend);
158
waitKey(0);
159
160
}
161
else if(num == 5)
162
{
163
illuminationChange(img0,res1,blend,alpha,beta);
164
imshow("Illum Change Image", blend);
165
waitKey(0);
166
}
167
else if(num == 6)
168
{
169
textureFlattening(img0,res1,blend,low_t,high_t,kernel_size);
170
imshow("Texture Flattened", blend);
171
waitKey(0);
172
}
173
174
}
175
if (event == EVENT_MBUTTONDOWN)
176
{
177
for(int i = 0; i < numpts ; i++)
178
{
179
pts[i].x=0;
180
pts[i].y=0;
181
}
182
var = 0;
183
flag1 = 0;
184
minx = INT_MAX; miny = INT_MAX; maxx = INT_MIN; maxy = INT_MIN;
185
imshow("Source", img0);
186
if(num == 1 || num == 2 || num == 3)
187
imshow("Destination",img2);
188
drag = 0;
189
}
190
}
191
192
void destination(int event, int x, int y, int, void*)
193
{
194
195
Mat im1;
196
minxd = INT_MAX; minyd = INT_MAX; maxxd = INT_MIN; maxyd = INT_MIN;
197
im1 = img2.clone();
198
if (event == EVENT_LBUTTONDOWN)
199
{
200
flag4 = 1;
201
if(flag1 == 1)
202
{
203
point = Point(x, y);
204
205
for(int i=0;i<var;i++)
206
{
207
pts2[i].x = point.x + pts_diff[i].x;
208
pts2[i].y = point.y + pts_diff[i].y;
209
}
210
211
for(int i=var;i<numpts;i++)
212
{
213
pts2[i].x = point.x + pts_diff[0].x;
214
pts2[i].y = point.y + pts_diff[0].y;
215
}
216
217
const Point* pts5[1] = {&pts2[0]};
218
polylines( im1, pts5, &numpts,1, 1, Scalar(0,0,255), 2, 8, 0);
219
220
destx = x;
221
desty = y;
222
223
imshow("Destination", im1);
224
}
225
}
226
if (event == EVENT_RBUTTONUP)
227
{
228
for(int i=0;i<flag;i++)
229
{
230
minxd = min(minxd,pts2[i].x);
231
maxxd = max(maxxd,pts2[i].x);
232
minyd = min(minyd,pts2[i].y);
233
maxyd = max(maxyd,pts2[i].y);
234
}
235
236
if(maxxd > im1.size().width || maxyd > im1.size().height || minxd < 0 || minyd < 0)
237
{
238
cout << "Index out of range" << endl;
239
exit(1);
240
}
241
242
final1 = Mat::zeros(img2.size(),CV_8UC3);
243
res = Mat::zeros(img2.size(),CV_8UC1);
244
for(int i=miny, k=minyd;i<(miny+leny);i++,k++)
245
for(int j=minx,l=minxd ;j<(minx+lenx);j++,l++)
246
{
247
for(int c=0;c<channel;c++)
248
{
249
final1.at<uchar>(k,l*channel+c) = final.at<uchar>(i,j*channel+c);
250
251
}
252
}
253
254
const Point* pts6[1] = {&pts2[0]};
255
fillPoly(res, pts6, &numpts, 1, Scalar(255, 255, 255), 8, 0);
256
257
if(num == 1 || num == 2 || num == 3)
258
{
259
seamlessClone(img0,img2,res1,point,blend,num);
260
imshow("Cloned Image", blend);
261
imwrite("cloned.png",blend);
262
waitKey(0);
263
}
264
265
for(int i = 0; i < flag ; i++)
266
{
267
pts2[i].x=0;
268
pts2[i].y=0;
269
}
270
271
minxd = INT_MAX; minyd = INT_MAX; maxxd = INT_MIN; maxyd = INT_MIN;
272
}
273
274
im1.release();
275
}
276
277
int main()
278
{
279
cout << endl;
280
cout << "Cloning Module" << endl;
281
cout << "---------------" << endl;
282
cout << "Step 1:" << endl;
283
cout << " -> In the source image, select the region of interest by left click mouse button. A Polygon ROI will be created by left clicking mouse button." << endl;
284
cout << " -> To set the Polygon ROI, click the right mouse button or use 'd' key" << endl;
285
cout << " -> To reset the region selected, click the middle mouse button or use 'r' key." << endl;
286
287
cout << "Step 2:" << endl;
288
cout << " -> In the destination image, select the point where you want to place the ROI in the image by left clicking mouse button." << endl;
289
cout << " -> To get the cloned result, click the right mouse button or use 'c' key." << endl;
290
cout << " -> To quit the program, use 'q' key." << endl;
291
cout << endl;
292
cout << "Options: " << endl;
293
cout << endl;
294
cout << "1) Normal Cloning " << endl;
295
cout << "2) Mixed Cloning " << endl;
296
cout << "3) Monochrome Transfer " << endl;
297
cout << "4) Local Color Change " << endl;
298
cout << "5) Local Illumination Change " << endl;
299
cout << "6) Texture Flattening " << endl;
300
301
cout << endl;
302
303
cout << "Press number 1-6 to choose from above techniques: ";
304
cin >> num;
305
cout << endl;
306
307
minx = INT_MAX; miny = INT_MAX; maxx = INT_MIN; maxy = INT_MIN;
308
309
minxd = INT_MAX; minyd = INT_MAX; maxxd = INT_MIN; maxyd = INT_MIN;
310
311
int flag3 = 0;
312
313
if(num == 1 || num == 2 || num == 3)
314
{
315
316
string src,dest;
317
cout << "Enter Source Image: ";
318
cin >> src;
319
320
cout << "Enter Destination Image: ";
321
cin >> dest;
322
323
img0 = imread(src);
324
325
img2 = imread(dest);
326
327
if(img0.empty())
328
{
329
cout << "Source Image does not exist" << endl;
330
exit(2);
331
}
332
if(img2.empty())
333
{
334
cout << "Destination Image does not exist" << endl;
335
exit(2);
336
}
337
338
channel = img0.channels();
339
340
res = Mat::zeros(img2.size(),CV_8UC1);
341
res1 = Mat::zeros(img0.size(),CV_8UC1);
342
final = Mat::zeros(img0.size(),CV_8UC3);
343
final1 = Mat::zeros(img2.size(),CV_8UC3);
344
//////////// source image ///////////////////
345
346
namedWindow("Source", 1);
347
setMouseCallback("Source", source, NULL);
348
imshow("Source", img0);
349
350
/////////// destination image ///////////////
351
352
namedWindow("Destination", 1);
353
setMouseCallback("Destination", destination, NULL);
354
imshow("Destination",img2);
355
356
}
357
else if(num == 4)
358
{
359
string src;
360
cout << "Enter Source Image: ";
361
cin >> src;
362
363
cout << "Enter RGB values: " << endl;
364
cout << "Red: ";
365
cin >> red;
366
367
cout << "Green: ";
368
cin >> green;
369
370
cout << "Blue: ";
371
cin >> blue;
372
373
img0 = imread(src);
374
375
if(img0.empty())
376
{
377
cout << "Source Image does not exist" << endl;
378
exit(2);
379
}
380
381
res1 = Mat::zeros(img0.size(),CV_8UC1);
382
final = Mat::zeros(img0.size(),CV_8UC3);
383
384
//////////// source image ///////////////////
385
386
namedWindow("Source", 1);
387
setMouseCallback("Source", source, NULL);
388
imshow("Source", img0);
389
390
}
391
else if(num == 5)
392
{
393
string src;
394
cout << "Enter Source Image: ";
395
cin >> src;
396
397
cout << "alpha: ";
398
cin >> alpha;
399
400
cout << "beta: ";
401
cin >> beta;
402
403
img0 = imread(src);
404
405
if(img0.empty())
406
{
407
cout << "Source Image does not exist" << endl;
408
exit(2);
409
}
410
411
res1 = Mat::zeros(img0.size(),CV_8UC1);
412
final = Mat::zeros(img0.size(),CV_8UC3);
413
414
//////////// source image ///////////////////
415
416
namedWindow("Source", 1);
417
setMouseCallback("Source", source, NULL);
418
imshow("Source", img0);
419
420
}
421
else if(num == 6)
422
{
423
string src;
424
cout << "Enter Source Image: ";
425
cin >> src;
426
427
cout << "low_threshold: ";
428
cin >> low_t;
429
430
cout << "high_threshold: ";
431
cin >> high_t;
432
433
cout << "kernel_size: ";
434
cin >> kernel_size;
435
436
img0 = imread(src);
437
438
if(img0.empty())
439
{
440
cout << "Source Image does not exist" << endl;
441
exit(2);
442
}
443
444
res1 = Mat::zeros(img0.size(),CV_8UC1);
445
final = Mat::zeros(img0.size(),CV_8UC3);
446
447
//////////// source image ///////////////////
448
449
namedWindow("Source", 1);
450
setMouseCallback("Source", source, NULL);
451
imshow("Source", img0);
452
}
453
else
454
{
455
cout << "Wrong Option Chosen" << endl;
456
exit(1);
457
}
458
459
for(;;)
460
{
461
char key = (char)waitKey(0);
462
463
if(key == 'd' && flag3 == 0)
464
{
465
flag1 = 1;
466
flag3 = 1;
467
img1 = img0.clone();
468
for(int i = var; i < numpts ; i++)
469
pts[i] = point;
470
471
if(var!=0)
472
{
473
const Point* pts3[1] = {&pts[0]};
474
polylines( img1, pts3, &numpts,1, 1, Scalar(0,0,0), 2, 8, 0);
475
}
476
477
for(int i=0;i<var;i++)
478
{
479
minx = min(minx,pts[i].x);
480
maxx = max(maxx,pts[i].x);
481
miny = min(miny,pts[i].y);
482
maxy = max(maxy,pts[i].y);
483
}
484
lenx = maxx - minx;
485
leny = maxy - miny;
486
487
int mid_pointx = minx + lenx/2;
488
int mid_pointy = miny + leny/2;
489
490
for(int i=0;i<var;i++)
491
{
492
pts_diff[i].x = pts[i].x - mid_pointx;
493
pts_diff[i].y = pts[i].y - mid_pointy;
494
}
495
496
flag = var;
497
498
final = Mat::zeros(img0.size(),CV_8UC3);
499
res1 = Mat::zeros(img0.size(),CV_8UC1);
500
const Point* pts4[1] = {&pts[0]};
501
502
fillPoly(res1, pts4,&numpts, 1, Scalar(255, 255, 255), 8, 0);
503
bitwise_and(img0, img0, final,res1);
504
505
imshow("Source", img1);
506
}
507
else if(key == 'r')
508
{
509
for(int i = 0; i < numpts ; i++)
510
{
511
pts[i].x=0;
512
pts[i].y=0;
513
}
514
var = 0;
515
flag1 = 0;
516
flag3 = 0;
517
flag4 = 0;
518
minx = INT_MAX; miny = INT_MAX; maxx = INT_MIN; maxy = INT_MIN;
519
imshow("Source", img0);
520
if(num == 1 || num == 2 || num == 3)
521
imshow("Destination",img2);
522
drag = 0;
523
}
524
else if ((num == 1 || num == 2 || num == 3) && key == 'c' && flag1 == 1 && flag4 == 1)
525
{
526
seamlessClone(img0,img2,res1,point,blend,num);
527
imshow("Cloned Image", blend);
528
imwrite("cloned.png",blend);
529
}
530
else if (num == 4 && key == 'c' && flag1 == 1)
531
{
532
colorChange(img0,res1,blend,red,green,blue);
533
imshow("Color Change Image", blend);
534
imwrite("cloned.png",blend);
535
}
536
else if (num == 5 && key == 'c' && flag1 == 1)
537
{
538
illuminationChange(img0,res1,blend,alpha,beta);
539
imshow("Illum Change Image", blend);
540
imwrite("cloned.png",blend);
541
}
542
else if (num == 6 && key == 'c' && flag1 == 1)
543
{
544
textureFlattening(img0,res1,blend,low_t,high_t,kernel_size);
545
imshow("Texture Flattened", blend);
546
imwrite("cloned.png",blend);
547
}
548
else if(key == 'q')
549
break;
550
}
551
return 0;
552
}
553
554