Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Decorrelation ask

Hi what I have to do is todo a decorellation on RGC components of an image to obtain an efficient decodification of input datas with PCA analysis, adapted to local statistics of image (rbgDecorr in matlab code. I finally obtain 3 components. This is done with colorDecorolletion() You can see Matlab's code to understand better:

main (I put here my question):

 [r c compc] = size(image);

 numColComp = compc; % 3 in case of color images, 1 in grayscale images
 inComp = reshape(im, r*c, numColComp);

 Decorr=colorDecorrelation(inComp,numColComp); 
 rgbDecorr= reshape(Decorr, r, c, numColComp); //does it create a 3dimensional array?
 clear Decorr;

 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 % Color Decorrelation
 % Input Parameters:
 %  im           - image matrix
 %  numColComp   - number of image channels
 %
 % Output value:
 %  Xdecorr  - decorrelated image, same dimensions as the original one
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

 function Xdecorr=colorDecorrelation(X,numColComp)

 if numColComp>1 
 mu = mean(X); 
 X = bsxfun(@minus, X, mu);
 A = X'*X; 
[V,D,notused] = svd(A);

Xdecorr=X*V;

else % in case of a grayscale image
Xdecorr = X;
end

My C++ code:

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

using namespace cv;
using namespace std;

#define PI 3.14159265

Mat colorDecorrelation(Mat img,int numColComp);

int main(){

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

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

padSize = new int;
sz2filt_r = new int;
sz2filt_c = new 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); //to return to first type

rgbDecorrImg = rgbDecorrImg.reshape(numColComp,img.rows); //is it a 2D image? so maybe I have an error

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