在python中从BackgroundSubtractorMOG2获取背景模型 [英] Get background model from BackgroundSubtractorMOG2 in python

查看:713
本文介绍了在python中从BackgroundSubtractorMOG2获取背景模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to get the background model of a Mixture of Gaussian with opencv. I know that there is a method called getBackgroundImage in C++ I searched if it is possible to get it in python interface but I haven't get good result. I Tried opencv 3.0.0-dev because it has BackgroundSubtractorMOG2 implementation, but help() function don't document method implementation for background model. Do you know if there is undocumented implementation? I searched how to edit opencv source to implement a python implementation but i haven't found documentation about it. I prefer avoid to use scipy.weave to compile c++ code, furthermore i don't know if scipy.weave is useful in thi situation

推荐答案

适应了 Zaw Lin 的解决方案

  • Ubuntu 18.04
  • 通过apt install libopencv-dev
  • 安装的OpenCV 3.2
  • Ubuntu 18.04
  • OpenCV 3.2 installed via apt install libopencv-dev

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

The main difference is that the result (fg / bg) images are created/allocated in python and then passed down to the c++ lib. Zaw Lin's solution was giving me errors (errno 139 - SIG_SEGV), because of the app was accessing invalid memory zones. Hope it saves someone a couple of hours :)

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

然后是python:

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)

这篇关于在python中从BackgroundSubtractorMOG2获取背景模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆