rename stuff, adds logging and replace shitty else if chain by switch

pull/3315/head
Adrien BAK 10 years ago
parent 1f6acc23fa
commit 33c15d6309
  1. 18
      modules/photo/src/seamless_cloning.cpp
  2. 13
      modules/photo/src/seamless_cloning.hpp
  3. 64
      modules/photo/src/seamless_cloning_impl.cpp

@ -39,10 +39,15 @@
// //
//M*/ //M*/
#include <string>
#include "precomp.hpp" #include "precomp.hpp"
#include "opencv2/photo.hpp" #include "opencv2/photo.hpp"
#include "opencv2/highgui.hpp"
#include <stdlib.h> #include <stdlib.h>
#include <iostream>
#include "seamless_cloning.hpp" #include "seamless_cloning.hpp"
using namespace std; using namespace std;
@ -87,6 +92,8 @@ void cv::seamlessClone(InputArray _src, InputArray _dst, InputArray _mask, Point
int lenx = maxx - minx; int lenx = maxx - minx;
int leny = maxy - miny; int leny = maxy - miny;
Mat patch = Mat::zeros(Size(leny, lenx), CV_8UC3);
int minxd = p.y - lenx/2; int minxd = p.y - lenx/2;
int maxxd = p.y + lenx/2; int maxxd = p.y + lenx/2;
int minyd = p.x - leny/2; int minyd = p.x - leny/2;
@ -97,15 +104,26 @@ void cv::seamlessClone(InputArray _src, InputArray _dst, InputArray _mask, Point
Rect roi_d(minyd,minxd,leny,lenx); Rect roi_d(minyd,minxd,leny,lenx);
Rect roi_s(miny,minx,leny,lenx); Rect roi_s(miny,minx,leny,lenx);
std::cout << "p : " << p.x << "\t"<<p.y<<"\n";
std::cout << "min : " << miny << "\t"<<minx<<"\n";
std::cout << "mind : " << minyd << "\t"<<minxd<<"\n";
Mat destinationROI = dst_mask(roi_d); Mat destinationROI = dst_mask(roi_d);
Mat sourceROI = cs_mask(roi_s); Mat sourceROI = cs_mask(roi_s);
gray(roi_s).copyTo(destinationROI); gray(roi_s).copyTo(destinationROI);
src(roi_s).copyTo(sourceROI,gray(roi_s)); src(roi_s).copyTo(sourceROI,gray(roi_s));
src(roi_s).copyTo(patch, gray(roi_s));
destinationROI = cd_mask(roi_d); destinationROI = cd_mask(roi_d);
cs_mask(roi_s).copyTo(destinationROI); cs_mask(roi_s).copyTo(destinationROI);
imwrite("/home/adrien/DATA/tmp/dst_mask.png", dst_mask);
imwrite("/home/adrien/DATA/tmp/cs_mask.png", cs_mask);
imwrite("/home/adrien/DATA/tmp/cd_mask.png", cd_mask);
imwrite("/home/adrien/DATA/tmp/patch.png", patch);
Cloning obj; Cloning obj;
obj.normal_clone(dest,cd_mask,dst_mask,blend,flags); obj.normal_clone(dest,cd_mask,dst_mask,blend,flags);

@ -53,7 +53,7 @@ namespace cv
class Cloning class Cloning
{ {
public: public:
void normal_clone(const cv::Mat &I, const cv::Mat &mask, const cv::Mat &wmask, cv::Mat &cloned, int num); void normal_clone(const cv::Mat& destination, const cv::Mat &mask, const cv::Mat &wmask, cv::Mat &cloned, int flag);
void illum_change(cv::Mat &I, cv::Mat &mask, cv::Mat &wmask, cv::Mat &cloned, float alpha, float beta); void illum_change(cv::Mat &I, cv::Mat &mask, cv::Mat &wmask, cv::Mat &cloned, float alpha, float beta);
void local_color_change(cv::Mat &I, cv::Mat &mask, cv::Mat &wmask, cv::Mat &cloned, float red_mul, float green_mul, float blue_mul); void local_color_change(cv::Mat &I, cv::Mat &mask, cv::Mat &wmask, cv::Mat &cloned, float red_mul, float green_mul, float blue_mul);
void texture_flatten(cv::Mat &I, cv::Mat &mask, cv::Mat &wmask, double low_threshold, double high_threhold, int kernel_size, cv::Mat &cloned); void texture_flatten(cv::Mat &I, cv::Mat &mask, cv::Mat &wmask, double low_threshold, double high_threhold, int kernel_size, cv::Mat &cloned);
@ -61,21 +61,22 @@ namespace cv
protected: protected:
void init_var(const cv::Mat &I, const cv::Mat &wmask); void init_var(const cv::Mat &I, const cv::Mat &wmask);
void initialization(const cv::Mat &I, const cv::Mat &mask, const cv::Mat &wmask); void compute_derivatives(const cv::Mat &I, const cv::Mat &mask, const cv::Mat &wmask);
void scalar_product(cv::Mat mat, float r, float g, float b); void scalar_product(cv::Mat mat, float r, float g, float b);
void array_product(cv::Mat mat1, cv::Mat mat2, cv::Mat mat3); void array_product(cv::Mat mat1, cv::Mat mat2, cv::Mat mat3);
void poisson(const cv::Mat &I, const cv::Mat &gx, const cv::Mat &gy, const cv::Mat &sx, const cv::Mat &sy); void poisson(const cv::Mat &I, const cv::Mat &gx, const cv::Mat &gy, const cv::Mat &sx, const cv::Mat &sy);
void evaluate(const cv::Mat &I, const cv::Mat &wmask, const cv::Mat &cloned); void evaluate(const cv::Mat &I, const cv::Mat &wmask, const cv::Mat &cloned);
void getGradientx(const cv::Mat &img, cv::Mat &gx);
void getGradienty(const cv::Mat &img, cv::Mat &gy);
void lapx(const cv::Mat &img, cv::Mat &gxx);
void lapy(const cv::Mat &img, cv::Mat &gyy);
void dst(double *mod_diff, double *sineTransform,int h,int w); void dst(double *mod_diff, double *sineTransform,int h,int w);
void idst(double *mod_diff, double *sineTransform,int h,int w); void idst(double *mod_diff, double *sineTransform,int h,int w);
void transpose(double *mat, double *mat_t,int h,int w); void transpose(double *mat, double *mat_t,int h,int w);
void solve(const cv::Mat &img, double *mod_diff, cv::Mat &result); void solve(const cv::Mat &img, double *mod_diff, cv::Mat &result);
void poisson_solver(const cv::Mat &img, cv::Mat &gxx , cv::Mat &gyy, cv::Mat &result); void poisson_solver(const cv::Mat &img, cv::Mat &gxx , cv::Mat &gyy, cv::Mat &result);
void computeGradientX(const cv::Mat &img, cv::Mat &gx);
void computeGradientY(const cv::Mat &img, cv::Mat &gy);
void computeLaplacianX(const cv::Mat &img, cv::Mat &gxx);
void computeLaplacianY(const cv::Mat &img, cv::Mat &gyy);
private: private:
std::vector <cv::Mat> rgb_channel, rgbx_channel, rgby_channel, output; std::vector <cv::Mat> rgb_channel, rgbx_channel, rgby_channel, output;

@ -47,7 +47,7 @@ using namespace cv;
using namespace std; using namespace std;
void Cloning::getGradientx( const Mat &img, Mat &gx) void Cloning::computeGradientX( const Mat &img, Mat &gx)
{ {
Mat kernel = Mat::zeros(1, 3, CV_8S); Mat kernel = Mat::zeros(1, 3, CV_8S);
kernel.at<char>(0,2) = 1; kernel.at<char>(0,2) = 1;
@ -55,7 +55,7 @@ void Cloning::getGradientx( const Mat &img, Mat &gx)
filter2D(img, gx, CV_32F, kernel); filter2D(img, gx, CV_32F, kernel);
} }
void Cloning::getGradienty( const Mat &img, Mat &gy) void Cloning::computeGradientY( const Mat &img, Mat &gy)
{ {
Mat kernel = Mat::zeros(3, 1, CV_8S); Mat kernel = Mat::zeros(3, 1, CV_8S);
kernel.at<char>(2,0) = 1; kernel.at<char>(2,0) = 1;
@ -63,7 +63,7 @@ void Cloning::getGradienty( const Mat &img, Mat &gy)
filter2D(img, gy, CV_32F, kernel); filter2D(img, gy, CV_32F, kernel);
} }
void Cloning::lapx( const Mat &img, Mat &gxx) void Cloning::computeLaplacianX( const Mat &img, Mat &gxx)
{ {
Mat kernel = Mat::zeros(1, 3, CV_8S); Mat kernel = Mat::zeros(1, 3, CV_8S);
kernel.at<char>(0,0) = -1; kernel.at<char>(0,0) = -1;
@ -71,7 +71,7 @@ void Cloning::lapx( const Mat &img, Mat &gxx)
filter2D(img, gxx, CV_32F, kernel); filter2D(img, gxx, CV_32F, kernel);
} }
void Cloning::lapy( const Mat &img, Mat &gyy) void Cloning::computeLaplacianY( const Mat &img, Mat &gyy)
{ {
Mat kernel = Mat::zeros(3, 1, CV_8S); Mat kernel = Mat::zeros(3, 1, CV_8S);
kernel.at<char>(0,0) = -1; kernel.at<char>(0,0) = -1;
@ -339,15 +339,15 @@ void Cloning::init_var(const Mat &I, const Mat &wmask)
gry32 = Mat(I.size(),CV_32FC3); gry32 = Mat(I.size(),CV_32FC3);
} }
void Cloning::initialization(const Mat &I, const Mat &mask, const Mat &wmask) void Cloning::compute_derivatives(const Mat &I, const Mat &mask, const Mat &wmask)
{ {
init_var(I,wmask); init_var(I,wmask);
getGradientx(I,grx); computeGradientX(I,grx);
getGradienty(I,gry); computeGradientY(I,gry);
getGradientx(mask,sgx); computeGradientX(mask,sgx);
getGradienty(mask,sgy); computeGradientY(mask,sgy);
Mat Kernel(Size(3, 3), CV_8UC1); Mat Kernel(Size(3, 3), CV_8UC1);
Kernel.setTo(Scalar(1)); Kernel.setTo(Scalar(1));
@ -392,8 +392,8 @@ void Cloning::poisson(const Mat &I, const Mat &gx, const Mat &gy, const Mat &sx,
Mat gxx = Mat(I.size(),CV_32FC3); Mat gxx = Mat(I.size(),CV_32FC3);
Mat gyy = Mat(I.size(),CV_32FC3); Mat gyy = Mat(I.size(),CV_32FC3);
lapx(fx,gxx); computeLaplacianX(fx,gxx);
lapy(fy,gyy); computeLaplacianY(fy,gyy);
split(gxx,rgbx_channel); split(gxx,rgbx_channel);
split(gyy,rgby_channel); split(gyy,rgby_channel);
@ -421,23 +421,23 @@ void Cloning::evaluate(const Mat &I, const Mat &wmask, const Mat &cloned)
merge(output,cloned); merge(output,cloned);
} }
void Cloning::normal_clone(const Mat &I, const Mat &mask, const Mat &wmask, Mat &cloned, int num) void Cloning::normal_clone(const Mat &destination, const Mat &mask, const Mat &wmask, Mat &cloned, int flag)
{ {
int w = I.size().width; int w = destination.size().width;
int h = I.size().height; int h = destination.size().height;
int channel = I.channels(); int channel = destination.channels();
initialization(I,mask,wmask); compute_derivatives(destination,mask,wmask);
if(num == 1) switch(flag)
{ {
array_product(srx32,sgx,smask); case NORMAL_CLONE:
array_product(sry32,sgy,smask); array_product(srx32,sgx,smask);
array_product(sry32,sgy,smask);
break;
} case MIXED_CLONE:
else if(num == 2)
{
for(int i=0;i < h; i++) for(int i=0;i < h; i++)
{ {
@ -464,36 +464,36 @@ void Cloning::normal_clone(const Mat &I, const Mat &mask, const Mat &wmask, Mat
} }
} }
} }
break;
} case MONOCHROME_TRANSFER:
else if(num == 3)
{
Mat gray = Mat(mask.size(),CV_8UC1); Mat gray = Mat(mask.size(),CV_8UC1);
Mat gray8 = Mat(mask.size(),CV_8UC3); Mat gray8 = Mat(mask.size(),CV_8UC3);
cvtColor(mask, gray, COLOR_BGR2GRAY ); cvtColor(mask, gray, COLOR_BGR2GRAY );
vector <Mat> temp; vector <Mat> temp;
split(I,temp); split(destination,temp);
gray.copyTo(temp[2]); gray.copyTo(temp[2]);
gray.copyTo(temp[1]); gray.copyTo(temp[1]);
gray.copyTo(temp[0]); gray.copyTo(temp[0]);
merge(temp,gray8); merge(temp,gray8);
getGradientx(gray8,sgx); computeGradientX(gray8,sgx);
getGradienty(gray8,sgy); computeGradientY(gray8,sgy);
array_product(srx32,sgx,smask); array_product(srx32,sgx,smask);
array_product(sry32,sgy,smask); array_product(sry32,sgy,smask);
break;
} }
evaluate(I,wmask,cloned); evaluate(destination,wmask,cloned);
} }
void Cloning::local_color_change(Mat &I, Mat &mask, Mat &wmask, Mat &cloned, float red_mul=1.0, void Cloning::local_color_change(Mat &I, Mat &mask, Mat &wmask, Mat &cloned, float red_mul=1.0,
float green_mul=1.0, float blue_mul=1.0) float green_mul=1.0, float blue_mul=1.0)
{ {
initialization(I,mask,wmask); compute_derivatives(I,mask,wmask);
array_product(srx32,sgx,smask); array_product(srx32,sgx,smask);
array_product(sry32,sgy,smask); array_product(sry32,sgy,smask);
@ -505,7 +505,7 @@ void Cloning::local_color_change(Mat &I, Mat &mask, Mat &wmask, Mat &cloned, flo
void Cloning::illum_change(Mat &I, Mat &mask, Mat &wmask, Mat &cloned, float alpha, float beta) void Cloning::illum_change(Mat &I, Mat &mask, Mat &wmask, Mat &cloned, float alpha, float beta)
{ {
initialization(I,mask,wmask); compute_derivatives(I,mask,wmask);
array_product(srx32,sgx,smask); array_product(srx32,sgx,smask);
array_product(sry32,sgy,smask); array_product(sry32,sgy,smask);
@ -534,7 +534,7 @@ void Cloning::illum_change(Mat &I, Mat &mask, Mat &wmask, Mat &cloned, float alp
void Cloning::texture_flatten(Mat &I, Mat &mask, Mat &wmask, double low_threshold, void Cloning::texture_flatten(Mat &I, Mat &mask, Mat &wmask, double low_threshold,
double high_threshold, int kernel_size, Mat &cloned) double high_threshold, int kernel_size, Mat &cloned)
{ {
initialization(I,mask,wmask); compute_derivatives(I,mask,wmask);
Mat out = Mat(mask.size(),CV_8UC1); Mat out = Mat(mask.size(),CV_8UC1);
Canny(mask,out,low_threshold,high_threshold,kernel_size); Canny(mask,out,low_threshold,high_threshold,kernel_size);

Loading…
Cancel
Save