Preventing auto scale for 16-bit image data - python

When i try to save 16-bit image data in python, pixel values are automatically scaled to 65536. For example, max value in 16-bit data is 1536. If i saved and read this data, then max value becomes 65k. How to prevent this scaling? I tried different imaging library including opencv, PIL, misc, imageio , but all of these used this scaling.
For example:
img_arr # let it contains 16-bit gray-scale image data
print np.amax(img_arr) # it prints 1536
cv2.imwrite(img_arr, "asd.png")
img_arr = cv2.imread("asd.png")
print np.amax(img_arr) # it prints 65k value

Using OpenCV, this does what you seem to want.
import cv2
import numpy as np
# Synthesize 200x200 uint16 image with 39,999 as max value
im = np.arange(0,40000,dtype=np.uint16).reshape((200,200))
# Write to disk
cv2.imwrite('result.png',im)
# Re-read from disk
rr = cv2.imread('result.png',cv2.IMREAD_UNCHANGED)
# Check maximum value
print(rr.max()) # prints 39999
PIL/Pillow doesn't support 16-bit greyscale, so you'd need to promote to 32-bit signed, as so:
from PIL import Image
import numpy as np
# Synthesize 32-bit signed image
im = np.arange(0,40000,dtype=np.int32).reshape((200,200))
# Make into PIL Image
pim = Image.fromarray(im,'I')
# Save as 16-bit PNG, or 16-bit NetPBM PGM
pim.save('result.png')
pim.save('result.pgm')
# Re-read either file for same result, namely 39999
rr = np.array(Image.open('result.pgm')).max()
rr = np.array(Image.open('result.png')).max()

Related

How to combine 3 TIFF images into 1 PNG image with python?

I have 1 tif image for each RGB colour channel, and I would like to combine the 3 images to make a single RGB image with all 3 channels in png format using python. I have tried several experiments using the PIL library but I can't get it.
I uploaded 3 sample images to Google Drive here. Does anyone know how to do this?
The answer depends on what you are really trying to achieve...
If you want an accurate merge of the 3 channels, you should probably use the tifffile module to understand the floating point values in your input files and accurately represent them in your output files. In fact, gdal would probably be even better as it understands the GeoTIFF tags in your file. PIL is unable to handle RGB float32 images.
If you want something that vaguely allows some sort of approximate visualisation as a PNG, you will need to do some work to scale your values to something sensible (but not accurate) because PNG cannot represent float data like your images contain.
Here is a more accurate merge of your channels with tifffile:
from tifffile import imread, imwrite
import numpy as np
r = imread('r.tif')
g = imread('g.tif')
b = imread('b.tif')
RGB = np.dstack((r,g,b))
imwrite('result.tif', RGB)
With PIL you would use Image.merge() but your data is float, so you will need to convert it to uint8/uint16 first to get something you can store in a PNG:
from PIL import Image
import numpy as np
# Open images
red = Image.open('red_channel.tif')
green = Image.open('green_channel.tif')
blue = Image.open('blue_channel.tif')
# Convert PIL Images to Numpy arrays
npRed = np.array(red)
npGreen = np.array(green)
npBlue = np.array(blue)
# Get rid of the pesky -3.4e+38 marker for out-of-bounds pixels
npRed[npRed < 0] = 0
npBlue[npBlue < 0] = 0
npGreen[npGreen < 0] = 0
# Find maximum across all channels for scaling
max = np.max([npRed,npGreen,npBlue])
# Scale all channels equally to range 0..255 to fit in a PNG (could use 65,535 and np.uint16 instead)
R = (npRed * 255/max).astype(np.uint8)
G = (npGreen * 255/max).astype(np.uint8)
B = (npBlue * 255/max).astype(np.uint8)
# Build a PNG
RGB = np.dstack((R,G,B))
Image.fromarray(RGB).save('result.png')

Convert 16-bit Tiff image to 8-bit RGB

I'm dealing with some satellite images, consisting of 16-bit .tiff images. The color is encoded as 16-bit per channel. I would like to know how I can convert these images to normal 8-bit RGB for further CNN processing.
I have tried OpenCV (cv2.read('file',-1)) and PIL (read('file')), but these two packages cannot recognize and read 16-bit tiff images.
Generally, when you want to read or write images in Python — of any bit-depth and format — it is best to use ImageIO. As the name suggests, its singular goal is to input/output images. Only caveat: It may ignore the image's meta data. That is: It may not deal correctly with images defining a color space other than the standard sRGB, or it might fail to preserve the image's intended orientation.
You would read in the image, say example.tif, like so:
import imageio
image = imageio.imread('example.tif')
As for the conversion, that's just basic math. The data structure in which you'll receive the pixel data is a NumPy array. Introspect image.shape and image.dtype. You should expect your images to have a shape of (y, x, 3), where y is the number of pixels in the vertical, x in the horizontal direction, and 3 represents the three color channels: red, green, blue. Its dtype (data type) should be uint16, meaning unsigned 16-bit integers.
Side note: As there are three color channels, each sampled with a 16-bit resolution, the color depth of the image is more commonly described as "48 bits" (per pixel).
16-bit integer numbers range between 0 and 65535 (= 216−1). They need to be coerced to the 8-bit range: 0 to 255 (= 28−1). So divide by 256 (= 28):
image = image / 256
This will yield an array of floating-point pixel values. Its data type must be explicitly cast to 8-bit integer in order to drop any fractions.
image = image.astype('uint8')
Equivalently, and more efficiently, you may also bit-shift the 16-bit values 8 bits to the right:
image = (image >> 8).astype('uint8')
This makes the conversion faster (by a factor of 2 or so on modern hardware) as it skips the floating-point operations.
Then, either use the final image array for further processing, or save it to a new file:
imageio.imwrite('example.png', image)
If all you want is to convert, your .tiff file's color space to RGB. Then Try:-
from PIL import Image
img = Image.open(r"Path_to_tiff_image")
img = img.convert("RGB")
img.save(r"path_of_destination_image")
The above code, first opens a .tiff image, then changes its color mode to RGB. And then saves it to the destination location.
Hey I used tifffile to handle the file and a calculation that I've found in a different thread here for rescaling the 16-bit image to 8-bit.
import numpy as np
import tifffile as tif
import cv2
image = tif.imread('/home/trance/test.tiff')
# Rescale 16-bit to 8-bit
img_rescaled = 255 * (image - image.min()) / (image.max() - image.min())
# Colourising image and saving it with opencv
img_col = cv2.applyColorMap(img_rescaled.astype(np.uint8), cv2.COLORMAP_INFERNO)
cv2.imwrite('/home/trance/test.png', img_col)

Skimage - Weird results of resize function

I am trying to resize a .jpg image with skimage.transform.resize function. Function returns me weird result (see image below). I am not sure if it is a bug or just wrong use of the function.
import numpy as np
from skimage import io, color
from skimage.transform import resize
rgb = io.imread("../../small_dataset/" + file)
# show original image
img = Image.fromarray(rgb, 'RGB')
img.show()
rgb = resize(rgb, (256, 256))
# show resized image
img = Image.fromarray(rgb, 'RGB')
img.show()
Original image:
Resized image:
I allready checked skimage resize giving weird output, but I think that my bug has different propeties.
Update: Also rgb2lab function has similar bug.
The problem is that skimage is converting the pixel data type of your array after resizing the image. The original image has a 8 bits per pixel, of type numpy.uint8, and the resized pixels are numpy.float64 variables.
The resize operation is correct, but the result is not being correctly displayed. For solving this issue, I propose 2 different approaches:
To change the data structure of the resulting image. Prior to changing to uint8 values, the pixels have to be converted to a 0-255 scale, as they are on a 0-1 normalized scale:
# ...
# Do the OP operations ...
resized_image = resize(rgb, (256, 256))
# Convert the image to a 0-255 scale.
rescaled_image = 255 * resized_image
# Convert to integer data type pixels.
final_image = rescaled_image.astype(np.uint8)
# show resized image
img = Image.fromarray(final_image, 'RGB')
img.show()
Update: This method is deprecated, as per scipy.misc.imshow
To use another library for displaying the image. Taking a look at the Image library documentation, there isn't any mode supporting 3xfloat64 pixel images. However, the scipy.misc library has the appropriate tools for converting the array format in order to display it correctly:
from scipy import misc
# ...
# Do OP operations
misc.imshow(resized_image)

How to access RGB pixel arrays from DICOM files using pydicom?

I try to access a DICOM file's RGB pixel array with unknown compression (maybe none). Extracting grayscale pixel arrays works completely fine.
However, using
import dicom
import numpy as np
data_set = dicom.read_file(path)
pixel_array = data_set.pixel_array
size_of_array = pixel_array.shape
if len(size_of_array ) == 3:
chanR = pixel_array[0][0:size_of_array[1], 0:size_of_array[2]]
chanG = pixel_array[1][0:size_of_array[1], 0:size_of_array[2]]
chanB = pixel_array[2][0:size_of_array[1], 0:size_of_array[2]]
output_array = (0.299 ** chanR) + (0.587 ** chanG) + (0.114 ** chanB)
with the goal to convert it to an common grayscale array. Unfortunately the result array output_array is not containing correct pixel data. Contents are not false scaled, they are spatially disturbed. Where is the issue?
It is not RGB pixel array and the better way is converting to gray image.
The way to get CT Image is to get the attribute of pixel_array in CT dicom file.
The type of elements in pixel_array of CT dicom file are all uint16.But a lot of tool in python, like OpenCV, Some AI stuff, cannot be compatible with the type.
After getting pixel_array (CT Image) from CT dicom file, you always need to convert the pixel_array into gray image, so that you can process this gray image by a lot of image processing tool in python.
The following code is a working example to convert pixel_array into gray image.
import matplotlib.pyplot as plt
import os
import pydicom
import numpy as np
# Abvoe code is to import dependent libraries of this code
# Read some CT dicom file here by pydicom library
ct_filepath = r"<YOUR_CT_DICOM_FILEPATH>"
ct_dicom = pydicom.read_file(ct_filepath)
img = ct_dicom.pixel_array
# Now, img is pixel_array. it is input of our demo code
# Convert pixel_array (img) to -> gray image (img_2d_scaled)
## Step 1. Convert to float to avoid overflow or underflow losses.
img_2d = img.astype(float)
## Step 2. Rescaling grey scale between 0-255
img_2d_scaled = (np.maximum(img_2d,0) / img_2d.max()) * 255.0
## Step 3. Convert to uint
img_2d_scaled = np.uint8(img_2d_scaled)
# Show information of input and output in above code
## (1) Show information of original CT image
print(img.dtype)
print(img.shape)
print(img)
## (2) Show information of gray image of it
print(img_2d_scaled.dtype)
print(img_2d_scaled.shape)
print(img_2d_scaled)
## (3) Show the scaled gray image by matplotlib
plt.imshow(img_2d_scaled, cmap='gray', vmin=0, vmax=255)
plt.show()
And the following is result of what I print out.
You probably worked around this by now, but I think pydicom doesn't interpret planar configuration correctly.
You need to do this first:
img = data_set.pixel_array
img = img.reshape([img.shape[1], img.shape[2], 3])
From here on your image will have shape [rows cols 3], with the channels separated
As said by #Daniel since you have a PlanarConfiguration== 1 you have to rearrange your colors in columns through np.reshape and then converting to grayscale, for example using OpenCV:
import pydicom as dicom
import numpy as np
import cv2 as cv
data_set = dicom.read_file(path)
pixel_array = data_set.pixel_array
## converting to shape (m,n,3)
pixel_array_rgb = pixel_array.reshape((pixel_array.shape[1], pixel_array.shape[2], 3))
## converting to grayscale
pixel_array_gs = cv.cvtColor(pixel_array_rgb, cv.COLOR_RGB2GRAY)

Blank image appearing

I am using simpleITK to process MRI images in .mha format. I subsequently convert it into a numpy array. I am able to visualize the images using matplotlib.However, if I perform any prerprocessing or I multiply the image by its binary mask, all I get is a blank image. Is there something I am missing. My simplified code is shown below.
import SimpleITK as sitk
import numpy as np
from matplotlib import pyplot as plt
input_image = sitk.ReadImage('MRI.mha')
input_array = sitk.GetArrayFromImage(input_image)
plt.imshow(input_array[0,:,:],cmap = 'gray') # I get an image for this. No preprocessing has been performed.
plt.show()
# However, if I replace input_array after preprocessing, I get a black square.
I think this has something to do with the range of the data, but I am not able to pinpoint where. The image visualized before preprocessing has a maximum value of 744. After preprocessing, this drops down to 4, and that is when problems crop up. Any pointers to where I might be going wrong?
You should check your image pixel type before any processing. The MRI image volume which you are testing on has a sitkInt32 (Signed 32 bit integer) pixel type. So there is a high chance that your processing (e.g. your division operations) would make your pixel values zeros and you get black images.
You can either cast your image to float using SimpleITK:
input_image = sitk.ReadImage('MRI.mha')
print(input_image.GetPixelIDTypeAsString())
input_image = sitk.Cast(input_image,sitk.sitkFloat32)
input_array = sitk.GetArrayFromImage(input_image)
or change your numpy array data type before processing:
input_image = sitk.ReadImage('MRI.mha')
input_array = sitk.GetArrayFromImage(input_image)
input_array = input_array.astype(np.float32)
Read more about pixel types at SimpleITK Image Basics notebook.

Categories