[Hyperspectral] Acquisition and display of hyperspectral data

1. Environmental preparation

        The spectral package needs to be installed, which is specially used for hyperspectral data display.

pip install spectral

2. Data loading

        To pre-prepare raw hyperspectral .mat data and classification data gt.mat (ground-turth); then use loadmat(.) in scipy.io to read it into the program.

from scipy.io import loadmat

org_img = loadmat('HDate/PaviaU/paviaU.mat')

        It should be noted that using loadmat(.) directly will output a dict dictionary, which is composed as follows:

        In order to obtain the real hyperspectral data matrix, you need to add an index after loadmat(.), so the complete read statement should be:

from scipy.io import loadmat

input_image = loadmat('HDate/PaviaU/paviaU.mat')['paviaU']
gt = loadmat("HDate/PaviaU/paviaU_gt.mat")['paviaU_gt']

        By outputting the kind and size of two variables:

         It can be seen that the hyperspectral image is a 610*340*103 matrix, similar to the traditional RGB image, the first two dimensions are the length and width, and the last dimension is the number of channels, but the number of channels is compared to the RGB image with only 3 channels. much higher.

        And gt is a pre-marked ground-turth matrix, which is equivalent to a grayscale image, and the value of each pixel is from 0-255, representing different types .

3. Data display

        Data display is mainly done using imshow(.) in spectral, the usage is the same as imshow(.) in opencv

        1. Hyperspectral image display

import spectral as spy

view1 = spy.imshow(data=input_image, bands=[69, 27, 11], title="img")

                tips. The bands parameter in the function is used to correct the color. If the bands parameter is not added, the rendered color will be incorrect (as shown in the figure below).

         2. Hyperspectral classification display

view2 = spy.imshow(classes=gt, title="gt")

        3. Hyperspectral image and classification overlay display

view3 = spy.imshow(data=input_image, bands=[69, 27, 11], classes=gt)

         4. Hyperspectral color channel display

                I don't know what it's for, it's just cool

spy.view_cube(input_image, bands=[69, 27, 11])

         5. Hyperspectral high-dimensional feature display

pc = spy.principal_components(input_image)
xdata = pc.transform(input_image)
spy.view_nd(xdata[:, :, :15], classes=gt)

        6. Data storage

                 Use the save_rgb(.) function to save the above images in the specified location in the form of RGB images. The function is as follows:

spy.save_rgb(data=input_image,bands=[69, 27, 11],filename='hello.jpg')

 4. Small experiment

        If opencv is used to display hyperspectral images, an error will be reported due to too many third-dimensional channels. But gt is equivalent to a grayscale image, and this problem will not occur.

        But because the gap between categories is too small (the category number is generally not higher than 10), the actual display is black. So each element is enlarged by 10 times and then displayed using opencv:

gtx = gt*10
cv2.imshow('Test',gtx)

Guess you like

Origin blog.csdn.net/weixin_37878740/article/details/130740764