noamgot
noamgot

Reputation: 4091

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 n*m*2 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: I can think of some looping solution for that, but I need my solution to be as efficient as possible, so I wanted to see if there's some built-in function

EDIT2 - I did not look for a solution where I get the absolute min/max value of the whole matrix - I wanted it pixel-wise, including a map which tells me from which image the value was chosen. For example:

A = [1 2 ;
     3 4 ]
B = [5 6 ;
     1 2 ]

-->
min(A,B) = [1 2 ;
            1 2 ]
// these are the actual values ^


minInd(A,B) = [1 1 ;
               2 2 ]
// these are the indices ^ where 1 means it was taken from A, and 2 means it was taken from B

Upvotes: 0

Views: 177

Answers (1)

multigrid101
multigrid101

Reputation: 68

EDIT: my original answer was useless

How about

MatExpr min(const Mat& a, const Mat& b)

from this post. If you start with original images A and B and C=min(A,B), then you could define D=A-C. If D(i,j)==0, then the value originates from A. If D(i,j)>0, then the pixel-value originates from B.

If A and B hold floats and not ints then you'd have to do the comparisons with some small tolerance, of course.

Upvotes: 1

Related Questions