In continue to my previous question (
In contrast, when I display it with matplotlib using the following code:
plt.imshow(image, cmap="gray")
(The 'cmap' parameter is not the issue here, It's only plot the image in Black & White)
I get an image the following result:
The second result is the desired one as far as I'm concerned - my question is how to make the image like this (by code only and without the need to save to a file and load the image) and make it so that I get the same image in opencv as well.
I researched the issue but did not find a solution.
This reference helps me understand the reason in general but I'm still don't know how to show the image in opencv like matplotlib view in this case.
Thank you!
CodePudding user response:
You can use the following steps:
- load the data from the file
- truncate the data into 0-255 values, same type as original (float64)
- filtering using the
==
operator, which gives True/False values, then multiplying by 255 to get 0/255 integer values - use
cv2.imshow
combined withastype
to get the required type
import numpy as np
import cv2
if __name__ == '__main__':
data = np.load(r'image.npy')
data2 = np.trunc(data)
data3 = (data2 == 255) * 255
cv2.imshow('title', data3.astype('float32'))
cv2.waitKey(0)
CodePudding user response:
Finally, I found the solution:
int_image = image.astype(np.uint8)
cv2.imshow('image', int_image)
cv2.waitKey()
plt.imshow(image, cmap="gray")
plt.title("image")
plt.show()
Now - The 2 plots are same.
Hope this helps more people in the future