zoukankan      html  css  js  c++  java
  • 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))
  • 相关阅读:
    机器学习框架之sklearn简介
    ubuntu 16.04下使用 python pip的安装问题。
    ubuntu 16.04 搭建git小型服务器
    使用config 来管理ssh的会话
    【linux】su、sudo、sudo su、sudo -i的用法和区别
    【python】确保文件写入结束
    【python】描述符descriptor
    【python】类(资料+疑惑)
    【pymongo】mongodb cursor id not valid error
    【python】继承关系和isinstance
  • 原文地址:https://www.cnblogs.com/hahanonym/p/10679323.html
Copyright © 2011-2022 走看看