diff --git a/ROADMAP.md b/ROADMAP.md index f5226183..22d2b18d 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -175,7 +175,7 @@ Your pull requests will be greatly appreciated! ## Contrib modules list - [ ] aruco. ArUco Marker Detection -- [ ] bgsegm. Improved Background-Foreground Segmentation Methods +- [X] bgsegm. Improved Background-Foreground Segmentation Methods - [ ] bioinspired. Biologically inspired vision models and derivated tools - [ ] ccalib. Custom Calibration Pattern for 3D reconstruction - [ ] cnn_3dobj. 3D object recognition and pose estimation API diff --git a/contrib/bgsegm.cpp b/contrib/bgsegm.cpp new file mode 100644 index 00000000..40717d33 --- /dev/null +++ b/contrib/bgsegm.cpp @@ -0,0 +1,13 @@ +#include "bgsegm.h" + +BackgroundSubtractorCNT BackgroundSubtractorCNT_Create() { + return new cv::Ptr(cv::bgsegm::createBackgroundSubtractorCNT()); +} + +void BackgroundSubtractorCNT_Close(BackgroundSubtractorCNT b) { + delete b; +} + +void BackgroundSubtractorCNT_Apply(BackgroundSubtractorCNT b, Mat src, Mat dst) { + (*b)->apply(*src, *dst); +} diff --git a/contrib/bgsegm.go b/contrib/bgsegm.go new file mode 100644 index 00000000..f515c725 --- /dev/null +++ b/contrib/bgsegm.go @@ -0,0 +1,50 @@ +package contrib + +/* +#include +#include "bgsegm.h" +*/ +import "C" + +import ( + "unsafe" + + "gocv.io/x/gocv" +) + +// BackgroundSubtractorCNT is a wrapper around the cv::BackgroundSubtractorCNT. +type BackgroundSubtractorCNT struct { + // C.BackgroundSubtractorCNT + p unsafe.Pointer +} + +// NewBackgroundSubtractorCNT returns a new BackgroundSubtractor algorithm +// of type CNT. CNT is Background subtraction algorithm based on counting. +// About as fast as MOG2 on a high end system. More than twice faster than MOG2 on cheap hardware (benchmarked on Raspberry Pi3). +// Algorithm by Sagi Zeevi +// +// For further details, please see: +// https://docs.opencv.org/3.4/de/dca/classcv_1_1bgsegm_1_1BackgroundSubtractorCNT.html +// +func NewBackgroundSubtractorCNT() BackgroundSubtractorCNT { + return BackgroundSubtractorCNT{p: unsafe.Pointer(C.BackgroundSubtractorCNT_Create())} +} + +// Close BackgroundSubtractorCNT. +func (b *BackgroundSubtractorCNT) Close() error { + C.BackgroundSubtractorCNT_Close((C.BackgroundSubtractorCNT)(b.p)) + b.p = nil + + return nil +} + +// Apply computes a foreground mask using the current BackgroundSubtractorCNT. +// +// For further details, please see: +// https://docs.opencv.org/3.4/de/dca/classcv_1_1bgsegm_1_1BackgroundSubtractorCNT.html +// +func (b *BackgroundSubtractorCNT) Apply(src gocv.Mat, dst *gocv.Mat) { + C.BackgroundSubtractorCNT_Apply((C.BackgroundSubtractorCNT)(b.p), (C.Mat)(src.Ptr()), (C.Mat)(dst.Ptr())) + + return +} \ No newline at end of file diff --git a/contrib/bgsegm.h b/contrib/bgsegm.h new file mode 100644 index 00000000..c7c171d7 --- /dev/null +++ b/contrib/bgsegm.h @@ -0,0 +1,26 @@ +#ifndef _OPENCV3_VIDEO_H_ +#define _OPENCV3_VIDEO_H_ + +#ifdef __cplusplus +#include +#include +extern "C" { +#endif + +#include "../core.h" + +#ifdef __cplusplus +typedef cv::Ptr* BackgroundSubtractorCNT; +#else +typedef void* BackgroundSubtractorCNT; +#endif + +BackgroundSubtractorCNT BackgroundSubtractorCNT_Create(); +void BackgroundSubtractorCNT_Close(BackgroundSubtractorCNT b); +void BackgroundSubtractorCNT_Apply(BackgroundSubtractorCNT b, Mat src, Mat dst); + +#ifdef __cplusplus +} +#endif + +#endif //_OPENCV3_VIDEO_H_ diff --git a/contrib/bgsegm_test.go b/contrib/bgsegm_test.go new file mode 100644 index 00000000..3602905c --- /dev/null +++ b/contrib/bgsegm_test.go @@ -0,0 +1,28 @@ +package contrib + +import ( + "testing" + "gocv.io/x/gocv" + v "gocv.io/x/gocv/contrib" +) + +func TestCNT(t *testing.T) { + img := gocv.IMRead("../images/face.jpg", gocv.IMReadColor) + if img.Empty() { + t.Error("Invalid Mat in CNT test") + } + defer img.Close() + + dst := gocv.NewMat() + defer dst.Close() + + cnt := v.NewBackgroundSubtractorCNT() + defer cnt.Close() + + cnt.Apply(img, &dst) + + if dst.Empty() { + t.Error("Error in TestCNT test") + } +} +