'How to save an image which only display properly using imshow(grayImg,[])?

I am working on an image encryption project, so I first converts color image to gray and do the rest, after encryption I can see the image only using

imshow(grayImg,[])

Not by

imshow(grayImg)

but if I store the image with

imwrite(grayImg,outputFileName);

Then it isn't saving as expected or not as I saw when using imshow(grayImg,[]) Does anyone know what kind of error am I making?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source