6
votes

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 1, if not, then I expect to receive -1.

Up to now, my written code is as follows:

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);

Since the images are read by series from the folder, so camethe cell images. 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 200x200. And at last, I gave these to serve as my training dataset, with 20 rows, and 200x200 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 1, 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.

2
images{i} = im2double(images{ii}); Why ii?chenaren
Does it return 1 on images from the training set which have a class of -1 ?Photon
sorry, for the {ii}, I typed it incorrectly in my question here. I edited it.E_learner
@Photon: I can try it on the training set then will give you more information. Thank you.E_learner
Ok, I solved this by myself. Thank you all for your interest.E_learner

2 Answers

4
votes

Here is a function to read all images that may help you

function X = ReadImgs(Folder,ImgType)
Imgs = dir(fullfile(Folder, ImgType));
NumImgs = size(Imgs,1);
image = double(imread(fullfile(Folder, Imgs(1).name)));
X = zeros([NumImgs size(image)]);
for i=1:NumImgs,
  img = double(imread(fullfile(Folder, Imgs(i).name)));
  if (size(image,3) == 1)
    X(i,:,:) = img;
  else
    X(i,:,:,:) = img;
end
end

Source: http://computervisionblog.wordpress.com/2011/04/13/matlab-read-all-images-from-a-folder-everything-starts-here/

0
votes

this is should be works in MATLAB

clear all; 
clc; 

folder = 'gambar 1'; 
dirImage = dir( folder ); 

numData = size(dirImage,1); 

M ={} ; 

% read image 
for i=1:numData
    nama = dirImage(i).name;  
    if regexp(nama, '(lion|tiger)-[0-9]{1,2}.jpg')
        B = cell(1,2); 
        if regexp(nama, 'lion-[0-9]{1,2}.jpg')
            B{1,1} = double(imread([folder, '/', nama]));
            B{1,2} = 1; 
        elseif regexp(nama, 'tiger-[0-9]{1,2}.jpg')
            B{1,1} = double(imread([folder, '/', nama]));
            B{1,2} = -1; 
        end
        M = cat(1,M,B); 
    end 
end

% convert image holder from cell to array
numDataTrain = size(M,1); 
class = zeros(numDataTrain,1);
arrayImage = zeros(numDataTrain, 300 * 300);

for i=1:numDataTrain
    im = M{i,1} ;
    im = rgb2gray(im); 
    im = imresize(im, [300 300]); 
    im = reshape(im', 1, 300*300); 
    arrayImage(i,:) = im; 
    class(i) = M{i,2}; 
end

SVMStruct = svmtrain(arrayImage, class);

% test for lion
lionTest = double(imread('gambar 1/lion-test.jpg' )); 
lionTest = rgb2gray(lionTest); 
lionTest = imresize(lionTest, [300 300]); 
lionTest = reshape(lionTest',1, 300*300); 
result = svmclassify(SVMStruct, lionTest);  

result 

https://github.com/gunungloli666/svm-test