python 调用C的DLL案例

前言: python不能直接调用C++只能调用纯C的DLL

此处案例是python模仿opencv的cv2包,但是用c的DLL调用

 

import os
import csv
import time
import ctypes
from ctypes import *
opencv = CDLL("opencv_world310.dll")


class
IplTileInfo(Structure): _fields_ = [] class IplROI(Structure): _fields_ =[ ('coi', c_int), ('xOffset', c_int), ('yOffset', c_int), ('width', c_int), ('height', c_int) ] class IplImage(Structure): def __repr__(self): res = [] for field in self._fields_: if field[0] in ['imageData', 'imageDataOrigin']: continue res.append('%s=%s' % (field[0], repr(getattr(self, field[0])))) return self.__class__.__name__ + '(' + ','.join(res) + ')' IplImage._fields_ = [ ("nSize",c_int), ("ID",c_int), ("nChannels",c_int), ("alphaChannel",c_int), ("depth",c_int), ("dataOrder",c_int), ("origin",c_int), ("align",c_int), ("width",c_int), ("height",c_int), ("imageSize",c_int), ("widthStep",c_int), ("BorderMode",c_int*4), ("BorderConst",c_int*4), ("colorModel",c_char*4), ("channelSeq",c_char*4), ("imageData",c_void_p), ("imageDataOrigin",c_char_p), ("imageId",c_void_p), ("roi", POINTER(IplROI)), ("maskROI", POINTER(IplImage)), ("tileInfo", POINTER(IplTileInfo))] loadimageFunc=opencv.cvLoadImage loadimageFunc.argtypes =[c_char_p,c_int] loadimageFunc.restype = POINTER(IplImage) cvimage = loadimageFunc(mpath,2).contents pt=POINTER(c_char) data=ctypes.cast(cvimage.imageData,pt) # 这里的转换非常重要

等同于

import os
import csv
import time
import cv2   #python opencv
import ctypes
from cv2 import *
from ctypes import *   

cvimage = cv2.imread(mpath, cv2.IMREAD_ANYDEPTH)
data=cvimage.ctypes.data_as(ctypes.POINTER(ctypes.c_char))

 

转载于:https://www.cnblogs.com/hahanonym/p/10679323.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值