# OpenCV Error at run-time

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!

blind stab in the dark: have you tried:

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

I have the same problem using as you said

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

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.

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?

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?

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

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

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.

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