I want to apply a function to every pixel of an image of shape (width, height, 3) and receive a new matrix of shape (width, height, 2) (XYZ -> u'v'). How can this be accomplished with np.vectorize()?
Is np.vectorize() the fastest solution here or are there better approaches?
I played around with the following code but received the error RuntimeWarning: invalid value encountered in long_scalars (9 * Y) / (X + 15 * Y + 3 * Z). Also it seems to be running forever...
# input is im_xyz with shape (width, height, 3)
im_xyz_reshape = im_xyz.reshape((im_xyz.shape[0] * im_xyz.shape[1], 3))
def xyz2uv(XYZ):
[X,Y,Z] = XYZ
return np.array([
(4 * X) / (X + (15 * Y) + (3 * Z)),
(9 * Y) / (X + 15 * Y + 3 * Z)
])
xyz2uv_v = np.vectorize(xyz2uv, signature='(xyz)->(uv)')
im_uv = xyz2uv_v(im_xyz_reshape).reshape((im.shape[0], im.shape[1], 2))