function cimg = classifyImage(img)
cimg = 0;
if size(img,3) ~= 3
errordlg('Data error: Image must be handed as an M-by-N-by-3 array');
return;
elseif ~isnumeric(img)
errordlg('Data error: Handed data does not appear to be an image');
return;
end
im_height = size(img,1);
im_width = size(img,2);
red_layer = img(:,:,1);
green_layer = img(:,:,2);
blue_layer = img(:,:,3);
red_layer = red_layer(:);
green_layer = green_layer(:);
blue_layer = blue_layer(:);
img_vector = [red_layer green_layer blue_layer];
size(blue_layer)
figure(1);
imshow(img);
title('Select background');
[xBack, yBack] = ginput(2);
title('Select foreground');
[xFront, yFront] = ginput(2);
xBack = round(xBack);
yBack = round(yBack);
xFront = round(xFront);
yFront = round(yFront);
close(1);
background = img(xBack(1):xBack(2),yBack(1):yBack(2),:);
foreground = img(xFront(1):xFront(2),yFront(1):yFront(2),:);
bg_red = background(:,:,1);
bg_red = bg_red(:);
bg_green = background(:,:,2);
bg_green = bg_green(:);
bg_blue = background(:,:,3);
bg_blue = bg_blue(:);
fg_red = foreground(:,:,1);
fg_red = fg_red(:);
fg_green = foreground(:,:,2);
fg_green = fg_green(:);
fg_blue = foreground(:,:,3);
fg_blue = fg_blue(:);
bg_feats = [bg_red bg_green bg_blue];
fg_feats = [fg_red fg_green fg_blue];
features = [bg_feats; fg_feats];
groups = [-ones(length(bg_red),1); ones(length(fg_red),1)];
[features, groups] = reduceFeatures(features,groups,500);
options = optimset('maxiter',1000);
tic
disp('Training...')
svm_struct = svmtrain(double(features), groups, 'kernel_function', 'rbf', ...
'method', 'QP', ...
'quadprog_opts',options)
toc
disp('Classifying...')
cimg = svmclassify(svm_struct,double(img_vector));
toc
disp('Re-assembling image...')
cimg = reshape(cimg, im_height, im_width);
toc
disp('Profit!')