我需要用opencv获取高斯混合的背景模型。我知道在 C++ 中有一个名为 getBackgroundImage 的方法,我搜索了是否可以在 python 接口中获取它,但我没有得到好的结果。我尝试了 opencv 3.0.0-dev,因为它具有 BackgroundSubtractorMOG2 实现,但 help() 函数没有记录背景模型的方法实现。你知道是否有未记录的实现吗?我搜索了如何编辑 opencv 源代码以实现 python 实现,但我还没有找到关于它的文档。我更喜欢避免使用 scipy.weave 来编译 c++ 代码,此外我不知道 scipy.weave 在这种情况下是否有用
问问题
9154 次
3 回答
12
改编了Zaw Lin的解决方案
- Ubuntu 18.04
- 通过安装 OpenCV 3.2
apt install libopencv-dev
主要区别在于结果(fg
/ bg
)图像在 python 中创建/分配,然后传递给 c++ lib。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)
于 2013-10-08T15:32:31.880 回答
4
这是一个使用 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;
}
Python
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)
于 2013-09-28T04:21:34.447 回答
0
开放式简历 3.0
bgd=dict(history=20,nmixtures=20,backgroundRatio=0.5,noiseSigma=0)
fgbg=cv2.bgsegm.createBackgroundSubtractorMOG(**bgd)
于 2015-10-23T20:59:54.770 回答