decolor module updated
This commit is contained in:
parent
0802c06571
commit
5b0ee9e085
@ -1,6 +1,7 @@
|
|||||||
#include "precomp.hpp"
|
#include "precomp.hpp"
|
||||||
#include "opencv2/photo.hpp"
|
#include "opencv2/photo.hpp"
|
||||||
#include "opencv2/imgproc.hpp"
|
#include "opencv2/imgproc.hpp"
|
||||||
|
#include <opencv2/highgui.hpp>
|
||||||
#include "math.h"
|
#include "math.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
@ -10,11 +11,11 @@
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace cv;
|
using namespace cv;
|
||||||
|
|
||||||
int rounding(double a);
|
double norm(double);
|
||||||
|
|
||||||
int rounding(double a)
|
double norm(double E)
|
||||||
{
|
{
|
||||||
return int(a + 0.5);
|
return (sqrt(pow(E,2)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void cv::decolor(InputArray _src, OutputArray _dst, OutputArray _boost)
|
void cv::decolor(InputArray _src, OutputArray _dst, OutputArray _boost)
|
||||||
@ -37,31 +38,18 @@ void cv::decolor(InputArray _src, OutputArray _dst, OutputArray _boost)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
float sigma = .02;
|
int maxIter = 15;
|
||||||
int maxIter = 8;
|
|
||||||
int iterCount = 0;
|
int iterCount = 0;
|
||||||
|
float tol = .0001;
|
||||||
int h = I.size().height;
|
double E = 0;
|
||||||
int w = I.size().width;
|
double pre_E = std::numeric_limits<double>::infinity();
|
||||||
|
|
||||||
Decolor obj;
|
Decolor obj;
|
||||||
|
|
||||||
Mat img;
|
Mat img;
|
||||||
|
|
||||||
double sizefactor;
|
|
||||||
|
|
||||||
if((h + w) > 900)
|
|
||||||
{
|
|
||||||
sizefactor = (double)900/(h+w);
|
|
||||||
resize(I,I,Size(rounding(h*sizefactor),rounding(w*sizefactor)));
|
|
||||||
img = Mat(I.size(),CV_32FC3);
|
img = Mat(I.size(),CV_32FC3);
|
||||||
I.convertTo(img,CV_32FC3,1.0/255.0);
|
I.convertTo(img,CV_32FC3,1.0/255.0);
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
img = Mat(I.size(),CV_32FC3);
|
|
||||||
I.convertTo(img,CV_32FC3,1.0/255.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
obj.init();
|
obj.init();
|
||||||
|
|
||||||
@ -83,9 +71,10 @@ void cv::decolor(InputArray _src, OutputArray _dst, OutputArray _boost)
|
|||||||
|
|
||||||
//////////////////////////////// main loop starting ////////////////////////////////////////
|
//////////////////////////////// main loop starting ////////////////////////////////////////
|
||||||
|
|
||||||
while (iterCount < maxIter)
|
while(norm(E-pre_E) > tol)
|
||||||
{
|
{
|
||||||
iterCount +=1;
|
iterCount +=1;
|
||||||
|
pre_E = E;
|
||||||
|
|
||||||
vector <double> G_pos;
|
vector <double> G_pos;
|
||||||
vector <double> G_neg;
|
vector <double> G_neg;
|
||||||
@ -150,6 +139,11 @@ void cv::decolor(InputArray _src, OutputArray _dst, OutputArray _boost)
|
|||||||
for(unsigned int i =0;i<wei.size();i++)
|
for(unsigned int i =0;i<wei.size();i++)
|
||||||
wei[i] = wei1[i];
|
wei[i] = wei1[i];
|
||||||
|
|
||||||
|
E = obj.energyCalcu(Cg,polyGrad,wei);
|
||||||
|
|
||||||
|
if(iterCount > maxIter)
|
||||||
|
break;
|
||||||
|
|
||||||
G_pos.clear();
|
G_pos.clear();
|
||||||
G_neg.clear();
|
G_neg.clear();
|
||||||
temp.clear();
|
temp.clear();
|
||||||
|
@ -18,6 +18,7 @@ class Decolor
|
|||||||
public:
|
public:
|
||||||
void init();
|
void init();
|
||||||
vector<double> product(vector < vector<int> > &comb, vector <double> &initRGB);
|
vector<double> product(vector < vector<int> > &comb, vector <double> &initRGB);
|
||||||
|
double energyCalcu(vector <double> &Cg, vector < vector <double> > &polyGrad, vector <double> &wei);
|
||||||
void singleChannelGradx(const Mat &img, Mat& dest);
|
void singleChannelGradx(const Mat &img, Mat& dest);
|
||||||
void singleChannelGrady(const Mat &img, Mat& dest);
|
void singleChannelGrady(const Mat &img, Mat& dest);
|
||||||
void gradvector(const Mat &img, vector <double> &grad);
|
void gradvector(const Mat &img, vector <double> &grad);
|
||||||
@ -25,12 +26,52 @@ class Decolor
|
|||||||
void add_vector(vector < vector <int> > &comb, int r,int g,int b);
|
void add_vector(vector < vector <int> > &comb, int r,int g,int b);
|
||||||
void add_to_vector_poly(vector < vector <double> > &polyGrad, vector <double> &curGrad);
|
void add_to_vector_poly(vector < vector <double> > &polyGrad, vector <double> &curGrad);
|
||||||
void weak_order(Mat img, vector <double> &alf);
|
void weak_order(Mat img, vector <double> &alf);
|
||||||
void grad_system(Mat img, vector < vector < double > > &polyGrad, vector < double > &Cg, vector < vector <int> >& comb);
|
void grad_system(Mat img, vector < vector < double > > &polyGrad,
|
||||||
|
vector < double > &Cg, vector < vector <int> >& comb);
|
||||||
void wei_update_matrix(vector < vector <double> > &poly, vector <double> &Cg, Mat &X);
|
void wei_update_matrix(vector < vector <double> > &poly, vector <double> &Cg, Mat &X);
|
||||||
void wei_inti(vector < vector <int> > &comb, vector <double> &wei);
|
void wei_inti(vector < vector <int> > &comb, vector <double> &wei);
|
||||||
void grayImContruct(vector <double> &wei, Mat img, Mat &Gray);
|
void grayImContruct(vector <double> &wei, Mat img, Mat &Gray);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int rounding(double a);
|
||||||
|
|
||||||
|
int rounding(double a)
|
||||||
|
{
|
||||||
|
return int(a + 0.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
float sigma = .02;
|
||||||
|
|
||||||
|
double Decolor::energyCalcu(vector <double> &Cg, vector < vector <double> > &polyGrad, vector <double> &wei)
|
||||||
|
{
|
||||||
|
vector <double> P;
|
||||||
|
|
||||||
|
vector <double> temp;
|
||||||
|
vector <double> temp1;
|
||||||
|
|
||||||
|
double val = 0.0;
|
||||||
|
for(unsigned int i=0;i< polyGrad[0].size();i++)
|
||||||
|
{
|
||||||
|
val = 0.0;
|
||||||
|
for(unsigned int j =0;j<polyGrad.size();j++)
|
||||||
|
val = val + (polyGrad[j][i] * wei[j]);
|
||||||
|
temp.push_back(val - Cg[i]);
|
||||||
|
temp1.push_back(val + Cg[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(unsigned int i=0;i<polyGrad[0].size();i++)
|
||||||
|
P.push_back(-1.0*log(exp(-1.0*pow(temp[i],2)/sigma) + exp(-1.0*pow(temp1[i],2)/sigma)));
|
||||||
|
|
||||||
|
double sum = 0.0;
|
||||||
|
for(unsigned int i=0;i<polyGrad[0].size();i++)
|
||||||
|
sum +=P[i];
|
||||||
|
|
||||||
|
return (sum/polyGrad[0].size());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void Decolor::init()
|
void Decolor::init()
|
||||||
{
|
{
|
||||||
kernel = Mat(1,2, CV_32FC1);
|
kernel = Mat(1,2, CV_32FC1);
|
||||||
@ -163,6 +204,15 @@ void Decolor::add_to_vector_poly(vector < vector <double> > &polyGrad, vector <d
|
|||||||
|
|
||||||
void Decolor::weak_order(Mat img, vector <double> &alf)
|
void Decolor::weak_order(Mat img, vector <double> &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(rounding(h*sizefactor),rounding(w*sizefactor)));
|
||||||
|
}
|
||||||
|
|
||||||
Mat curIm = Mat(img.size(),CV_32FC1);
|
Mat curIm = Mat(img.size(),CV_32FC1);
|
||||||
Mat red = Mat(img.size(),CV_32FC1);
|
Mat red = Mat(img.size(),CV_32FC1);
|
||||||
Mat green = Mat(img.size(),CV_32FC1);
|
Mat green = Mat(img.size(),CV_32FC1);
|
||||||
@ -191,6 +241,7 @@ void Decolor::weak_order(Mat img, vector <double> &alf)
|
|||||||
gradvector(red,Rg);
|
gradvector(red,Rg);
|
||||||
gradvector(green,Gg);
|
gradvector(green,Gg);
|
||||||
gradvector(blue,Bg);
|
gradvector(blue,Bg);
|
||||||
|
|
||||||
double level = .05;
|
double level = .05;
|
||||||
|
|
||||||
for(unsigned int i=0;i<Rg.size();i++)
|
for(unsigned int i=0;i<Rg.size();i++)
|
||||||
@ -263,14 +314,24 @@ void Decolor::weak_order(Mat img, vector <double> &alf)
|
|||||||
tmp1.clear();
|
tmp1.clear();
|
||||||
tmp2.clear();
|
tmp2.clear();
|
||||||
tmp3.clear();
|
tmp3.clear();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Decolor::grad_system(Mat img, vector < vector < double > > &polyGrad, vector < double > &Cg, vector < vector <int> >& comb)
|
void Decolor::grad_system(Mat img, vector < vector < double > > &polyGrad,
|
||||||
|
vector < double > &Cg, vector < vector <int> >& comb)
|
||||||
{
|
{
|
||||||
int h = img.size().height;
|
int h = img.size().height;
|
||||||
int w = img.size().width;
|
int w = img.size().width;
|
||||||
|
|
||||||
|
|
||||||
|
double sizefactor;
|
||||||
|
if((h + w) > 800)
|
||||||
|
{
|
||||||
|
sizefactor = (double)800/(h+w);
|
||||||
|
resize(img,img,Size(rounding(h*sizefactor),rounding(w*sizefactor)));
|
||||||
|
}
|
||||||
|
|
||||||
|
h = img.size().height;
|
||||||
|
w = img.size().width;
|
||||||
colorGrad(img,Cg);
|
colorGrad(img,Cg);
|
||||||
|
|
||||||
Mat curIm = Mat(img.size(),CV_32FC1);
|
Mat curIm = Mat(img.size(),CV_32FC1);
|
||||||
|
Loading…
Reference in New Issue
Block a user