# Matlab min equivalent in OpenCV

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 close merge delete

Sort by » oldest newest most voted

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 dst;
min(a, b, dst);
cout << " a = " << a << "\n";
cout << " b = " << b << "\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]
255,   0,   0;
0,   0,   0]
dst = [1, 2, 3;
4, 5, 4;
3, 2, 1]

more

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...

2

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

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

@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 :) )

Official site

GitHub

Wiki

Documentation