ubuntu18.04 下python调用c++ opencv

mac2025-09-30  10

安装下载并配置好opencv环境https://blog.csdn.net/weixin_39875161/article/details/92005225#%E4%BA%8C%E3%80%81%E5%AE%89%E8%A3%85C%2FC%2B%2B%E6%8F%92%E4%BB%B6示例cpp代码如下,命名为opencvcall.cpp #include <opencv2/opencv.hpp> using namespace cv; extern "C" { void test(int height, int width, uchar* frame_data) { int count = 0; Mat image(height, width, CV_8UC3); uchar* pxvec =image.ptr<uchar>(0); for(int row = 0; row < height; row++) { pxvec = image.ptr<uchar>(row); for(int col = 0; col < width; col++) { for(int c = 0; c < 3; c++) { pxvec[col*3+c] = frame_data[count]; count++; } } } imshow("result", image); waitKey(0); return; } }

 

编写cmake文件,命名为CMakeLists.txt

cmake_minimum_required(VERSION 2.8) project(test ) find_package( OpenCV REQUIRED ) add_library(test SHARED opencvcall.cpp) target_link_libraries( test ${OpenCV_LIBS} )

 

在文件目录下打开终端,执行

cmake . make

 

调用编译成功的c++程序,运行下列程序测试是否成功

import cv2 import ctypes import numpy as np ll = ctypes.cdll.LoadLibrary lib = ll("./libtest.so") lib.test.restype = ctypes.c_float frame = cv2.imread('1.jpg') frame_data = np.asarray(frame, dtype=np.uint8) frame_data = frame_data.ctypes.data_as(ctypes.c_char_p) lib.test(frame.shape[0], frame.shape[1], frame_data)

 

最新回复(0)