cloning_gui.cpp 14.2 KB
Newer Older
wester committed
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
/*
* cloning.cpp
*
* Author:
* Siddharth Kherada <siddharthkherada27[at]gmail[dot]com>
*
* This tutorial demonstrates how to use OpenCV seamless cloning
* module.
*
* 1- Normal Cloning
* 2- Mixed Cloning
* 3- Monochrome Transfer
* 4- Color Change
* 5- Illumination change
* 6- Texture Flattening

* The program takes as input a source and a destination image (for 1-3 methods)
* and ouputs the cloned image.

* Step 1:
* -> 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.
* -> To set the Polygon ROI, click the right mouse button or 'd' key.
* -> To reset the region selected, click the middle mouse button or 'r' key.

* Step 2:
* -> In the destination image, select the point where you want to place the ROI in the image by left clicking mouse button.
* -> To get the cloned result, click the right mouse button or 'c' key.
* -> To quit the program, use 'q' key.
*
* Result: The cloned image will be displayed.
*/

#include <signal.h>
#include "opencv2/photo.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/core.hpp"
#include <iostream>
#include <stdlib.h>

using namespace std;
using namespace cv;

Mat img0, img1, img2, res, res1, final, final1, blend;

Point point;
int drag = 0;
int destx, desty;

int numpts = 100;
Point* pts = new Point[100];
Point* pts2 = new Point[100];
Point* pts_diff = new Point[100];

int var = 0;
int flag = 0, flag1 = 0, flag4 = 0;

int minx, miny, maxx, maxy, lenx, leny;
int minxd, minyd, maxxd, maxyd, lenxd, lenyd;

int channel, num, kernel_size;

float alpha,beta;

float red, green, blue;

float low_t, high_t;

void source(int, int, int, int, void*);
void destination(int, int, int, int, void*);
void checkfile(char*);

void source(int event, int x, int y, int, void*)
{

    if (event == EVENT_LBUTTONDOWN && !drag)
    {
        if(flag1 == 0)
        {
            if(var==0)
                img1 = img0.clone();
            point = Point(x, y);
            circle(img1,point,2,Scalar(0, 0, 255),-1, 8, 0);
            pts[var] = point;
            var++;
            drag  = 1;
            if(var>1)
                line(img1,pts[var-2], point, Scalar(0, 0, 255), 2, 8, 0);

            imshow("Source", img1);
        }
    }

    if (event == EVENT_LBUTTONUP && drag)
    {
        imshow("Source", img1);

        drag = 0;
    }
    if (event == EVENT_RBUTTONDOWN)
    {
        flag1 = 1;
        img1 = img0.clone();
        for(int i = var; i < numpts ; i++)
            pts[i] = point;

        if(var!=0)
        {
            const Point* pts3[1] = {&pts[0]};
            polylines( img1, pts3, &numpts,1, 1, Scalar(0,0,0), 2, 8, 0);
        }

        for(int i=0;i<var;i++)
        {
            minx = min(minx,pts[i].x);
            maxx = max(maxx,pts[i].x);
            miny = min(miny,pts[i].y);
            maxy = max(maxy,pts[i].y);
        }
        lenx = maxx - minx;
        leny = maxy - miny;

        int mid_pointx = minx + lenx/2;
        int mid_pointy = miny + leny/2;

        for(int i=0;i<var;i++)
        {
            pts_diff[i].x = pts[i].x - mid_pointx;
            pts_diff[i].y = pts[i].y - mid_pointy;
        }

        imshow("Source", img1);
    }

    if (event == EVENT_RBUTTONUP)
    {
        flag = var;

        final = Mat::zeros(img0.size(),CV_8UC3);
        res1 = Mat::zeros(img0.size(),CV_8UC1);
        const Point* pts4[1] = {&pts[0]};

        fillPoly(res1, pts4,&numpts, 1, Scalar(255, 255, 255), 8, 0);
        bitwise_and(img0, img0, final,res1);

        imshow("Source", img1);

        if(num == 4)
        {
            colorChange(img0,res1,blend,red,green,blue);
            imshow("Color Change Image", blend);
            waitKey(0);

        }
        else if(num == 5)
        {
            illuminationChange(img0,res1,blend,alpha,beta);
            imshow("Illum Change Image", blend);
            waitKey(0);
        }
        else if(num == 6)
        {
            textureFlattening(img0,res1,blend,low_t,high_t,kernel_size);
            imshow("Texture Flattened", blend);
            waitKey(0);
        }

    }
    if (event == EVENT_MBUTTONDOWN)
    {
        for(int i = 0; i < numpts ; i++)
        {
            pts[i].x=0;
            pts[i].y=0;
        }
        var = 0;
        flag1 = 0;
        minx = INT_MAX; miny = INT_MAX; maxx = INT_MIN; maxy = INT_MIN;
        imshow("Source", img0);
        if(num == 1 || num == 2 || num == 3)
            imshow("Destination",img2);
        drag = 0;
    }
}

void destination(int event, int x, int y, int, void*)
{

    Mat im1;
    minxd = INT_MAX; minyd = INT_MAX; maxxd = INT_MIN; maxyd = INT_MIN;
    im1 = img2.clone();
    if (event == EVENT_LBUTTONDOWN)
    {
        flag4 = 1;
        if(flag1 == 1)
        {
            point = Point(x, y);

            for(int i=0;i<var;i++)
            {
                pts2[i].x = point.x + pts_diff[i].x;
                pts2[i].y = point.y + pts_diff[i].y;
            }

            for(int i=var;i<numpts;i++)
            {
                pts2[i].x = point.x + pts_diff[0].x;
                pts2[i].y = point.y + pts_diff[0].y;
            }

            const Point* pts5[1] = {&pts2[0]};
            polylines( im1, pts5, &numpts,1, 1, Scalar(0,0,255), 2, 8, 0);

            destx = x;
            desty = y;

            imshow("Destination", im1);
        }
    }
    if (event == EVENT_RBUTTONUP)
    {
        for(int i=0;i<flag;i++)
        {
            minxd = min(minxd,pts2[i].x);
            maxxd = max(maxxd,pts2[i].x);
            minyd = min(minyd,pts2[i].y);
            maxyd = max(maxyd,pts2[i].y);
        }

        if(maxxd > im1.size().width || maxyd > im1.size().height || minxd < 0 || minyd < 0)
        {
            cout << "Index out of range" << endl;
            exit(1);
        }

        final1 = Mat::zeros(img2.size(),CV_8UC3);
        res = Mat::zeros(img2.size(),CV_8UC1);
        for(int i=miny, k=minyd;i<(miny+leny);i++,k++)
            for(int j=minx,l=minxd ;j<(minx+lenx);j++,l++)
            {
                for(int c=0;c<channel;c++)
                {
                    final1.at<uchar>(k,l*channel+c) = final.at<uchar>(i,j*channel+c);

                }
            }

        const Point* pts6[1] = {&pts2[0]};
        fillPoly(res, pts6, &numpts, 1, Scalar(255, 255, 255), 8, 0);

        if(num == 1 || num == 2 || num == 3)
        {
            seamlessClone(img0,img2,res1,point,blend,num);
            imshow("Cloned Image", blend);
            imwrite("cloned.png",blend);
            waitKey(0);
        }

        for(int i = 0; i < flag ; i++)
        {
            pts2[i].x=0;
            pts2[i].y=0;
        }

        minxd = INT_MAX; minyd = INT_MAX; maxxd = INT_MIN; maxyd = INT_MIN;
    }

    im1.release();
}

int main()
{
    cout << endl;
    cout << "Cloning Module" << endl;
    cout << "---------------" << endl;
    cout << "Step 1:" << endl;
    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;
    cout << " -> To set the Polygon ROI, click the right mouse button or use 'd' key" << endl;
    cout << " -> To reset the region selected, click the middle mouse button or use 'r' key." << endl;

    cout << "Step 2:" << endl;
    cout << " -> In the destination image, select the point where you want to place the ROI in the image by left clicking mouse button." << endl;
    cout << " -> To get the cloned result, click the right mouse button or use 'c' key." << endl;
    cout << " -> To quit the program, use 'q' key." << endl;
    cout << endl;
    cout << "Options: " << endl;
    cout << endl;
    cout << "1) Normal Cloning " << endl;
    cout << "2) Mixed Cloning " << endl;
    cout << "3) Monochrome Transfer " << endl;
    cout << "4) Local Color Change " << endl;
    cout << "5) Local Illumination Change " << endl;
    cout << "6) Texture Flattening " << endl;

    cout << endl;

    cout << "Press number 1-6 to choose from above techniques: ";
    cin >> num;
    cout << endl;

    minx = INT_MAX; miny = INT_MAX; maxx = INT_MIN; maxy = INT_MIN;

    minxd = INT_MAX; minyd = INT_MAX; maxxd = INT_MIN; maxyd = INT_MIN;

    int flag3 = 0;

    if(num == 1 || num == 2 || num == 3)
    {

        string src,dest;
        cout << "Enter Source Image: ";
        cin >> src;

        cout << "Enter Destination Image: ";
        cin >> dest;

        img0 = imread(src);

        img2 = imread(dest);

        if(img0.empty())
        {
            cout << "Source Image does not exist" << endl;
            exit(2);
        }
        if(img2.empty())
        {
            cout << "Destination Image does not exist" << endl;
            exit(2);
        }

        channel = img0.channels();

        res = Mat::zeros(img2.size(),CV_8UC1);
        res1 = Mat::zeros(img0.size(),CV_8UC1);
        final = Mat::zeros(img0.size(),CV_8UC3);
        final1 = Mat::zeros(img2.size(),CV_8UC3);
        //////////// source image ///////////////////

        namedWindow("Source", 1);
        setMouseCallback("Source", source, NULL);
        imshow("Source", img0);

        /////////// destination image ///////////////

        namedWindow("Destination", 1);
        setMouseCallback("Destination", destination, NULL);
        imshow("Destination",img2);

    }
    else if(num == 4)
    {
        string src;
        cout << "Enter Source Image: ";
        cin >> src;

        cout << "Enter RGB values: " << endl;
        cout << "Red: ";
        cin >> red;

        cout << "Green: ";
        cin >> green;

        cout << "Blue: ";
        cin >> blue;

        img0 = imread(src);

        if(img0.empty())
        {
            cout << "Source Image does not exist" << endl;
            exit(2);
        }

        res1 = Mat::zeros(img0.size(),CV_8UC1);
        final = Mat::zeros(img0.size(),CV_8UC3);

        //////////// source image ///////////////////

        namedWindow("Source", 1);
        setMouseCallback("Source", source, NULL);
        imshow("Source", img0);

    }
    else if(num == 5)
    {
        string src;
        cout << "Enter Source Image: ";
        cin >> src;

        cout << "alpha: ";
        cin >> alpha;

        cout << "beta: ";
        cin >> beta;

        img0 = imread(src);

        if(img0.empty())
        {
            cout << "Source Image does not exist" << endl;
            exit(2);
        }

        res1 = Mat::zeros(img0.size(),CV_8UC1);
        final = Mat::zeros(img0.size(),CV_8UC3);

        //////////// source image ///////////////////

        namedWindow("Source", 1);
        setMouseCallback("Source", source, NULL);
        imshow("Source", img0);

    }
    else if(num == 6)
    {
        string src;
        cout << "Enter Source Image: ";
        cin >> src;

        cout << "low_threshold: ";
        cin >> low_t;

        cout << "high_threshold: ";
        cin >> high_t;

        cout << "kernel_size: ";
        cin >> kernel_size;

        img0 = imread(src);

        if(img0.empty())
        {
            cout << "Source Image does not exist" << endl;
            exit(2);
        }

        res1 = Mat::zeros(img0.size(),CV_8UC1);
        final = Mat::zeros(img0.size(),CV_8UC3);

        //////////// source image ///////////////////

        namedWindow("Source", 1);
        setMouseCallback("Source", source, NULL);
        imshow("Source", img0);
    }
    else
    {
        cout << "Wrong Option Choosen" << endl;
        exit(1);
    }

    for(;;)
    {
a  
Kai Westerkamp committed
456
        char key = (char) waitKey(0);
wester committed
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 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547

        if(key == 'd' && flag3 == 0)
        {
            flag1 = 1;
            flag3 = 1;
            img1 = img0.clone();
            for(int i = var; i < numpts ; i++)
                pts[i] = point;

            if(var!=0)
            {
                const Point* pts3[1] = {&pts[0]};
                polylines( img1, pts3, &numpts,1, 1, Scalar(0,0,0), 2, 8, 0);
            }

            for(int i=0;i<var;i++)
            {
                minx = min(minx,pts[i].x);
                maxx = max(maxx,pts[i].x);
                miny = min(miny,pts[i].y);
                maxy = max(maxy,pts[i].y);
            }
            lenx = maxx - minx;
            leny = maxy - miny;

            int mid_pointx = minx + lenx/2;
            int mid_pointy = miny + leny/2;

            for(int i=0;i<var;i++)
            {
                pts_diff[i].x = pts[i].x - mid_pointx;
                pts_diff[i].y = pts[i].y - mid_pointy;
            }

            flag = var;

            final = Mat::zeros(img0.size(),CV_8UC3);
            res1 = Mat::zeros(img0.size(),CV_8UC1);
            const Point* pts4[1] = {&pts[0]};

            fillPoly(res1, pts4,&numpts, 1, Scalar(255, 255, 255), 8, 0);
            bitwise_and(img0, img0, final,res1);

            imshow("Source", img1);
        }
        else if(key == 'r')
        {
            for(int i = 0; i < numpts ; i++)
            {
                pts[i].x=0;
                pts[i].y=0;
            }
            var = 0;
            flag1 = 0;
            flag3 = 0;
            flag4 = 0;
            minx = INT_MAX; miny = INT_MAX; maxx = INT_MIN; maxy = INT_MIN;
            imshow("Source", img0);
            if(num == 1 || num == 2 || num == 3)
                imshow("Destination",img2);
            drag = 0;
        }
        else if ((num == 1 || num == 2 || num == 3) && key == 'c' && flag1 == 1 && flag4 == 1)
        {
            seamlessClone(img0,img2,res1,point,blend,num);
            imshow("Cloned Image", blend);
            imwrite("cloned.png",blend);
        }
        else if (num == 4 && key == 'c' && flag1 == 1)
        {
            colorChange(img0,res1,blend,red,green,blue);
            imshow("Color Change Image", blend);
            imwrite("cloned.png",blend);
        }
        else if (num == 5 && key == 'c' && flag1 == 1)
        {
            illuminationChange(img0,res1,blend,alpha,beta);
            imshow("Illum Change Image", blend);
            imwrite("cloned.png",blend);
        }
        else if (num == 6 && key == 'c' && flag1 == 1)
        {
            textureFlattening(img0,res1,blend,low_t,high_t,kernel_size);
            imshow("Texture Flattened", blend);
            imwrite("cloned.png",blend);
        }
        else if(key == 'q')
            break;
    }
    return 0;
}