本文介绍了将numpy数组转换为cython指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个来自cv2.imread
的numpy数组,所以dtype = np.uint8
和ndim = 3
也是如此.
I have a numpy array which came from a cv2.imread
and so has dtype = np.uint8
and ndim = 3
.
我想将其转换为Cython unsigned int*
,以便与外部cpp库一起使用.
I want to convert it to a Cython unsigned int*
for use with an external cpp library.
我正在尝试cdef unsigned int* buff = <unsigned int*>im.data
,但是出现错误Python objects cannot be cast to pointers of primitive types
I am trying cdef unsigned int* buff = <unsigned int*>im.data
however I get the error Python objects cannot be cast to pointers of primitive types
我在做什么错了?
谢谢
推荐答案
感谢您的评论.解决方法:
thanks for your comments. solved by:
cdef np.ndarray[np.uint32_t, ndim=3, mode = 'c'] np_buff = np.ascontiguousarray(im, dtype = np.uint32)
cdef unsigned int* im_buff = <unsigned int*> np_buff.data
这篇关于将numpy数组转换为cython指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!