Matlab R2016b
显示 更早的评论
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 个评论
Joe Anton
2020-5-27
Image Analyst
2020-5-27
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?
回答(1 个)
Steven Lord
2020-5-27
1 个投票
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Classification Ensembles 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!