How to classify image data using SVM

1 view (last 30 days)
Petrus van Aswegen
Petrus van Aswegen on 10 May 2021
Answered: Akanksha Shrimal on 27 Apr 2022
I am having an issue with using the fitcsvm() function to classify my data. I have used the following code to load my 190 RGB images and their respective groundtruths, divide the these images into 4x4 patches and then read and store the colour information in each patch.
bh = 4; %dimensions of the blocks
bw = 4;
%obtain the folder and the files within them note that any name with "g" is
%groundtruth specific
folname = 'C:\Users\Petru\Documents\Uni\Yr4Sem1\Thesis\Images';
folnameg = 'C:\Users\Petru\Documents\Uni\Yr4Sem1\Thesis\Groundtruth';
fils = dir(fullfile(folname,'*.png'));
filsg = dir(fullfile(folnameg,'*.png'));
%Read the images in from the directory created above
Io = imread(fullfile(fils(1).folder,fils(1).name));
Ig = imread(fullfile(filsg(1).folder,filsg(1).name));
%obtain some metadata from the first image into a table
N1 = bh*ones(floor(size(Io,1)/bh),1);
N1g = bh*ones(floor(size(Ig,1)/bh),1);
N2 = bw*ones(floor(size(Io,2)/bw),1);
N2g = bw*ones(floor(size(Ig,2)/bw),1);
%Loop through both directories at the same time and store information in
%tables L and Lg
L = cell(numel(fils),1);
Lg = cell(numel(filsg),1);
for ii = 1:numel(fils)
I = imread(fullfile(fils(ii).folder,fils(ii).name));
b = mat2cell(I(1:bh*numel(N1),1:bw*numel(N2),:),N1,N2,3);
Ig = imread(fullfile(filsg(ii).folder,filsg(ii).name));
bg = mat2cell(Ig(1:bh*numel(N1g),1:bw*numel(N2g),:),N1g,N2g);
L{ii} = cat(4,b{:});
Lg{ii} = cat(4,bg{:});
end
Does anyone know how to make the data usable for the function, or if there is another process that needs to be used

Answers (1)

Akanksha Shrimal
Akanksha Shrimal on 27 Apr 2022
Hi,
It is my understanding that you want to use fitcsvm() function for the given predictors L and labels Lg.
You can use fitcsvm() for one-class or two-class classification in the following manner.
Mdl = fitcsvm(L,Lg)
Given that L and Lg are matrices, the above code returns a classifier trained on given predictors and labels.
For multi-class classification refer to the following documentation.
Hope this helps.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!