惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
Microsoft Security Blog
Microsoft Security Blog
云风的 BLOG
云风的 BLOG
B
Blog
The Register - Security
The Register - Security
L
LangChain Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
N
Netflix TechBlog - Medium
F
Full Disclosure
The GitHub Blog
The GitHub Blog
Recorded Future
Recorded Future
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Blog — PlanetScale
Blog — PlanetScale
Jina AI
Jina AI
美团技术团队
宝玉的分享
宝玉的分享
Hugging Face - Blog
Hugging Face - Blog
阮一峰的网络日志
阮一峰的网络日志
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
D
DataBreaches.Net
Martin Fowler
Martin Fowler
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - Franky
The Cloudflare Blog
博客园 - 【当耐特】
U
Unit 42
月光博客
月光博客
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
博客园 - 聂微东
I
InfoQ
B
Blog RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
Cyberwarzone
Cyberwarzone
V
V2EX
S
Securelist
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Security @ Cisco Blogs
PCI Perspectives
PCI Perspectives
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Heimdal Security Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Hacker News
The Hacker News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Tor Project blog

博客园 - 星际浪子

In Theano, how to do Reverse-MaxPooling in the Convolutional MaxPooling Auto-Encoder 如何让Ubuntu中virtualbox虚拟机用上USB设备 Deep Learning Stuffs(持续更新中……) 放弃MATLAB!简述winpython为什么比MATLAB更方便 虚拟机Ubuntu10.04无法显示windows中文目录和文件 W: GPG 错误 用debugfs挂载硬盘,删除损坏的文件 数据资源 机器学习资源 镜头的参数指标 PCA和ICA GIT diff 命令 GIT 常用命令手册 opencv中snake的调用方法示例 偏最小二乘法回归(Partial Least Squares Regression) Linux常用几种shell Git详解-Git分支 网线连接并PPPOE拨号后无法扫描无线网络问题的解决办法 gdb 命令详细解释
实例描述如何用python组件ctypes调用c的dll中的函数
星际浪子 · 2014-01-20 · via 博客园 - 星际浪子

工具:winpython,vc2010(我认为只要能生成dll的编译工具都行)

步骤1:

新建一个VC的DLL工程zm_funs,实现函数:

 1 #include "stdio.h"
 2 #include "string.h"
 3 
 4 typedef unsigned char uchar;
 5 //__declspec(dllexport)
 6 void gen_lbp59img(uchar *pu8Img, uchar *pu8LBP59Img, int dwW, int dwH, uchar *pu8LBP89Table)
 7 {
 8     int dwRI, dwCI, dwPI;
 9     int dwOft, dwVal;
10     int adwOffset8[8] = {-dwW - 1, -dwW, -dwW + 1, 1, dwW + 1, dwW, dwW - 1, -1};
11 
12     memset(pu8LBP59Img, 0, dwW * dwH);
13 
14     for (dwRI = 1; dwRI < dwH - 1; dwRI++)
15     {
16         for (dwCI = 1; dwCI < dwW - 1; dwCI++)
17         {
18             dwOft = dwRI * dwW + dwCI;
19             dwVal = 0;
20             for (dwPI = 0; dwPI < 8; dwPI++)
21             {
22                 if (pu8Img[dwOft + adwOffset8[dwPI]] > pu8Img[dwOft])
23                 {
24                     dwVal += 1 << (7 - dwPI);
25                 }
26             }
27             pu8LBP59Img[dwOft] = pu8LBP89Table[dwVal] - 1;
28         }
29     }
30 
31     return;
32 }

gen_lbp59img

用def文件或__declspec(dllexport)将函数名gen_lbp59img导出。然后编译成DLL(DLL的名字是zm_funs.dll)。

步骤2:

打开winpython,写一个同名的函数(之所以同名是为了好记)用来准备好要传给DLL中函数的数据:

 1 import scipy as sp
 2 import ctypes as ct
 3 
 4 zmfuns = ct.CDLL('zm_funs.dll')
 5 
 6 def gen_lbp59img(img, lbptable):
 7     sz = img.shape
 8     if len(sz) > 2:
 9         print 'can''t handle color image.'
10         return
11     imgh, imgw = sz
12     lbpimg = sp.zeros(sz, dtype = sp.uint8)
13 #    call gen_lbp59img of c
14     zmfuns.gen_lbp59img(img.ctypes, lbpimg.ctypes, imgw, imgh, lbptable.ctypes)
15     
16     return lbpimg

gen_lbp59img

注意,传进去的数据是array中的ctypes。实际上,array中的ctypes记录的是指向array数据的地址。

结果:

经过测度,用python实现gen_lbp59img处理QCIF的灰度图像,需要耗时200ms左右,而用C实现后,耗时不到1.5ms。这差距太大了吧!看来PYTHON虽然方便了编程,但是耗时太可观了!