Ask Your Question
0

Run time error

asked 2013-06-23 07:47:02 -0500

residentelvio gravatar image

I have this 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 instan
ce 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

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_tr*img;
        V = svd(A).u;
        Xdecorr = img*V;
   }
   else
   Xdecorr = img;

    return Xdecorr;
    }

The error is for sure when I do : A=img_tr*img

edit retag flag offensive close merge delete

Comments

did you check the type() of img ? gemm wants float types

if img,type() is CV_8UC3, you'd need to img.convertTo(img_f,CV_32FC3); before the transpose

berak gravatar imageberak ( 2013-06-23 13:53:12 -0500 )edit

I have the same problem. When I open the img, his datas are:

     r=486,c=648,chann=3,type=16,depth=0

For this I suppose the type is what you said. Before entrering the function i reshape it and I obtain r=314928 & c=1, fot this I use transpoce to obtain the covariance matrix.But now printing the img_tr datas are:

   r=1,c=314928,type=21,chann=3,depth)5
residentelvio gravatar imageresidentelvio ( 2013-06-24 07:39:43 -0500 )edit

yes. now what is your question there ?

does the matrix mul work now ?

berak gravatar imageberak ( 2013-06-24 09:04:05 -0500 )edit

IT doesn' t work, and running I have this message:

  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 instan ce of 'cv::Exception

residentelvio gravatar imageresidentelvio ( 2013-06-24 09:38:55 -0500 )edit

ouch, sorry , i did not read properly.

the error states, that it wants 1 (or 2) channel matrices for multiplication, 3 channels won't work

also found a little bug in your code: mean() returns a Scalar, not a Mat. please change that( the Mat is just invalid in that case, and subtract will ignore it)

berak gravatar imageberak ( 2013-06-24 10:12:03 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-06-24 10:16:05 -0500

berak gravatar image

updated 2013-06-24 11:01:20 -0500

ahh, remember the strange find, that the matlab code was making a 3 row, 1 channel img of it ? seems, you got to do the same here.

below code compiles and runs, but no idea, if the outcome is the right thing

Mat colorDecorrelation(Mat img,int numColComp)
{
    Mat imgf;
    Mat V,D,A,Xdecorr,img_tr;
    SVD svd;
    Scalar mu;
    if(numColComp>1){
        img.convertTo(imgf,CV_32FC3);
        imgf = imgf.reshape(1,img.rows*img.cols); // r=3,c=314928,chan=1

        mu=mean(imgf);
        subtract(imgf,mu,imgf);
        transpose(imgf,img_tr);
        A = img_tr*imgf;
        V = svd(A).u;
        Xdecorr = imgf*V;
        Xdecorr = Xdecorr.reshape(3,img.rows); // back to original shape
        // do we need to convert back to CV_8UC3 ?
    }
    else
        Xdecorr = img;

    return Xdecorr;
}

int main(int argc, char **argv)
{

    Mat img = imread("herring.jpeg");
    Mat r = colorDecorrelation(img,3);
    imshow("i",r);
    imwrite("herrin_decorr.jpeg",r);
    waitKey(0);
    return 0;
}

before after

edit flag offensive delete link more

Comments

Now running it goes out from the function. A :

     r=3,c=3,type=5,channel=1,depth=5

I dont know how is the relationship between type and depth. does exist some datasheet explaining it? Well, I tried to print Xdecorr opening a window with namedWindow,imshow,waitKey and destroyWindow to understand if does what I want (Decorrelation of the image about RGB components obtening the 3 components without color relatives to r,g,b components). But I have this error:

residentelvio gravatar imageresidentelvio ( 2013-06-24 10:54:06 -0500 )edit
      The program 'Image Display' received an X Window System error.
      This probably reflects a bug in the program.
      The error was 'BadAlloc (insufficient resources for operation)'.
     (Details: serial 361 error_code 11 request_code 53 minor_code 0)
     (Note to programmers: normally, X errors are reported asynchronously;
      that is, you will receive the error a while after causing it.
      To debug your program, run it with the --sync command line
      option to change this behavior. You can then get a meaningful
      backtrace from your debugger if you break on the gdk_x_error() function.)

Thanks helping me!

residentelvio gravatar imageresidentelvio ( 2013-06-24 10:55:27 -0500 )edit

Ok thanks, when I printed in the main it worked, just I converted in the main!

residentelvio gravatar imageresidentelvio ( 2013-06-24 11:23:52 -0500 )edit
1

what a story ... ;)

berak gravatar imageberak ( 2013-06-24 11:34:56 -0500 )edit

Hi man, I have the same error in another function, I mean the first one but in dft function,not gemm:

  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 + padding2,img.cols + padding2,img.depth());                  
  copyMakeBorder(img,padded,padding,padding,padding,padding,BORDER_CONSTANT);
   *sz2filt_r=padded.rows; *sz2filt_c=padded.cols;
    padded.convertTo(pad,CV_32FC3);
   //pad=pad.reshape(1,img.rows*img.cols);
   dft(pad,imfft); 
   return imfft; }

Till using dft function it works!

residentelvio gravatar imageresidentelvio ( 2013-06-24 12:42:16 -0500 )edit

didn't check this time, but for sure you want: Mat padded(img.rows + padding2,img.cols + padding2,img.type()); // instead of depth

berak gravatar imageberak ( 2013-06-24 12:49:01 -0500 )edit

I don' t know why but using depth or type I have same output image. The error was in reshape (using img.rows and not pad.rows). Now the code goes out from the function. In main I have

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

Datas of imfft2:

  r=433008,c=3,type=0,chann=1,depth=0

If I reshape as

   imfft2= imfft2.reshape(numColComp,img.rows);

I have an error at runtime for bad arguments in reshape.I suppose it is when I pass to CV_32FC3 with a picture padded and after using the dft (because it cutoff part of image) For this reason I have to pad.So the error must be when I reshape after converting because of pad

residentelvio gravatar imageresidentelvio ( 2013-06-24 13:25:56 -0500 )edit

Using the

    padded.convertTo(pad,CV_32FC3);
    pad=pad.reshape(1,img.rows); // and not (1,img.rows*img.cols)

When I return in the main the imfft2 has the same datas of initial picture

residentelvio gravatar imageresidentelvio ( 2013-06-24 14:16:27 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-06-23 07:47:02 -0500

Seen: 4,722 times

Last updated: Jun 24 '13