Matlab reads hyperspectral remote sensing data .Mat and .tif (2020.12.27)


1. Introduction to Hyperspectral Remote Sensing Data

        Remote sensing images have four resolutions: temporal resolution, spatial resolution, radiometric resolution, and spectral resolution. Among them, in terms of spectral resolution, according to the number of spectral channel divisions of imaging sensors, it can be divided into multispectral , hyperspectral and hyperspectral . It is generally believed that the number of multispectral bands is less than 100, the number of hyperspectral bands is between 100 and 10,000, and the number of hyperspectral bands is more than 10,000.

2. Two open source hyperspectral remote sensing datasets

        Hyperspectral Remote Sensing Scenes : Including Indian Pines, Salinas, Pavia Center and University, Cuprite, Kennedy Space Center, Botswana, and anomaly detection (7 regions). Each region will have the number of introduction types and bands, which can be downloaded directly.
insert image description here

        Hyperspectral Images : Portion of Southern Tippecanoe County, Indiana, Northwest Tippecanoe County, Indiana, Washington DC Mall.
insert image description here
        Here, I only downloaded the data of Washington DC Mall Image, as shown in the following figure:
insert image description here

3. Common formats of hyperspectral remote sensing data

3.1 .Mat

insert image description here

3.2 .Tif

insert image description here

4. Matlab reads hyperspectral remote sensing data

4.1 Matlab reads hyperspectral remote sensing data in .Mat format

        Here take Salinas.mat as an example, first open the Matlab software, enter the downloaded hyperspectral remote sensing data directory as a workspace; then modify the input file name and run the following code.
insert image description here

4.1.1 Matlab code to read .mat

load('Salinas.mat')
InputMatImg=salinas;
b = size(InputMatImg);
fprintf('输入图像宽度为 %d\n',b(1));
fprintf('输入图像高度为 %d\n',b(2));
fprintf('输入图像波段数为 %d\n',b(3));
i=120;j=180;k=220;%自选三个波段
InputImg_r= InputMatImg(:,:,i);%获取第i个波段
InputImg_g= InputMatImg(:,:,j);%获取第j个波段
InputImg_b= InputMatImg(:,:,k);%获取第k个波段
InputImg_r= uint8(InputImg_r);%将i波段的灰度值转为0~255
InputImg_g= uint8(InputImg_g);%将j波段的灰度值转为0~255
InputImg_b= uint8(InputImg_b);%将k波段的灰度值转为0~255
RGBImg=cat(3,InputImg_r,InputImg_g,InputImg_b);%将i、j、k三个波段进行合成
figure;
subplot(221);imshow(InputImg_r);title('红色波段');
subplot(222);imshow(InputImg_g);title('绿色波段');
subplot(223);imshow(InputImg_b);title('蓝色波段');
subplot(224);imshow(RGBImg);title('合成波段');
imwrite(InputImg_r,['MATBand',num2str(i),'.jpg']);
imwrite(InputImg_g,['MATBand',num2str(j),'.jpg']);
imwrite(InputImg_b,['MATBand',num2str(k),'.jpg']);
imwrite(RGBImg,'compositeMATRGBimg.jpg');

4.1.2 Running results (after integration):

Band 120
Band 180
Band 220
three-band combination
Salinas_gt

4.2 Matlab reads hyperspectral remote sensing data in .tif format

        Here take dc.tif (Washington DC Mall) as an example, first enter the directory where the file is located:
insert image description here
        Matlab code function: get the i, j, kth three bands, and then synthesize the three bands.

4.2.1 Matlab code to read .tif

InputTIFImage = importdata('dc.tif');
Info=imfinfo('dc.tif')
i=130;j=160;k=190;%自选三个波段
InputImage_r= InputTIFImage(:,:,i);%获取第i个波段
InputImage_g= InputTIFImage(:,:,j);%获取第j个波段
InputImage_b= InputTIFImage(:,:,k);%获取第k个波段
Image_r= uint8(InputImage_r);%将i波段的灰度值转为0~255
Image_g= uint8(InputImage_g);%将j波段的灰度值转为0~255
Image_b= uint8(InputImage_b);%将k波段的灰度值转为0~255
RGBImg=cat(3,Image_r,Image_g,Image_b);%将i、j、k三个波段进行合成
figure;
subplot(221);imshow(Image_r);title('红色波段');
subplot(222);imshow(Image_g);title('绿色波段');
subplot(223);imshow(Image_b);title('蓝色波段');
subplot(224);imshow(RGBImg);title('合成波段');
imwrite(Image_r,['TIFBand',num2str(i),'.jpg']);
imwrite(Image_g,['TIFBand',num2str(j),'.jpg']);
imwrite(Image_b,['TIFBand',num2str(k),'.jpg']);
imwrite(RGBImg,'compositeRGBTIFimg.jpg');

4.2.2 Running results (after integration):

Band 130
Band 160
Band 190
three-band combination

5 Mat to Tif function (MATLAB)

Mat2Tif.m function script file

%InputMatFileName如D:/360极速浏览器下载/Pavia.mat
%OutputTifFilename如D:/360极速浏览器下载/PaviaTIF.tif
% Mat2Tif('D:/360极速浏览器下载/Pavia.mat','D:/360极速浏览器下载/TifPavia.tif');
function Mat2Tif(InputMatFileName,OutputTifFilename)
    load(InputMatFileName);
    InputMatImg=pavia;
    t = Tiff(OutputTifFilename,'w');
    if size(InputMatImg,3) == 3
        t.setTag('Photometric',Tiff.Photometric.RGB);
    else
        t.setTag('Photometric',Tiff.Photometric.MinIsBlack);%颜色空间解释方式
    end
    t.setTag('Compression',Tiff.Compression.None);%无压缩
    t.setTag('BitsPerSample',64);% 由于输入.mat为double类型,所以选择了64
    t.setTag('SamplesPerPixel',size(InputMatImg,3));% 每个像素的波段数目
    t.setTag('SampleFormat',Tiff.SampleFormat.IEEEFP);% 配合BitsPerSample64位double类型,选择IEEEFP来对应
    t.setTag('ImageLength',size(InputMatImg,1));% 影像宽度
    t.setTag('ImageWidth',size(InputMatImg,2));% 影像高度
    t.setTag('PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);%平面配置选择集中式
    t.write(InputMatImg);% 准备好了头文件,开始写影像数据
    t.close();% 关闭影像
%下面的代码仅为了测试显示结果
%tdc = Tiff('D:/360极速浏览器下载/TifPavia.tif','r');
%TifPavia = read(tdc);
%close(tdc);
% i=12;j=55;k=89;%自选三个波段
% InputImg_r= TifPavia(:,:,i);%获取第i个波段
% InputImg_g= TifPavia(:,:,j);%获取第j个波段
% InputImg_b= TifPavia(:,:,k);%获取第k个波段
% InputImg_r= uint8(InputImg_r);%将i波段的灰度值转为0~255
% InputImg_g= uint8(InputImg_g);%将j波段的灰度值转为0~255
% InputImg_b= uint8(InputImg_b);%将k波段的灰度值转为0~255
% RGBImg=cat(3,InputImg_r,InputImg_g,InputImg_b);%将i、j、k三个波段进行合成
% figure;
% subplot(221);imshow(InputImg_r);title('红色波段');
% subplot(222);imshow(InputImg_g);title('绿色波段');
% subplot(223);imshow(InputImg_b);title('蓝色波段');
% subplot(224);imshow(RGBImg);title('合成波段');

        The comparison between the Mat three-band display before conversion and the Tif three-band display after conversion is shown in the figure below.
insert image description here

Three-band image display of the original Mat

insert image description here

Three-band image display after converting the original Mat to Tif

Guess you like

Origin blog.csdn.net/jing_zhong/article/details/111770040