Skip to content

Commit

Permalink
Update seamless_cloning.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
sturkmen72 committed Sep 18, 2018
1 parent 43f889a commit 30597e5
Showing 1 changed file with 34 additions and 70 deletions.
104 changes: 34 additions & 70 deletions modules/photo/src/seamless_cloning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,64 +47,45 @@
using namespace std;
using namespace cv;

void cv::seamlessClone(InputArray _src, InputArray _dst, InputArray _mask, Point p, OutputArray _blend, int flags)
static Mat checkMask(InputArray _mask, Size size)
{
CV_INSTRUMENT_REGION();

const Mat src = _src.getMat();
const Mat dest = _dst.getMat();
const Mat mask = _mask.getMat();
dest.copyTo(_blend);
Mat blend = _blend.getMat();

Mat mask = _mask.getMat();
Mat gray;

if(mask.channels() == 3)
cvtColor(mask, gray, COLOR_BGR2GRAY );
if (mask.channels() == 3)
cvtColor(mask, gray, COLOR_BGR2GRAY);
else
{
if (mask.empty())
gray = Mat(src.rows, src.cols, CV_8UC1, Scalar(255));
gray = Mat(size.height, size.width, CV_8UC1, Scalar(255));
else
mask.copyTo(gray);
}

Mat gray_inner = gray(Rect(1, 1, gray.cols - 2, gray.rows - 2));
copyMakeBorder(gray_inner, gray, 1, 1, 1, 1, BORDER_ISOLATED | BORDER_CONSTANT, Scalar(0));

int minx = INT_MAX, miny = INT_MAX, maxx = INT_MIN, maxy = INT_MIN;
int h = gray.size().height;
int w = gray.size().width;
return gray;
}

for(int i=0;i<h;i++)
{
for(int j=0;j<w;j++)
{
if(gray.at<uchar>(i,j) == 255)
{
miny = std::min(miny,i);
maxy = std::max(maxy,i);
minx = std::min(minx,j);
maxx = std::max(maxx,j);
}
}
}
void cv::seamlessClone(InputArray _src, InputArray _dst, InputArray _mask, Point p, OutputArray _blend, int flags)
{
CV_INSTRUMENT_REGION();

int lenx = maxx - minx + 1;
int leny = maxy - miny + 1;
const Mat src = _src.getMat();
const Mat dest = _dst.getMat();
Mat mask = checkMask(_mask, src.size());
dest.copyTo(_blend);
Mat blend = _blend.getMat();

int minxd = p.x - lenx/2;
int minyd = p.y - leny/2;
Mat mask_inner = mask(Rect(1, 1, mask.cols - 2, mask.rows - 2));
copyMakeBorder(mask_inner, mask, 1, 1, 1, 1, BORDER_ISOLATED | BORDER_CONSTANT, Scalar(0));

Rect roi_d(minxd,minyd,lenx,leny);
Rect roi_s(minx,miny,lenx,leny);
Rect roi_s = boundingRect(mask);
Rect roi_d(p.x - roi_s.width / 2, p.y - roi_s.height / 2, roi_s.width, roi_s.height);

Mat destinationROI = dest(roi_d).clone();

Mat sourceROI = Mat::zeros(leny, lenx, src.type());
src(roi_s).copyTo(sourceROI,gray(roi_s));
Mat sourceROI = Mat::zeros(roi_s.height, roi_s.width, src.type());
src(roi_s).copyTo(sourceROI,mask(roi_s));

Mat maskROI = gray(roi_s);
Mat maskROI = mask(roi_s);
Mat recoveredROI = blend(roi_d);

Cloning obj;
Expand All @@ -116,43 +97,31 @@ void cv::colorChange(InputArray _src, InputArray _mask, OutputArray _dst, float
CV_INSTRUMENT_REGION();

Mat src = _src.getMat();
Mat mask = _mask.getMat();
Mat mask = checkMask(_mask, src.size());
_dst.create(src.size(), src.type());
Mat blend = _dst.getMat();

Mat gray, cs_mask;

if(mask.channels() == 3)
cvtColor(mask, gray, COLOR_BGR2GRAY );
else
mask.copyTo(gray);

src.copyTo(cs_mask,gray);
Mat cs_mask = Mat::zeros(src.size(), src.type());
src.copyTo(cs_mask, mask);

Cloning obj;
obj.localColorChange(src,cs_mask,gray,blend,red,green,blue);
obj.localColorChange(src, cs_mask, mask, blend, red, green, blue);
}

void cv::illuminationChange(InputArray _src, InputArray _mask, OutputArray _dst, float alpha, float beta)
{
CV_INSTRUMENT_REGION();

Mat src = _src.getMat();
Mat mask = _mask.getMat();
Mat mask = checkMask(_mask, src.size());
_dst.create(src.size(), src.type());
Mat blend = _dst.getMat();

Mat gray, cs_mask;

if(mask.channels() == 3)
cvtColor(mask, gray, COLOR_BGR2GRAY );
else
mask.copyTo(gray);

src.copyTo(cs_mask,gray);
Mat cs_mask = Mat::zeros(src.size(), src.type());
src.copyTo(cs_mask, mask);

Cloning obj;
obj.illuminationChange(src,cs_mask,gray,blend,alpha,beta);
obj.illuminationChange(src, cs_mask, mask, blend, alpha, beta);

}

Expand All @@ -162,18 +131,13 @@ void cv::textureFlattening(InputArray _src, InputArray _mask, OutputArray _dst,
CV_INSTRUMENT_REGION();

Mat src = _src.getMat();
Mat mask = _mask.getMat();
Mat mask = checkMask(_mask, src.size());
_dst.create(src.size(), src.type());
Mat blend = _dst.getMat();
Mat gray, cs_mask;

if(mask.channels() == 3)
cvtColor(mask, gray, COLOR_BGR2GRAY );
else
mask.copyTo(gray);

src.copyTo(cs_mask,gray);
Mat cs_mask = Mat::zeros(src.size(), src.type());
src.copyTo(cs_mask, mask);

Cloning obj;
obj.textureFlatten(src,cs_mask,gray,low_threshold,high_threshold,kernel_size,blend);
obj.textureFlatten(src, cs_mask, mask, low_threshold, high_threshold, kernel_size, blend);
}

0 comments on commit 30597e5

Please sign in to comment.