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)
原文地址:https://www.cnblogs.com/haiyang21/p/12268319.html
时间: 2024-11-05 20:49:09