ubuntu18.04 下python调用c++ opencv

  1. 安装下载并配置好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
  2. 示例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;
        }
    }
    

     

  3. 编写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} )

     

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

    cmake .
    make

     

  5. 调用编译成功的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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值