codelearner
codelearner

Reputation: 21

Getting black image when saving it with cv2.imwrite

I am trying to save image after calculating local entropy of the image. I tried with cv2.imwrite but it saved black images. I tried with io.imsave. It saved the image but with a lossy conversion due to float32 to uint8 conversion. How to save the images without any loss?

Upvotes: 1

Views: 890

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207425

You have a couple of choices. Either save to a format that supports floating point values, such as:

  • TIFF
  • PFM
  • EXR - as suggested by @Rotem in the comments

Or, use cv2.normalize() to normalize your data's range and type to:

  • 0..255 uint8 and save as JPEG or PNG, or
  • 0..65536 uint16 and save as PNG.

In general, if an image looks black or nearly black, it is because the values are all low and the image has limited contrast. You can bump up the contrast very simply with ImageMagick in your Terminal with:

magick DARKIMAGE.png -auto-level OUTPUT.png

Upvotes: 1

Related Questions