Reputation: 5708
i am trying to use np's vectorize but imshow is showing a black image where it should be white if i understand vectorize correctly. i think the problem is the outputtype but i cant get it to work.
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)
Upvotes: 2
Views: 2117
Reputation: 67427
From the docs :
The function imshow displays an image in the specified window. If the window was created with the CV_WINDOW_AUTOSIZE flag, the image is shown with its original size. Otherwise, the image is scaled to fit the window. The function may scale the image, depending on its depth:
- If the image is 8-bit unsigned, it is displayed as is.
- If the image is 16-bit unsigned or 32-bit integer, the pixels are divided by 256. That is, the value range [0,255*256] is mapped to [0,255].
- If the image is 32-bit floating-point, the pixel values are multiplied by 255. That is, the value range [0,1] is mapped to [0,255].
If you run the following code:
class Test():
def run(self):
arr = np.zeros((25,25))
arr[:]=255
print arr.dtype
flatarr = np.reshape(arr,25*25)
vfunc = np.vectorize(self.func)
flatres = vfunc(flatarr)
print flatres.dtype
shouldbewhite = np.reshape(flatres,(25,25))
print shouldbewhite.dtype
def func(self,a):
return 255
You'll get something like:
float64
int32
int32
So your second case is divided by 256, and it being integer division, it rounds off to 0. Try with
vfunc = np.vectorize(self.func,otypes=[np.uint8])
and you may also want to consider replacing the first array with
arr = np.zeros((25,25), dtype='uint8')
Upvotes: 5