OpenCV Error at run-time

asked 2013-06-20 10:25:37 -0600

residentelvio gravatar image

updated 2020-10-24 15:57:45 -0600

I have this error:

  OpenCV Error: Sizes of input arguments do not match (The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array') in arithm_op, file /build/buildd/opencv-2.3.1/modules/core/src/arithm.cpp, line 1253
  terminate called after throwing an instance of 'cv::Exception'
  what():  /build/buildd/opencv-2.3.1/modules/core/src/arithm.cpp:1253: error: (-209) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function arithm_op

I m sure is generated in a function I use because I used a print function after using that function. Originally what I have to do is translate to C with openCV this matlab's code:

   function Xdecorr= color Decorrelation(X,numChannels)

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

    else
     Xdecorr=Xdecorr;
    end

I translated in C as:

      Mat colorDecorrelation(Mat img,int numColComp){

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

       if(numColComp>1){

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

        return Xdecorr;
        }

I suppose I dont use well svd function!

edit retag flag offensive close merge delete

Comments

blind stab in the dark: have you tried:

V = svd(A).u.t();

? (i.e. multiply with the transposed mat)

berak gravatar imageberak ( 2013-06-20 10:34:59 -0600 )edit

I have the same problem using as you said

residentelvio gravatar imageresidentelvio ( 2013-06-20 10:52:28 -0600 )edit

Decorrelation means that the image from a rgb image has to become in a grey-scale? I mean from 3 channels to just 1. If it s the meaning and exists a function directly does it, I can use it without do all this things

residentelvio gravatar imageresidentelvio ( 2013-06-20 11:28:00 -0600 )edit

Its a bit confusing, as you have mentioned A = X' * X in the matlab function and you have used X * X' in C version. Can you please clarify? And i would like to know a bit more about what you are doing (in words or some resources) if its not a problem with you. I just don't get why are you finding SVD of a correlation matrix. Its something new that i have come across and i would like to know more.

And also when you are debugging, try using a debugger and its backtrace and breakpoints to look at values of variables. Just like you would do in MATLAB. Use cout statements wherever you think there might be an error and check values. It is the easiest way to debug.

Prasanna gravatar imagePrasanna ( 2013-06-20 12:20:01 -0600 )edit

Correct me if i am wrong, You are applying the whitening transform after obtaining the covariance matrix (X * X'). Got it. But why are you taking SVD of Covariance ? Isnt that supposed to be eigenvalue decomposition? Or am i missing something fundamentally wrong?

Prasanna gravatar imagePrasanna ( 2013-06-20 13:46:42 -0600 )edit

Sorry to pester you too much, but I am not that strong with Linear Algebra. So can we go step by step? Let me summarize what i understood -

First - You need to decorrelate the RGB vector associated with each co ordinate so as to use them for some other statistical applications like PCA etc. We will get to that later. Second - You need to whiten it as to give equal importance to all the directions irrespective of its variance. (It will be like a ball rather than elliptical)

Now my question is how do you decorrelate the data? you took the reshaped Image, You mean centered across observations, and you calculated the covariance. Here is where i have a doubt. Shouldn't you be evaluating the eigenvectors of this covariance matrix?Why SVD?

Prasanna gravatar imagePrasanna ( 2013-06-20 14:40:59 -0600 )edit

To add to your earlier comment about subtract and mean, OpenCV does give you the mean for all the values in the mat for each channel separately (returns it as Scalar). Please refer documentation more carefully. I advise you to reshape your mat accordingly. And subtract can handle scalar. It will subtract each element of the mat with corresponding mean per channel. So The error as i speculate can be due to reshape. Please cout your rows, cols and channels and check if thats what you need

Prasanna gravatar imagePrasanna ( 2013-06-20 14:50:01 -0600 )edit

In the documentation i see that destination is a Mat:

void subtract(const Mat& src1, const Scalar& sc, Mat& dst, const Mat& mask=Mat())

the error must be here, because when I try to print the elements (rows,cols,depth,etc...) of img after subtract, it doesn t return nothing, just the error

residentelvio gravatar imageresidentelvio ( 2013-06-21 06:17:55 -0600 )edit

How can I print a Scalar object? As a Mat ? Because I tried to print as a Matlab, but it doesn t print the elements.

residentelvio gravatar imageresidentelvio ( 2013-06-21 06:35:41 -0600 )edit

You can index the elements of a Scalar, just like any other array and print it one by one. And I really suspect that it is an error because of your reshape. Think a bit more on this and make sure you are doing the required reshape. Try printing rows,cols,channels before subtract and see if subtract can handle the scalar 'mu' (channels in img must be equal to number of elements in scalar). Go through the docs once more and see the example they have given for subtract and mean. Then I think you can proceed further with ease.

Cheers

Prasanna gravatar imagePrasanna ( 2013-06-21 06:46:01 -0600 )edit

the Matlab's project works perfectly. Just in C I have this problem. So when I have to translate in C:

               [r c compc] = size(im);
               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);

Size return the rows,cols and channels of the image (im).maybe it s here the error?After it reshape the image as r*c as r and c=1(but really the channels is 3 because is in color the picture).after ther is the decorrelation and svd is used to decomposition in 3 matrix. I send an email to the teacher to confirm what it means, but he doesn' t answer till now.

residentelvio gravatar imageresidentelvio ( 2013-06-21 08:35:46 -0600 )edit

Wait. After reshape you find mean of img and store it in mu ( A scalar) . Then you subtract the mean. Till this its fine. Now you need to reshape your mat back to (rc,numColComp,1) for convenience of using SVD on it directly. So Now your mat will be rc rows, numColComp columns and 1 channel and finding XX' should be done one this. You should be having a 3x3 matrix as Covariace Mat (XX'). Check this first. I think this shud solve your problem

Prasanna gravatar imagePrasanna ( 2013-06-21 09:32:02 -0600 )edit

Hi , I saw the error. It was a stupid error, because mu has to be declared as Scalar and not as a Mat. Now subtract works good. I mean printing the image after the subtract function I have the same image components of the reshape. But after subtract (before the transpose) I have this other error:

OpenCV Error: Assertion failed (type == B.type() && (type == CV_32FC1 || type == CV_64FC1 || type == CV_32FC2 || type == CV_64FC2)) in gemm, file /build/buildd/opencv- 2.3.1/modules/core/src/matmul.cpp, line 701 terminate called after throwing an instance of 'cv::Exception' what(): /build/buildd/opencv-2.3.1/modules/core/src/matmul.cpp:701: error: (-215) type == B.type() && (type == CV_32FC1 || type == CV_64FC1 || type == CV_32FC2 || type == CV_64FC2) in function gemm

residentelvio gravatar imageresidentelvio ( 2013-06-22 13:31:19 -0600 )edit

P.s. I tried to reshape as you said after subtract with:

   img=img.reshape(1,img.rows);

obtening as components af image reshaped:

      r=314928, c=3,type=0,ch=1,depth=0

I have the same error. The new reshape changed the image in channel (before was 3), in type (before was 16) and in columns(before was just one obviously)

residentelvio gravatar imageresidentelvio ( 2013-06-22 13:38:16 -0600 )edit