How is the pre-process used in my Lecturer's code?

Im learning about image classification and there's a section that "pre-processes the image but i can't work out if its being used
clear
close all
clc
% Load training and test data using |imageDatastore|.
Training_Dir = fullfile(toolboxdir('vision'),'visiondata','digits','synthetic');
Testing_Dir = fullfile(toolboxdir('vision'),'visiondata','digits','handwritten');
% imageDatastore recursively scans the directory tree containing the
%
trainingSet = imageDatastore(Training_Dir,'IncludeSubfolders',true,'LabelSource','foldernames');
testingSet = imageDatastore(Testing_Dir,'IncludeSubfolders',true,'LabelSource','foldernames');
%% Show a few of the training and test images
close all
figure('Name','Training Image Examples','NumberTitle','off');
movegui("northwest")
subplot(3,3,1);
imshow(trainingSet.Files{12});
subplot(3,3,2);
imshow(trainingSet.Files{315});
subplot(3,3,3);
imshow(trainingSet.Files{810});
subplot(3,3,4);
imshow(trainingSet.Files{1000});
subplot(3,3,5);
imshow(trainingSet.Files{500});
subplot(3,3,6);
imshow(testingSet.Files{10});
subplot(3,3,7);
imshow(testingSet.Files{35});
subplot(3,3,8);
imshow(testingSet.Files{80});
subplot(3,3,9);
imshow(testingSet.Files{105});
%% Show pre-processing and Feature Extraction results
exTestImage = readimage(testingSet,12);
Gray_IM=im2gray(exTestImage);
Binary_IM = imbinarize(Gray_IM);
figure('Name','Preprocessed Image','NumberTitle','off');
movegui("north")
subplot(1,3,1)
imshow(exTestImage)
subplot(1,3,2)
imshow(Gray_IM)
subplot(1,3,3)
imshow(Binary_IM)
%% Extract HOG features from image
[img, imgInfo] = readimage(trainingSet, 508);
imgInfo
cellSize = [1 1];
[hog_4x4, vis4x4] = extractHOGFeatures(img,'CellSize',cellSize);
figure('Name','Histogram of Gradients','NumberTitle','off')
movegui("northeast")
plot(vis4x4);
figure('Name','Training Data with HOG','NumberTitle','off');
movegui("southwest")
imshow(img, 'InitialMagnification',2000)
hold on
plot(vis4x4,'color', 'b')
%set(gcf,'position',[0,0,800,800])
hold off
%% Extracting HOG Features
% HOG features from each image in training set.
hogFeatureSize = length(hog_4x4);
numImages = numel(trainingSet.Files);
trainingFeatures = zeros(numImages,hogFeatureSize,'single');
for i = 1:numImages
img = readimage(trainingSet,i);
img = im2gray(img); %convertign the specified truecolor The RGB Images to a grayscale intensity image
img = imbinarize(img); % pre-processing step to conver the Imges into black & white
trainingFeatures(i, :) = extractHOGFeatures(img,'CellSize',cellSize);
end
% similar procedure will be used with testing set.
numImages_test = numel(testingSet.Files);
testingFeatures = zeros(numImages_test,hogFeatureSize,'single');
for i = 1:numImages_test
img = readimage(testingSet,i);
img = im2gray(img); %convertign the specified truecolor The RGB Images to a grayscale intensity image
img = imbinarize(img); % pre-processing step to conver the Imges into black & white
testingFeatures(i, :) = extractHOGFeatures(img,'CellSize',cellSize);
end
%% Train and Testing a Decision Tree Classifier
% Get labels for each image.
trainingLabels = trainingSet.Labels;
tree = fitctree(trainingFeatures,trainingLabels);
% Evaluating the the Classifier
% Get labels for each image.
testLabels = testingSet.Labels;
predictedLabels = predict(tree, testingFeatures);
% Tabulate the results using a confusion matrix.
figure("Name","Confusion Matrix",'NumberTitle','off')
movegui("south")
cm = confusionchart(testLabels,predictedLabels,'RowSummary','row-normalized','ColumnSummary','column-normalized');
%confMat = confusionmat(testLabels, predictedLabels);
%% Testing the performance of Trained model on some testing images
M=1;
predicted_Image = predict(tree, testingFeatures(M,:));
figure("Name","Predicted Digit", "NumberTitle","off")
imshow(testingSet.Files{M}, 'InitialMagnification',800);
caption = sprintf('Predicted Digit is: %c', predicted_Image );
title(caption, 'FontSize', 18);

Answers (1)

The section
%% Show pre-processing and Feature Extraction results
is used to display example results for very specific images.
The section
for i = 1:numImages
img = readimage(trainingSet,i);
img = im2gray(img); %convertign the specified truecolor The RGB Images to a grayscale intensity image
img = imbinarize(img); % pre-processing step to conver the Imges into black & white
trainingFeatures(i, :) = extractHOGFeatures(img,'CellSize',cellSize);
end
is doing the preprocessing for all of the images in the training set.
for i = 1:numImages_test
img = readimage(testingSet,i);
img = im2gray(img); %convertign the specified truecolor The RGB Images to a grayscale intensity image
img = imbinarize(img); % pre-processing step to conver the Imges into black & white
testingFeatures(i, :) = extractHOGFeatures(img,'CellSize',cellSize);
end
is doing the preprocessing for all of the files in the testing set.
The preprocessed training image data is used in
tree = fitctree(trainingFeatures,trainingLabels);
The preprocessed test image data is used in
predictedLabels = predict(tree, testingFeatures);

Products

Release

R2021b

Asked:

on 14 Apr 2022

Answered:

on 15 Apr 2022

Community Treasure Hunt

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

Start Hunting!