通过IPython动态交互Cython
测试一
斐波那契函数传参测试。
# 我们在jupyter上运行,执行上面代码便会加载Cython的一些魔法函数
In [1]: %load_ext cython
# 加上一个魔法命令,就可以直接写Cython代码
In [2]: %%cython
...: def fib(int n):
...: """这是一个Cython函数,在IPython上编写"""
...: cdef int i
...: cdef double a = 0.0, b = 1.0
...: for i in range(n):
...: a, b = a + b, a
...: return a
# 测试用时
In [6]: %timeit fib(50)
测试二
图像处理函数测试,参数为png文件。
%load_ext cython
%%cython
import cv2
import numpy as np
def rotate_image(angle,image_name):
images = cv2.imread(image_name)
M = cv2.getRotationMatrix2D(((1024)/2,(1024)/2),angle,1)
images = cv2.warpAffine(images,M,(1024,1024))
bChannel,gChannel,rChannel=cv2.split(images)
zero = np.zeros(gChannel.shape, dtype=gChannel.dtype)
ret, thresh= cv2.threshold(gChannel, 100, 255, cv2.THRESH_BINARY)
img_BGRA = cv2.merge(( zero,gChannel,gChannel,thresh))
cv2.imwrite("imageRotate1.png",img_BGRA)
rotate_image(30,"star.png")