Skip to content

Commit

Permalink
Add ReduceArgMax and ReduceArgMin
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Christie authored and deadprogram committed Mar 13, 2023
1 parent bd4b086 commit 213b6e7
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 7 deletions.
9 changes: 9 additions & 0 deletions core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,15 @@ void Mat_Reduce(Mat src, Mat dst, int dim, int rType, int dType) {
cv::reduce(*src, *dst, dim, rType, dType);
}

void Mat_ReduceArgMax(Mat src, Mat dst, int axis, bool lastIndex) {
cv::reduceArgMax(*src, *dst, axis, lastIndex);
}

void Mat_ReduceArgMin(Mat src, Mat dst, int axis, bool lastIndex) {
cv::reduceArgMin(*src, *dst, axis, lastIndex);
}


void Mat_Repeat(Mat src, int nY, int nX, Mat dst) {
cv::repeat(*src, nY, nX, *dst);
}
Expand Down
33 changes: 26 additions & 7 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,24 @@ func Reduce(src Mat, dst *Mat, dim int, rType ReduceTypes, dType MatType) {
C.Mat_Reduce(src.p, dst.p, C.int(dim), C.int(rType), C.int(dType))
}

// Finds indices of max elements along provided axis.
//
// For further details, please see:
// https://docs.opencv.org/master/d2/de8/group__core__array.html#gaa87ea34d99bcc5bf9695048355163da0
//
func ReduceArgMax(src Mat, dst *Mat, axis int, lastIndex bool) {
C.Mat_ReduceArgMax(src.p, dst.p, C.int(axis), C.bool(lastIndex))
}

// Finds indices of min elements along provided axis.
//
// For further details, please see:
// https://docs.opencv.org/master/d2/de8/group__core__array.html#gaeecd548276bfb91b938989e66b722088
//
func ReduceArgMin(src Mat, dst *Mat, axis int, lastIndex bool) {
C.Mat_ReduceArgMin(src.p, dst.p, C.int(axis), C.bool(lastIndex))
}

// Repeat fills the output array with repeated copies of the input array.
//
// For further details, please see:
Expand Down Expand Up @@ -2590,6 +2608,7 @@ func (buffer *NativeByteBuffer) Len() int {
func (buffer *NativeByteBuffer) Close() {
C.StdByteVectorFree(buffer.nativePointer())
}

// Points2fVector is a wrapper around a std::vector< std::vector< cv::Point2f > >*
type Points2fVector struct {
p C.Points2fVector
Expand All @@ -2604,7 +2623,7 @@ func NewPoints2fVector() Points2fVector {
// initialized to a slice of slices of Point2f.
func NewPoints2fVectorFromPoints(pts [][]Point2f) Points2fVector {
pvf := NewPoints2fVector()
for j := 0;j<len(pts);j++{
for j := 0; j < len(pts); j++ {
pv := NewPoint2fVectorFromPoints(pts[j])
pvf.Append(pv)
pv.Close()
Expand All @@ -2619,7 +2638,7 @@ func (pvs Points2fVector) P() C.Points2fVector {
// ToPoints returns a slice of slices of Point2f for the data in this Points2fVector.
func (pvs Points2fVector) ToPoints() [][]Point2f {
ppoints := make([][]Point2f, pvs.Size())
for j := 0;j < pvs.Size();j++{
for j := 0; j < pvs.Size(); j++ {
pts := pvs.At(j)
points := pts.ToPoints()
ppoints[j] = points
Expand All @@ -2642,7 +2661,7 @@ func (pvs Points2fVector) At(idx int) Point2fVector {
if idx > pvs.Size() {
return Point2fVector{}
}
return Point2fVector{p : C.Points2fVector_At(pvs.p, C.int(idx))}
return Point2fVector{p: C.Points2fVector_At(pvs.p, C.int(idx))}
}

// Append appends a Point2fVector at end of the Points2fVector.
Expand Down Expand Up @@ -2736,7 +2755,7 @@ func (pfv Point3fVector) Append(point Point3f) {
x: C.float(point.X),
y: C.float(point.Y),
z: C.float(point.Z),
});
})
}

// ToPoints returns a slice of Point3f for the data in this Point3fVector.
Expand Down Expand Up @@ -2767,7 +2786,7 @@ func NewPoints3fVector() Points3fVector {
// initialized to a slice of slices of Point3f.
func NewPoints3fVectorFromPoints(pts [][]Point3f) Points3fVector {
pvf := NewPoints3fVector()
for j := 0;j<len(pts);j++{
for j := 0; j < len(pts); j++ {
pv := NewPoint3fVectorFromPoints(pts[j])
pvf.Append(pv)
pv.Close()
Expand All @@ -2778,7 +2797,7 @@ func NewPoints3fVectorFromPoints(pts [][]Point3f) Points3fVector {
// ToPoints returns a slice of slices of Point3f for the data in this Points3fVector.
func (pvs Points3fVector) ToPoints() [][]Point3f {
ppoints := make([][]Point3f, pvs.Size())
for j := 0;j < pvs.Size();j++{
for j := 0; j < pvs.Size(); j++ {
pts := pvs.At(j)
points := pts.ToPoints()
ppoints[j] = points
Expand All @@ -2801,7 +2820,7 @@ func (pvs Points3fVector) At(idx int) Point3fVector {
if idx > pvs.Size() {
return Point3fVector{}
}
return Point3fVector{p : C.Points3fVector_At(pvs.p, C.int(idx))}
return Point3fVector{p: C.Points3fVector_At(pvs.p, C.int(idx))}
}

// Append appends a Point3fVector at end of the Points3fVector.
Expand Down
2 changes: 2 additions & 0 deletions core.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ bool Mat_Solve(Mat src1, Mat src2, Mat dst, int flags);
int Mat_SolveCubic(Mat coeffs, Mat roots);
double Mat_SolvePoly(Mat coeffs, Mat roots, int maxIters);
void Mat_Reduce(Mat src, Mat dst, int dim, int rType, int dType);
void Mat_ReduceArgMax(Mat src, Mat dst, int axis, bool lastIndex);
void Mat_ReduceArgMin(Mat src, Mat dst, int axis, bool lastIndex);
void Mat_Repeat(Mat src, int nY, int nX, Mat dst);
void Mat_ScaleAdd(Mat src1, double alpha, Mat src2, Mat dst);
void Mat_SetIdentity(Mat src, double scalar);
Expand Down
54 changes: 54 additions & 0 deletions core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,60 @@ func TestMatReduceToSingleColumn(t *testing.T) {
}
}

func TestMatReduceArgMax(t *testing.T) {
rows := 2
cols := 3
src := NewMatWithSize(rows, cols, MatTypeCV8U)
defer src.Close()
dst := NewMat()
defer dst.Close()

for row := 0; row < rows; row++ {
for col := 0; col < cols; col++ {
src.SetUCharAt(row, col, uint8(col+1))
}
}

ReduceArgMax(src, &dst, 1, false)

sz := dst.Size()
if sz[0] != 2 && sz[1] != 1 {
t.Errorf("TestMatReduceArgMax incorrect size: %v\n", sz)
}

if dst.GetUCharAt(0, 0) != 2 || dst.GetUCharAt(1, 0) != 2 {
t.Errorf("TestMatReduceArgMax incorrect reduce result: %v at (0, 0) expected 1, %v at (1, 0) expected 1",
dst.GetUCharAt(0, 0), dst.GetUCharAt(1, 0))
}
}

func TestMatReduceArgMin(t *testing.T) {
rows := 2
cols := 3
src := NewMatWithSize(rows, cols, MatTypeCV8U)
defer src.Close()
dst := NewMat()
defer dst.Close()

for row := 0; row < rows; row++ {
for col := 0; col < cols; col++ {
src.SetUCharAt(row, col, uint8(col+1))
}
}

ReduceArgMin(src, &dst, 1, false)

sz := dst.Size()
if sz[0] != 2 && sz[1] != 1 {
t.Errorf("TestMatReduceArgMax incorrect size: %v\n", sz)
}

if dst.GetUCharAt(0, 0) != 0 || dst.GetUCharAt(1, 0) != 0 {
t.Errorf("TestMatReduceArgMax incorrect reduce result: %v at (0, 0) expected 0, %v at (1, 0) expected 0",
dst.GetUCharAt(0, 0), dst.GetUCharAt(1, 0))
}
}

func TestRepeat(t *testing.T) {
rows := 1
cols := 3
Expand Down

0 comments on commit 213b6e7

Please sign in to comment.