Home > Back-end >  problem in visualized image derived from npz file
problem in visualized image derived from npz file

Time:03-08

I am trying to open and visualize images of different ct slices in a npz formatted file to proceed my NN segmentation task later on. I used the following code:

data = np.load('case0005_slice060.npz')
image = data['image']
img = Image.fromarray(image,'RGB')

and finally, I could visualize the image, but it seems there is a problem somewhere that I can't understand. here is the output (the problem is that I may need to solve overlapped images but I don't know how)

enter image description here

CodePudding user response:

Thanks. I normalized the image which was float32 and removed the RGB here is the final code:

from numpy import load
import matplotlib.pyplot as plt
def normalize8(I):
    mn = I.min()
    mx = I.max()

    mx -= mn

    I = ((I - mn)/mx) * 255
    return I.astype(np.uint8)

data = np.load('case0005_slice060.npz')
print(data.files)
image = data['image']
image=normalize8( image)
img = Image.fromarray(image)

enter image description here

CodePudding user response:

First, check what you have. You need to know the shape and type of your Numpy array:

print(image.shape, image.dtype)

If the shape is of the form h,w,3 it's likely RGB. If of the form h,w, it's likely greyscale and you could put 'L' as the mode when creating your PIL Image from it instead of 'RGB', though you can normally leave the mode out and it is inferred from the Numpy array shape.

You then need to consider the dtype. If np.uint8, you're all set. If other than that, you may need to provide one of the modes PIL accepts, or cast with something like:

img = Image.fromarray(image.astype(np.uint8))
  • Related