Image processing project with an error I can not solve

asked 2014-01-31 14:13:07 -0500

residentelvio gravatar image

Hi people. I hope you can help me to solve this problem I can not solve from long time. I have to finish a project about image processing and I saw an error in the initial step. I have a code in Matlab I have to obtain it in C++ with OpenCV. I did not study Matlab and C++ neither too (just C) , so I don t know how continue my work. Please help.

As in this picture : C:\fakepath\imag.png

the initial target is :

Initial decorrelation on RGB color components of each image/frame to have an efficient codification (without duplication)of the input data through analysis of the principal components(PCA),adapted to local image statistics (rgbDecorr in Matlab's code). We obtain so 3 components Z1,Z2,Z3 in the image attached in the block figure Whitening color. This operation is performed by colorDecorrelaion function().

Matlab' s code :

 //initial stuff
 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); //HERE

 clear Decorr;

 % Fourier transform of the achromatic component
 [imfft,sz2filt,padSize]=FourierTransform2D(rgbDecorr(:,:,1),achrLambda_max);

 //more stuff

 conspic2 = zeros(r,c);

   for i_c_comp=2:numColComp

     % FILTERING CHROMATIC COMPONENT rgbDecorr(:,:,2) and  rgbDecorr(:,:,2)
    [imfft,sz2filt,padSize]=FourierTransform2D(rgbDecorr(:,:,i_c_comp),chrLambda_max);

I don t understand in Matlab where is obtained the 3 components. I supposed where I commented HERE and I suppose but I m not sure that is created a 3dimensional array where in a dimension is located the 3 components. Am I right? How to do it and how to obtain this stuff? I don t understan where it obtain this 3 images of the image attached by the input image:

Howewer here the colorDecorrelation function MAtlab's code:

function Xdecorr=colorDecorrelation(X,numColComp){

 if numColComp>1  %color images                  
 mu = mean(X); %mean of each feature of the original data
 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

So In C++ what I did is:

//initial statements and Image control if empty or not

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

 // TO RETURN INITIAL VALUES BEACUSE I HAD TO CHANGE IT IN COLORDECORRELATION FUNCTION
 imgDecorr.convertTo(rgbDecorrImg,CV_8UC3); 

 //HERE ERROR FOR SURE
 rgbDecorrImg = rgbDecorrImg.reshape(numColComp,imgDecorr.rows);


...
...

 Mat colorDecorrelation(Mat img,int numColComp){

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

  if(numColComp>1){

    //I have to convert because of gemm function need a float
    img.convertTo(img_f,CV_32FC3);
    img_f=img_f.reshape(1,img.rows*img.cols); // 1 channel

     mu=mean(img_f); //media degli elementi della matrice
     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;
   }
edit retag flag offensive close merge delete

Comments

1

Hello residentelvio

So you are taking an image: a) make a copy of, blue, copy green, and red, from the original image.

b) turn all the single color images to a gray scale.

c) get and average gray scale for all gray scale images.

Am i close?

keghn gravatar imagekeghn ( 2014-01-31 17:58:11 -0500 )edit

Hello keghn. Thanks for reply. You are very close. A & B ok. The whitening procedure consisting in decorrelation and variance normalization is simply applied to the r, g, b components of the image. Being x1=r, x2=g and x3=b the RGB coordinates of any pixel in the image, they are involved in the transformation (x1,x2,x3)→(z1,z2,z3). Thereby I obtain z contain z1 to z3 components.Z is whitened with a new vector associated to each pixel. If x is the representation of the image in the original space, y the representation in principal components, and z (z-scores) the corresponding representation in the whitened coordinates. That is, x = (xj) →y = (yj) →z = (zj) with j=1…M, where M is the number of components. Look the picture I attached to understand better

residentelvio gravatar imageresidentelvio ( 2014-02-01 09:00:09 -0500 )edit
1

Maybe part c) would be more like take one of the mono colored images say the green mat, and then for each green pixel change it over to grey scale pixel. Then move it on to a new image at the same, intensity, and location. Then hit that gray pixel with an optional constant. Then do the same for blue and red mono colored images?

keghn gravatar imagekeghn ( 2014-02-01 14:34:28 -0500 )edit

Yes it s more or less what u said. the C is the operation of whitening. It s done in the function colorDecorrelation. The work done in Matlab works , so it can be just a translation from it to C++ as I done in code. I suppose it s right this part. It s the first & second part I didn t do. Thanks to help me

residentelvio gravatar imageresidentelvio ( 2014-02-03 03:44:20 -0500 )edit

do you know how can I solve this part?

residentelvio gravatar imageresidentelvio ( 2014-02-04 06:24:00 -0500 )edit

Yes. But sense I am new to OpenCV I am stopping and dwelling on all of interesting little detail I come across. Am is seeing three gray scale for each mono colored image? Or one gray scale image for each mono colored picture?

keghn gravatar imagekeghn ( 2014-02-04 15:18:06 -0500 )edit

I did not understand your answer. Howewer we have an image.from this we obtain Rgb images inthe same array 3dimensional. In 3rd dimension you have the 3 images. After the matrix is whitened for every pixel.

residentelvio gravatar imageresidentelvio ( 2014-02-05 04:20:36 -0500 )edit

Oh, maybe I understand reading better. 1 grayscale image for each mono colored picture, as in the picture attached.if you open it, you ll understan easily. Thanks

residentelvio gravatar imageresidentelvio ( 2014-02-05 10:52:17 -0500 )edit