clc
clear all
close all
% Define the directory where images are located
imageDirectory = 'C:\Users\acer\OneDrive\Desktop\benign';
% Get a list of all folders within the directory
folders = dir(imageDirectory);
folders = folders([folders.isdir]);
folders = folders(3:end); % Remove '.' and '..' entries
% Initialize feature matrix and labels to store the extracted features and their corresponding labels
featureMatrix = [];
labels = [];
% Loop through each folder
for folderIdx = 1:length(folders)
folderPath = fullfile(imageDirectory, folders(folderIdx).name);
fprintf('Processing images in folder: %s\n', folderPath);
% Get a list of all images within the folder
imageFiles = dir(fullfile(folderPath, '*.png'));
% Determine the label for this folder (benign or malignant)
if contains(lower(folders(folderIdx).name), 'benign')
label = 0; % Benign
else
label = 1; % Malignant
end
% Loop through each image
for imageIdx = 1:length(imageFiles)
imagePath = fullfile(folderPath, imageFiles(imageIdx).name);
% Read the image
originalImage = imread(imagePath);
% Preprocessing techniques (use histogram equalization as enhancement)
preprocessedImage = histeq(originalImage);
% Convert to grayscale if the image has multiple channels
if size(preprocessedImage, 3) > 1
preprocessedImage = rgb2gray(preprocessedImage);
end
% Apply thresholding
thresholdedImage = imbinarize(preprocessedImage, graythresh(preprocessedImage));
% Feature extraction (same as before)
meanIntensity = mean2(preprocessedImage);
stdDeviation = std2(preprocessedImage);
medianIntensity = median(preprocessedImage(:));
rangeValue = range(preprocessedImage(:));
skewnessValue = skewness(double(preprocessedImage(:)));
kurtosisValue = kurtosis(double(preprocessedImage(:)));
percentiles = prctile(double(preprocessedImage(:)), [5, 25, 50, 75, 95]);
entropyValue = entropy(preprocessedImage);
varianceValue = var(double(preprocessedImage(:)));
histogramFeatures = [mean(preprocessedImage(:)), var(double(preprocessedImage(:))), ...
skewness(double(preprocessedImage(:))), kurtosis(double(preprocessedImage(:))), ...
mode(preprocessedImage(:)), entropy(preprocessedImage(:))];
% Append the features to the feature matrix
features = [meanIntensity, stdDeviation, medianIntensity, rangeValue, ...
skewnessValue, kurtosisValue, percentiles, entropyValue, varianceValue, histogramFeatures];
featureMatrix = [featureMatrix; features];
% Append the label to the labels array
labels = [labels; label];
end
end
% Convert featureMatrix and labels to double data type
featureMatrix = double(featureMatrix);
labels = double(labels);
% SVM Classifier
svmModel = fitcsvm(featureMatrix, labels, 'KernelFunction', 'linear');
% Evaluate the SVM model using k-fold cross-validation
numFolds = 5; % Number of cross-validation folds
cv = cvpartition(size(featureMatrix, 1), 'KFold', numFolds);
accuracy = zeros(numFolds, 1);
for foldIdx = 1:numFolds
trainIdx = cv.training(foldIdx);
testIdx = cv.test(foldIdx);
svmModel = fitcsvm(featureMatrix(trainIdx, :), labels(trainIdx), 'KernelFunction', 'linear');
predictedLabels = predict(svmModel, featureMatrix(testIdx, :));
accuracy(foldIdx) = sum(predictedLabels == labels(testIdx)) / numel(labels(testIdx));
end
% Calculate the mean accuracy across all folds
meanAccuracy = mean(accuracy);
fprintf('Mean Accuracy: %.2f%%\n', meanAccuracy * 100);
What I have tried:
I tried to find the image path, and then use for loop to open and read all the images, preprocess all the image, extract feature and finally apply SVM classifier