Ask Your Question
0

Matlab min equivalent in OpenCV

asked 2017-12-24 07:47:57 -0500

noamgot gravatar image

I'm looking for an equivalent for the min function of Matlab in OpenCV, with this particular functionality (taken from the official Matlab documentation):

[M,I] = min(___) finds the indices of the minimum values of A and returns them in output vector I, using any of the input arguments in the previous syntaxes. If the minimum value occurs more than once, then min returns the index corresponding to the first occurrence.

In my specific case, I have 2 images. I want to create a new image with the minimum value of each pixel (wrt those 2 images), and I need to store a map (i.e a Mat object or something similar with a similar size) where each pixel of the map tells me whether the minimum value was taken from he first or the second image (You can consider them as nm2 image where I want to take the minimum value channel-wise, and want to be able to tell from which channel I got this value).

Is there an easy way to do this in OpenCV?

btw, It's for an Android app, so Java answers are preferred over C++. thanks.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2017-12-24 08:12:37 -0500

LBerger gravatar image

updated 2017-12-24 08:13:44 -0500

I don't think you can do it with only one function in opencv but you can do like this:

    Mat a = (Mat_<float>(3, 3) << 1, 2, 3, 4, 5, 6, 7, 8, 9);
    Mat b = (Mat_<float>(3, 3) << 9, 8, 7, 6, 5, 4, 3, 2, 1);
    Mat mask=a<b;
    Mat dst;
    min(a, b, dst);
    cout << " a = " << a << "\n";
    cout << " b = " << b << "\n";
    cout << " mask = " << mask << "\n";
    cout << " dst = " << dst << "\n";

and results are

 a = [1, 2, 3;
 4, 5, 6;
 7, 8, 9]
 b = [9, 8, 7;
 6, 5, 4;
 3, 2, 1]
 mask = [255, 255, 255;
 255,   0,   0;
   0,   0,   0]
 dst = [1, 2, 3;
 4, 5, 4;
 3, 2, 1]
edit flag offensive delete link more

Comments

It looks like something that might work..! Nice idea, I will check that and update... Do you know what might be the OpenCV4Android equivalent for Mat mask = a<b ? I'm pretty new in this stuff...

noamgot gravatar imagenoamgot ( 2017-12-24 08:20:51 -0500 )edit
2

nevermind, found it - compare(A,B,dst,Core.CMP_LT) will save in dst a binary matrix according to A<B

noamgot gravatar imagenoamgot ( 2017-12-24 08:34:48 -0500 )edit

Just to say there is a 3.0.0 doc for java but unfortunately no 3.4 doc

LBerger gravatar imageLBerger ( 2017-12-24 08:42:02 -0500 )edit

@LBerger this doc is pretty bad anyway, and is mostly copied from the C++ doc (which is not to perfect either, but that's the best we got :) )

noamgot gravatar imagenoamgot ( 2017-12-24 08:45:06 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-12-24 07:47:57 -0500

Seen: 96 times

Last updated: Dec 24 '17