Ältere Kommentare anzeigen
clc;
clear;
close all;
warning off all;
image_folder = 'pengolahan\data latih';
filenames = diri(fullfile(image_folder,'*.jpg'));
total_images = numel(filenames);
area = zeros(1,total_images);
perimeter = zeros(1,total_images);
metric = zeros(1,total_images);
eccentricity = zeros(1,total_images);
for n = 1 : total_images
full_name = fullfile(image_folder, filenames(n).name);
our_images = logical(imread(full_name));
our_images = bwconfull(our_images,'objects');
stats = regionprops(our_images,'Area','Perimeter','Eccentricity');
area(n) = stats.Area;
perimeter(n) = stats.Perimeter;
metric(n) = 4*pi*area(n)/(perimeter(n)^2);
eccentricity(n) = stats.Eccentricity;
trainset = [metric;eccentricity]';
end
%prepare class label for first run of SVM
class = cell(75,1);
class(1:35,1) = {'Sub Tropis'};
class(36:75,1)= {'Tropis'};
%perform run of svm
figure
SVMModel = fitcsvm(trainset,class);
sv = SVMModel.SupportVectors;
gscatter(trainset(:,1), trainset(:,2),class)
grid on
hold on
plot(sv(:,1),sv(:,2),'ko','MarkerSize',10)
legend('Sub Tropis','Tropis','Support Vector')
hold off
xlabel ('Metric')
ylabel ('Eccentricity')
save SVMModel.mat SVMModel
[SL: formatted code as code and added line breaks for readability. Also fixed one missing ' in the line where filenames is first defined.]
2 Kommentare
Joe Anton
am 27 Mai 2020
Image Analyst
am 27 Mai 2020
And why do you think that it somehow knows what trainset is if you never defined it? It can't just make stuff up for you. You need to define it. Where did you do that?
Antworten (1)
Steven Lord
am 27 Mai 2020
1 Stimme
My suspicion is that your images folder contains no JPG files. If it doesn't, your for loop does not execute its loop body and the variable trainset is never created.
Kategorien
Mehr zu Classification Ensembles finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!