通过IPython动态交互Cython

通过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")

在这里插入图片描述

参考链接

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值