I am working on a Hyperspectral Image(HSI) of Indian Pines. Initially the data was stored in .mat file and then using loadmat function I have read the data in an array. The array dimension is (145,145,200). Now When I am trying to save this array as a Tiff(.tif) image somehow things are not working well. I am using tifffile package and it's imwrite function to save the image. But when I am opening the image in QGIS software there it's showing only one band instead of 200 bands.
I am attaching the code here below:
import tifffile
import numpy as np
from scipy.io import loadmat
def read_HSI():
X = loadmat('Indian_pines_corrected.mat')['indian_pines_corrected']#dataset
y = loadmat('Indian_pines_gt.mat')['indian_pines_gt']#ground truth
print(f"X shape: {X.shape}\ny shape: {y.shape}")
return X, y
X, y = read_HSI()
tifffile.imwrite('IndianPines(inputX).tif', X)
If there is any other way to save a .mat file into .tif format then please let me know.
Thank you in advance.
I was able to save it using scikit-image:
import numpy as np
import skimage
data = np.dstack([skimage.data.astronaut(),skimage.data.astronaut()])
data = data.swapaxes(0,2)
data = data.swapaxes(1,2)
import matplotlib.pyplot as plt
plt.imshow(data[4])
Under the hood it uses tiffile plugin:
skimage.io.imsave('test2.tiff', data,photometric='minisblack')
Related
i using SLIVER07 dataset for liver segmentation task but i am stuck in reading that images.
import SimpleITK as sitk
import numpy as np
import matplotlib.pyplot as plt
# reading .mhd file from slive07 dataset
itkimage = sitk.ReadImage('liver-orig001.mhd')
ct_scan = sitk.GetArrayFromImage(itkimage)
plt.imshow(ct_scan[1])
You are trying to pass the entire 3D image volume to imshow. You could instead try:
plt.imshow(ct_scan[40,:,:])
Which will show the 40th slice.
Of interest might be the platipy library, available here, or just $ pip install platipy. The built-in image visualiser (based on matplotlib) is perfect for 3D image visualisation in python, and has lots of cool features.
A little demo:
from platipy.imaging import ImageVisualiser
img = sitk.ReadImage("image_filename.mhd")
vis = ImageVisualiser(img)
fig = vis.show()
I need to read a folder full of multi-page tiffs generated by the Suite2p neurobiology package.
From the Suite2p source code the multi-tiffs are created as follows:
import numpy as np
from tifffile import TiffWriter
# fake example images
img_5_tiles = np.random.randint(0,65535,(5,10,20), dtype='uint16')
img_4_tiles = img_5_tiles[1:,...]
# save fake images
with TiffWriter(r'D:\5tiles.tiff') as tif:
for frame in img_5_tiles:
tif.save(frame)
with TiffWriter(r'D:\4tiles.tiff') as tif:
for frame in img_4_tiles:
tif.save(frame)
When I try to read them into dask, skimage.io default tifffile plugin fails to get the correct shape:
from dask.array.image import imread
from skimage import io
def read_with_tifffile(path):
return io.imread(path, plugin='tifffile')
# should produce (5,10,20) instead of (1,10,20)
imread(r'D:\5tiles.tiff', imread=read_with_tifffile).shape
I can overcome this by using the non-default skimage.io.imread plugin 'pil'
def read_with_pil(path):
return io.imread(path, plugin='pil', mode='L')
# gives (1,5,10,20) which is acceptable
imread(r'D:\5tiles.tiff', imread=read_with_pil).shape
Unfortunately, if the number of tiles equals to 4, skimage starts to handle the shape differently:
# gives (1,10,20,4) instead of (1,4,10,20)
imread(r'D:\4tiles.tiff', imread=read_with_pil).shape
From reading skimage docs, it's probably trying to interpret my image as RGBA and then fails.
Is there a solution to force 'uint16' multipage read for all image shapes?
Any advice is appreciated!
Based on cgohlke's comment:
from dask.array.image import imread
import numpy as np
from skimage import io
import tifffile
# fake data
img_with_4_tiles = np.random.randint(0,65535,(4,10,20), dtype='uint16')
with tifffile.TiffWriter(r'D:\4tiles.tiff') as tif:
for frame in img_with_4_tiles:
tif.save(frame)
# my original bad solution
def read_with_pil(path):
return io.imread(path, plugin='pil', mode='L')
print(imread(r'D:\4tiles.tiff', imread=read_with_pil).shape)
# a good solution from the horse's mouth (https://pypi.org/user/cgohlke/)
def read_with_tifffile(path):
return tifffile.imread(path, is_shaped=False)
print(imread(r'D:\4tiles.tiff', imread=read_with_tifffile).shape)
I've seen that vtkplotter supports a wide range of volumetric image formats, but does it support plotting numpy arrays? I have an image in a .mhd format that I converted to .npz file and would like to plot it using the mentioned library. Is there a way to do so?
import numpy as np
data = np.zeros((3,3,3))
# how to plot this?
If you know any other tools for visualizing volumetric data, suited for medical analysis, please tell so. I've tried mayavi but it consumes so much memory that it crashes my computer.
You can use numpy_support to convert numpy arrays to vtkImageData (i.e. a VTK 3D image).
An example follows, there's a numpy warning about type conversion: I don't think it's relevant but I'm not a python guru...
from vtk.util import numpy_support
import vtk
import numpy as np
data = np.zeros((3,3,3))
# vtkImageData is the vtk image volume type
imdata = vtk.vtkImageData()
# this is where the conversion happens
depthArray = numpy_support.numpy_to_vtk(data.ravel(), deep=True, array_type=vtk.VTK_DOUBLE)
# fill the vtk image data object
imdata.SetDimensions(data.shape)
imdata.SetSpacing([1,1,1])
imdata.SetOrigin([0,0,0])
imdata.GetPointData().SetScalars(depthArray)
# f.ex. save it as mhd file
writer = vtk.vtkMetaImageWriter()
writer.SetFileName("test.mhd")
writer.SetInputData(imdata)
writer.Write()
... and to visualize vtk volume data you can use Paraview.
How can I view images stored with a .npy extension and save my own files in that format?
.npy is the file extension for numpy arrays - you can read them using numpy.load:
import numpy as np
img_array = np.load('filename.npy')
One of the easiest ways to view them is using matplotlib's imshow function:
from matplotlib import pyplot as plt
plt.imshow(img_array, cmap='gray')
plt.show()
You could also use PIL or pillow:
from PIL import Image
im = Image.fromarray(img_array)
# this might fail if `img_array` contains a data type that is not supported by PIL,
# in which case you could try casting it to a different dtype e.g.:
# im = Image.fromarray(img_array.astype(np.uint8))
im.show()
These functions aren't part of the Python standard library, so you may need to install matplotlib and/or PIL/pillow if you haven't already. I'm also assuming that the files are either 2D [rows, cols] (black and white) or 3D [rows, cols, rgb(a)] (color) arrays of pixel values.
Thanks Ali_m. In my case I inspect the npy file to check how many images was in the file with:
from PIL import Image
import numpy as np
data = np.load('imgs.npy')
data.shape
then I plotted the images in a loop:
from matplotlib import pyplot as plt
for i in range(len(data)):
plt.imshow(data[i], cmap='gray')
plt.show()
I am looking for a way to rescale the matrix given by reading in a png file using the matplotlib routine imread,
e.g.
from pylab import imread, imshow, gray, mean
from matplotlib.pyplot import show
a = imread('spiral.png')
#generates a RGB image, so do
show()
but actually I want to manually specify the dimension of $a$, say 200x200 entries, so I need some magic command (which I assume exists but cannot be found by myself) to interpolate the matrix.
Thanks for any useful comments : )
Cheers
You could try using the PIL (Image) module instead, together with numpy. Open and resize the image using Image then convert to array using numpy. Then display the image using pylab.
import pylab as pl
import numpy as np
from PIL import Image
path = r'\path\to\image\file.jpg'
img = Image.open(path)
img.resize((200,200))
a = np.asarray(img)
pl.imshow(a)
pl.show()
Hope this helps.