0

I have a .tif file from a CT scan which I want to visualize as an image. And later use for training a Convolutional Neural Network.

I would like to preserve as much as possible the original format.

import numpy as np
import tifffile as tiff

a = tiff.imread(local_path+'demd122000_MLO_Left.tif')
a.shape
Out[16]: (227, 227, 3)

np.max(a)
Out[6]: 3590

a.dtype
Out[19]: dtype('uint16')

Which library can show this kind of images?

How could I normalize it without losing information?

Community
  • 1
  • 1
Boels Maxence
  • 349
  • 3
  • 16
  • What do you mean with normalizing images? – João Castilho Apr 03 '20 at 16:41
  • 1
    Try **OpenCV** `cv2.imshow('Title', a.astype(np.float32)/np.max(a))` since it expects values in range 0..1.0 for floats. Follow by `cv2.waitKey(0)` – Mark Setchell Apr 03 '20 at 16:45
  • The output images doesn't charge correctly and forces to close python. Any suggestion for saving the image in any format and open back in python? – Boels Maxence Apr 03 '20 at 16:56
  • It outputs the image. I am a bit worried about the np.max(a) function since I need to work with a data set and comparing on the same basis. Should I normalize by the highest values in the dataset. ex: np.max(np.array(dataset)) ? – Boels Maxence Apr 03 '20 at 17:17
  • @MarkSetchell Can you write your comment as an answer so I can put the green flag on it. – Boels Maxence Apr 03 '20 at 17:42
  • I prefer to only make answers from code I have tested, and just comment otherwise. Doing what I suggested is more *"expedient"* than scientific and it will fail to display smooth gradients and will also make it hard to compare images on an absolute basis. If you can make an answer from my suggestion you are welcome to answer your own question and mark it as correct. – Mark Setchell Apr 03 '20 at 17:47

1 Answers1

1

This code fixes the issue but is reducing the range (normalizing) of the values of the original if file.

 cv2.imshow('Title', a.astype(np.float32)/np.max(a))
Boels Maxence
  • 349
  • 3
  • 16
  • 1
    I guess if you divided by 65,535 instead of `np.max(a)` your results would be directly comparable with each other, they would just potentially be dark and lacking contrast and brightness resolution. – Mark Setchell Apr 03 '20 at 19:49