Skip to content

Commit

Permalink
Add BackgroundSubtractorCNT
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim authored and deadprogram committed Dec 23, 2019
1 parent 07c5c53 commit 0e1148f
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions contrib/bgsegm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "bgsegm.h"

BackgroundSubtractorCNT BackgroundSubtractorCNT_Create() {
return new cv::Ptr<cv::bgsegm::BackgroundSubtractorCNT>(cv::bgsegm::createBackgroundSubtractorCNT());
}

void BackgroundSubtractorCNT_Close(BackgroundSubtractorCNT b) {
delete b;
}

void BackgroundSubtractorCNT_Apply(BackgroundSubtractorCNT b, Mat src, Mat dst) {
(*b)->apply(*src, *dst);
}
50 changes: 50 additions & 0 deletions contrib/bgsegm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package contrib

/*
#include <stdlib.h>
#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
}
26 changes: 26 additions & 0 deletions contrib/bgsegm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef _OPENCV3_VIDEO_H_
#define _OPENCV3_VIDEO_H_

#ifdef __cplusplus
#include <opencv2/opencv.hpp>
#include <opencv2/bgsegm.hpp>
extern "C" {
#endif

#include "../core.h"

#ifdef __cplusplus
typedef cv::Ptr<cv::bgsegm::BackgroundSubtractorCNT>* 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_
28 changes: 28 additions & 0 deletions contrib/bgsegm_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}

0 comments on commit 0e1148f

Please sign in to comment.