-1

I have a bunch of medical images in a hdf5 file.

 path_to_data = "data/train_large_axial_350_x_350.hdf5"
 data = h5py.File(path_to_data,"r")
 image_list = data["images"]
 image = image_list[5]

If I do

 cv2.imwrite("maps/image.jpg", image)

I get:

enter image description here

But, if I do

plt.imshow(image)

I get:

enter image description here

What is the cause of this and how can I plot the image so that it looks like that which is produced by cv2.imwrite?

HansHirse
  • 18,010
  • 10
  • 38
  • 67
GhostRider
  • 2,109
  • 7
  • 35
  • 53

1 Answers1

0

This is because matplotlib plots the image in the range of the gray values of your image. For example, if your image contains gray values between 21 to 88, then matplotlib plots it with this range, and not between 0 and 255.

This can be fixed by the following:

# from matplotlib import cm    
plt.imshow(image, cmap=cm.gray, vmin=0, vmax=255)

For a more in-depth discussion, check out this github issue.

Lalith Nag
  • 36
  • 2