decolor module updated

This commit is contained in:
siddharth 2013-07-26 11:33:27 +05:30
parent 0802c06571
commit 5b0ee9e085
2 changed files with 391 additions and 336 deletions

View File

@ -1,6 +1,7 @@
#include "precomp.hpp"
#include "opencv2/photo.hpp"
#include "opencv2/imgproc.hpp"
#include <opencv2/highgui.hpp>
#include "math.h"
#include <vector>
#include <limits>
@ -10,11 +11,11 @@
using namespace std;
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)
@ -37,31 +38,18 @@ void cv::decolor(InputArray _src, OutputArray _dst, OutputArray _boost)
return;
}
float sigma = .02;
int maxIter = 8;
int maxIter = 15;
int iterCount = 0;
int h = I.size().height;
int w = I.size().width;
float tol = .0001;
double E = 0;
double pre_E = std::numeric_limits<double>::infinity();
Decolor obj;
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);
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();
@ -83,9 +71,10 @@ void cv::decolor(InputArray _src, OutputArray _dst, OutputArray _boost)
//////////////////////////////// main loop starting ////////////////////////////////////////
while (iterCount < maxIter)
while(norm(E-pre_E) > tol)
{
iterCount +=1;
pre_E = E;
vector <double> G_pos;
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++)
wei[i] = wei1[i];
E = obj.energyCalcu(Cg,polyGrad,wei);
if(iterCount > maxIter)
break;
G_pos.clear();
G_neg.clear();
temp.clear();

View File

@ -18,6 +18,7 @@ class Decolor
public:
void init();
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 singleChannelGrady(const Mat &img, Mat& dest);
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_to_vector_poly(vector < vector <double> > &polyGrad, vector <double> &curGrad);
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_inti(vector < vector <int> > &comb, vector <double> &wei);
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()
{
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)
{
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 red = 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(green,Gg);
gradvector(blue,Bg);
double level = .05;
for(unsigned int i=0;i<Rg.size();i++)
@ -263,14 +314,24 @@ void Decolor::weak_order(Mat img, vector <double> &alf)
tmp1.clear();
tmp2.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 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);
Mat curIm = Mat(img.size(),CV_32FC1);