MATLAB Programs for Image Processing
and Machine Learning Experiments
Experiment 1: Image preprocessing techniques: resizing, filtering,
thresholding
MATLAB Code:
% Experiment 1: Image Preprocessing
img = imread('peppers.png');
gray = rgb2gray(img);
resized = imresize(gray, [200 200]);
filtered = imgaussfilt(resized, 2);
bw = imbinarize(filtered);
imshowpair(gray, bw, 'montage');
title('Original vs Thresholded Image');
Experiment 2: Edge detection using Sobel, Canny, and Prewitt operators
MATLAB Code:
% Experiment 2: Edge Detection
img = rgb2gray(imread('peppers.png'));
edges_sobel = edge(img,'sobel');
edges_canny = edge(img,'canny');
edges_prewitt = edge(img,'prewitt');
subplot(1,3,1), imshow(edges_sobel), title('Sobel');
subplot(1,3,2), imshow(edges_canny), title('Canny');
subplot(1,3,3), imshow(edges_prewitt), title('Prewitt');
Experiment 3: Object detection using boundaries and bounding boxes
MATLAB Code:
% Experiment 3: Object Detection using boundaries
img = imread('coins.png');
bw = imbinarize(rgb2gray(img));
bw = imfill(bw,'holes');
[B,L] = bwboundaries(bw,'noholes');
imshow(label2rgb(L,@jet,[.5 .5 .5]))
hold on;
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
end
title('Detected Objects with Boundaries');
Experiment 4: Feature extraction using HOG
MATLAB Code:
% Experiment 4: Feature Extraction using HOG
img = rgb2gray(imread('peppers.png'));
[featureVector,hogVisualization] = extractHOGFeatures(img);
imshow(img); hold on;
plot(hogVisualization);
title('HOG Feature Extraction');
Experiment 5: Face detection using Viola-Jones algorithm
MATLAB Code:
% Experiment 5: Face Detection using Viola-Jones
facedetector = vision.CascadeObjectDetector();
img = imread('visionteam.jpg');
bbox = step(facedetector, img);
out = insertObjectAnnotation(img, 'rectangle', bbox, 'Face');
imshow(out);
title('Face Detection');
Experiment 6: Train a KNN classifier for image classification
MATLAB Code:
% Experiment 6: Simple Image Classification using KNN
digitDatasetPath = fullfile(matlabroot,'toolbox','nnet','nndemos', ...
'nndatasets','DigitDataset');
imds = imageDatastore(digitDatasetPath, ...
'IncludeSubfolders',true,'LabelSource','foldernames');
[imdsTrain,imdsTest] = splitEachLabel(imds,0.7,'randomize');
bag = bagOfFeatures(imdsTrain);
categoryClassifier = trainImageCategoryClassifier(imdsTrain,bag);
confMatrix = evaluate(categoryClassifier,imdsTest);
disp('Confusion Matrix:');
disp(confMatrix);
Experiment 7: Decision Tree Classifier on Iris Dataset
MATLAB Code:
% Experiment 7: Decision Tree Classifier
load fisheriris
X = meas;
Y = species;
tree = fitctree(X,Y);
view(tree,'Mode','graph');
label = predict(tree,X(1,:));
disp(['Predicted class: ', char(label)]);
Experiment 8: Logistic Regression for binary classification
MATLAB Code:
% Experiment 8: Logistic Regression
load fisheriris
X = meas(51:end,1:2);
Y = strcmp(species(51:end),'versicolor');
mdl = fitglm(X,Y,'Distribution','binomial');
disp(mdl);
plot(mdl);
title('Logistic Regression');
Experiment 9: PCA for feature reduction and visualization
MATLAB Code:
% Experiment 9: PCA Feature Reduction
load fisheriris
[coeff,score,latent] = pca(meas);
gscatter(score(:,1),score(:,2),species);
xlabel('Principal Component 1');
ylabel('Principal Component 2');
title('PCA on Iris Dataset');
Experiment 10: Simple Neural Network Training
MATLAB Code:
% Experiment 10: Simple Neural Network (MATLAB)
[x,t] = simplefit_dataset;
net = feedforwardnet(10);
net = train(net,x,t);
y = net(x);
plotperform(net);
title('Simple Neural Network Training');
Experiment 11: CNN for Digit Recognition (MNIST)
MATLAB Code:
% Experiment 11: CNN using MATLAB
digitDatasetPath =
fullfile(matlabroot,'toolbox','nnet','nndemos','nndatasets','DigitData
set');
imds =
imageDatastore(digitDatasetPath,'IncludeSubfolders',true,'LabelSo
urce','foldernames');
[imdsTrain,imdsTest] = splitEachLabel(imds,0.7,'randomize');
layers = [
imageInputLayer([28 28 1])
convolution2dLayer(3,8,'Padding','same')
reluLayer
maxPooling2dLayer(2,'Stride',2)
fullyConnectedLayer(10)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm','MaxEpochs',4,'Plots','training-
progress');
net = trainNetwork(imdsTrain,layers,options);
YPred = classify(net,imdsTest);
YTest = imdsTest.Labels;
accuracy = sum(YPred == YTest)/numel(YTest);
disp(['Accuracy: ', num2str(accuracy)]);
Experiment 12: Real-time Emotion Recognition using webcam (basic
demo)
MATLAB Code:
% Experiment 12: Real-time Emotion Recognition (basic webcam
demo)
cam = webcam;
faceDetector = vision.CascadeObjectDetector();
for i = 1:50
img = snapshot(cam);
bbox = step(faceDetector, img);
if ~isempty(bbox)
out = insertObjectAnnotation(img,'rectangle',bbox,'Face');
else
out = img;
end
imshow(out);
drawnow;
end
clear cam;