0

I am new to SVM, and I am interested in segmenting two distinct objects in an image (apples and grapes. For training, I have created new images (812x185) by extracted apples and grapes. But, when I use MATLAB SVM to test an image containing apples and grapes, I only receive the class of one object instead a segment of two objects. Can anyone assist me on what I am doing wrong?

source={'apple1.jpg','apple2.jpg','apple3.jpg','grape1.jpg', ...
'grape2.jpg','grape3.jpg'};
nfiles = 6; 

for i = 1:nfiles
currentfilename = source{i}
currentimage = imread(currentfilename);
images{i} = currentimage;
images{i} = im2double(images{i});
images{i} = rgb2gray(images{i});
images{i} = imresize(images{i},[900 700]);
images{i} = reshape(images{i}', 1, size(images{i},1)*size(images{i},2));
end

 trainData = zeros(nfiles, 630000);

 for ii=1:nfiles
 trainData(ii,:) = images{ii};
 end

 class = [1 1 1 -1 -1 -1];
 SVMStruct = svmtrain (trainData, class);


inputImg = imread('test_image.jpg');
 inputImg = im2double(inputImg);
 inputImg = rgb2gray(inputImg);
     images{i} = imresize(images{i},[900 700]);
  inputImg = reshape (inputImg', 1, size(inputImg,1)*size(inputImg,2));
 result = svmclassify(SVMStruct, inputImg);
Shai
  • 111,146
  • 38
  • 238
  • 371
Carnez Davis
  • 65
  • 3
  • 7
  • It is best [not to use `i` as a variable name in Matlab](http://stackoverflow.com/questions/14790740/using-i-and-j-as-variables-in-matlab) – Shai Jul 07 '13 at 15:18

1 Answers1

0

You are confusing per-pixel classification with per-image classification.

You are training a classifier to distinguish between an image of apples to an image of grapes. Therefore, when you input a test image you get a single bit output saying whether this image is of apples or grapes.

If you want a per-pixel decision, you need to provide a per-pixel labeling: which pixel is a grape and which is an apple, then you may expect your output to be at the pixel-level.

A classic paper on semantic segmentation (which seems like what you are trying to accomplish) is the textonBoost paper, by Shotton, Winn, rother and Criminisi from MSRC.

Shai
  • 111,146
  • 38
  • 238
  • 371