2013-08-05 14:42:28 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of the copyright holders may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
2013-07-12 11:48:37 +08:00
|
|
|
#include "precomp.hpp"
|
|
|
|
#include "opencv2/photo.hpp"
|
|
|
|
#include <iostream>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "seamless_cloning.hpp"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace cv;
|
|
|
|
|
2013-08-05 14:42:28 +08:00
|
|
|
void cv::seamlessClone(InputArray _src, InputArray _dst, InputArray _mask, Point p, OutputArray _blend, int flags)
|
2013-07-12 11:48:37 +08:00
|
|
|
{
|
2013-09-03 23:42:05 +08:00
|
|
|
Mat src = _src.getMat();
|
|
|
|
Mat dest = _dst.getMat();
|
|
|
|
Mat mask = _mask.getMat();
|
|
|
|
_blend.create(dest.size(), CV_8UC3);
|
|
|
|
Mat blend = _blend.getMat();
|
2013-08-05 14:42:28 +08:00
|
|
|
|
|
|
|
int minx = INT_MAX, miny = INT_MAX, maxx = INT_MIN, maxy = INT_MIN;
|
|
|
|
int h = mask.size().height;
|
|
|
|
int w = mask.size().width;
|
|
|
|
|
|
|
|
Mat gray = Mat(mask.size(),CV_8UC1);
|
|
|
|
Mat dst_mask = Mat::zeros(dest.size(),CV_8UC1);
|
|
|
|
Mat cs_mask = Mat::zeros(src.size(),CV_8UC3);
|
|
|
|
Mat cd_mask = Mat::zeros(dest.size(),CV_8UC3);
|
|
|
|
|
|
|
|
if(mask.channels() == 3)
|
|
|
|
cvtColor(mask, gray, COLOR_BGR2GRAY );
|
|
|
|
else
|
|
|
|
gray = mask;
|
|
|
|
|
|
|
|
for(int i=0;i<h;i++)
|
|
|
|
{
|
|
|
|
for(int j=0;j<w;j++)
|
|
|
|
{
|
|
|
|
if(gray.at<uchar>(i,j) == 255)
|
|
|
|
{
|
|
|
|
minx = std::min(minx,i);
|
|
|
|
maxx = std::max(maxx,i);
|
|
|
|
miny = std::min(miny,j);
|
|
|
|
maxy = std::max(maxy,j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int lenx = maxx - minx;
|
|
|
|
int leny = maxy - miny;
|
|
|
|
|
|
|
|
int minxd = p.y - lenx/2;
|
|
|
|
int maxxd = p.y + lenx/2;
|
|
|
|
int minyd = p.x - leny/2;
|
|
|
|
int maxyd = p.x + leny/2;
|
|
|
|
|
|
|
|
if(minxd < 0 || minyd < 0 || maxxd > dest.size().height || maxyd > dest.size().width)
|
|
|
|
{
|
|
|
|
cout << "Index out of range" << endl;
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i=minx, k=minxd;i<(minx+lenx);i++,k++)
|
|
|
|
for(int j=miny,l=minyd ;j<(miny+leny);j++,l++)
|
|
|
|
{
|
|
|
|
dst_mask.at<uchar>(k,l) = gray.at<uchar>(i,j);
|
|
|
|
}
|
|
|
|
|
|
|
|
int channel = 3;
|
|
|
|
|
|
|
|
for(int i=minx;i<(minx+lenx);i++)
|
|
|
|
for(int j=miny;j<(miny+leny);j++)
|
|
|
|
{
|
|
|
|
for(int c=0;c<3;c++)
|
|
|
|
{
|
|
|
|
if(gray.at<uchar>(i,j) == 255)
|
|
|
|
cs_mask.at<uchar>(i,j*channel+c) = src.at<uchar>(i,j*channel+c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i=minx, k=minxd;i<(minx+lenx);i++,k++)
|
|
|
|
for(int j=miny,l=minyd ;j<(miny+leny);j++,l++)
|
|
|
|
{
|
|
|
|
for(int c=0;c<channel;c++)
|
|
|
|
cd_mask.at<uchar>(k,l*channel+c) = cs_mask.at<uchar>(i,j*channel+c);
|
|
|
|
}
|
|
|
|
|
|
|
|
Cloning obj;
|
|
|
|
obj.normal_clone(dest,cd_mask,dst_mask,blend,flags);
|
2013-07-12 11:48:37 +08:00
|
|
|
}
|
|
|
|
|
2013-08-05 14:42:28 +08:00
|
|
|
void cv::colorChange(InputArray _src, InputArray _mask, OutputArray _dst, float r, float g, float b)
|
2013-07-12 11:48:37 +08:00
|
|
|
{
|
2013-09-03 23:42:05 +08:00
|
|
|
Mat src = _src.getMat();
|
|
|
|
Mat mask = _mask.getMat();
|
|
|
|
_dst.create(src.size(), src.type());
|
|
|
|
Mat blend = _dst.getMat();
|
|
|
|
|
|
|
|
float red = r;
|
|
|
|
float green = g;
|
|
|
|
float blue = b;
|
2013-08-05 14:42:28 +08:00
|
|
|
|
|
|
|
Mat gray = Mat::zeros(mask.size(),CV_8UC1);
|
|
|
|
|
2013-08-19 15:46:04 +08:00
|
|
|
if(mask.channels() == 3)
|
|
|
|
cvtColor(mask, gray, COLOR_BGR2GRAY );
|
|
|
|
else
|
|
|
|
gray = mask;
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-08-05 14:42:28 +08:00
|
|
|
Mat cs_mask = Mat::zeros(src.size(),CV_8UC3);
|
|
|
|
|
|
|
|
int channel = 3;
|
|
|
|
for(int i=0;i<mask.size().height;i++)
|
|
|
|
for(int j=0;j<mask.size().width;j++)
|
|
|
|
{
|
|
|
|
if(gray.at<uchar>(i,j) == 255)
|
|
|
|
{
|
|
|
|
for(int c=0;c<channel;c++)
|
|
|
|
{
|
|
|
|
cs_mask.at<uchar>(i,j*channel+c) = src.at<uchar>(i,j*channel+c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Cloning obj;
|
|
|
|
obj.local_color_change(src,cs_mask,gray,blend,red,green,blue);
|
2013-07-12 11:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-05 14:42:28 +08:00
|
|
|
void cv::illuminationChange(InputArray _src, InputArray _mask, OutputArray _dst, float a, float b)
|
2013-07-12 11:48:37 +08:00
|
|
|
{
|
|
|
|
|
2013-09-03 23:42:05 +08:00
|
|
|
Mat src = _src.getMat();
|
|
|
|
Mat mask = _mask.getMat();
|
|
|
|
_dst.create(src.size(), src.type());
|
|
|
|
Mat blend = _dst.getMat();
|
|
|
|
float alpha = a;
|
|
|
|
float beta = b;
|
2013-08-05 14:42:28 +08:00
|
|
|
|
|
|
|
Mat gray = Mat::zeros(mask.size(),CV_8UC1);
|
2013-08-19 15:46:04 +08:00
|
|
|
|
|
|
|
if(mask.channels() == 3)
|
|
|
|
cvtColor(mask, gray, COLOR_BGR2GRAY );
|
|
|
|
else
|
|
|
|
gray = mask;
|
2013-08-05 14:42:28 +08:00
|
|
|
|
|
|
|
Mat cs_mask = Mat::zeros(src.size(),CV_8UC3);
|
|
|
|
|
|
|
|
int channel = 3;
|
|
|
|
for(int i=0;i<mask.size().height;i++)
|
|
|
|
for(int j=0;j<mask.size().width;j++)
|
|
|
|
{
|
|
|
|
if(gray.at<uchar>(i,j) == 255)
|
|
|
|
{
|
|
|
|
for(int c=0;c<channel;c++)
|
|
|
|
{
|
|
|
|
cs_mask.at<uchar>(i,j*channel+c) = src.at<uchar>(i,j*channel+c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-08-05 14:42:28 +08:00
|
|
|
Cloning obj;
|
|
|
|
obj.illum_change(src,cs_mask,gray,blend,alpha,beta);
|
2013-07-12 11:48:37 +08:00
|
|
|
|
|
|
|
}
|
2013-08-19 15:46:04 +08:00
|
|
|
|
2013-09-03 23:42:05 +08:00
|
|
|
void cv::textureFlattening(InputArray _src, InputArray _mask, OutputArray _dst,
|
|
|
|
double low_threshold, double high_threshold, int kernel_size)
|
2013-07-12 11:48:37 +08:00
|
|
|
{
|
|
|
|
|
2013-09-03 23:42:05 +08:00
|
|
|
Mat src = _src.getMat();
|
|
|
|
Mat mask = _mask.getMat();
|
|
|
|
_dst.create(src.size(), src.type());
|
|
|
|
Mat blend = _dst.getMat();
|
2013-07-12 11:48:37 +08:00
|
|
|
|
2013-08-19 15:46:04 +08:00
|
|
|
Mat gray = Mat::zeros(mask.size(),CV_8UC1);
|
|
|
|
|
|
|
|
if(mask.channels() == 3)
|
|
|
|
cvtColor(mask, gray, COLOR_BGR2GRAY );
|
|
|
|
else
|
|
|
|
gray = mask;
|
|
|
|
|
|
|
|
Mat cs_mask = Mat::zeros(src.size(),CV_8UC3);
|
|
|
|
|
|
|
|
int channel = 3;
|
|
|
|
for(int i=0;i<mask.size().height;i++)
|
|
|
|
for(int j=0;j<mask.size().width;j++)
|
|
|
|
{
|
|
|
|
if(gray.at<uchar>(i,j) == 255)
|
|
|
|
{
|
|
|
|
for(int c=0;c<channel;c++)
|
|
|
|
{
|
|
|
|
cs_mask.at<uchar>(i,j*channel+c) = src.at<uchar>(i,j*channel+c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-09-03 23:42:05 +08:00
|
|
|
|
2013-08-19 15:46:04 +08:00
|
|
|
Cloning obj;
|
2013-09-03 23:42:05 +08:00
|
|
|
obj.texture_flatten(src,cs_mask,gray,low_threshold,high_threshold,kernel_size,blend);
|
2013-07-12 11:48:37 +08:00
|
|
|
}
|