我需要使用opencv获得高斯混合的背景模型.我知道在C++中有一个名为getBackgroundImage的方法我搜索了是否可以在python接口中获取它但我没有得到好的结果.我试过opencv 3.0.0-dev因为它有BackgroundSubtractorMOG2实现,但是help()函数没有为背景模型记录方法实现.你知道是否有无证实施?我搜索了如何编辑opencv源来实现python实现,但我还没有找到有关它的文档.我更喜欢避免使用scipy.weave来编译c ++代码,而且我不知道scipy.weave在这种情况下是否有用
Mat*_*yas 11
Zaw Lin在Ubuntu 12.04中的解决方案:
主要区别在于结果(fg
/ bg
)图像是在python中创建/分配的,然后传递给c ++ lib.Zaw Lin的解决方案给了我错误(错误139 - SIG_SEGV),因为应用程序正在访问无效的内存区域.希望它能节省几个小时:)
mog2.cpp:
#include <opencv2/opencv.hpp>
cv::BackgroundSubtractorMOG2 mog(100, 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(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);
}
Run Code Online (Sandbox Code Playgroud)
编译它像:
gcc -shared -o libmog2.so -fPIC ./mog2.cpp -lopencv_core -lopencv_highgui -lopencv_objdetect -lopencv_imgproc -lopencv_features2d -lopencv_ml -lopencv_calib3d -lopencv_contrib -lopencv_video
Run Code Online (Sandbox Code Playgroud)
然后是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)
Run Code Online (Sandbox Code Playgroud)
这是一个使用 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;
}
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7409 次 |
最近记录: |