spectralpython / spectral

Python module for hyperspectral image processing
MIT License
571 stars 139 forks source link

files displayed outside loop #87

Closed beachmouse closed 5 years ago

beachmouse commented 5 years ago

Hello,

Thank you for any help you may be able to provide.

We want to display hyperspectral images one-by-one within a for loop. We seem to be having an issue with SpectralPython displaying files outside a for loop. The information for a particular image is displayed after it is loaded, but the image itself is not. Instead, all the images are displayed after the loop exits. Code (python 2.7.15) follows:

import numpy as np

from spectral import *
import spectral.io.envi as envi
import spectral.io.erdas as erdas

import os
import os.path
import time
#from os import listdir
#from os.path import isfile, join

#import matplotlib.pyplot as plt
DIR = ''
while DIR != 'end':
 DIR = raw_input("Enter the name of the directory containing HSI files. Type end when finished")
 if DIR == 'end':
   break
 print(DIR)  
 arr = os.listdir(DIR)
 print(arr)
 print (" Entering for loop for all the images")
 for i in range(0,len(arr),2):
    print(arr[i])
    print(arr[i+1])
    hsi_hdr = os.path.join(DIR,arr[i])
    hsi_fn = os.path.join(DIR,arr[i+1])
    print(hsi_hdr)
    print(hsi_fn)
    print("Loading Image")
    img = envi.open(hsi_hdr, hsi_fn)
    view = imshow(img)
    print(view)
    print(img)

And here is the output:

Enter the name of the directory containing HSI files. Type end when finished:\MicroHSI\May 8 test subset e:\MicroHSI\May 8 test subset ['GE2013_05_08_162859_377.hdr', 'GE2013_05_08_162859_377.hsi', 'GE2013_05_08_163243_011.hdr', 'GE2013_05_08_163243_011.hsi', 'GE2013_05_08_163806_197.hdr', 'GE2013_05_08_163806_197.hsi'] Entering for loop for all the images GE2013_05_08_162859_377.hdr GE2013_05_08_162859_377.hsi e:\MicroHSI\May 8 test subset\GE2013_05_08_162859_377.hdr e:\MicroHSI\May 8 test subset\GE2013_05_08_162859_377.hsi Loading Image ImageView object: Display bands : [0, 126, 251] Interpolation : RGB data limits : R: [0.0, 4095.0] G: [86.0, 4095.0] B: [33.0, 4095.0]

Data Source:   'e:\MicroHSI\May 8 test subset\GE2013_05_08_162859_377.hsi'
# Rows:            486
# Samples:         680
# Bands:           252
Interleave:        BIL
Quantization:  16 bits
Data format:     int16

GE2013_05_08_163243_011.hdr GE2013_05_08_163243_011.hsi e:\MicroHSI\May 8 test subset\GE2013_05_08_163243_011.hdr e:\MicroHSI\May 8 test subset\GE2013_05_08_163243_011.hsi Loading Image ImageView object: Display bands : [0, 126, 251] Interpolation : RGB data limits : R: [0.0, 1235.0] G: [65.0, 4095.0] B: [30.0, 4095.0]

Data Source:   'e:\MicroHSI\May 8 test subset\GE2013_05_08_163243_011.hsi'
# Rows:            510
# Samples:         680
# Bands:           252
Interleave:        BIL
Quantization:  16 bits
Data format:     int16

GE2013_05_08_163806_197.hdr GE2013_05_08_163806_197.hsi e:\MicroHSI\May 8 test subset\GE2013_05_08_163806_197.hdr e:\MicroHSI\May 8 test subset\GE2013_05_08_163806_197.hsi Loading Image ImageView object: Display bands : [0, 126, 251] Interpolation : RGB data limits : R: [0.0, 1194.0] G: [65.0, 4095.0] B: [36.0, 4095.0]

Data Source:   'e:\MicroHSI\May 8 test subset\GE2013_05_08_163806_197.hsi'
# Rows:            395
# Samples:         680
# Bands:           252
Interleave:        BIL
Quantization:  16 bits
Data format:     int16

All the images are displayed at this point, instead of after their information.

Thanks again

tboggs commented 5 years ago

Are you doing this from within ipython or are you running a python script using the builtin interpreter? If you were running this code from an interactive session, I expect it would work (though from the way your code is structured, it would open an additional window for each image).

I think your main issue is how python, ipython, and spectral deal with matplotlib's interactive loop. If you simply want a window that cycles through the images, try replacing your imshow line above with something like this:

plt.imshow(spy.get_rgb(img))
plt.show()
beachmouse commented 5 years ago

We are editing and running the code from one cell in Jupyter Notebook; will try your suggestion--thank you!

beachmouse commented 5 years ago

Here is the error; seems like something may not be installed...

NameError Traceback (most recent call last)

in () 31 img = envi.open(hsi_hdr, hsi_fn) 32 # view = imshow(img) ---> 33 plt.imshow(spy.get_rgb(img)) 34 plot.show() 35 # print(view) NameError: name 'spy' is not defined
tboggs commented 5 years ago

You need to

import spectral as spy

But if you're trying to do this in jupyter, that's a whole separate issue. You should still use the spy.get_rgb function but you need to figure out how to do dynamic plots in jupyter (e.g., here).

beachmouse commented 5 years ago

Thanks for the link and for your help :)

beachmouse commented 5 years ago

import spectral as spy did the trick!

tboggs commented 5 years ago

Excellent. Glad that worked for you.

beachmouse commented 5 years ago

Interesting occurrence just now. Yesterday, I ran the code with

import spectral as spy

and images were displayed. Today, initially, when I ran the same code, the images were not being displayed. I had to add

%matplotlib inline

to get them displayed. Any insight as to why this was needed today but not yesterday will be greatly appreciated!

tboggs commented 5 years ago

I don't think the import statement would change anything since you were already importing functions from the module.

I don't know why anything would have changed since yesterday (there were no commits to spectral) but having "%matplotlib inline" is standard for using matplotlib with jupyter.