码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-02-06 14:38:10      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:asa   home   lib   idt   ESS   col   float   ann   c++   

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

标签:asa   home   lib   idt   ESS   col   float   ann   c++   

原文地址:https://www.cnblogs.com/haiyang21/p/12268319.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!