为超光谱卫星图像创建具有地面实况的训练和测试集

create training and testing set with ground truth for Hyper spectral satellite imagery

我正在尝试根据以 tif(光栅)格式呈现的基本事实(观察)数据创建训练和测试集。

实际上,我有一个高光谱图像(卫星图像),它有 200 个维度(channels/bands)以及存储在另一个图像中的相应标签(17 class)。现在,我的目标是实现一个 class 化算法,然后用测试数据集检查准确性。

我的问题是,我不知道如何向我的算法描述哪个像素属于哪个 class,然后将它们拆分为去皮重和测试集。

我已经提供了我的目标的构想如下: 但我不想这样做,因为我有 145 * 145 像素暗淡,所以定义这些像素的位置并手动分配给它们对应的 class 并不容易。

请注意,以下示例是针对 3D 图像的,我有 200D 图像和标签(基本事实),因此我不需要像下面的代码那样指定它们,但我确实想将它们分配给它们的像素成员。

   % Assigning pixel(by their location)to different groups. 
  tpix=[1309,640 ,1;... % Group 1
        1218,755 ,1;... 
        1351,1409,2;... % Group 2
        673 ,394 ,2;...
        285 ,1762,3;... % Group 3
        177 ,1542,3;...
        538 ,1754,4;... % Group 4
        432 ,1811,4;...
        1417,2010,5;... % Group 5
        163 ,1733,5;...
        652 ,677 ,6;... % Group 6
        864 ,1032,6];

 row=tpix(:,1);   % y-value
 col=tpix(:,2);   % x-value
 group=tpix(:,3); % group number
 ngroup=max(group);

 % create trainingset 
 train=[];

 for i=1:length(group)
  train=[train; r(row(i),col(i)), g(row(i),col(i)), b(row(i),col(i))];
 end %for

我理解的对吗?在第二行,train 变量获取到现在为止的值 + 红色、绿色和蓝色的像素?例如,您希望它们仅以红色、绿色和蓝色显示?只是某些还是全部?我可以想象我们定义一个图像矩阵,然后将值放在图像的红色、绿色和蓝色层中。那会有帮助吗?如果这是你的问题,我会给你代码:)

编辑:解决方案

%download the .mats from the website and put them in folder of script
load 'Indian_pines_corrected.mat'; 
load 'Indian_pines_gt.mat';

ipc = indian_pines_corrected;
gt  = indian_pines_gt;

%initiating cell
train = cell(16,1);

%loop to search class number of the x and y pixel coordinates 
for c = 1:16
    for i = 1:145
        for j = 1:145

            % if the classnumber is equal to the number in the gt pixel, 
            % then place the pixel from ipc(x,y,:) it in the train{classnumber}(x,y,:)  
            if gt(i,j) == c
                train{c}(i,j,:) = ipc(i,j,:);


            end %if
        end %for j
    end %for i
end %for c

现在你得到了在每个单元格中都有一个矩阵的列车单元格。每个单元格都是一个 class,并且只有您想要的像素。您可以自己检查 classes 是否与形状相对应。

最终,我可以解决我的问题。下面的代码将矩阵(栅格)重塑为矢量,然后我索引地面实况数据以找到高光谱图像中相应像素的位置。

请注意,我正在寻找一种构建训练和测试集的有效方法。

GT = indian_pines_gt;
data = indian_pines_corrected;
data_vec=reshape(data, 145*145,200);
GT_vec = reshape(GT,145*145,1);
[GT_vec_sort,idx] = sort(GT_vec);

%INDEXING.
index = find(and(GT_vec_sort>0,GT_vec_sort<=16));
classes_num = GT_vec_sort(index);

%length(index)


for k = 1: length(index)
  classes(k,:) = data_vec(idx(index(k)),:);
end 

figure(1)
plot(GT_vec_sort)

新。

我已经为#Hyperspectral 图像(Pine 数据集)创建了训练和测试集。无需使用 for 循环

clear all
load('Indian_pines_corrected.mat');
load Indian_pines_gt.mat;
GT = indian_pines_gt;
data = indian_pines_corrected;

%Convert image from raster to vector. 
data_vec = reshape(data, 145*145, 200);

%Provide location of the desired classes.  
GT_loc = find(and(GT>0,GT<=16));
GT_class = GT(GT_loc)
data_value = data_vec(GT_loc,:)

% explanatories plus Respond variable. 
%[200(variable/channel)+1(labels)= 201])
dat = [data_value, GT_class];

% create random Test and Training set.
[m,n] = size(dat);
P = 0.70 ;
idx = randperm(m);
Train = dat(idx(1:round(P*m)),:); 
Test = dat(idx(round(P*m)+1:end),:);
X_train = Train(:,1:200); y_train = Train(:, 201);
X_test = Test(:,1:200); y_test = Test(:, 201);