Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Real strange thing-someone help me

Hi people it happened something strange. this is all the code of my project code I have to present to the 3 of july. It doesnt do what I want(don t open the windows I called SALMAP in the end of main) Really I was testing it. I arrived till here:

if(numColComp!=1){

conspic2 = Mat::zeros(img.rows,img.cols,CV_8UC3);

for(int i_c_comp=2;i_c_comp<numColComp;i_c_comp++){


imfft=FourierTrasform2D(rgbDecorrImg,chrLambda_max,imfft,padSize,sz2filt_r,sz2filt_c);
imfft.convertTo(imfft2,CV_8UC3);
imfft2= imfft2.reshape(numColComp,img.rows);

 namedWindow("Image Display", CV_WINDOW_AUTOSIZE);
 imshow("Image Display", imfft2);
 waitKey(0);
 destroyWindow("Image Display");

rows=*sz2filt_r;
cols=*sz2filt_c;
printf("r =%d , c= %d",rows,cols);

Till this part of code it works good. But after modify something to do it well as convertTO and reshape function to return with type CV_8UC3, because in some function Iuse some functions need a float matrix. Now I try to print matrix to see my errors, but it' impossible. If I want to print everything, a simple Hello world. It doesn t appear in the console.Im using eclipse. I hope it s not broken something. But how output I can see the window of some image(during wowking with code) and it works good.

Does anybody copy and paste on his pc the code and see if you have the same problem? Or help me to see what is the error

#include <stdio.h>
#include <cxcore.h>
#include <cv.h>
#include <highgui.h>
#include <math.h>

using namespace cv;

#define PI 3.14159265

struct ts{
  Mat data;
}tiese[4];

Mat colorDecorrelation(Mat img,int numColComp);
Mat FourierTrasform2D(Mat img, double Lambda_max,Mat imfft,int *padSize,int *sz2filt_r,int *sz2filt_c);
int roundIt(double num);
void pre_filter_Computations(double **radius,double **theta,int cols,int rows);
Mat logGabor(double **filter,Mat imfft,double r_o, double theta_o,double sigma_theta,double **radius,double **theta,int cols,int rows,double sigma_r,int *padSize);
Mat whitening_sqr_meanDistance(Mat X);
void ftshifting(double **filter,int rows,int cols);


int main(){

double achrScales,achrOrient,achrLambda_min,achrScFactor,achrDeltaAng,achrBwF,achrLambda_max,chrScales,chrOrient,chrLambda_min,chrScFactor,chrDeltaAng,chrBwF,chrLambda_max,sigma_r,wavelengths,sigma_theta,sigma,r_o,theta_o;
int nChannels,numColComp,*padSize,rows,cols,maxhw,*sz2filt_r,*sz2filt_c;
double **radius,**theta,**filter;
Mat img = imread("/home/elvio/workspace/aws/src/andaina.JPG");
Mat rgbDecorrImg,imfft,imfft2,imgDecorr,conspic1,conspic,conspic2,Tsquared,inCompImg,Salmap,SumSalmap,maxT,GaussKernel;
Mat achrRpad[4][7],chrRpad[4][5];
vector<Mat>stack;

if(img.empty()){
 printf("The image is empty \n");
 return -1;
}

namedWindow("Image Display", CV_WINDOW_AUTOSIZE);
imshow("Image Display", img);
waitKey(0);
destroyWindow("Image Display");

padSize=(int*)malloc(sizeof(int));
sz2filt_r=(int*)malloc(sizeof(int));
sz2filt_c=(int*)malloc(sizeof(int));

rows=img.rows;
cols= img.cols;
nChannels= img.channels();
numColComp = nChannels ;

inCompImg = img.reshape(numColComp,rows*cols);

imgDecorr = colorDecorrelation(inCompImg,numColComp);

rows=imgDecorr.rows;
cols=imgDecorr.cols;
nChannels= imgDecorr.channels();

imgDecorr.convertTo(rgbDecorrImg,CV_8UC3);
rgbDecorrImg = rgbDecorrImg.reshape(numColComp,img.rows);

namedWindow("Image Display", CV_WINDOW_AUTOSIZE);
imshow("Image Display", rgbDecorrImg);
waitKey(0);
destroyWindow("Image Display");

if(numColComp==1){
 achrScales=7;
 achrOrient=4;
 achrLambda_min=3;
 achrScFactor=2;
 achrDeltaAng = 1;
 achrBwF=1.1;
 achrLambda_max= (achrLambda_min) * (pow(achrScFactor,achrScales-1));
}
else {
   chrScales=5;
 chrOrient=4;
 chrLambda_min=6;
 chrScFactor=2;
 chrDeltaAng = 1.7;
 chrBwF=1.1;
 chrLambda_max= (chrLambda_min) * (pow(chrScFactor,chrScales-1));
}

if(numColComp==1){

 imfft=FourierTrasform2D(rgbDecorrImg,achrLambda_max,imfft,padSize,sz2filt_r,sz2filt_c);

 rows = *sz2filt_r;
 cols = *sz2filt_c;

 filter =(double**)malloc(sizeof(double*)*rows);
      for(int x=0;x<cols;x++)
          filter[x]=(double*)malloc(sizeof(double)*cols);

 theta =(double**)malloc(sizeof(double*)*rows);
      for(int y=0;y<cols;y++)
          theta[y]=(double*)malloc(sizeof(double)*cols);

 radius =(double**)malloc(sizeof(double*)*rows);
      for(int z=0;z<cols;z++)
          radius[z]=(double*)malloc(sizeof(double)*cols);

 pre_filter_Computations(radius,theta,cols,rows);

 sigma_r = exp(-(pow(2,achrBwF -1))/(pow(2,achrBwF + 1)));
 sigma_theta = (PI/achrOrient)/achrDeltaAng;

 for(int i_orient=1;i_orient<=achrOrient;i_orient++){
     for(int i_scale=1;i_scale<=achrScales;i_scale++){

       wavelengths = achrLambda_min * (pow(achrScFactor,i_scale-1));
       r_o = 2/wavelengths;
       theta_o = (i_orient-1)*(PI/achrOrient);

       achrRpad[i_orient-1][i_scale-1]=abs(logGabor(filter,imfft,r_o,theta_o,sigma_theta,radius,theta,cols,rows,sigma_r,padSize));
     }
 }
  conspic1 = Mat::zeros(img.rows,img.cols,CV_8UC3);

  for(int i_orient=1;i_orient<=achrOrient;i_orient++){
      for(int i_scale=1;i_scale<=achrScales;i_scale++){

          stack.push_back(achrRpad[i_orient-1][i_scale-1]);

      }
  }

  for(int i_orient=1;i_orient<=achrOrient;i_orient++){

    inCompImg = Mat(stack).reshape(1).t();
    inCompImg = inCompImg.reshape(0,rows*cols);
    Tsquared=whitening_sqr_meanDistance(inCompImg);
    conspic = Tsquared.reshape(0,rows);

    divide(conspic,max(0,conspic),conspic);
    maxhw = max(0,(int)floor(min(rows/2,cols/2)-1));
    sigma = max(rows,cols)*(0.025);
    GaussianBlur(conspic,conspic,Size(maxhw,maxhw),sigma,sigma,BORDER_DEFAULT);

    conspic1 = conspic1 + conspic;
  }

  divide(conspic1,max(0,conspic1),conspic1);
  GaussianBlur(conspic,conspic,Size(maxhw,maxhw),sigma,sigma,BORDER_DEFAULT);

  }

if(numColComp!=1){

conspic2 = Mat::zeros(img.rows,img.cols,CV_8UC3);

 for(int i_c_comp=2;i_c_comp<numColComp;i_c_comp++){


    imfft=FourierTrasform2D(rgbDecorrImg,chrLambda_max,imfft,padSize,sz2filt_r,sz2filt_c);
    imfft.convertTo(imfft2,CV_8UC3);
    imfft2= imfft2.reshape(numColComp,img.rows);

     namedWindow("Image Display", CV_WINDOW_AUTOSIZE);
     imshow("Image Display", imfft2);
     waitKey(0);
     destroyWindow("Image Display");

    rows=*sz2filt_r;
    cols=*sz2filt_c;
    printf("r =%d , c= %d",rows,cols);


    filter =(double**)malloc(sizeof(double*)*img.rows);
              for(int x=0;x<img.cols;x++)
                  filter[x]=(double*)malloc(sizeof(double)*img.cols);

    theta =(double**)malloc(sizeof(double*)*img.rows);
              for(int y=0;y<img.cols;y++)
                  theta[y]=(double*)malloc(sizeof(double)*img.cols);

    radius =(double**)malloc(sizeof(double*)*img.rows);
              for(int z=0;z<img.cols;z++)
                  radius[z]=(double*)malloc(sizeof(double)*img.cols);

    pre_filter_Computations(radius,theta,cols,rows);

     sigma_r = exp(-(pow(2,chrBwF -1))/(pow(2,chrBwF + 1)));
     sigma_theta = (PI/chrOrient)/chrDeltaAng;

   for(int i_orient=1;i_orient<=chrOrient;i_orient++){
      for(int i_scale=1;i_scale<=chrScales;i_scale++){

         wavelengths = chrLambda_min * (pow(chrScFactor,i_scale -1));
         r_o = 2/wavelengths;
         theta_o = (i_orient-1)*(PI/chrOrient);
         chrRpad[i_orient-1][i_scale -1]=abs(logGabor(filter,imfft,r_o,theta_o,sigma_theta,radius,theta,cols,rows,sigma_r,padSize));
       }
   }

  conspic = Mat::zeros(img.rows,img.cols,CV_8UC3);

    for(int i_orient=1;i_orient<=achrOrient;i_orient++){
            for(int i_scale=1;i_scale<=achrScales;i_scale++){

                stack.push_back(achrRpad[i_orient-1][i_scale-1]);

            }
    }

  for(int i_orient=1;i_orient<=achrOrient;i_orient++){

      inCompImg = Mat(stack).reshape(numColComp).t();
      inCompImg = inCompImg.reshape(0,rows*cols);
    Tsquared=whitening_sqr_meanDistance(inCompImg);

    tiese[i_orient-1].data= Tsquared.reshape(0,rows);

    for(int a=1;a<=chrOrient;a++){
        maxT=max(0,tiese[a-1].data);
    }

    tiese[i_orient-1].data= tiese[i_orient-1].data/ maxT;

    maxhw = max(0,(int)floor(min(rows/2,cols/2)-1));
    sigma = max(rows,cols)*(0.025);
    GaussianBlur(tiese[i_orient-1].data,tiese[i_orient-1].data,Size(maxhw,maxhw),sigma,sigma,BORDER_DEFAULT);


    conspic=conspic + tiese[i_orient-1].data;



  }
    divide(conspic,max(0,conspic),conspic);

    GaussianBlur(conspic,conspic,Size(maxhw,maxhw),sigma,sigma,BORDER_DEFAULT);

    conspic2 = conspic2 + conspic;
  }

   multiply(conspic1,2,Salmap);
   Salmap= Salmap + conspic2;
   SumSalmap= sum(Salmap);
   Salmap = Salmap / SumSalmap;

  namedWindow("Salmap Display", CV_WINDOW_AUTOSIZE);
  imshow("Salmap Display", Salmap);
  waitKey(0);
  destroyWindow("Salmap Display");

  return 0;
  }

 }


 Mat colorDecorrelation(Mat img,int numColComp){

 Mat V,D,A,Xdecorr,img_tr,img_f;
 Scalar mu;
 SVD svd;

 if(numColComp>1){

    img.convertTo(img_f,CV_32FC3);
    img_f=img_f.reshape(1,img.rows*img.cols);

     mu=mean(img_f);
     subtract(img_f,mu,img_f);
    transpose(img_f,img_tr);
     A = img_tr * img_f;
    V = svd(A).u;
    Xdecorr = img_f*V;

   }
 else
  Xdecorr=img;

return Xdecorr;

}

Mat FourierTrasform2D(Mat img, double Lambda_max,Mat imfft,int *padSize,int *sz2filt_r,int *sz2filt_c){

 int padding;
 Mat pad;

 *padSize = roundIt(Lambda_max/2);
 padding = *padSize;

 Mat padded(img.rows + padding*2,img.cols + padding*2,img.type());
 copyMakeBorder(img,padded,padding,padding,padding,padding,BORDER_CONSTANT);

 namedWindow("Image Display", CV_WINDOW_AUTOSIZE);
 imshow("Image Display", padded);
 waitKey(0);
 destroyWindow("Image Display");

 *sz2filt_r=padded.rows;
 *sz2filt_c=padded.cols;
 printf("%d %d/n",img.rows + padding*2,img.cols + padding*2);
 printf("R=%d , C=%d",*sz2filt_r,*sz2filt_c);

 img.convertTo(pad,CV_32FC3);
 pad=pad.reshape(1,pad.rows);
 dft(pad,imfft);

 return imfft;

 }

 int roundIt(double num){
  int numero;
  double diff = fabs(num - (int)num);

  if((diff >= 0.5 && num < 0) || (diff < 0.5 && num >= 0))
  return numero=(int)num;
  else if(diff >=0.5 && num >= 0)
  return numero=(int)num + 1;
  else if(diff >0.5 && num < 0)
  return numero=(int)num - 1;
  }

  void pre_filter_Computations(double **radius,double **theta,int cols,int rows){

  double x[cols],y[rows];
  double X[cols][rows], Y[cols][rows];
  double epsilon=0.0001;


  for(int j=0;j<cols;j++){
  for(int i=-(cols/2);i<=(cols/2)-1;i++){
   x[j]= i/(cols/2);
  }
  }
  for(int k=0;k<rows;k++){
   for(int z=-(rows/2);z<=(rows/2)-1;z++){
        y[k]= -z/(rows/2);
   }
  }

  for(int i=0;i<cols;i++){
   for(int z=0;z<rows;z++){
      X[i][z]=x[i];
      Y[i][z]=y[z];
  }
  }

  for(int a=0;a<rows;a++){
        for(int b=0;b<cols;b++){

           radius[a][b] = sqrt((X[a][b]*X[a][b])+(Y[a][b]*Y[a][b]));
      }
  }
  radius[(int)floor(rows/2+1)][(int)floor(cols/2+1)]=1;

  for(int a=0;a<rows;a++){
       for(int b=0;b<cols;b++){
           radius [a][b]= radius[a][b] + epsilon;
           theta[a][b] = atan2(Y[a][b],X[a][b])*180/PI;
       }

  }
  }

 Mat whitening_sqr_meanDistance(Mat X){

  Mat mu,A,V,D,S,Z,Xwh,mu_wh,whMat;
  SVD svd;
  double epsilon = 0.0001;
  int r,c;

  mu = mean(X);
  subtract(X,mu,X);

  transpose(X,A);
  A=A*X;

  V = svd(A).u;
  D = svd(D).w;

  r= X.rows;
  c= 1;

  S= Mat::eye((int)r*epsilon,(int)c*epsilon,CV_8U);

  Z=(D+S);
  Z=Z.inv(DECOMP_LU);
  sqrt(Z,S);
  whMat=V*S;

  Xwh=X*whMat;

  mu_wh=mean(Xwh);
  subtract(Xwh,mu_wh,Xwh);

  Z= Mat::zeros(r,c,CV_8U);

  sqrt(Xwh,Z);

  return Z;
 }


 Mat logGabor(double **filter,Mat imfft,double r_o, double theta_o,double sigma_theta,double **radius,double **theta,int cols,int rows,double sigma_r,int *padSize){

 Mat rpad;
 int k=*padSize;
 double sintheta[rows][cols],costheta[rows][cols];
 double ds[rows][cols],dc[rows][cols],divis[rows][cols],divis2[rows][cols];
 double dalpha[rows][cols],spread[rows][cols];

 for(int a=0; a<rows; a++){
  for(int b=0; b<cols; b++){

    sintheta[a][b]=sin(theta[a][b]);
    costheta[a][b]=cos(theta[a][b]);
    ds[a][b] = (sintheta[a][b] * cos(theta_o) - costheta[a][b] * sin(theta_o));
    dc[a][b] = (costheta[a][b] * cos(theta_o) + sintheta[a][b] * sin(theta_o));
    dalpha[a][b] = abs(atan2(ds[a][b],dc[a][b]));
    divis[a][b] = -(dalpha[a][b]*dalpha[a][b])/(2*(sigma_theta*sigma_theta));
    spread[a][b]= exp(divis[a][b]);
    divis2[a][b]= -(log((radius[a][b]/r_o) * (radius[a][b]/r_o))/(2*log(sigma_r*sigma_r)));
    filter[a][b]= exp(divis2[a][b]);
    filter[a][b]= spread[a][b]*(filter[a][b]);

  }
 }

 ftshifting(filter,rows,cols);

 for(int m=0;m<rows;m++){
  for(int n=0;n<cols;n++) {

      imfft =  imfft.mul(filter[m][n]);

  }
}

dft(imfft,rpad, DFT_INVERSE | DFT_REAL_OUTPUT);

Rect roi(k,k,rpad.rows-(2*k),rpad.cols-(2*k));
rpad = rpad(roi);

return rpad;
}

void ftshifting(double **filter,int rows,int cols){

int r2,c2,tmp1_3,tmp2_4;

r2 = (rows+1)/2;
c2 = (cols+1)/2;

for (int i = 0; i<rows/2; i++){
 for (int k = 0; k<cols/2; k++){

      tmp1_3 = filter[i][k];
      filter[i][k] = filter[i+r2][k+c2];
      filter[i+r2][k+c2] = tmp1_3;

      tmp2_4 = filter[i+r2][k];
      filter[i+r2][k] = filter[i][k+c2];
       filter[i][k+c2] = tmp2_4;
 }
}
}