Reputation: 1
I have a function in which I turn binary images [True, False] in to gray image [0, 255] such that they can be used for opencv function. However, I found out that this particular function takes almost the longest. Is there a way I can optimize this function?
def change_image(image):
unique_values = np.unique(image)
mean_unique = np.mean(unique_values)
if 255 in unique_values:
threshold_image = image > mean_unique
return threshold_image
if True in image:
image = np.where(image==False, 0, 255)
return image
else:
return image
Upvotes: 0
Views: 56
Reputation: 432
To convert binary image to grayscale image, multiply the whole image by 255, so that [0, 1] maps to [0, 255].
Also, grayscale images might not contain 255, so the condition 255 in unique_values
is not a reliable test.
Upvotes: 1