从python中的BackgroundSubtractorMOG2获取背景模型



我需要获取高斯与opencv的混合的背景模型。我知道有一个名为getBackgroundImage的方法,C++我搜索是否可以在python界面中获得它,但我没有得到好的结果。我尝试了opencv 3.0.0-dev,因为它具有BackgroundSubtractorMOG2实现,但是help()函数没有记录背景模型的方法实现。您知道是否有未记录的实施吗?我搜索了如何编辑opencv源代码以实现python实现,但我还没有找到有关它的文档。我更喜欢避免使用 scipy.weave 来编译 c++ 代码,此外我不知道 scipy.weave 在这种情况下是否有用

改编了 Zaw Lin的解决方案

  • 乌班图 18.04
  • 通过apt install libopencv-dev安装的OpenCV 3.2

主要区别在于结果(fg/bg)图像是在python中创建/分配的,然后传递给c ++库。Zaw Lin的解决方案给了我错误(errno 139 - SIG_SEGV),因为该应用程序正在访问无效的内存区域。希望它可以节省某人几个小时:)

mog2.cpp:

#include <opencv2/opencv.hpp>
cv::BackgroundSubtractorMOG2 *mog = cv::createBackgroundSubtractorMOG2 (500, 16, false);
extern "C" void getfg(int rows, int cols, unsigned char* imgData,
        unsigned char *fgD) {
    cv::Mat img(rows, cols, CV_8UC3, (void *) imgData);
    cv::Mat fg(rows, cols, CV_8UC1, fgD);
    mog->apply(img, fg);
}
extern "C" void getbg(int rows, int cols, unsigned char *bgD) {
    cv::Mat bg = cv::Mat(rows, cols, CV_8UC3, bgD);
    mog->getBackgroundImage(bg);
}

编译它像:

gcc  
    -shared 
    -o libmog2.so  
    -fPIC ./mog2.cpp  
    -lopencv_core -lopencv_highgui -lopencv_objdetect -lopencv_imgproc -lopencv_features2d -lopencv_ml -lopencv_calib3d -lopencv_video

然后是蟒蛇:

mog2.py

import numpy as np
import ctypes as C
import cv2
libmog = C.cdll.LoadLibrary('path/to/libmog2.so')
def getfg(img):
    (rows, cols) = (img.shape[0], img.shape[1])
    res = np.zeros(dtype=np.uint8, shape=(rows, cols))
    libmog.getfg(img.shape[0], img.shape[1],
                       img.ctypes.data_as(C.POINTER(C.c_ubyte)),
                       res.ctypes.data_as(C.POINTER(C.c_ubyte)))
    return res

def getbg(img):
    (rows, cols) = (img.shape[0], img.shape[1])
    res = np.zeros(dtype=np.uint8, shape=(rows, cols, 3))
    libmog.getbg(rows, cols, res.ctypes.data_as(C.POINTER(C.c_ubyte)))
    return res

if __name__ == '__main__':
    c = cv2.VideoCapture(0)
    while 1:
        _, f = c.read()
        cv2.imshow('f', f)
        cv2.imshow('fg', getfg(f))
        cv2.imshow('bg', getbg(f))
        if cv2.waitKey(1) == 27:
            exit(0)

这是一个使用 ctypes 的简单包装器,我只在 Windows 上测试过

CPP,构建为 DLL

#include "opencv2/opencv.hpp"
cv::BackgroundSubtractorMOG2 mog(100, 16, false);
cv::Mat bg;
cv::Mat fg;
extern "C" __declspec(dllexport)  unsigned char*  getfg(int rows,int cols, unsigned char* fdata)
{
    cv::Mat frame= cv::Mat(rows, cols, CV_8UC3,fdata);
    mog(frame,fg);
    //check fg.iscont(), copy as needed
    return fg.data;
}

extern "C" __declspec(dllexport)   unsigned char*  getbg()
{
    mog.getBackgroundImage(bg);
    return bg.data;
}

import cv2
import numpy as np
import ctypes as C
lib = C.cdll.LoadLibrary('wrapper.dll')
def getfg(img):
    ptr = lib.getfg(img.shape[0],img.shape[1],img.ctypes.data_as(C.POINTER(C.c_ubyte)))
    buf = (C.c_ubyte * img.shape[0] * img.shape[1]  * 1).from_address(ptr)
    res = np.ndarray(buffer=buf, dtype=np.uint8,
                       shape=(img.shape[0], img.shape[1], 1))
    return res
def getbg(img):
    ptr = lib.getbg()
    buf = (C.c_ubyte * img.shape[0] * img.shape[1]  * 3).from_address(ptr)
    res = np.ndarray(buffer=buf, dtype=np.uint8,
                       shape=(img.shape[0], img.shape[1], 3))
    return res
c = cv2.VideoCapture(0)
while(1):
    _,f = c.read()
    cv2.imshow('f',f)
    cv2.imshow('fg',getfg(f))
    cv2.imshow('bg',getbg(f))
    if cv2.waitKey(1)==27:
        exit(0)    

opencv 3.0

bgd=dict(history=20,nmixtures=20,backgroundRatio=0.5,noiseSigma=0)
fgbg=cv2.bgsegm.createBackgroundSubtractorMOG(**bgd)

相关内容

  • 没有找到相关文章

最新更新