Ask Your Question

Calculating a magnitude

asked 2019-04-16 04:40:19 -0500

Filippo9669 gravatar image

Hi there, I have a hard time compiling this piece of code:

UMat dx, dy;
Sobel(GREY_IMAGE, dx, GREY_IMAGE.depth(), 1, 0, 3);
Sobel(GREY_IMAGE, dy, GREY_IMAGE.depth(), 0, 1, 3);
resize(dx, dx, Size(dx.rows, dx.rows), 0, 0, CV_INTER_AREA);
resize(dy, dy, Size(dx.rows, dx.rows), 0, 0, CV_INTER_AREA);
UMat mag(dx.size(), dx.type());
magnitude(dx, dy, mag);

Whenever I try it a get a response:

{msg="OpenCV(4.0.0-dev) E:\opencv-master\modules\core\src\mathfuncs.cpp:151: error: (-215:Assertion failed) src1.size() == src2.size() && type == src2.type() && (depth == CV_32F || depth == CV_64F) in funct... ...}

I have no idea what's wrong with the code above. Dimensions are ok, all three matrixes are quadratic. What shall I do?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2019-04-16 05:22:38 -0500

berak gravatar image
  • problem 1: the depth:

    GREY_IMAGE.depth() is probably just CV_8U, and you need some float format for magnitude() later. you're also truncating negative values from the sobel like this(again, unsigned char ...), so, a bad idea

  • problem 2: handling UMat:

    please do not reuse UMats for src and dst, this causes known reference problems. you can't do resize() in-place anyway,.

UMat dx, dy, dxs,dys;
Sobel(gr, dx, CV_32F, 1, 0, 3); // float result !
Sobel(gr, dy, CV_32F, 0, 1, 3);
resize(dx, dxs, Size(dx.rows, dx.rows), 0, 0, CV_INTER_AREA); // use fresh result UMat header
resize(dy, dys, Size(dx.rows, dx.rows), 0, 0, CV_INTER_AREA);
UMat mag; // leave empty.
magnitude(dxs, dys, mag);

// just to visualize;
Mat res;
hconcat(dxs,dys, res);
hconcat(res,mag, res);

image description

edit flag offensive delete link more


Thanks a lot! I would like to ask you about one more thing - is the gradient magnitude calculated in a way that the highest obtained difference between adjacent pixels either in the x or y direction is given a value of 1 and the lowest becomes 0? It turns out to be like that and I need it to be slightly different - e.x if the max difference is 120 I need my (normalized) gradient to be 120, not 255. Is it possible?

Filippo9669 gravatar imageFilippo9669 ( 2019-04-16 05:49:15 -0500 )edit

is it normalized ?


e.x if the max difference is 120 I need my (normalized) gradient to be 120, not 255. Is it possible?

normalize(x,y .., NORM_MINMAX,...);

berak gravatar imageberak ( 2019-04-17 02:04:43 -0500 )edit

Thanks. Nevertheless, I have spotted some kind of inaccuracy in computing the gradient via Sobel. In my case, min and max values of gradient obtained by creating a convolution with a 3x3 Sobel kernel are -680 and 659, respectively. It is nowhere near values of -255 and 255. I guess it is caused by the fact that Sobel describes the gradient qualitatively, but in my case I really need values to be in between the range above. Do you see what I mean? EDIT: I have probably solved it out by adding scale of 0.125 and delta of 125, now it seemes to give some plausible results. Is it right?

Filippo9669 gravatar imageFilippo9669 ( 2019-04-17 04:31:38 -0500 )edit

It is nowhere near values of -255 and 255

why did you expect that range ?

berak gravatar imageberak ( 2019-04-17 04:37:04 -0500 )edit

Based on luminosity, these are extreme differences between adjacent pixels

Filippo9669 gravatar imageFilippo9669 ( 2019-04-17 04:45:53 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-04-16 04:40:19 -0500

Seen: 33 times

Last updated: Apr 16