[和小菜鸡一起玩OpenCV] Windows下NumPy ndarray与C++ cv::Mat的互相转换

前言

当我们想要从Python调用C++中的OpenCV方法时,通常需要将Python中的ndarray类型转换成C++中的cv::Mat。本文使用的方法是Boost.python + github上Algomorph的pyboostcvconverte,其中Boost版本为1.70.0。使用的其他软件环境为Windows x64 + CMake 3.15.0 + Visual Studio 2017 + Python 3.6.5 + OpenCV 3.4.6。

Boost库

简介

Boost库是C++的一系列扩展库,根据Boost官网介绍,其中一部分库已经被纳入C++ 11标准,而他们的目标是使更多的Boost库出现在C++ 17标准中,总之是相当厉害。本文中主要用到的是Boost.python,其主要提供了Python的接口,从而方便Python与C++的混合编程。关于Boost以及Boost.python安装编译的相关知识,个人推荐Henry606的这篇博文:C++与Python混合编程:Boost.python的安装与使用

版本

起初我使用的是Boost 1.68.0版本,在运行官方示例代码时,会发生错误'unwind_type': identifier not found。经过一通搜索,在微软相关文档中找到如下文字:

Note: In the Boost python library, there has been for a long time an MSVC-specific workaround for a template forward declaration in unwind_type.hpp. Under /permissive- mode starting with Visual Studio 2017 version 15.8 (_MSC_VER=1915), the MSVC compiler does argument-dependent name lookup (ADL) correctly and is consistent with other compilers, making this workaround guard unnecessary. To avoid error C3861: ‘unwind_type’: identifier not found, see PR 229 in the Boost repo to update the header file. We’ve already patched the vcpkg Boost package, so if you get or upgrade your Boost sources from vcpkg then you don’t need to apply the patch separately.

总之从Visual Studio 2017 version 15.8 起就会出这个错,若要避免就从vcpkg更新Boost源文件。因而在本文中,我使用了Boost 1.70.0版本,这也引发了之后要做一系列配置修改,具体内容在之后详细说明。

编译Boost.python

首先进入Boost源码的目录,执行其下的bootstrap.bat脚本。接着使用b2.exe对我们需要的库进行编译。注意, 由于之后要用到包含头文件的include目录,这里编译要使用Install模式而非通常教程中的Stage模式。由于我想使用的是annaconda下的python,需要额外写一个user-config.jam来指定python路径。user-config.jam的具体内容如下:


                
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值