You are on page 1of 3

clc;

clear all;
close all;

[file path] = uigetfile('*.*','Pick the Satellite Image');


img = imread([path file]);
imshow(img);
title('Original Image');

%Read the individual images


cotton_img = imread('cotton.tif');
wheat_img = imread('wheat.tif');
gram_img = imread('gram.tif');

%Now apply svm for this


disp('Training SVMs');
tic;
svm_cw = svmtrain(double([cotton_img(1:50) wheat_img(1:50)]),[ones([1
50]) (ones([1 50])*2)]);
svm_cg = svmtrain(double([cotton_img(1:50) gram_img(1:50)]),[ones([1
50]) (ones([1 50])*3)]);
svm_wg = svmtrain(double([wheat_img(1:50) gram_img(1:50)]),[(ones([1
50])*2) (ones([1 50])*3)]);

ITERATIONS = 10;
POPULATION = 50;

%Evaluate the SVM for each column


out_img = img;
disp('Evaluating SVM');
for r=1:size(img,1)
for c=1:size(img,2)

red = img(r,c,1);
green = img(r,c,2);
blue = img(r,c,3);

if( (red > green && red > blue))


%This can be a cotton portion
cotton = 255;
wheat = 0;
gram = 0;
end

if( (green > red && green > blue))


%This can be a wheat portion
cotton = 0;
wheat = 255;
gram = 0;
end

if( (blue > red && blue > green))


%This can be a gram portion
cotton = 0;
wheat = 0;
gram = 255;
end

try
if(c == 1 && mod(r,50) == 0)
svm_img = img(r:r+49);
new_pop = [];
for count=1:ITERATIONS
pop = new_pop;
fitness_arr = zeros([1 POPULATION]);
for pop_count=1:POPULATION
idx = ceil(rand*3);
pop(pop_count,1) = idx;
fitness = 0;
%Find the population fitness
if(idx == 1 && cotton == 255)
pop(pop_count,2) = 2;
pop(pop_count,3) = 3;
fitness = fitness + 1;
elseif(idx == 2 && wheat == 255)
pop(pop_count,2) = 1;
pop(pop_count,3) = 3;
fitness = fitness + 1;
elseif(idx == 3 && gram == 255)
pop(pop_count,2) = 1;
pop(pop_count,3) = 2;
fitness = fitness + 1;
end
fitness_arr(pop_count) = fitness;
end

%Find the best fit population


fit_pop_idx = find(fitness_arr == 1);

%Get the population which is the fitest


fit_pop = pop(fit_pop_idx,:);

%Now pass this to the next iteration


new_pop = fit_pop;
end

%Get the best population for output


pop_best = fit_pop(1,:)*255;

cw = svmclassify(svm_cw,double(pop_best'));
cg = svmclassify(svm_cg,double(pop_best'));
wg = svmclassify(svm_wg,double(pop_best'));
%Now get the max class
total_class = [cw;cg;wg];
length_cotton = length(find(total_class == 1));
length_wheat = length(find(total_class == 2));
length_gram = length(find(total_class == 3));

if(length_cotton > length_wheat && length_cotton >


length_gram)
Cotton = 255;
Wheat = 0;
Gram = 0;
fprintf('.');
elseif(length_wheat > length_cotton && length_wheat >
length_gram)
Cotton = 0;
Wheat = 255;
Gram = 0;
fprintf('.');
else
Cotton = 0;
Wheat = 0;
Gram = 255;
fprintf('.');
end
end
end

out_img(r,c,1) = cotton; %Cotton in Red Color


out_img(r,c,2) = wheat; %Wheat in Green Color
out_img(r,c,3) = gram; %Gram in Blue Color

end
end

img_classified = rgb2gray(out_img);
img_classified = imresize(img_classified,[256 256]);
img_classified = img_classified + (abs(min(min(img_classified))));
img_classified = img_classified / (max(max(img_classified)));
img_classified = round(img_classified*255);
kappa = abs(findKappa(double(img_classified),-1,0.5));

time = toc;

disp('SVM + GA');
kappa
time

fprintf('\n');
subplot(2,1,1);
imshow(img);
title('Original Image');
subplot(2,1,2);
imshow(out_img);
title('Classified Image (R:Cotton,G:Wheat:B:Gram');

You might also like