背景介绍
需要将python 处理得到的numpy bgr24数据,通过接口传递给c++,并转换为cv::mat
方法
查了一些资料,
使用ndpointer 的方式测试性能不怎么好,自己摸索下快速传递的方法就是输入地址指针,c 接口定义:
int lib_Mat(int rows, int cols, int type, void* data)
{
cv::Mat frame = cv::Mat(rows, cols, CV_8UC3, data);
return 0;
}
python 的调用方法:
def lib_Mat(rows:int, cols:int, type:int, data):
libfc.lib_Mat.restype = c_int32
libfc.lib_Mat.argtypes = (c_int32,c_int32,c_int32,c_void_p)
return libfc.lib_Mat(rows,cols,type,data)
# frame 为bgr24的numpy 对象
lib_Mat(frame.shape[0],frame.shape[1],1,frame.__array_interface__['data'][0])
注意点
cv::mat 默认的存储格式bgr24,所以需要注意numpy对象的格式也是bgr24,否则需要在转为cv::mat后再进行一次RGB–>BGR的转换。