【发布时间】:2013-01-08 21:04:51
【问题描述】:
我正在尝试使用 np 的矢量化,但 imshow 显示黑色图像,如果我正确理解矢量化,它应该是白色的。我认为问题是输出类型,但我无法让它工作。
import numpy as np
import cv2
class Test():
def run(self):
arr = np.zeros((25,25))
arr[:]=255
cv2.imshow('white',arr)
flatarr = np.reshape(arr,25*25)
vfunc = np.vectorize(self.func)
#vfunc = np.vectorize(self.func,otypes=[np.int])#same effect
flatres = vfunc(flatarr)
shouldbewhite = np.reshape(flatres,(25,25))
cv2.imshow('shouldbewhite',shouldbewhite)
def func(self,a):
return 255
cv2.namedWindow('white',0)
cv2.namedWindow('shouldbewhite',0)
a = Test()
a.run()
cv2.waitKey(0)
【问题讨论】:
-
附带说明,您无需展平(重塑)数组即可使用矢量化。例如这工作
vectorize(lambda x : x + 1)([[1, 2], [3, 4]]) == array([[2, 3], [4, 5]]) -
哇!你说的对!这非常有用。谢谢