Ask Your Question

Assertion failed error

asked 2014-04-03 15:31:26 -0500

FLY gravatar image

updated 2014-04-04 05:32:46 -0500

i am trying to use below code for my work but when i run it , it show me error on assertion failed (type=b.type() && (type = ....) ) , Actually i am trying to convert the python code into c++ from here

Mat img = imread ("D:\\33.jpg");
Mat a,b,c,d,e;
a = getGaussianKernel(img.cols,300,64F);
b = getGaussianKernel(img.rows,300,64F);
double minVal;     
double maxVal;          
cv::minMaxLoc(c, &minVal, &maxVal);
d = c/maxVal;
e = img*d;

But it showing me error of assertion failed , most probably on line e= img*d

Its not showing me error when i simply did img*d but when i assign it to any Mat it show me error

This is the algorithm which i am trying to implement

  1. Load the image, Get its number of rows and columns
  2. Create two Gaussian Kernels of size rows and columns, say A,B. Its variance depends upon your needs.
  3. C = transpose(A)*B, ie multiply a column vector with a row vector such that result array should be same size as that of the image.
  4. D = C/C.max()
  5. E = img*D

here is its python version which is working accordingly

img = cv2.imread('temp.jpg',0)
row,cols = img.shape

a = cv2.getGaussianKernel(cols,300)
b = cv2.getGaussianKernel(rows,300)
c = b*a.T
d = c/c.max()
e = img*d

edit retag flag offensive close merge delete


  • please read up on getGaussianKernel . your args don't make any sense there.
  • img is CV_8U3C. you can only (matrix-)multiply float mats.

again, what's the whole thing trying to do ?

(honestly, you have to explain here what you're up to. i, as many others, won't read your SO question)

berak gravatar imageberak ( 2014-04-04 05:06:29 -0500 )edit

okay let me update it and i read getGaussianKernal but i didn't get its first argument

FLY gravatar imageFLY ( 2014-04-04 05:10:47 -0500 )edit

havent got time to solve it now, but the plan here seems to be:

  • make a 'intensity distribution' vector for rows and cols ( those values would not make much sense for a filter2d kernel, but here, they're just 1d 'gaussian distribution' vectors, that's ok.)
  • a 'matrix multiplication' , a * b.t(), will give you a nice 2d vignette map. (at least i tested that)
  • then you want a per element multiplication between your image and the vignette map. that's multiply(img,d,e) , not e=img*d.

there's still some work to do for that, as both mat's need to have same type/size/format . also, you're probably ending up with some float mat, and will have to convert back

berak gravatar imageberak ( 2014-04-04 07:58:15 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2014-04-04 14:37:29 -0500

berak gravatar image

updated 2014-04-04 14:38:57 -0500

ugly, but kinda functional. (please don't upvote)

again, main point here: you want a element-wise multiplication between img and vignette-mat, not a matrix-mult.

Mat img = imread ("lena.jpg");
Mat a,b,c,d,e,f;
double sigma = 80; // vignette 'aperture', the param to play with
a = getGaussianKernel(img.cols,sigma,CV_32F);
b = getGaussianKernel(img.rows,sigma,CV_32F);
double minVal;     
double maxVal;          
cv::minMaxLoc(c, &minVal, &maxVal);
d = c/maxVal;
d.convertTo(d,CV_8U,255);      // hrmff, there should
cvtColor(d,d,COLOR_GRAY2RGB);  // be a way 
d.convertTo(d,CV_32F,1.0/255); // without all this muck

image description

edit flag offensive delete link more



@berak you can use cv::normalize(c,d,0,1,cv::NORM_MINMAX); instead of all that 4 like mulk

FLY gravatar imageFLY ( 2014-10-05 13:43:34 -0500 )edit

answered 2014-04-03 21:41:36 -0500

salkuma gravatar image

updated 2014-04-04 01:36:35 -0500

I don't figure out what you supposed to do, but what you need to know is,

since assignment of matrix multiplication only supports certain types such as CV_32FC1, CV_32FC2, CV_64FC1, and CV_64FC2 as assertion failure shows, "img" and "d" must follow one of these types and also "img" and "d" have to be the same type.

I don't know properties of "33.jpg" yet, imread would set img as channel of 3 if "33.jpg" is color image. So, "img" divides into three matrices for each channel with 32F or 64F type. Then do multiplication.

Or, simply use convertTo() and reshape() to make correct types. For example,

img.convertTo(img, CV_64F);
d.convertTo(d, CV_64F);
e = img*d;


I missed several things. Here's full code.

Mat img = imread ("D:\\33.jpg");
Mat a,b,c,d,e;
a = getGaussianKernel(img.cols,300,CV_64F);
b = getGaussianKernel(img.rows,300,CV_64F);
double minVal;     
double maxVal;

c = img;    // assign img to c because d must be the same dimension to img
cv::minMaxLoc(c, &minVal, &maxVal);
d = c/maxVal;

img.convertTo(img, CV_64F);       // convert Depth for multiplication
d.convertTo(d, CV_64F);

Mat imgPlanes[3], dPlanes[3], res[3];
split(img, imgPlanes);                 // split 3 channels to 1 channel matrices
split(d, dPlanes);

res[0] = imgPlanes[0]*dPlanes[0];   // I guess this is what you supposed to multiply
res[1] = imgPlanes[1]*dPlanes[1];
res[2] = imgPlanes[2]*dPlanes[2];

merge(res, 3, img);            // combine single channels to one multi-channel

return 0;

Also, I would like to mention that reshape() makes channels to columns. For instance, if 300 (row) x 300 (col) x 3 (ch) reshapes to 1 channel matrix, it will be changed to 300 (row) x 900 (col) x 1 (ch).

edit flag offensive delete link more


getting error too

FLY gravatar imageFLY ( 2014-04-03 23:44:15 -0500 )edit

did you tried that code ? its still not working

FLY gravatar imageFLY ( 2014-04-04 01:45:59 -0500 )edit

Surely, I tested on visual studio 2012. I used 300 x 300 size color jpg file and worked find. Would you let me know size of image you use? and plz double check image path.

salkuma gravatar imagesalkuma ( 2014-04-04 02:22:14 -0500 )edit

yes but its not giving me my required result , it show me the white image , You kindly visit this for better understanding

FLY gravatar imageFLY ( 2014-04-04 02:39:19 -0500 )edit

Question Tools


Asked: 2014-04-03 15:31:26 -0500

Seen: 3,469 times

Last updated: Apr 04 '14