python调用c++接口,参数为opencv读取数据

Posted BlueOceans

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python调用c++接口,参数为opencv读取数据相关的知识,希望对你有一定的参考价值。

opencvcall.cpp

#include <opencv2/opencv.hpp>

using namespace cv;

extern "C"
{
    void test(int height, int width, int channel, uchar* frame_data)
    {
        int count = 0;
        Mat image(height, width, CV_8UC3);
        memcpy(image.data, frame_data, sizeof(uchar) * width * height * channel);
        imwrite("result.jpg", image);

        return;
    }
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

set(PROJECT_NAME test)
set(OPENCV_INSTALL_PTH /home/packages/opencv/)

INCLUDE_DIRECTORIES(${OPENCV_INSTALL_PTH}/include)
list(APPEND LINK_FILES ${OPENCV_INSTALL_PTH}/lib/libopencv_imgproc.so)
list(APPEND LINK_FILES ${OPENCV_INSTALL_PTH}/lib/libopencv_imgcodecs.so)
list(APPEND LINK_FILES ${OPENCV_INSTALL_PTH}/lib/libopencv_core.so)
list(APPEND LINK_FILES ${OPENCV_INSTALL_PTH}/lib/libopencv_tracking.so)
list(APPEND LINK_FILES ${OPENCV_INSTALL_PTH}/lib/libopencv_tracking.so)
list(APPEND LINK_FILES ${OPENCV_INSTALL_PTH}/lib/libopencv_highgui.so)
list(APPEND LINK_FILES ${OPENCV_INSTALL_PTH}/lib/libopencv_videoio.so)

aux_source_directory(src DIR_SRCS)
MESSAGE(STATUS "Src file: ${DIR_SRCS}")

#set(CMAKE_CXX_FLAGS_RELEASE "-w -O3 -std=c++11")
#set(CMAKE_CXX_FLAGS_DEBUG "-w -g -std=c++11")

add_library(${PROJECT_NAME} SHARED ${DIR_SRCS})
target_link_libraries(${PROJECT_NAME} ${LINK_FILES})

test.py

import cv2
import ctypes
import numpy as np

ll = ctypes.cdll.LoadLibrary
lib = ll("./build/libtest.so")
lib.test.restype = ctypes.c_float
frame = cv2.imread(image/test.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.shape[2], frame_data)

 

以上是关于python调用c++接口,参数为opencv读取数据的主要内容,如果未能解决你的问题,请参考以下文章

2022-05-01 python opencv 读取图片返回None

OpenCV视频读取播放,视频转换为图片

在matlab中调用时无法在C++中使用opencv读取图像

OpenCV读取图片判断读取是否成功显示图片的代码(C++代码和Python代码)

c++ 和 python 层之间的 opencv.groupRectangles() 差异

在opencv(C++)中从caffe读取多维数组