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

opencvcall.cpp

#include 

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读取数据)