c++ - Cython C++ 包装器 operator() 重载错误

标签 c++ python opencv cython

与我之前的问题有关。 Using Cython to wrap a C++ class that uses OpenCV types as parameters

现在我陷入了另一个错误。我的 OpenCV 类型 Matx33d 的 cython 包装代码如下所示:

cdef extern from "opencv2/core/core.hpp" namespace "cv":
    cdef cppclass Matx33d "cv::Matx<double, 3, 3>":
        Matx33d()
        Matx33d(double v0, double v1, double v2, double v3, double v4, double v5, double v6, double v7, double v8)
        double& operator()(int i, int j)

然后我定义了一个函数来将 Matx33d 复制到一个 numpy 数组。

cdef Matx33d2numpy(Matx33d &m):
    cdef np.ndarray[np.double_t, ndim=2] np_m = np.empty((3,3), dtype=np.float64)  
    np_m[0,0]= m(0,0); np_m[0,1]= m(0,1); np_m[0,2]= m(0,2)
    np_m[1,0]= m(1,0); np_m[1,1]= m(1,1); np_m[1,2]= m(1,2)
    np_m[2,0]= m(2,0); np_m[2,1]= m(2,1); np_m[2,2]= m(2,2)    
    return np_m

当我编译 cython 包装器时出现这些错误

geom_gateway.cpp(2528) error C3861: '()': identifier not found

这对应于第一次使用 Matx33d::operator(),即在上面的代码中访问 m(0,0) 时。 如果我查看生成的 geom_gateway.cpp 行 2528,我得到:

  *__Pyx_BufPtrStrided2d(__pyx_t_5numpy_double_t *, __pyx_pybuffernd_np_m.rcbuffer->pybuffer.buf, __pyx_t_6, __pyx_pybuffernd_np_m.diminfo[0].strides, __pyx_t_7, __pyx_pybuffernd_np_m.diminfo[1].strides) = operator()(0, 0);

我不明白这个 operator()(0, 0) 单独在行尾没有任何对象!!这怎么可能?这是 Cython 错误吗?还是我用于 operator() 的语法有误? 感谢您的帮助!

最佳答案

好吧,我不知道为什么会发生这个错误,对我来说它看起来像语法

double& operator()(int i, int j)

...应该可以,但没有。此语法适用于其他运算符,如 +-/*

另一种有效的语法如下:

double& get "operator()"(int i, int j)

然后在 cython 代码中,当我们想使用 operator()(i,j) 时,我们调用 get(i, j)

关于c++ - Cython C++ 包装器 operator() 重载错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18690005/

相关文章:

C++ , Cheat Engine/OllyDBG 从多级指针中找到基址 "static"

c++ - Keil uVision4 armcc : Using C++ standard includes <cstdint>

c++ - C++ 中的编码标准/编码最佳实践

c# - 使用 Kinect 和 EMGU(OpenCV 包装器)定位机器人

c++ - OpenCv 输入样本必须是浮点矩阵 ERROR

c++ - VC2008 编译器错误打开 sbr 文件 (C2418 C1903 C2471)

python - 如何在 pip.log 中隐藏用户名和密码?

Python:执行脚本没有错误,不执行任何操作

Python - 在缺少索引的地方用零填充元组列表

c++ - 显示矩阵 opencv 的奇怪行为