栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

ctypes 传递opencv二值化图像给C++函数

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ctypes 传递opencv二值化图像给C++函数

ctypes 传递opencv二值化图像给C++函数

C++文件,因为python-opencv的二值化图像其实是存在连续的一个二维numpy数组中的,且取值为0-255,因此这里是unsigned char*,还有一点要注意,若要打印出matrix[i*columns + j],需要cout<<(int)matrix[i*columns + j];这么写

// 编译命令 g++ -o libtryPython.so -shared -fPIC tryPython.cpp
#include
#include
using namespace std;
extern "C"{
void show_matrix( unsigned char* matrix, int rows, int columns)
{
    ofstream outfile;
    outfile.open("output.txt");
    int i, j;
    for (i=0; i 
import cv2
import numpy as np
import ctypes
import time



lower = np.array([19,83,116])
upper = np.array([65,165,245])
COL=160
ROW=120

def imgProcess():
    '''
    description: 处理图片得到二值化图片
    param {*}
    return {*}
    '''    
    global img
    oriImg = cv2.imread("0.jpg")
    img_hsv = cv2.cvtColor(oriImg,cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(img_hsv,lower,upper)
    img_resize = cv2.resize(mask,(COL,ROW),interpolation=cv2.INTER_NEAREST)
    element = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
    imgdilate = cv2.dilate(img_resize, element)  # 膨胀
    img = cv2.morphologyEx(imgdilate, cv2.MORPH_CLOSE, element)  # 先腐蚀后膨胀操作,原为腐蚀操作
    print(img.dtype)

def sendToC():
    lib = ctypes.cdll.LoadLibrary("./test.so")
    rows, cols = img.shape
    print(rows,cols)
    start = time.time()
    uint8_type = np.ctypeslib.ndpointer(ctypes.c_ubyte)
    lib.show_matrix.argtypes = [uint8_type,ctypes.c_int,ctypes.c_int]
    lib.show_matrix(img, rows, cols)
    print(time.time()-start)

imgProcess()
sendToC()




转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/844185.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号