/*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*/ #include "precomp.hpp" #include "opencv2/photo.hpp" #include "math.h" #include #include using namespace std; using namespace cv; class Decolor { private: Mat kernelx; Mat kernely; int order; public: float sigma; void init(); vector product(vector &comb, const double initRGB[3]); double energyCalcu(vector &Cg, vector < vector > &polyGrad, vector &wei); void singleChannelGradx(const Mat &img, Mat& dest); void singleChannelGrady(const Mat &img, Mat& dest); void gradvector(const Mat &img, vector &grad); void colorGrad(Mat img, vector &Cg); void add_vector(vector &comb, int &idx, int r,int g,int b); void add_to_vector_poly(vector < vector > &polyGrad, vector &curGrad, int &idx1); void weak_order(Mat img, vector &alf); void grad_system(Mat img, vector < vector < double > > &polyGrad, vector < double > &Cg, vector & comb); void wei_update_matrix(vector < vector > &poly, vector &Cg, Mat &X); void wei_inti(vector &comb, vector &wei); void grayImContruct(vector &wei, Mat img, Mat &Gray); }; int round_num(double a); int round_num(double a) { return int(a + 0.5); } double Decolor::energyCalcu(vector &Cg, vector < vector > &polyGrad, vector &wei) { const size_t size = polyGrad[0].size(); vector energy(size); vector temp(size); vector temp1(size); double val = 0.0; for(size_t i=0;i< polyGrad[0].size();i++) { val = 0.0; for(size_t j =0;j(0,0)=1.0; kernelx.at(0,1)=-1.0; kernely.at(0,0)=1.0; kernely.at(1,0)=-1.0; order = 2; sigma = 0.02f; } vector Decolor::product(vector &comb, const double initRGB[3]) { vector res(comb.size()); double dp; for (size_t i=0;i(i,w-1)=0.0; } void Decolor::singleChannelGrady(const Mat &img, Mat& dest) { int w=img.size().width; int h=img.size().height; Point anchor(kernely.cols - kernely.cols/2 - 1, kernely.rows - kernely.rows/2 - 1); filter2D(img, dest, -1, kernely, anchor, 0.0, BORDER_CONSTANT); for(int j=0;j(h-1,j)=0.0; } void Decolor::gradvector(const Mat &img, vector &grad) { Mat dest; Mat dest1; singleChannelGradx(img,dest); singleChannelGrady(img,dest1); Mat d_trans=dest.t(); Mat d1_trans=dest1.t(); int height = d_trans.size().height; int width = d_trans.size().width; grad.resize(width * height * 2); for(int i=0;i(i, j); const int offset = width * height; for(int i=0;i(i, j); } void Decolor::colorGrad(Mat img, vector &Cg) { Mat lab; cvtColor(img,lab,COLOR_BGR2Lab); vector lab_channel; split(lab,lab_channel); vector ImL; vector Ima; vector Imb; gradvector(lab_channel[0],ImL); gradvector(lab_channel[1],Ima); gradvector(lab_channel[2],Imb); Cg.resize(ImL.size()); double res =0.0; for(size_t i=0;i &comb, int &idx, int r,int g,int b) { comb.push_back(Vec3i(r, g, b)); idx++; } void Decolor::add_to_vector_poly(vector < vector > &polyGrad, vector &curGrad, int &idx1) { polyGrad.push_back(curGrad); idx1++; } void Decolor::weak_order(Mat img, vector &alf) { int h = img.size().height; int w = img.size().width; double sizefactor; if((h + w) > 800) { sizefactor = (double)800/(h+w); resize(img,img,Size(round_num(h*sizefactor),round_num(w*sizefactor))); } Mat curIm = Mat(img.size(),CV_32FC1); vector rgb_channel; split(img,rgb_channel); vector Rg, Gg, Bg; gradvector(rgb_channel[2],Rg); gradvector(rgb_channel[1],Gg); gradvector(rgb_channel[0],Bg); vector t1(Rg.size()), t2(Rg.size()), t3(Rg.size()); vector tmp1(Rg.size()), tmp2(Rg.size()), tmp3(Rg.size()); double level = .05; for(unsigned int i=0;i level) t1[i] = 1.0; else t1[i] = 0.0; if(Gg[i] > level) t2[i] = 1.0; else t2[i] = 0.0; if(Bg[i] > level) t3[i] = 1.0; else t3[i] = 0.0; if(Rg[i] < -1.0*level) tmp1[i] = 1.0; else tmp1[i] = 0.0; if(Gg[i] < -1.0*level) tmp2[i] = 1.0; else tmp2[i] = 0.0; if(Bg[i] < -1.0*level) tmp3[i] = 1.0; else tmp3[i] = 0.0; } alf.resize(Rg.size()); for(size_t i =0 ;i < Rg.size();i++) alf[i] = (t1[i] * t2[i] * t3[i]); for(size_t i =0 ;i < Rg.size();i++) alf[i] -= tmp1[i] * tmp2[i] * tmp3[i]; double sum =0.0; for(unsigned int i=0;i > &polyGrad, vector < double > &Cg, vector & comb) { int h = img.size().height; int w = img.size().width; double sizefactor; if((h + w) > 800) { sizefactor = (double)800/(h+w); resize(img,img,Size(round_num(h*sizefactor),round_num(w*sizefactor))); } h = img.size().height; w = img.size().width; colorGrad(img,Cg); Mat curIm = Mat(img.size(),CV_32FC1); vector rgb_channel; split(img,rgb_channel); int idx = 0, idx1 = 0; for(int r=0 ;r <=order; r++) for(int g=0; g<=order;g++) for(int b =0; b <=order;b++) { if((r+g+b)<=order && (r+g+b) > 0) { add_vector(comb,idx,r,g,b); for(int i = 0;i(i,j)= pow(rgb_channel[2].at(i,j),r)*pow(rgb_channel[1].at(i,j),g)* pow(rgb_channel[0].at(i,j),b); vector curGrad; gradvector(curIm,curGrad); add_to_vector_poly(polyGrad,curGrad,idx1); } } } void Decolor::wei_update_matrix(vector < vector > &poly, vector &Cg, Mat &X) { int size = static_cast(poly.size()), size0 = static_cast(poly[0].size()); Mat P = Mat(size, size0, CV_32FC1); Mat A = Mat(size, size, CV_32FC1); for (int i = 0; i < size; i++) for (int j = 0; j < size0;j++) P.at(i,j) = (float) poly[i][j]; Mat P_trans = P.t(); Mat B = Mat(size, size0, CV_32FC1); for(int i =0;i < size;i++) { for(int j = 0, end = (int)Cg.size(); j < end;j++) B.at(i,j) = (float) (poly[i][j] * Cg[j]); } A = P*P_trans; solve(A, B, X, DECOMP_NORMAL); } void Decolor::wei_inti(vector &comb, vector &wei) { double initRGB[3] = { .33, .33, .33 }; wei = product(comb,initRGB); vector sum(comb.size()); for(unsigned int i=0;i &wei, Mat img, Mat &Gray) { int h=img.size().height; int w=img.size().width; vector rgb_channel; split(img,rgb_channel); int kk =0; for(int r =0;r<=order;r++) for(int g=0;g<=order;g++) for(int b=0;b<=order;b++) if((r + g + b) <=order && (r+g+b) > 0) { for(int i = 0;i(i,j)=Gray.at(i,j) + (float) wei[kk]*pow(rgb_channel[2].at(i,j),r)*pow(rgb_channel[1].at(i,j),g)* pow(rgb_channel[0].at(i,j),b); kk=kk+1; } float minval = FLT_MAX; float maxval = -FLT_MAX; for(int i=0;i(i,j) < minval) minval = Gray.at(i,j); if(Gray.at(i,j) > maxval) maxval = Gray.at(i,j); } Gray -= minval; Gray /= maxval - minval; }