Skip to content

Commit fa88e5b

Browse files
committed
cuda: add implmentations for more arith functions
This adds the following cuda wrapper functions: - absSum - calcAbsSum - findMinMax - findMinMaxLoc - minMax - minMaxLoc - normalize Signed-off-by: deadprogram <ron@hybridgroup.com>
1 parent 23902e7 commit fa88e5b

File tree

5 files changed

+415
-7
lines changed

5 files changed

+415
-7
lines changed

ROADMAP.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,18 +211,18 @@ Your pull requests will be greatly appreciated!
211211
- [ ] [cv::cuda::pow](https://docs.opencv.org/4.x/d8/d34/group__cudaarithm__elem.html#ga82d04ef4bcc4dfa9bfbe76488007c6c4)
212212

213213
- [ ] **matrix reductions - WORK STARTED** The following functions still need implementation:
214-
- [ ] [cv::cuda::absSum](https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga690fa79ba4426c53f7d2bebf3d37a32a)
215-
- [ ] [cv::cuda::calcAbsSum](https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga15c403b76ab2c4d7ed0f5edc09891b7e)
214+
- [X] [cv::cuda::absSum](https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga690fa79ba4426c53f7d2bebf3d37a32a)
215+
- [X] [cv::cuda::calcAbsSum](https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga15c403b76ab2c4d7ed0f5edc09891b7e)
216216
- [ ] [cv::cuda::calcSqrSum](https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#gac998c83597f6c206c78cee16aa87946f)
217217
- [ ] [cv::cuda::calcSum](https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga98a09144047f09f5cb1d6b6ea8e0856f)
218218
- [ ] [cv::cuda::countNonZero](https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga98a09144047f09f5cb1d6b6ea8e0856f)
219-
- [ ] [cv::cuda::findMinMax](https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#gae7f5f2aa9f65314470a76fccdff887f2)
220-
- [ ] [cv::cuda::findMinMaxLoc](https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga93916bc473a62d215d1130fab84d090a)
219+
- [X] [cv::cuda::findMinMax](https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#gae7f5f2aa9f65314470a76fccdff887f2)
220+
- [X] [cv::cuda::findMinMaxLoc](https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga93916bc473a62d215d1130fab84d090a)
221221
- [ ] [cv::cuda::integral](https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga07e5104eba4bf45212ac9dbc5bf72ba6)
222222
- [ ] [cv::cuda::meanStdDev](https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga990a4db4c6d7e8f0f3a6685ba48fbddc)
223-
- [ ] [cv::cuda::minMax](https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga8d7de68c10717cf25e787e3c20d2dfee)
224-
- [ ] [cv::cuda::minMaxLoc](https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga5cacbc2a2323c4eaa81e7390c5d9f530)
225-
- [ ] [cv::cuda::normalize](https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga4da4738b9956a5baaa2f5f8c2fba438a)
223+
- [X] [cv::cuda::minMax](https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga8d7de68c10717cf25e787e3c20d2dfee)
224+
- [X] [cv::cuda::minMaxLoc](https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga5cacbc2a2323c4eaa81e7390c5d9f530)
225+
- [X] [cv::cuda::normalize](https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga4da4738b9956a5baaa2f5f8c2fba438a)
226226
- [ ] [cv::cuda::rectStdDev](https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#gac311484a4e57cab2ce2cfdc195fda7ee)
227227
- [ ] [cv::cuda::reduce](https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga21d57f661db7be093caf2c4378be2007)
228228
- [ ] [cv::cuda::sqrIntegral](https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga40c75196202706399a60bf6ba7a052ac)

cuda/arithm.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,100 @@ OpenCVResult GpuRShift(GpuMat src, Scalar shift, GpuMat dst, Stream s) {
431431
return errorResult(e.code, e.what());
432432
}
433433
}
434+
435+
OpenCVResult GpuAbsSum(GpuMat src, GpuMat mask, struct Scalar* result) {
436+
try {
437+
cv::Scalar s = cv::cuda::absSum(*src, mask == NULL ? cv::noArray() : *mask);
438+
result->val1 = s[0];
439+
result->val2 = s[1];
440+
result->val3 = s[2];
441+
result->val4 = s[3];
442+
return successResult();
443+
} catch(const cv::Exception& e) {
444+
return errorResult(e.code, e.what());
445+
}
446+
}
447+
448+
OpenCVResult GpuCalcAbsSum(GpuMat src, GpuMat dst, GpuMat mask, Stream s) {
449+
try {
450+
if (s == NULL) {
451+
cv::cuda::calcAbsSum(*src, *dst, mask == NULL ? cv::noArray() : *mask);
452+
} else {
453+
cv::cuda::calcAbsSum(*src, *dst, mask == NULL ? cv::noArray() : *mask, *s);
454+
}
455+
456+
return successResult();
457+
} catch(const cv::Exception& e) {
458+
return errorResult(e.code, e.what());
459+
}
460+
}
461+
462+
OpenCVResult GpuMinMax(GpuMat src, GpuMat mask, double* minVal, double* maxVal) {
463+
try {
464+
if (mask == NULL) {
465+
cv::cuda::minMax(*src, minVal, maxVal, cv::noArray());
466+
} else {
467+
cv::cuda::minMax(*src, minVal, maxVal, *mask);
468+
}
469+
return successResult();
470+
} catch(const cv::Exception& e) {
471+
return errorResult(e.code, e.what());
472+
}
473+
}
474+
475+
OpenCVResult GpuMinMaxLoc(GpuMat src, GpuMat mask, double* minVal, double* maxVal, int* minLocX, int* minLocY, int* maxLocX, int* maxLocY) {
476+
try {
477+
cv::Point minLoc, maxLoc;
478+
if (mask == NULL) {
479+
cv::cuda::minMaxLoc(*src, minVal, maxVal, &minLoc, &maxLoc, cv::noArray());
480+
} else {
481+
cv::cuda::minMaxLoc(*src, minVal, maxVal, &minLoc, &maxLoc, *mask);
482+
}
483+
*minLocX = minLoc.x;
484+
*minLocY = minLoc.y;
485+
*maxLocX = maxLoc.x;
486+
*maxLocY = maxLoc.y;
487+
return successResult();
488+
} catch(const cv::Exception& e) {
489+
return errorResult(e.code, e.what());
490+
}
491+
}
492+
493+
OpenCVResult GpuNormalize(GpuMat src, GpuMat dst, double alpha, double beta, int normType, int dtype, GpuMat mask, Stream s) {
494+
try {
495+
if (s == NULL) {
496+
cv::cuda::normalize(*src, *dst, alpha, beta, normType, dtype, mask == NULL ? cv::noArray() : *mask);
497+
} else {
498+
cv::cuda::normalize(*src, *dst, alpha, beta, normType, dtype, mask == NULL ? cv::noArray() : *mask, *s);
499+
}
500+
return successResult();
501+
} catch(const cv::Exception& e) {
502+
return errorResult(e.code, e.what());
503+
}
504+
}
505+
506+
OpenCVResult GpuFindMinMaxLoc(GpuMat src, GpuMat minMaxVals, GpuMat loc, GpuMat mask, Stream s) {
507+
try {
508+
if (s == NULL) {
509+
cv::cuda::findMinMaxLoc(*src, *minMaxVals, *loc, mask == NULL ? cv::noArray() : *mask);
510+
} else {
511+
cv::cuda::findMinMaxLoc(*src, *minMaxVals, *loc, mask == NULL ? cv::noArray() : *mask, *s);
512+
}
513+
return successResult();
514+
} catch(const cv::Exception& e) {
515+
return errorResult(e.code, e.what());
516+
}
517+
}
518+
519+
OpenCVResult GpuFindMinMax(GpuMat src, GpuMat dst, GpuMat mask, Stream s) {
520+
try {
521+
if (s == NULL) {
522+
cv::cuda::findMinMax(*src, *dst, mask == NULL ? cv::noArray() : *mask);
523+
} else {
524+
cv::cuda::findMinMax(*src, *dst, mask == NULL ? cv::noArray() : *mask, *s);
525+
}
526+
return successResult();
527+
} catch(const cv::Exception& e) {
528+
return errorResult(e.code, e.what());
529+
}
530+
}

cuda/arithm.go

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ package cuda
1111
import "C"
1212

1313
import (
14+
"image"
1415
"unsafe"
1516

1617
"gocv.io/x/gocv"
@@ -641,3 +642,147 @@ func RShiftWithStream(src GpuMat, shift gocv.Scalar, dst *GpuMat, s Stream) erro
641642
}
642643
return OpenCVResult(C.GpuRShift(src.p, cShift, dst.p, s.p))
643644
}
645+
646+
// AbsSum computes the sum of absolute values of array elements.
647+
// For further details, see:
648+
// https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga690fa79ba4426c53f7d2bebf3d37a32a
649+
func AbsSum(src GpuMat) (gocv.Scalar, error) {
650+
var cResult C.struct_Scalar
651+
err := OpenCVResult(C.GpuAbsSum(src.p, nil, &cResult))
652+
return gocv.Scalar{
653+
Val1: float64(cResult.val1),
654+
Val2: float64(cResult.val2),
655+
Val3: float64(cResult.val3),
656+
Val4: float64(cResult.val4),
657+
}, err
658+
}
659+
660+
// AbsSumWithMask computes the sum of absolute values of array elements using a mask.
661+
// For further details, see:
662+
// https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga690fa79ba4426c53f7d2bebf3d37a32a
663+
func AbsSumWithMask(src, mask GpuMat) (gocv.Scalar, error) {
664+
var cResult C.struct_Scalar
665+
err := OpenCVResult(C.GpuAbsSum(src.p, mask.p, &cResult))
666+
return gocv.Scalar{
667+
Val1: float64(cResult.val1),
668+
Val2: float64(cResult.val2),
669+
Val3: float64(cResult.val3),
670+
Val4: float64(cResult.val4),
671+
}, err
672+
}
673+
674+
// CalcAbsSum computes the sum of absolute values of array elements and stores the result in dst.
675+
// For further details, see:
676+
// https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga15c403b76ab2c4d7ed0f5edc09891b7e
677+
func CalcAbsSum(src GpuMat, dst *GpuMat) error {
678+
return OpenCVResult(C.GpuCalcAbsSum(src.p, dst.p, nil, nil))
679+
}
680+
681+
// CalcAbsSumWithMask computes the sum of absolute values of array elements using a mask and stores the result in dst.
682+
// For further details, see:
683+
// https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga15c403b76ab2c4d7ed0f5edc09891b7e
684+
func CalcAbsSumWithMask(src GpuMat, dst *GpuMat, mask GpuMat) error {
685+
return OpenCVResult(C.GpuCalcAbsSum(src.p, dst.p, mask.p, nil))
686+
}
687+
688+
// CalcAbsSumWithStream computes the sum of absolute values of array elements and stores the result in dst using a Stream.
689+
// For further details, see:
690+
// https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga15c403b76ab2c4d7ed0f5edc09891b7e
691+
func CalcAbsSumWithStream(src GpuMat, dst *GpuMat, mask GpuMat, s Stream) error {
692+
return OpenCVResult(C.GpuCalcAbsSum(src.p, dst.p, mask.p, s.p))
693+
}
694+
695+
// MinMax computes the global minimum and maximum in a GpuMat.
696+
// For further details, see:
697+
// https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga8d7de68c10717cf25e787e3c20d2dfee
698+
func MinMax(src GpuMat) (minVal, maxVal float64, err error) {
699+
var cMin, cMax C.double
700+
e := OpenCVResult(C.GpuMinMax(src.p, nil, &cMin, &cMax))
701+
return float64(cMin), float64(cMax), e
702+
}
703+
704+
// MinMaxWithMask computes the global minimum and maximum in a GpuMat using a mask.
705+
// For further details, see:
706+
// https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga8d7de68c10717cf25e787e3c20d2dfee
707+
func MinMaxWithMask(src, mask GpuMat) (minVal, maxVal float64, err error) {
708+
var cMin, cMax C.double
709+
e := OpenCVResult(C.GpuMinMax(src.p, mask.p, &cMin, &cMax))
710+
return float64(cMin), float64(cMax), e
711+
}
712+
713+
// MinMaxLoc finds the global minimum and maximum in a GpuMat as well as their locations.
714+
// For further details, see:
715+
// https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga5cacbc2a2323c4eaa81e7390c5d9f530
716+
func MinMaxLoc(src GpuMat) (minVal, maxVal float64, minLoc, maxLoc image.Point, err error) {
717+
var cMin, cMax C.double
718+
var minLocX, minLocY, maxLocX, maxLocY C.int
719+
e := OpenCVResult(C.GpuMinMaxLoc(src.p, nil, &cMin, &cMax, &minLocX, &minLocY, &maxLocX, &maxLocY))
720+
return float64(cMin), float64(cMax),
721+
image.Pt(int(minLocX), int(minLocY)),
722+
image.Pt(int(maxLocX), int(maxLocY)),
723+
e
724+
}
725+
726+
// MinMaxLocWithMask finds the global minimum and maximum in a GpuMat as well as their locations, using a mask.
727+
// For further details, see:
728+
// https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga5cacbc2a2323c4eaa81e7390c5d9f530
729+
func MinMaxLocWithMask(src, mask GpuMat) (minVal, maxVal float64, minLoc, maxLoc image.Point, err error) {
730+
var cMin, cMax C.double
731+
var minLocX, minLocY, maxLocX, maxLocY C.int
732+
e := OpenCVResult(C.GpuMinMaxLoc(src.p, mask.p, &cMin, &cMax, &minLocX, &minLocY, &maxLocX, &maxLocY))
733+
return float64(cMin), float64(cMax),
734+
image.Pt(int(minLocX), int(minLocY)),
735+
image.Pt(int(maxLocX), int(maxLocY)),
736+
e
737+
}
738+
739+
// Normalize scales and shifts array elements so that they cover a certain range.
740+
// For further details, see:
741+
// https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga4da4738b9956a5baaa2f5f8c2fba438a
742+
func Normalize(src GpuMat, dst *GpuMat, alpha, beta float64, normType gocv.NormType, dtype int) error {
743+
return OpenCVResult(C.GpuNormalize(src.p, dst.p, C.double(alpha), C.double(beta), C.int(normType), C.int(dtype), nil, nil))
744+
}
745+
746+
// NormalizeWithMask scales and shifts array elements so that they cover a certain range, using a mask.
747+
func NormalizeWithMask(src GpuMat, dst *GpuMat, alpha, beta float64, normType gocv.NormType, dtype int, mask GpuMat) error {
748+
return OpenCVResult(C.GpuNormalize(src.p, dst.p, C.double(alpha), C.double(beta), C.int(normType), C.int(dtype), mask.p, nil))
749+
}
750+
751+
// NormalizeWithStream scales and shifts array elements so that they cover a certain range, using a mask and Stream.
752+
func NormalizeWithStream(src GpuMat, dst *GpuMat, alpha, beta float64, normType gocv.NormType, dtype int, mask GpuMat, s Stream) error {
753+
return OpenCVResult(C.GpuNormalize(src.p, dst.p, C.double(alpha), C.double(beta), C.int(normType), C.int(dtype), mask.p, s.p))
754+
}
755+
756+
// FindMinMaxLoc finds the minimum and maximum values and their locations in a GpuMat.
757+
// For further details, see:
758+
// https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#ga93916bc473a62d215d1130fab84d090a
759+
func FindMinMaxLoc(src GpuMat, minMaxVals, loc *GpuMat) error {
760+
return OpenCVResult(C.GpuFindMinMaxLoc(src.p, minMaxVals.p, loc.p, nil, nil))
761+
}
762+
763+
// FindMinMaxLocWithMask finds the minimum and maximum values and their locations in a GpuMat using a mask.
764+
func FindMinMaxLocWithMask(src GpuMat, minMaxVals, loc *GpuMat, mask GpuMat) error {
765+
return OpenCVResult(C.GpuFindMinMaxLoc(src.p, minMaxVals.p, loc.p, mask.p, nil))
766+
}
767+
768+
// FindMinMaxLocWithStream finds the minimum and maximum values and their locations in a GpuMat using a mask and Stream.
769+
func FindMinMaxLocWithStream(src GpuMat, minMaxVals, loc *GpuMat, mask GpuMat, s Stream) error {
770+
return OpenCVResult(C.GpuFindMinMaxLoc(src.p, minMaxVals.p, loc.p, mask.p, s.p))
771+
}
772+
773+
// FindMinMax finds the minimum and maximum values in a GpuMat and stores them in dst.
774+
// For further details, see:
775+
// https://docs.opencv.org/4.x/d5/de6/group__cudaarithm__reduce.html#gae7f5f2aa9f65314470a76fccdff887f2
776+
func FindMinMax(src GpuMat, dst *GpuMat) error {
777+
return OpenCVResult(C.GpuFindMinMax(src.p, dst.p, nil, nil))
778+
}
779+
780+
// FindMinMaxWithMask finds the minimum and maximum values in a GpuMat using a mask and stores them in dst.
781+
func FindMinMaxWithMask(src GpuMat, dst *GpuMat, mask GpuMat) error {
782+
return OpenCVResult(C.GpuFindMinMax(src.p, dst.p, mask.p, nil))
783+
}
784+
785+
// FindMinMaxWithStream finds the minimum and maximum values in a GpuMat using a mask and Stream, and stores them in dst.
786+
func FindMinMaxWithStream(src GpuMat, dst *GpuMat, mask GpuMat, s Stream) error {
787+
return OpenCVResult(C.GpuFindMinMax(src.p, dst.p, mask.p, s.p))
788+
}

cuda/arithm.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ double GpuNorm(GpuMat src1, GpuMat src2, int typ);
4545
OpenCVResult GpuCompare(GpuMat src1, GpuMat src2, GpuMat dst, int typ, Stream s);
4646
OpenCVResult GpuLShift(GpuMat src, Scalar shift, GpuMat dst, Stream s);
4747
OpenCVResult GpuRShift(GpuMat src, Scalar shift, GpuMat dst, Stream s);
48+
OpenCVResult GpuAbsSum(GpuMat src, GpuMat mask, struct Scalar* result);
49+
OpenCVResult GpuAbsSumWithMask(GpuMat src, GpuMat mask, struct Scalar* result);
50+
OpenCVResult GpuCalcAbsSum(GpuMat src, GpuMat dst, GpuMat mask, Stream s);
51+
OpenCVResult GpuMinMax(GpuMat src, GpuMat mask, double* minVal, double* maxVal);
52+
OpenCVResult GpuMinMaxLoc(GpuMat src, GpuMat mask, double* minVal, double* maxVal, int* minLocX, int* minLocY, int* maxLocX, int* maxLocY);
53+
OpenCVResult GpuNormalize(GpuMat src, GpuMat dst, double alpha, double beta, int normType, int dtype, GpuMat mask, Stream s);
54+
OpenCVResult GpuFindMinMaxLoc(GpuMat src, GpuMat minMaxVals, GpuMat loc, GpuMat mask, Stream s);
55+
OpenCVResult GpuFindMinMax(GpuMat src, GpuMat dst, GpuMat mask, Stream s);
4856

4957
//LookUpTable
5058
LookUpTable Cuda_Create_LookUpTable(GpuMat lut);

0 commit comments

Comments
 (0)