Unclear difference in displaying the same image by opencv and matplotlib

Question:

During my work on image processing I encounter a strange phenomenon that is not clear to me.

I have an image with dimensions of: (256, 256, 1)
And when I display it with opencv using the following code:

cv2.imshow('image', image)
cv2.waitKey()

I get the following result:

image from opencv

In contrast, when I display it with matplotlib using the following code:

plt.imshow(image, cmap="gray")

I get the following result:

image output from matplotlib

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!

Asked By: Daniel

||

Answers:

I post the answer in this link, Also copy to here:

   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

Answered By: Daniel