5 * Siddharth Kherada <siddharthkherada27[at]gmail[dot]com>
7 * This tutorial demonstrates how to use OpenCV seamless cloning
12 * 3- Monochrome Transfer
14 * 5- Illumination change
15 * 6- Texture Flattening
17 * The program takes as input a source and a destination image (for 1-3 methods)
18 * and ouputs the cloned image.
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.
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.
30 * Result: The cloned image will be displayed.
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"
45 Mat img0, img1, img2, res, res1, final, final1, blend;
52 Point* pts = new Point[100];
53 Point* pts2 = new Point[100];
54 Point* pts_diff = new Point[100];
57 int flag = 0, flag1 = 0, flag4 = 0;
59 int minx, miny, maxx, maxy, lenx, leny;
60 int minxd, minyd, maxxd, maxyd, lenxd, lenyd;
62 int channel, num, kernel_size;
66 float red, green, blue;
70 void source(int, int, int, int, void*);
71 void destination(int, int, int, int, void*);
72 void checkfile(char*);
74 void source(int event, int x, int y, int, void*)
77 if (event == EVENT_LBUTTONDOWN && !drag)
84 circle(img1,point,2,Scalar(0, 0, 255),-1, 8, 0);
89 line(img1,pts[var-2], point, Scalar(0, 0, 255), 2, 8, 0);
91 imshow("Source", img1);
95 if (event == EVENT_LBUTTONUP && drag)
97 imshow("Source", img1);
101 if (event == EVENT_RBUTTONDOWN)
105 for(int i = var; i < numpts ; i++)
110 const Point* pts3[1] = {&pts[0]};
111 polylines( img1, pts3, &numpts,1, 1, Scalar(0,0,0), 2, 8, 0);
114 for(int i=0;i<var;i++)
116 minx = min(minx,pts[i].x);
117 maxx = max(maxx,pts[i].x);
118 miny = min(miny,pts[i].y);
119 maxy = max(maxy,pts[i].y);
124 int mid_pointx = minx + lenx/2;
125 int mid_pointy = miny + leny/2;
127 for(int i=0;i<var;i++)
129 pts_diff[i].x = pts[i].x - mid_pointx;
130 pts_diff[i].y = pts[i].y - mid_pointy;
133 imshow("Source", img1);
136 if (event == EVENT_RBUTTONUP)
140 final = Mat::zeros(img0.size(),CV_8UC3);
141 res1 = Mat::zeros(img0.size(),CV_8UC1);
142 const Point* pts4[1] = {&pts[0]};
144 fillPoly(res1, pts4,&numpts, 1, Scalar(255, 255, 255), 8, 0);
145 bitwise_and(img0, img0, final,res1);
147 imshow("Source", img1);
151 colorChange(img0,res1,blend,red,green,blue);
152 imshow("Color Change Image", blend);
158 illuminationChange(img0,res1,blend,alpha,beta);
159 imshow("Illum Change Image", blend);
164 textureFlattening(img0,res1,blend,low_t,high_t,kernel_size);
165 imshow("Texture Flattened", blend);
170 if (event == EVENT_MBUTTONDOWN)
172 for(int i = 0; i < numpts ; i++)
179 minx = INT_MAX; miny = INT_MAX; maxx = INT_MIN; maxy = INT_MIN;
180 imshow("Source", img0);
181 if(num == 1 || num == 2 || num == 3)
182 imshow("Destination",img2);
187 void destination(int event, int x, int y, int, void*)
191 minxd = INT_MAX; minyd = INT_MAX; maxxd = INT_MIN; maxyd = INT_MIN;
193 if (event == EVENT_LBUTTONDOWN)
200 for(int i=0;i<var;i++)
202 pts2[i].x = point.x + pts_diff[i].x;
203 pts2[i].y = point.y + pts_diff[i].y;
206 for(int i=var;i<numpts;i++)
208 pts2[i].x = point.x + pts_diff[0].x;
209 pts2[i].y = point.y + pts_diff[0].y;
212 const Point* pts5[1] = {&pts2[0]};
213 polylines( im1, pts5, &numpts,1, 1, Scalar(0,0,255), 2, 8, 0);
218 imshow("Destination", im1);
221 if (event == EVENT_RBUTTONUP)
223 for(int i=0;i<flag;i++)
225 minxd = min(minxd,pts2[i].x);
226 maxxd = max(maxxd,pts2[i].x);
227 minyd = min(minyd,pts2[i].y);
228 maxyd = max(maxyd,pts2[i].y);
231 if(maxxd > im1.size().width || maxyd > im1.size().height || minxd < 0 || minyd < 0)
233 cout << "Index out of range" << endl;
237 final1 = Mat::zeros(img2.size(),CV_8UC3);
238 res = Mat::zeros(img2.size(),CV_8UC1);
239 for(int i=miny, k=minyd;i<(miny+leny);i++,k++)
240 for(int j=minx,l=minxd ;j<(minx+lenx);j++,l++)
242 for(int c=0;c<channel;c++)
244 final1.at<uchar>(k,l*channel+c) = final.at<uchar>(i,j*channel+c);
249 const Point* pts6[1] = {&pts2[0]};
250 fillPoly(res, pts6, &numpts, 1, Scalar(255, 255, 255), 8, 0);
252 if(num == 1 || num == 2 || num == 3)
254 seamlessClone(img0,img2,res1,point,blend,num);
255 imshow("Cloned Image", blend);
256 imwrite("cloned.png",blend);
260 for(int i = 0; i < flag ; i++)
266 minxd = INT_MAX; minyd = INT_MAX; maxxd = INT_MIN; maxyd = INT_MIN;
275 cout << "Cloning Module" << endl;
276 cout << "---------------" << endl;
277 cout << "Step 1:" << endl;
278 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;
279 cout << " -> To set the Polygon ROI, click the right mouse button or use 'd' key" << endl;
280 cout << " -> To reset the region selected, click the middle mouse button or use 'r' key." << endl;
282 cout << "Step 2:" << endl;
283 cout << " -> In the destination image, select the point where you want to place the ROI in the image by left clicking mouse button." << endl;
284 cout << " -> To get the cloned result, click the right mouse button or use 'c' key." << endl;
285 cout << " -> To quit the program, use 'q' key." << endl;
287 cout << "Options: " << endl;
289 cout << "1) Normal Cloning " << endl;
290 cout << "2) Mixed Cloning " << endl;
291 cout << "3) Monochrome Transfer " << endl;
292 cout << "4) Local Color Change " << endl;
293 cout << "5) Local Illumination Change " << endl;
294 cout << "6) Texture Flattening " << endl;
298 cout << "Press number 1-6 to choose from above techniques: ";
302 minx = INT_MAX; miny = INT_MAX; maxx = INT_MIN; maxy = INT_MIN;
304 minxd = INT_MAX; minyd = INT_MAX; maxxd = INT_MIN; maxyd = INT_MIN;
308 if(num == 1 || num == 2 || num == 3)
312 cout << "Enter Source Image: ";
315 cout << "Enter Destination Image: ";
324 cout << "Source Image does not exist" << endl;
329 cout << "Destination Image does not exist" << endl;
333 channel = img0.channels();
335 res = Mat::zeros(img2.size(),CV_8UC1);
336 res1 = Mat::zeros(img0.size(),CV_8UC1);
337 final = Mat::zeros(img0.size(),CV_8UC3);
338 final1 = Mat::zeros(img2.size(),CV_8UC3);
339 //////////// source image ///////////////////
341 namedWindow("Source", 1);
342 setMouseCallback("Source", source, NULL);
343 imshow("Source", img0);
345 /////////// destination image ///////////////
347 namedWindow("Destination", 1);
348 setMouseCallback("Destination", destination, NULL);
349 imshow("Destination",img2);
355 cout << "Enter Source Image: ";
358 cout << "Enter RGB values: " << endl;
372 cout << "Source Image does not exist" << endl;
376 res1 = Mat::zeros(img0.size(),CV_8UC1);
377 final = Mat::zeros(img0.size(),CV_8UC3);
379 //////////// source image ///////////////////
381 namedWindow("Source", 1);
382 setMouseCallback("Source", source, NULL);
383 imshow("Source", img0);
389 cout << "Enter Source Image: ";
402 cout << "Source Image does not exist" << endl;
406 res1 = Mat::zeros(img0.size(),CV_8UC1);
407 final = Mat::zeros(img0.size(),CV_8UC3);
409 //////////// source image ///////////////////
411 namedWindow("Source", 1);
412 setMouseCallback("Source", source, NULL);
413 imshow("Source", img0);
419 cout << "Enter Source Image: ";
422 cout << "low_threshold: ";
425 cout << "high_threshold: ";
428 cout << "kernel_size: ";
435 cout << "Source Image does not exist" << endl;
439 res1 = Mat::zeros(img0.size(),CV_8UC1);
440 final = Mat::zeros(img0.size(),CV_8UC3);
442 //////////// source image ///////////////////
444 namedWindow("Source", 1);
445 setMouseCallback("Source", source, NULL);
446 imshow("Source", img0);
450 cout << "Wrong Option Choosen" << endl;
456 char key = (char) waitKey(0);
458 if(key == 'd' && flag3 == 0)
463 for(int i = var; i < numpts ; i++)
468 const Point* pts3[1] = {&pts[0]};
469 polylines( img1, pts3, &numpts,1, 1, Scalar(0,0,0), 2, 8, 0);
472 for(int i=0;i<var;i++)
474 minx = min(minx,pts[i].x);
475 maxx = max(maxx,pts[i].x);
476 miny = min(miny,pts[i].y);
477 maxy = max(maxy,pts[i].y);
482 int mid_pointx = minx + lenx/2;
483 int mid_pointy = miny + leny/2;
485 for(int i=0;i<var;i++)
487 pts_diff[i].x = pts[i].x - mid_pointx;
488 pts_diff[i].y = pts[i].y - mid_pointy;
493 final = Mat::zeros(img0.size(),CV_8UC3);
494 res1 = Mat::zeros(img0.size(),CV_8UC1);
495 const Point* pts4[1] = {&pts[0]};
497 fillPoly(res1, pts4,&numpts, 1, Scalar(255, 255, 255), 8, 0);
498 bitwise_and(img0, img0, final,res1);
500 imshow("Source", img1);
504 for(int i = 0; i < numpts ; i++)
513 minx = INT_MAX; miny = INT_MAX; maxx = INT_MIN; maxy = INT_MIN;
514 imshow("Source", img0);
515 if(num == 1 || num == 2 || num == 3)
516 imshow("Destination",img2);
519 else if ((num == 1 || num == 2 || num == 3) && key == 'c' && flag1 == 1 && flag4 == 1)
521 seamlessClone(img0,img2,res1,point,blend,num);
522 imshow("Cloned Image", blend);
523 imwrite("cloned.png",blend);
525 else if (num == 4 && key == 'c' && flag1 == 1)
527 colorChange(img0,res1,blend,red,green,blue);
528 imshow("Color Change Image", blend);
529 imwrite("cloned.png",blend);
531 else if (num == 5 && key == 'c' && flag1 == 1)
533 illuminationChange(img0,res1,blend,alpha,beta);
534 imshow("Illum Change Image", blend);
535 imwrite("cloned.png",blend);
537 else if (num == 6 && key == 'c' && flag1 == 1)
539 textureFlattening(img0,res1,blend,low_t,high_t,kernel_size);
540 imshow("Texture Flattened", blend);
541 imwrite("cloned.png",blend);