ctypes 传递opencv二值化图像给C++函数
生活随笔
收集整理的這篇文章主要介紹了
ctypes 传递opencv二值化图像给C++函数
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
ctypes 傳遞opencv二值化圖像給C++函數(shù)
C++文件,因?yàn)閜ython-opencv的二值化圖像其實(shí)是存在連續(xù)的一個二維numpy數(shù)組中的,且取值為0-255,因此這里是unsigned char*,還有一點(diǎn)要注意,若要打印出matrix[i*columns + j],需要cout<<(int)matrix[i*columns + j];這么寫
// 編譯命令 g++ -o libtryPython.so -shared -fPIC tryPython.cpp #include<iostream> #include<fstream> 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<rows; i++) {for (j=0; j<columns; j++) {if(matrix[i*columns + j]==255){outfile<<1;}else outfile<<(int)matrix[i*columns + j];}outfile<<endl;} } } import cv2 import numpy as np import ctypes import timelower = np.array([19,83,116]) upper = np.array([65,165,245]) COL=160 ROW=120def imgProcess():'''description: 處理圖片得到二值化圖片param {*}return {*}''' global imgoriImg = 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.shapeprint(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()總結(jié)
以上是生活随笔為你收集整理的ctypes 传递opencv二值化图像给C++函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 常用的英文文献引用格式
- 下一篇: 纸鸢|物联网云平台小工具集合常见 MQT