title: SVM MATLAB Binary Classification
body: <p>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?</p>
  
  <pre><code>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);
  </code></pre>