├── Indian_pines_gt.png ├── README.md ├── RemoteSensingPart3.m ├── classes.png └── load data /Indian_pines_gt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obravo7/Hyperspectral-Image-Segmentation-using-Support-Vector-Machines/c152024090b48151d77fc7d5fc2608c32e94758a/Indian_pines_gt.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Hyperspectral-Image-Segmentation-using-Support-Vector-Machines 2 | This script utilizes an open source SVM library that can perform multiclass classification. The library was created by Chih-Chung Chang and Chih-Jen Lin, called [LIBSVM](https://www.csie.ntu.edu.tw/~cjlin/libsvm/). 3 | 4 | This was an assignment for a graduate course in Remote Sensing taught at [Shanghai Jiao Tong University](www.sjtu.edu.cn). I decided that the script may be useful to other students. The assignemt was as follows: 5 | 1. Perform principle component analysis to view data 6 | 2. Take the first 3 principle components and create an RGB image from them 7 | 3. Train an SVM model to do pixelwise classification 8 | 9 | # Data 10 | 11 | [The indian pines](http://www.ehu.eus/ccwintco/index.php?title=Hyperspectral_Remote_Sensing_Scenes#Indian_Pines) scene is open source hyper spectral image data. The data is well documented, and list the classes with the corresponding number of pixels. The script takes only nine of these classes. 12 | 13 | ![classes](classes.png) 14 | 15 | 16 | A ground truth file exist, and can be found in the **Grupo de Inteligencia Computacional (GIC)** [website](http://www.ehu.eus/ccwintco/index.php?title=Archivo:Indian_pines_gt.png). Here is a snapshot of the ground truth file. 17 | 18 | ![Ground](Indian_pines_gt.png) 19 | 20 | 21 | #Results 22 | 23 | WIP 24 | -------------------------------------------------------------------------------- /RemoteSensingPart3.m: -------------------------------------------------------------------------------- 1 | % Remote sensing part 3: Hyperspectral Imaging 2 | clear all; clc 3 | %% load data 4 | fname = ['indian_pines_corrected']; 5 | newimage = load(fname); 6 | pines = newimage.indian_pines_corrected; 7 | %% PCA of Hyperspectral Image 8 | %reshape the data for PCA 9 | X = reshape(pines,size(pines,1)*size(pines,2),200); 10 | coeff = pca(X); 11 | Ptransformed = X*coeff; 12 | %take the first 4 principle components 13 | Ipc1 = reshape(Ptransformed(:,1),size(pines,1),size(pines,2)); 14 | Ipc2 = reshape(Ptransformed(:,2),size(pines,1),size(pines,2)); 15 | Ipc3 = reshape(Ptransformed(:,3),size(pines,1),size(pines,2)); 16 | Ipc4 = reshape(Ptransformed(:,3),size(pines,1),size(pines,2)); 17 | figure, imshow(Ipc1,[]); 18 | figure, imshow(Ipc2,[]); 19 | figure, imshow(Ipc3,[]); 20 | figure, imshow(Ipc3,[]); 21 | redChannel = uint8(255 * mat2gray(Ipc1)); 22 | greenChannel = uint8(255 * mat2gray(Ipc2)); 23 | blueChannel = uint8(255 * mat2gray(Ipc3)); 24 | rgbImage = cat(3, redChannel, greenChannel, blueChannel); 25 | figure, imshow(rgbImage); 26 | %% SVM classifier 27 | % Segment the data for SVM classification 28 | fnamegt = 'indian_pines_gt'; 29 | newimg = load(fnamegt); 30 | pines_gt = newimg.indian_pines_gt; %ground truth image 31 | 32 | % Select the 9 landcover classes containing the most number of pixels. 33 | % Select 200 pixels for each class at random 34 | % Store the values that we don't select as well as the labels 35 | numop = 200; %number of pixels to select 36 | 37 | %corn notil 38 | corn_notil = find(pines_gt == 2); 39 | corn_n = corn_notil(randperm(length(corn_notil), numop)); 40 | ind_2 = pines_gt(corn_n); 41 | test_cn = setdiff(corn_notil, corn_n); 42 | label_cn = pines_gt(test_cn); 43 | %corn mintill 44 | corn_mintill = find(pines_gt == 3); 45 | corn_m = corn_mintill(randperm(length(corn_mintill), numop)); 46 | ind_3 = pines_gt(corn_m); 47 | test_cm = setdiff(corn_mintill, corn_m); 48 | label_cm = pines_gt(test_cm); 49 | %green pasture 50 | grass_pasture = find(pines_gt == 5); 51 | grass_p = grass_pasture(randperm(length(grass_pasture), numop)); 52 | ind_5 = pines_gt(grass_p); 53 | test_gp = setdiff(grass_pasture, grass_p); 54 | label_gp = pines_gt(test_gp); 55 | %grass trees 56 | grass_trees = find(pines_gt == 6); 57 | grass_t = grass_trees(randperm(length(grass_trees), numop)); 58 | ind_6 = pines_gt(grass_t); 59 | test_gt = setdiff(grass_trees, grass_t); 60 | label_gt = pines_gt(test_gt); 61 | %hay windrowed 62 | hay_wind = find(pines_gt == 8); 63 | hay_w = hay_wind(randperm(length(hay_wind), numop)); 64 | ind_8 = pines_gt(hay_w); 65 | test_hw = setdiff(hay_wind, hay_w); 66 | label_hw = pines_gt(test_hw); 67 | %soybean notil 68 | soybean_notil = find(pines_gt == 10); 69 | soybean_n = soybean_notil(randperm(length(soybean_notil), numop)); 70 | ind_10 = pines_gt(soybean_n); 71 | test_sn = setdiff(soybean_notil, soybean_n); 72 | label_sn = pines_gt(test_sn); 73 | %soybean mintill 74 | soybean_mintill = find(pines_gt == 11); 75 | soybean_m = soybean_mintill(randperm(length(soybean_mintill), numop)); 76 | ind_11 = pines_gt(soybean_m); 77 | test_sm = setdiff(soybean_mintill, soybean_m); 78 | label_sm = pines_gt(test_sm); 79 | %soybean clean 80 | soybean_clean = find(pines_gt == 12); 81 | soybean_c = soybean_clean(randperm(length(soybean_clean), numop)); 82 | ind_12 = pines_gt(soybean_c); 83 | test_sc = setdiff(soybean_clean, soybean_c); 84 | label_sc = pines_gt(test_sc); 85 | %wheat 86 | wheat = find(pines_gt == 13); 87 | wht = wheat(randperm(length(wheat), numop)); 88 | ind_13 = pines_gt(wht); 89 | test_wh = setdiff(wheat, wht); 90 | label_wh = pines_gt(test_wh); 91 | %% Training data for SVM 92 | % concatinated indexes are our training label 93 | training_label = [ind_2;ind_3;ind_5;ind_6;ind_8;ind_10;ind_11;ind_12;ind_13]; 94 | 95 | % indexes 96 | index = [2; 3; 5; 6; 8; 10; 11; 12; 13]; 97 | % training data: (this is probably the most inefficient way to do this but I ran out of time) 98 | c_n = []; 99 | for i = 1: 200 100 | a = pines(:,:,i); 101 | b = a(corn_n); 102 | c_n = [c_n b]; 103 | end 104 | 105 | c_m = []; 106 | for i = 1: 200 107 | a = pines(:,:,i); 108 | b = a(corn_m); 109 | c_m = [c_m b]; 110 | end 111 | g_p = []; 112 | for i = 1: 200 113 | a = pines(:,:,i); 114 | b = a(grass_p); 115 | g_p = [g_p b]; 116 | end 117 | g_t = []; 118 | for i = 1: 200 119 | a = pines(:,:,i); 120 | b = a(grass_t); 121 | g_t = [g_t b]; 122 | end 123 | h_w = []; 124 | for i = 1: 200 125 | a = pines(:,:,i); 126 | b = a(hay_w); 127 | h_w = [h_w b]; 128 | end 129 | s_n = []; 130 | for i = 1: 200 131 | a = pines(:,:,i); 132 | b = a(soybean_n); 133 | s_n = [s_n b]; 134 | end 135 | s_m = []; 136 | for i = 1: 200 137 | a = pines(:,:,i); 138 | b = a(soybean_m); 139 | s_m = [s_m b]; 140 | end 141 | s_c = []; 142 | for i = 1: 200 143 | a = pines(:,:,i); 144 | b = a(soybean_c); 145 | s_c = [s_c b]; 146 | end 147 | w_h = []; 148 | for i = 1: 200 149 | a = pines(:,:,i); 150 | b = a(wht); 151 | w_h = [w_h b]; 152 | end 153 | % training instance matrix 154 | training_instance_matrix = [c_n;c_m;g_p;g_t;h_w;s_n;s_m;s_c;w_h]; 155 | clear c_n c_m 156 | % create our model 157 | model = svmtrain(training_label, training_instance_matrix,'-t 1 -h 0'); %'-t 1' 158 | %model = svmtrain(training_label, training_instance_matrix,'-c 1 -g 0.07 -b 1'); 159 | %% Test our model 160 | 161 | % testing labels 162 | testing_label_matrix = [label_cn;label_cm;label_gp;label_gt;label_hw;label_sn;label_sm;label_sc;label_wh]; 163 | 164 | % setting the testing data 165 | 166 | tc_n = []; 167 | for i = 1: 200 168 | a = pines(:,:,i); 169 | b = a(test_cn); 170 | tc_n = [tc_n b]; 171 | end 172 | 173 | tc_m = []; 174 | for i = 1: 200 175 | a = pines(:,:,i); 176 | b = a(test_cm); 177 | tc_m = [tc_m b]; 178 | end 179 | tg_p = []; 180 | for i = 1: 200 181 | a = pines(:,:,i); 182 | b = a(test_gp); 183 | tg_p = [tg_p b]; 184 | end 185 | tg_t = []; 186 | for i = 1: 200 187 | a = pines(:,:,i); 188 | b = a(test_gt); 189 | tg_t = [tg_t b]; 190 | end 191 | th_w = []; 192 | for i = 1: 200 193 | a = pines(:,:,i); 194 | b = a(test_hw); 195 | th_w = [th_w b]; 196 | end 197 | ts_n = []; 198 | for i = 1: 200 199 | a = pines(:,:,i); 200 | b = a(test_sn); 201 | ts_n = [ts_n b]; 202 | end 203 | ts_m = []; 204 | for i = 1: 200 205 | a = pines(:,:,i); 206 | b = a(test_sm); 207 | ts_m = [ts_m b]; 208 | end 209 | ts_c = []; 210 | for i = 1: 200 211 | a = pines(:,:,i); 212 | b = a(test_sc); 213 | ts_c = [ts_c b]; 214 | end 215 | tw_h = []; 216 | for i = 1: 200 217 | a = pines(:,:,i); 218 | b = a(test_wh); 219 | tw_h = [tw_h b]; 220 | end 221 | % Testing matrix 222 | testing_instance_matrix = [tc_n;tc_m;tg_p;tg_t;th_w;ts_n;ts_m;ts_c;tw_h]; 223 | 224 | % test the model 225 | [predicted_label, accuracy, prob_estimates] = svmpredict(testing_label_matrix, testing_instance_matrix, model); 226 | -------------------------------------------------------------------------------- /classes.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/obravo7/Hyperspectral-Image-Segmentation-using-Support-Vector-Machines/c152024090b48151d77fc7d5fc2608c32e94758a/classes.png -------------------------------------------------------------------------------- /load data: -------------------------------------------------------------------------------- 1 | # In progress 2 | --------------------------------------------------------------------------------