i want to classify color feature of dataset with SVM, it can or not?

18 views (last 30 days)
Anggita Puspawardani
Anggita Puspawardani on 19 May 2019
Hi, I'm a newbee of Matlab. I have a problem that i want to classify with SVM, but i cannot. For segmentation, I'm using K-Means. And for feature extraction, I'm using Histogram.
I just want to classify of dataset based on color feature. So, what should i do?
here's my code:
clc;clear;close all;
[filename,pathname] = uigetfile({'*.*';'*.bmp';'*.tif';'*.gif';'*.png'},'Pick a Disease Affected Leaf');
I = imread([pathname,filename]);
figure, imshow(I);title('Disease Affected Leaf');
% Color Image Segmentation Using K-Means Clustering
cform = makecform('srgb2lab');
lab_he = applycform(I,cform);
figure, imshow(lab_he), title('L*a*b color space');
ab = double(lab_he(:,:,2:3));
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);
nColors = 3;
[cluster_idx cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...
'Replicates',3);
pixel_labels = reshape(cluster_idx,nrows,ncols);
RGB = label2rgb(pixel_labels);
figure, imshow(RGB,[]), title('image labeled by cluster index');
segmented_images = cell(1,3);
rgb_label = repmat(pixel_labels,[1,1,3]);
for k = 1:nColors
colors = I;
colors(rgb_label ~= k) = 0;
segmented_images{k} = colors;
end
figure, subplot(3,1,1);imshow(segmented_images{1});title('Cluster 1'); subplot(3,1,2);imshow(segmented_images{2});title('Cluster 2');
subplot(3,1,3);imshow(segmented_images{3});title('Cluster 3');
% Leaf segmentation
area_cluster1 = sum(sum(pixel_labels==1));
area_cluster2 = sum(sum(pixel_labels==2));
area_cluster3 = sum(sum(pixel_labels==3));
[~,cluster_leaf] = min([area_cluster1,area_cluster2,area_cluster3]);
leaf_bw = (pixel_labels==cluster_leaf);
leaf_bw = imfill(leaf_bw,'holes');
leaf_bw = bwareaopen(leaf_bw,1000);
leaf = I;
R = leaf(:,:,1);
G = leaf(:,:,2);
B = leaf(:,:,3);
R(~leaf_bw) = 0;
G(~leaf_bw) = 0;
B(~leaf_bw) = 0;
leaf_rgb = cat(3,R,G,B);
figure, imshow(leaf_rgb), title('the leaf only (RGB Color Space)');
% RGB Features Extraction
R_stats = regionprops(leaf_bw,R,'PixelValues','MeanIntensity',...
'MaxIntensity','MinIntensity');
G_stats = regionprops(leaf_bw,G,'PixelValues','MeanIntensity',...
'MaxIntensity','MinIntensity');
B_stats = regionprops(leaf_bw,B,'PixelValues','MeanIntensity',...
'MaxIntensity','MinIntensity');
R_pix_val = R_stats.PixelValues;
G_pix_val = G_stats.PixelValues;
B_pix_val = B_stats.PixelValues;
figure,
histogram(R_pix_val,256,'FaceColor','r','EdgeColor','r')
set(gca,'XLim',[0 255])
set(gca,'YLim',[0 15000])
grid on
title('Histogram of Red Channel')
figure,
histogram(G_pix_val,256,'FaceColor','g','EdgeColor','g')
set(gca,'XLim',[0 255])
set(gca,'YLim',[0 15000])
grid on
title('Histogram of Green Channel')
figure,
histogram(B_pix_val,256,'FaceColor','b','EdgeColor','b')
set(gca,'XLim',[0 255])
set(gca,'YLim',[0 15000])
grid on
title('Histogram of Blue Channel')
R_mean = R_stats.MeanIntensity;
G_mean = G_stats.MeanIntensity;
B_mean = B_stats.MeanIntensity;
R_max = R_stats.MaxIntensity;
G_max = G_stats.MaxIntensity;
B_max = B_stats.MaxIntensity;
R_min = R_stats.MinIntensity;
G_min = G_stats.MinIntensity;
B_min = B_stats.MinIntensity;
% HSV features extraction
leaf = rgb2hsv(I);
H = leaf(:,:,1);
S = leaf(:,:,2);
V = leaf(:,:,3);
H(~leaf_bw) = 0;
S(~leaf_bw) = 0;
V(~leaf_bw) = 0;
leaf_hsv = cat(3,H,S,V);
figure, imshow(leaf_hsv), title('the leaf only (HSV Color Space)');
H_stats = regionprops(leaf_bw,H,'PixelValues','MeanIntensity',...
'MaxIntensity','MinIntensity');
S_stats = regionprops(leaf_bw,S,'PixelValues','MeanIntensity',...
'MaxIntensity','MinIntensity');
V_stats = regionprops(leaf_bw,V,'PixelValues','MeanIntensity',...
'MaxIntensity','MinIntensity');
H_mean = H_stats.MeanIntensity;
S_mean = S_stats.MeanIntensity;
V_mean = V_stats.MeanIntensity;
H_max = H_stats.MaxIntensity;
S_max = S_stats.MaxIntensity;
V_max = V_stats.MaxIntensity;
H_min = H_stats.MinIntensity;
S_min = S_stats.MinIntensity;
V_min = V_stats.MinIntensity;
% Print the features in the command window.
disp('>>> Features Extracted <<<')
disp('<1> Red Channel')
fprintf('Mean = %6.2f \n',R_mean);
fprintf('Max = %6.0f \n',R_max);
fprintf('Min = %6.0f \n',R_min);
disp('<2> Green Channel')
fprintf('Mean = %6.2f \n',G_mean);
fprintf('Max = %6.0f \n',G_max);
fprintf('Min = %6.0f \n',G_min);
disp('<3> Blue Channel')
fprintf('Mean = %6.2f \n',B_mean);
fprintf('Max = %6.0f \n',B_max);
fprintf('Min = %6.0f \n',B_min);
disp('<4> Hue Channel')
fprintf('Mean = %6.4f \n',H_mean);
fprintf('Max = %6.4f \n',H_max);
fprintf('Min = %6.4f \n',H_min);
disp('<5> Saturation Channel')
fprintf('Mean = %6.4f \n',S_mean);
fprintf('Max = %6.4f \n',S_max);
fprintf('Min = %6.4f \n',S_min);
disp('<6> Value Channel')
fprintf('Mean = %6.4f \n',V_mean);
fprintf('Max = %6.4f \n',V_max);
fprintf('Min = %6.4f \n',V_min);
%% Classification Using SVM
% Load the training set
load Diseaseset.mat
% 'diasesefeat' contains the features of the disease affected leaves of both
% the types
% 'diseasetype' contains the corresponding label
% Train the classifier
svmStructDisease = fitcsvm(diseasefeat,diseasetype);
% Classify the test leaf
species_disease = predict(svmStructDisease,feat_disease)
% Observe the results on the command window
  2 Comments
Anggita Puspawardani
Anggita Puspawardani on 12 Oct 2020
What is your MATLAB version? You should change the function based on your MATLAB version.

Sign in to comment.

Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!

Translated by