spectralpython / spectral

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

Problem in opening HSI file #43

Closed shah1459 closed 8 years ago

shah1459 commented 8 years ago

I am trying to open hsi file, but it is not working. It keeps saying, "Unable to determine file type" I am using the command as always: x1 = open_image(filename). I am attaching the file here. This is actually HSI file, but because I cannot attach HSI file here, I just changed the extension to png.

image006

tboggs commented 8 years ago

The file you have attached is a text file and is not recognized as an HSI file format by SPy. Your two best options are to either read it as a text file with numpy (e.g., using numpy.fromfile) or to convert it to an HSI format recognized by SPy (e.g., ENVI format), then use spectral.open_image). The first option is probably easiest but if you are going to be opening it many times or generating many similar files, it is probably best to convert the files to a recognized binary image file format.

shah1459 commented 8 years ago

Thanks a lot. Someone sent me this file and told me that it is HSI file. In fact, extension of the file is in hsi. I see that it a text file. When I went and opened it as image using imshow, it does not shown any bands. I expected it to be different. How can I convert this text file to hsi format?

tboggs commented 8 years ago

You need to figure out how many rows, bands, and columns are in the image. You can load the data like this:

In [11]: img = np.loadtxt('image006.png')

In [12]: img.shape
Out[12]: (99, 7364)

The data in the table are 2D so you need to reshape the numpy array so that it has dimentions (R, C, B), where R, B, and C are the number of rows, columns, and bands in the image. Then, you can use SPy to save the array to an ENVI-formatted file like this:

In [13]: import spectral as spy

In [14]: spy.envi.save_image('image006.hdr', img)
Saving /home/thomas/temp/image006.img

After that, you can use spy.open_image('image006.hdr') to open the ENVI file.

shah1459 commented 8 years ago

Thanks for your help. After saving into hdr file how come size of the file is now only 1 kb? Another question is, when I try to find the size of the original file, it does give me (99 L,7364L). What does L mean here?

tboggs commented 8 years ago

The .hdr file is just the header. ENVI format consists of two files: the .hdr file containing image metadata and a separate image data file (without the .hdr extension). The (99, 7364) means the text file had 99 rows and 7364 columns. I assume the "L" you see in your output just means long integer and you can ignore that.

Again, you need to figure out how many bands are in the image because it isn't clear from the ascii data file. Maybe it is a single row of 7364 pixels and 99 bands. Or maybe it is a row of 99 pixels with 7364 bands. Or maybe it has 99 bands but has multiple image rows concatenated on each line of the text file. That is what you need to determine and structure your numpy array accordingly.

shah1459 commented 8 years ago

Thanks so much for your help. This looks like the instruments is producing 7364 points total in a 10 by 10 image size. Is this ok? I was using the command : import matplotlib as pla ; pla.plot(img). I was expecting to see a spectrum. Finally, I see that you were suggesting to save the file as .img, but shouldn't I save that as .hsi?

tboggs commented 8 years ago

The extension doesn't matter. Just pick a name with ".hdr" at the data file will have the same name without the ".hdr".