Subplots with multiple spectral.imshow() objec

328 views Asked by At

I want to plot multiple hyperspectral images with sp.imshow. I know this returns a R,G,B visualization. I have 13 HSI files (13 .hdr and 13 .img files). I know how to plot and analyze individual files but I want an overview of all my samples in a grid.

I am also aware of creating the fig, axes previously. Yet subplots are still confusing. This is what I have so far.

from pathlib import Path
import spectral as sp
import matplotlib.pyplot as plt

files_path = Path(r"C:\data\Reflectance_Calibrated")
hdr_list = list(files_path.glob('*.hdr'))
bin_list = list(files_path.glob('*.img'))

targets = list(zip(hdr_list,bin_list))

i = 0

## Here is where I tried doing a for loop, yet it did not work.

for k, target in enumerate(targets):
    target_open = sp.envi.open(targets[i][0], targets[i][1])
    sp.imshow(target_open)
    i += 1

I am looking for something like sp.imshow(target_open).add_subplot(ax)

Has anyone tried doing subplots with spectral.imshow objects?

Any help would be appreciated.

1

There are 1 answers

2
bogatron On

There are a few options to achieve what you want. One is to use plt.subplot to select each grid cell, then when you call sp.imshow, pass the fignum keyword. For example, to create an Nx1 grid of images (i.e., a grid with a single column):

fig = plt.figure()
for k, target in enumerate(targets):
    target_open = sp.envi.open(targets[k][0], targets[k][1])
    plt.subplot(len(targets), 1, k + 1)
    sp.imshow(target_open, fignum=fig.number)

Another option is to use sp.get_rgb to retrieve the RGB image data for each image, then use plt.imshow to do the rendering instead of sp.imshow.