【PyElas安装2025】Elas双目立体匹配算法安装Python版本过程记录

任务:在Python环境中运行Elas双目立体匹配算法

环境:Windows11, Visual Studio 2022, Python 3.9

报错1:使用pip install pyelas==1.0.1时报错:

报错2:使用如下代码库,运行python setup.py install安装时报错

  • 报错内容:descriptor.h(39): error C2371: “int8_t”: 重定义;不同的基类型

  • 代码库地址:https://github.com/mkturkcan/PyElas

  • 解决2:对代码库中所有.h文件中typedef __int8 int8_t; 全部注释掉

    #ifndef _MSC_VER
    #include <stdint.h>
    #else
    //  typedef __int8            int8_t;
    typedef __int16           int16_t;
    typedef __int32           int32_t;
    typedef __int64           int64_t;
    typedef unsigned __int8   uint8_t;
    typedef unsigned __int16  uint16_t;
    typedef unsigned __int32  uint32_t;
    typedef unsigned __int64  uint64_t;
    #endif
    
  • 原因2:问题出在 int8_t 类型的重定义。这是因为在代码中可能同时包含了多个定义 int8_t 的头文件,导致编译器报错。

报错3:修改.h文件的int8_t 部分后,继续报错

src/pyelas.cpp(342): error C7556: 不能将指定初始值设定项与非指定初始值设定项混合
error: command 'C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.37.32822\\bin\\HostX86\\x64\\cl.exe' failed with exit code 2
  • 解决3:替换src/pyelas.cpp 的在约341行的结构体代码

    • 原代码

      PyTypeObject ElasType = {
      	PyVarObject_HEAD_INIT(NULL, 0)
      	.tp_name      = "elas.Elas",
      	.tp_basicsize = sizeof(ElasObject),
      	.tp_itemsize  = 0,
      	.tp_flags     = Py_TPFLAGS_DEFAULT,
      	.tp_doc       = "The main Elas class",
      	.tp_methods   = Elas_methods,
      	.tp_members   = Elas_members,
      	.tp_init      = reinterpret_cast<initproc>(Elas_init),
      	.tp_new       = Elas_new,
      };
      
    • 替换代码

      PyTypeObject ElasType = {
      	.ob_base = {0},
      	.tp_name = "elas.Elas",
      	.tp_basicsize = sizeof(ElasObject),
      	.tp_itemsize = 0,
      	.tp_flags = Py_TPFLAGS_DEFAULT,
      	.tp_doc = "The main Elas class",
      	.tp_methods = Elas_methods,
      	.tp_members = Elas_members,
      	.tp_init = reinterpret_cast<initproc>(Elas_init),
      	.tp_new = Elas_new,
      };
      
  • 原因3:问题出在初始化列表中混合了指定成员初始化和非指定成员初始化。在C语言中,初始化结构体时,不能混合使用指定成员初始化(如 .tp_name = "elas.Elas")和非指定成员初始化(如直接赋值 PyVarObject_HEAD_INIT(NULL, 0))。这种混合使用是C语言标准所不允许的。

最后,运行python setup.py install 安装PyElas即可

posted @ 2025-05-21 11:49  Hahnunah  阅读(74)  评论(0)    收藏  举报