How to enable Nearest Neighbor Classifier
I would like to use "Optimizable KNN" for this training, but the whole NNC section is disabled for some reasons. Please help to solve this. The code is attached in the bottom.
% Define the file path
imageFolderPath = "C:UsersxxooxOneDriveデスクトップMATLAB worksDataMathWorks ImagesRoadside 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’);I would like to use "Optimizable KNN" for this training, but the whole NNC section is disabled for some reasons. Please help to solve this. The code is attached in the bottom.
% Define the file path
imageFolderPath = "C:UsersxxooxOneDriveデスクトップMATLAB worksDataMathWorks ImagesRoadside 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’); I would like to use "Optimizable KNN" for this training, but the whole NNC section is disabled for some reasons. Please help to solve this. The code is attached in the bottom.
% Define the file path
imageFolderPath = "C:UsersxxooxOneDriveデスクトップMATLAB worksDataMathWorks ImagesRoadside 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’); matlab, plotting, machine learing, knn, nnc MATLAB Answers — New Questions