spectralpython / spectral

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

Open ENVI file #72

Closed prkyprky closed 6 years ago

prkyprky commented 6 years ago

Hi, when I try open ENVI file (in .pix format) by spectral, image is broken. After loading is image defected like this. My code is really simply, but it doesnt work. Original image is aerial image. Could you please help me?

Thank you

from spectral import *

file_hdr = 'data/CASI_2017_05_18_120034.cc.dc.sl.fss.so.rc.sc.rY_g.pix.hdr'
CASI_op = envi.open(file_hdr)
CASI = CASI_op.open_memmap(writeable=True)

snimek obrazovky porizeny 2018-02-13 15-38-27

tboggs commented 6 years ago

Can you post the contents of the .hdr file and the output of CASI_op.shape?

prkyprky commented 6 years ago

Sure. In .hdr file is:

ENVI description = { Image File: ../pix_files/CASI_2017_05_18_120034.cc.dc.sl.fss.so.rc.sc.rY.rad_g.pix, created by geocor V:5.6.3 (A) [2017/11/02 15:13:29] } samples = 3646 lines = 4934 bands = 48 header offset = 82432 file type = PCI data type = 12 interleave = BIP sensor type = Unknown byte order = 1 read procedures = {pci_read_spatial, pci_read_spectral} map info = { UTM, 1, 1, 321711.200, 5561737.600, 0.80, 0.80, 33, North, WGS-84 } pixel size = { 0.800, 0.800, units=meters }

(I skiped band names, fwhm and wavelength)

and output of CASI_op.shape is:

(4934, 3646, 48)

Thank you.

tboggs commented 6 years ago

I could be wrong here but it looks like that is just how the image data are formatted, perhaps for convenient display puposes. The header states that there are 4934 rows and 3646 columns, whereas the flightline itself has an aspect ration of around 10:1. What is the output of CASI_op[50, 50]?

tboggs commented 6 years ago

Also, what is the exact size of the data file?

prkyprky commented 6 years ago

It depend. In our case, we are using lot of different shapes of flightline. This is relatively short and with angle 45°. Around flight line is image filled by zeroes for creating bbox. Output of CASI_op[50,50] is: CASI_op[100,100] array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=uint16)

Size of image is: 1.7 GB and 2676.8m x 3947.2m

Original image is this: snimek obrazovky porizeny 2018-02-14 09-50-44

tboggs commented 6 years ago

It appears that there is some additional data being appended (or padding added) at the end of each row. It also appears that the original file did not use the standard ENVI file input routines. Instead, it appears it was using a custom file reader, based on this line in the header file:

read procedures = {pci_read_spatial, pci_read_spectral}

Do you happen to have those routines? Without those routines, the only hack I can think of is if you have the exact file size (in bytes), you could use that info with the other metadata to figure out how to modify the samples parameter in the header file for the image data to be structured correctly.

prkyprky commented 6 years ago

Hi, I tried it and it doesnt work, I tried to determinate where is appended some data and I couldnt find it. I also tried remove read procedures and it also doesnt work.

tboggs commented 6 years ago

Removing read_procedures from the header won't help. My point was that read_procedures specifies custom IDL scripts used by ENVI for reading the data (i.e., it is not a standard ENVI data file).

Can you tell me the exact file size, in bytes? It should be 10 digits long.

prkyprky commented 6 years ago

Ok, I'sorry. Exact size of pix file is 1729062400 and hdr file

tboggs commented 6 years ago

It doesn't look like there is a fixed amount of padding per image row; otherwise, it would probably be possible to adjust the samples parameter of the header to display the data properly. Unfortunately, I haven't found any documentation on how to read PCI data files. I'll see if I can find IDL scripts for pci_read_spatial, pci_read_spectral. If I can't find that soon, there should probably be an update for envi.open to raise an exception whenever the header has file type = PCI.

While the visual display is warped, it looks like you could still do spectral processing on the file by generating a mask from any of the bands, since non-image pixels are all zeros.

prkyprky commented 6 years ago

I solved this problem. Now I tried edit HDR file manualy and I changed line 'samples'. If I change number and add + 2 then image after loading and saving by Spectral is OK. Later I will test it with more images. Thank you so much for help.