hybridgroup / gocv

Go package for computer vision using OpenCV 4 and beyond. Includes support for DNN, CUDA, and OpenCV Contrib.
https://gocv.io
Other
6.62k stars 863 forks source link

MinMaxIdx function does not return the correct index. #1151

Closed chronicom closed 6 months ago

chronicom commented 7 months ago

The problem seems to me a typo in the code.

func MinMaxIdx(input Mat) (minVal, maxVal float32, minIdx, maxIdx int) {
    var cMinVal C.double
    var cMaxVal C.double
    var cMinIdx C.int
    var cMaxIdx C.int

    C.Mat_MinMaxIdx(input.p, &cMinVal, &cMaxVal, &cMinIdx, &cMaxIdx)

    return float32(cMinVal), float32(cMaxVal), int(minIdx), int(maxIdx)
}

In stead of returning

float32(cMinVal), float32(cMaxVal), int(minIdx), int(maxIdx)

I guess it should be returning

float32(cMinVal), float32(cMaxVal), int(cMinIdx), int(cMaxIdx)

Moreover, the opencv version of MinMaxIdx returns the indexs in the form of an array of size corresponding to the dimension of the input mat. Here the index is simply a scalar. Does it mean it is a linearized version of the nd-index?

HIGHER98 commented 7 months ago

Nice spot on the typo! Besides the name of the return type, it also seems that an array should be returned with mat.dims elements. This array should always contain at least two elements as per the OpenCV spec:

When minIdx is not NULL, it must have at least 2 elements (as well as maxIdx), even if src is a single-row or single-column matrix. In OpenCV (following MATLAB) each array has at least 2 dimensions, i.e. single-column matrix is Mx1 matrix (and therefore minIdx/maxIdx will be (i1,0)/(i2,0)) and single-row matrix is 1xN matrix (and therefore minIdx/maxIdx will be (0,j1)/(0,j2)).

deadprogram commented 6 months ago

Released as part of 0.36 so now closing. Thank you!