- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个用 Boost Python 包装的 C++ 类。
其中一个类方法接收两个 cv::Mat
,如下所示:
MyClass::do_something(cv::Mat input, cv::Mat output)
我用 python 提供的功能包括上述方法、一个构造函数和一些打印方法。
初始化和打印方法(用于调试)在 C++ 和 Python 包装器中运行良好:
obj = MyClass(arg1, arg2, arg3)
obj.print_things()
这些调用成功完成。
我遇到了 do_something()
调用的问题(在 Python 绑定(bind)中,它在 C++ 中成功完成):
from libmyclass import *
import cv
rgb = cv.CreateMat(256,256,cv.CV_8UC3)
result = cv.CreateMat(256,256,cv.CV_8UC3)
#...fill "rgb"
obj.do_something(rgb,result)
我在执行上面的python代码时得到的错误是:
Boost.Python.ArgumentError: Python argument types in
MyClass.do_something(MyClass, cv2.cv.cvmat, cv2.cv.cvmat)
did not match C++ signature:
do_something(MyClass {lvalue}, cv::Mat, cv::Mat)
这是 cv2.cv.Mat 和 cv::Mat 之间的差异吗?我有 OpenCV 2.3.1 和 2.4,它们都带有 Boost Python 绑定(bind)。
如果相关,这是我的 Boost 包装器的样子:
#include <boost/python.hpp>
#include "MyClass.h"
#include <cv.h>
using namespace boost::python;
BOOST_PYTHON_MODULE(libmyclass) {
class_<MyClass>("MyClass", init<std::string, std::string, std::string>())
.def("print_things", &MyClass::print_things)
.def("do_something", &MyClass::do_something)
;
}
最佳答案
Boost python 不会自动将您的 cv2.cv.Mat(在 python 中)转换为 cv::Mat (C++)。
您将需要声明您的 C++ 方法以采用 boost::object * 并在 C++ 中使用额外代码将对象转换为 cv::Mat。
这是我为包装 STASM 事件形状模型库所做的示例
#ifndef ASMSearcher_HPP
#define ASMSearcher_HPP
#include <string>
#include <boost/python.hpp>
#include <opencv2/core/core.hpp>
class ASMSearcher;
/*
* Wrapper around STASM ASMSearcher class so that we don't mix boost python code into the STASM library.
*/
struct memtrack_t {
PyObject_HEAD
void *ptr;
Py_ssize_t size;
};
struct cvmat_t
{
PyObject_HEAD
CvMat *a;
PyObject *data;
size_t offset;
};
struct iplimage_t {
PyObject_HEAD
IplImage *a;
PyObject *data;
size_t offset;
};
namespace bp = boost::python;
class Stasm
{
public:
Stasm();
Stasm(const std::string &conf_file0, const std::string &conf_file1);
~Stasm();
bp::list detect(bp::object image, const std::string &conf_file0="",
const std::string &conf_file1="");
private:
ASMSearcher *asmLandmarksSearcher;
cv::Mat convertObj2Mat(bp::object image);
cv::Mat convert_from_cviplimage(PyObject *o,const char *name);
cv::Mat convert_from_cvmat(PyObject *o, const char* name);
};
#endif
#include "stasm.hpp"
#include "stasm_ocv.hpp"
#include <opencv2/highgui/highgui.hpp>
Stasm::Stasm()
{
asmLandmarksSearcher = NULL;
}
Stasm::~Stasm()
{
if (asmLandmarksSearcher != NULL)
delete asmLandmarksSearcher;
}
Stasm::Stasm(const std::string &conf_file0, const std::string &conf_file1)
{
asmLandmarksSearcher = new ASMSearcher(conf_file0, conf_file1);
}
/*Detect asm facial landmarks in image*/
bp::list Stasm::detect(bp::object image,
const std::string &conf_file0,
const std::string &conf_file1)
{
const char *file0 = conf_file0 == "" ? NULL : conf_file0.c_str();
const char *file1 = conf_file1 == "" ? NULL : conf_file1.c_str();
// Convert pyobject to IplImage/Mat etc.
cv::Mat img = convertObj2Mat(image);
bool isColor = img.channels() == 3 ? true : false;
int nlandmarks;
int landmarks[500]; // space for x,y coords of up to 250 landmarks
asmLandmarksSearcher->search(&nlandmarks, landmarks,
"image_name", (const char*)img.data, img.cols, img.rows,
isColor /* is_color */, file0 /* conf_file0 */, file1 /* conf_file1 */);
//isColor /* is_color */, NULL /* conf_file0 */, NULL /* conf_file1 */);
// Convert landmarks to python list object
bp::list pyLandmarks;
for (int i = 0; i < 2*nlandmarks; i++)
pyLandmarks.append(landmarks[i]);
return pyLandmarks;
}
cv::Mat Stasm::convert_from_cvmat(PyObject *o, const char* name)
{
cv::Mat dest;
cvmat_t *m = (cvmat_t*)o;
void *buffer;
Py_ssize_t buffer_len;
m->a->refcount = NULL;
if (m->data && PyString_Check(m->data))
{
assert(cvGetErrStatus() == 0);
char *ptr = PyString_AsString(m->data) + m->offset;
cvSetData(m->a, ptr, m->a->step);
assert(cvGetErrStatus() == 0);
dest = m->a;
}
else if (m->data && PyObject_AsWriteBuffer(m->data, &buffer, &buffer_len) == 0)
{
cvSetData(m->a, (void*)((char*)buffer + m->offset), m->a->step);
assert(cvGetErrStatus() == 0);
dest = m->a;
}
else
{
printf("CvMat argument '%s' has no data", name);
//failmsg("CvMat argument '%s' has no data", name);
}
return dest;
}
cv::Mat Stasm::convert_from_cviplimage(PyObject *o,const char *name)
{
cv::Mat dest;
iplimage_t *ipl = (iplimage_t*)o;
void *buffer;
Py_ssize_t buffer_len;
if (PyString_Check(ipl->data)) {
cvSetData(ipl->a, PyString_AsString(ipl->data) + ipl->offset, ipl->a->widthStep);
assert(cvGetErrStatus() == 0);
dest = ipl->a;
} else if (ipl->data && PyObject_AsWriteBuffer(ipl->data, &buffer, &buffer_len) == 0) {
cvSetData(ipl->a, (void*)((char*)buffer + ipl->offset), ipl->a->widthStep);
assert(cvGetErrStatus() == 0);
dest = ipl->a;
} else {
printf("IplImage argument '%s' has no data", name);
}
return dest;
}
cv::Mat Stasm::convertObj2Mat(bp::object image)
{
if(strcmp(image.ptr()->ob_type->tp_name,"cv2.cv.iplimage") == 0)
{
return convert_from_cviplimage(image.ptr(),image.ptr()->ob_type->tp_name);
}
else
return convert_from_cvmat(image.ptr(), image.ptr()->ob_type->tp_name);
}
测试它的示例代码如下所示:
#!/usr/bin/env python
import cv2
import pystasm
import numpy as np
import sys
DEFAULT_TEST_IMAGE = "428.jpg"
def getFacePointsMapping():
mapping = {}
fhd = open('mapping2.txt')
line = fhd.readline()
a = line.split()
for i, n in enumerate(a):
mapping[int(n)] = i
return mapping
def drawFaceKeypoints(img, landmarks):
mapping = getFacePointsMapping()
numpyLandmarks = np.asarray(landmarks)
numLandmarks = len(landmarks) / 2
numpyLandmarks = numpyLandmarks.reshape(numLandmarks, -1)
for i in range(0, len(landmarks) - 1, 2):
pt = (landmarks[i], landmarks[i+1])
#cv2.polylines(img, [numpyLandmarks], False, (0, 255, 0))
number = mapping[i/2]
cv2.circle(img, pt, 3, (255, 0, 0), cv2.cv.CV_FILLED)
cv2.putText(img, str(number), pt, cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0, 0, 255))
return img
def getFacePointsMapping():
mapping = []
fhd = open('mapping2.txt')
line = fhd.readline()
a = line.split()
for n in a:
mapping.append(n)
return mapping
def main():
asmsearcher = pystasm.Stasm('mu-68-1d.conf', 'mu-76-2d.conf')
if len(sys.argv) == 2:
imagename = sys.argv[1]
else:
imagename = DEFAULT_TEST_IMAGE
# Detect facial keypoints in image
img = cv2.imread(imagename)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
landmarks = asmsearcher.detect(cv2.cv.fromarray(img))
img = drawFaceKeypoints(img, landmarks)
#numpyLandmarks = np.asarray(landmarks)
#numLandmarks = len(landmarks) / 2
#numpyLandmarks = numpyLandmarks.reshape(numLandmarks, -1)
#for i in range(0, len(landmarks) - 1, 2):
# pt = (landmarks[i], landmarks[i+1])
# #cv2.polylines(img, [numpyLandmarks], False, (0, 255, 0))
# number = mapping[i/2]
# cv2.circle(img, pt, 3, (255, 0, 0), cv2.cv.CV_FILLED)
# cv2.putText(img, str(number), pt, cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0, 0, 255))
cv2.imshow("test", img)
cv2.waitKey()
if __name__ == '__main__':
main()
抱歉,我没有时间清理代码。请注意,您需要调用 cv2.cv.fromarray(numpy_array) 才能使其正常工作。我仍在尝试弄清楚如何将 numpy 数组直接传递给 python boost。如果您已经弄明白了,请告诉我 :)。
顺便说一句,我应该补充一点,用于转换 boost 对象和 opencv 的 IplImage 和 Mat 的代码取自 OpenCV 的源代码。
关于c++ - 使用 cv::Mat boost Python 包装器和 OpenCv 参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12008536/
我到处都找了很多,找不到我的问题的答案。我试图从这个线程复制一个文本检测软件(Extracting text OpenCV)但是在代码的末尾有一条消息错误说没有匹配的矩形,即使我已经在上面绘制了一个并
我已经彻底搜索过,但没有找到直接的答案。 将 opencv 矩阵 (cv::Mat) 作为参数传递给函数,我们传递的是智能指针。我们对函数内部的输入矩阵所做的任何更改也会改变函数范围之外的矩阵。 我读
在我的应用程序中,我有一个通过引用接收 cv::Mat 对象的函数。这是函数的声明: void getChains(cv::Mat &img,std::vector &chains,cv::
我正在使用 Qt 编写一个 GUI 程序,并使用 OpenCV 进行一些视频处理。我在主 GUI 线程的标签中显示 OpenCV 进程(在单独的线程中)的结果。 我遇到的问题是 cv::waitKey
Mat a = (Mat_(3,3) = 2 int dims; //! the number of rows and columns or (-1, -1) when the arr
我尝试运行下面的代码,但出现错误。我正在为名为“Mat::at”的 OpenCV 函数创建一个包装器,并尝试使用“G++”将其编译为 Ubuntu Trusty 上的“.so”。我在下面列出了“.cp
我在 C# 中使用 EmguCV,当我想从网络摄像头抓取帧时遇到问题,语句中出现红色下划线: imgOrg = capturecam.QueryFrame(); error: Cannot impli
我正在尝试从另外两个矩阵生成一个 cv::Mat C,以便获得第三个矩阵,该矩阵由通过组合矩阵 A 和 B 的一维点生成的二维点构成。 我的问题是,我尝试的所有操作都只是连接矩阵,并没有真正将每个点与
我用 cv.imread在 python 中读取 png 文件。然后当我使用 cv.imwrite立即保存图像的功能我然后发现图像中的颜色略有变化。我正在尝试在此图像上执行字符识别,而 OCR 在 p
我尝试将 cv::bitwise_not 转换为 double 值的 cv::Mat 矩阵。我申请了 cv::bitwise_not(img, imgtemp); img是0和1的CV_64F数据。但
我正在尝试使用函数 cv.glmnet 找到最佳的 lambda(使用 RIDGE 回归)以预测某些对象的归属类别。所以我使用的代码是: CVGLM<-cv.glmnet(x,y,nfolds=34,
我有这个方法: static void WriteMatVect(const std::string& filename, const std::vector& mats); ... void Fil
下面的转换是我想要做的。 对于源图像中的每个图 block ,我知道每个角的坐标,并且我知道输出图像中每个对应角的坐标,所以我可以调用 cvWarpPerspective 扭曲每个图 block ,然
我必须在C++ / CLI中的托管和非托管代码中都使用OpenCV。 我正在尝试在托管代码中使用Emgu CV来包装OpenCV对象,但是在进行转换时遇到了麻烦。 我该怎么做: Emgu::CV::M
我正在尝试在 cv::Mat 中使用 CV_32FC4,以便它存储 RGBA32 图像。但是当我使用 cv::imwrite 将其保存为 png 文件时,结果文件始终是一个空图像。 例如,我创建了这样
无法在 VS 2017 中设置 OpenCV。我做错了什么?是的,我已将所有其他帖子设为红色。 代码: #include "opencv2/highgui/highgui.hpp" u
我有两个(相同大小,相同类型)cv:Mat 让我们称它们为 A,B。我还有另一个 cv::Mat,它是一个掩码(0 和 1 值或其他值,0 和 255 也适用)让我们称它为 M。 我需要构造一个新的
使用 OpenCV 中实现的 Scalar 类,我不明白这段代码有什么区别: Mat test; test = Scalar::all(0); 还有这个: Mat test = Scalar::all
我对这行代码感到困惑: cv::Mat_::iterator 我知道 Mat_ 属于 cv 命名空间和 vec3b 也。但是之后的最后一个 :: 操作符和 iterator 让我感到困惑!它也属于 c
我想优雅地将 Mat 转换为 Vec3f。目前我是这样做的: Mat line; Vec3f ln; ln[0] = line.
我是一名优秀的程序员,十分优秀!