How to enable Nearest Neighbor Classifier
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Asuka
am 21 Jun. 2024
Beantwortet: Image Analyst
am 22 Jun. 2024
I would like to use "Optimizable KNN" for this training, but the whole NNC section is disabled for some reason. Please help to solve this. The code is attached in the bottom.
% Define the file path
imageFolderPath = "C:\Users\xxoox\OneDrive\デスクトップ\MATLAB works\Data\MathWorks Images\Roadside Ground Cover";
% Create an imageDatastore
imds = imageDatastore(imageFolderPath, ...
'IncludeSubfolders', true, ...
'LabelSource', 'foldernames');
% Display the count of each label
labelCount = countEachLabel(imds);
disp('Initial label count:');
disp(labelCount);
% Split the datastore into training and testing subsets (85% training, 15% testing)
[imdsTrain, imdsTest] = splitEachLabel(imds, 0.85, 'randomized');
% Display the count of each label in the training datastore
labelCountTrain = countEachLabel(imdsTrain);
disp('Training label count:');
disp(labelCountTrain);
% Count the images labeled as "Snow" in the training datastore
numSnowTrain = sum(imdsTrain.Labels == "Snow");
disp(['Number of "Snow" images in the training datastore: ', num2str(numSnowTrain)]);
% Define the file path to the specific image
imageFilePath = fullfile(imageFolderPath, 'No Snow', 'RoadsideA_1.jpg');
% Read the image
img = imread(imageFilePath);
% Convert the image from RGB to HSV color space
hsvImage = rgb2hsv(img);
% Extract the saturation channel
saturationChannel = hsvImage(:,:,2);
% Calculate the mean saturation value
meanSaturation = mean(saturationChannel(:));
% Display the mean saturation
disp(['The mean saturation for the "No Snow" labeled image "RoadsideA_1.jpg" is: ', num2str(meanSaturation)]);
% Calculate the standard deviation of the saturation values
stdSaturation = std(saturationChannel(:));
disp(['The standard deviation of the saturation for the "No Snow" labeled image "RoadsideA_1.jpg" is: ', num2str(stdSaturation)]);
% Initialize arrays to store the results
fileNames = imdsTrain.Files;
labels = imdsTrain.Labels;
meanSaturations = zeros(length(fileNames), 1);
stdSaturations = zeros(length(fileNames), 1);
% Loop through each image in the training datastore
for i = 1:length(fileNames)
% Read the image
img = imread(fileNames{i});
% Convert the image from RGB to HSV color space
hsvImage = rgb2hsv(img);
% Extract the saturation channel
saturationChannel = hsvImage(:,:,2);
% Calculate the mean and standard deviation of the saturation
meanSaturations(i) = mean(saturationChannel(:));
stdSaturations(i) = std(saturationChannel(:));
end
% Create a table with the results
trainingTable = table(fileNames, labels, meanSaturations, stdSaturations, ...
'VariableNames', {'Filename', 'Label', 'MeanSaturation', 'StdSaturation'});
% Display the table
disp(trainingTable);
% Create the scatter plot
figure;
gscatter(trainingTable.MeanSaturation, trainingTable.StdSaturation, trainingTable.Label, 'rb', 'ox');
xlabel('Mean Saturation');
ylabel('Standard Deviation of Saturation');
title('Grouped Scatter Plot of Mean Saturation and Standard Deviation of Saturation');
legend('No Snow', 'Snow');
% Save the figure
saveas(gcf, 'GroupedScatterPlot.png');
% Display the result
disp('Scatter plot created and saved as GroupedScatterPlot.png');
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 22 Jun. 2024
I don't see how your code is doing any KNN classification. Where is the call to knnsearch? Are you sure you have training data and unknown/test data loaded in to the applet? You are using the Classification Learner app, right? Can you upload the data you used and tell us what you used for the dataset variable and the response variable?
0 Kommentare
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!