title: Matlab SVM for Image Classification
body: <p>I am using SVM function of Matlab to classify images that are read from a folder. What I want to do is first read 20 images from the folder, then use these to train the SVM, and then give a new image as input to decide whether this input image falls into the same category of these 20 training images or not. If it is, then the classification result should give me <code>1</code>, if not, then I expect to receive <code>-1</code>. </p>
  
  <p>Up to now, my written code is as follows: </p>
  
  <pre><code>imagefiles = dir('*.jpg');
  nfiles = 20; 
  
  for i = 1:nfiles
      currentfilename = imagefiles(i).name;
      currentimage = imread(currentfilename);
      images{i} = currentimage;
      images{i} = im2double(images{i});
      images{i} = rgb2gray(images{i});
      images{i} = imresize(images{i},[200 200]);
      images{i} = reshape(images{i}', 1, size(images{i},1)*size(images{i},2));
  end
  
  trainData = zeros(nfiles, 40000);
  
  for ii=1:nfiles
      trainData(ii,:) = images{ii};
  end
  
  class = [1 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 1 1 1 1 1 1];
  SVMStruct = svmtrain (trainData, class);
  
  inputImg = imread('testImg.jpg');
  inputImg = im2double(inputImg);
  inputImg = rgb2gray(inputImg);
  inputImg = imresize(inputImg, [200 200]);
  inputImg = reshape (inputImg', 1, size(inputImg,1)*size(inputImg,2));
  result = svmclassify(SVMStruct, inputImg);
  </code></pre>
  
  <p>Since the images are read by series from the folder, so camethe cell <code>images</code>. Then I converted them to grayscale as shown in the code, and resized them, since those images were NOT of same size. Thus after this step, I had 20 images, all of each with size <code>200x200</code>. And at last, I gave these to serve as my training dataset, with <code>20</code> rows, and <code>200x200</code> columns. I checked all of these size results, and they seemed to work fine. But right now the only problem is, no matter what kind of input image I give it to predict, it always gives me a result as <code>1</code>, even for those very different images. Seems like it is not working correctly. Could someone help me check out where should be the problem here? I couldn't find any explanation from the existing sources on the internet. Thanks in advance. </p>