Skip to content

Commit

Permalink
Adjusting TestMatMinMaxIdx3d function to create a zero'd 3d matrix. E…
Browse files Browse the repository at this point in the history
…rrors were occuring before because of a non-zero'd mat being created. Malloc'ing space for min and max index in MinMaxIdx rather than making array and passing in address to [0] index.
  • Loading branch information
HIGHER98 committed Feb 28, 2024
1 parent 65b5663 commit c74a716
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
2 changes: 0 additions & 2 deletions core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,8 +660,6 @@ void Mat_MinMaxIdx(Mat m, double* minVal, double* maxVal, int* minIdx, int* maxI

for(unsigned int a = 0; a < sizeof(cMinIdx)/sizeof(cMinIdx[0]); a++) {
minIdx[a] = cMinIdx[a];
}
for(unsigned int a = 0; a < sizeof(cMaxIdx)/sizeof(cMaxIdx[0]); a++) {
maxIdx[a] = cMaxIdx[a];
}
}
Expand Down
27 changes: 20 additions & 7 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -1517,18 +1517,31 @@ func MinMaxIdx(input Mat) (minVal, maxVal float32, minIdx, maxIdx []int) {
var cMaxVal C.double

dims := len(input.Size())
cMinIdx := make([]C.int, dims)
cMaxIdx := make([]C.int, dims)
cMinIdx := (*C.int)(C.malloc(C.size_t(C.sizeof_int * dims)))
cMaxIdx := (*C.int)(C.malloc(C.size_t(C.sizeof_int * dims)))
defer C.free(unsafe.Pointer(cMinIdx))
defer C.free(unsafe.Pointer(cMaxIdx))

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

for i := 0; i < dims; i++ {
minIdx = append(minIdx, int(cMinIdx[i]))
h := &reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(cMinIdx)),
Len: dims,
Cap: dims,
}
for i := 0; i < dims; i++ {
maxIdx = append(maxIdx, int(cMaxIdx[i]))
minIndex := *(*[]C.int)(unsafe.Pointer(h))

h = &reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(cMaxIdx)),
Len: dims,
Cap: dims,
}
maxIndex := *(*[]C.int)(unsafe.Pointer(h))

for i := 0; i < dims; i++ {
minIdx = append(minIdx, int(minIndex[i]))
maxIdx = append(maxIdx, int(maxIndex[i]))
}
return float32(cMinVal), float32(cMaxVal), minIdx, maxIdx
}

Expand Down
10 changes: 9 additions & 1 deletion core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2575,7 +2575,6 @@ func TestMatMinMaxIdx(t *testing.T) {
src.SetFloatAt(4, 4, 16)

minVal, maxVal, minIdx, maxIdx := MinMaxIdx(src)

if minVal != 0 {
t.Error("TestMatMinMaxIdx minVal should be 0.")
}
Expand All @@ -2593,6 +2592,15 @@ func TestMatMinMaxIdx(t *testing.T) {
func TestMatMinMaxIdx3d(t *testing.T) {
src := NewMatWithSizes([]int{3,3,3}, MatTypeCV32FC1)
defer src.Close()

for x := 0; x < 3; x++ {
for y := 0; y < 3; y++ {
for z := 0; z < 3; z++ {
src.SetFloatAt3(x, y, z, 0)
}
}
}

src.SetFloatAt3(2, 1, 2, 2)

minVal, maxVal, minIdx, maxIdx := MinMaxIdx(src)
Expand Down

0 comments on commit c74a716

Please sign in to comment.