Pin*_*Jie 10 python opencv numpy
我按照OpenCV cookbook中的代码进行python接口,将cvMat转换为numpy数组:
mat = cv.CreateMat(3,5,cv.CV_32FC1)
cv.Set(mat,7)
a = np.asarray(mat)
Run Code Online (Sandbox Code Playgroud)
但是在我的电脑上使用OpenCV 2.1,它不起作用.结果一这里是一个对象数组,使用"打印"不打印所有元素一个,只打印<cvmat(type=42424005 rows=3 cols=5 step=20 )>.所以如何将OpenCV Mat对象完全转换为原始的numpy.ndarray对象.
尝试在你的调用中使用附加[:,:]到矩阵(即使用mat[:,:]而不是mat)np.asarray- 这样做也可以asarray处理图像.
你的例子:
>>> import cv
>>> import numpy as np
>>> mat = cv.CreateMat( 3 , 5 , cv.CV_32FC1 )
>>> cv.Set( mat , 7 )
>>> a = np.asarray( mat[:,:] )
>>> a
array([[ 7., 7., 7., 7., 7.],
[ 7., 7., 7., 7., 7.],
[ 7., 7., 7., 7., 7.]], dtype=float32)
Run Code Online (Sandbox Code Playgroud)
对于图像:
>>> im = cv.CreateImage( ( 5 , 5 ) , 8 , 1 )
>>> cv.Set( im , 100 )
>>> im_array = np.asarray( im )
>>> im_array
array(<iplimage(nChannels=1 width=5 height=5 widthStep=8 )>, dtype=object)
>>> im_array = np.asarray( im[:,:] )
>>> im_array
array([[100, 100, 100, 100, 100],
[100, 100, 100, 100, 100],
[100, 100, 100, 100, 100],
[100, 100, 100, 100, 100],
[100, 100, 100, 100, 100]], dtype=uint8)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18499 次 |
| 最近记录: |