Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Updated 21-Jul-23 23:23pm
Comments
Member 15627495 22-Jul-23 5:05am    
You provide too much code. Where are the lines ton enhance ?
Is the analysis of the images which needs new code ?
Or another part ?

Can you be more specific in your question please ?

explain your project deeper, it will help us to help you !

where comes 64.44% from ? explain us please !
Kiana Mahtabi Nourani 23-Jul-23 3:17am    
Hi. In this code I have 3 types or classes of images, benign, malignant, and normal where from each of these have image classes I have 60 images. I saved all the images in a main folder. inside that folder I have 3 different folders related to each image class respectively. so first the code is trying to find and read the main folder and the classed image folders(benign,malignant,normal). The code will label the images and show the extracted features in matrix form. the using a for loop, the code will first read the original images and then, apply histogram equalization as enhancement method on each of the images and threshold the enhanced images. after that the code will move to feature extraction part and represent the extracted features on matrix form. Then it will start applying SVM classifier in the preprocessed images that we already extracted their features and finally it will find the extracted features accuracy. where after running the code I got the accuracy as 64.44% and I want to increase this accuracy.
Member 15627495 22-Jul-23 5:22am    
save your code as 1st version, with the "Date/your name/the aim of the code" as comments.

and work on a second version
you can gain speed, by avoid delay with vars.
you have lot of duets and allocations for nothing.
mind that 'constants' are not changing and you allocate memory slots to make a copy, then you use the copy ( 20 times in your code ). one single var ( and its value ) is enough, why make two ? or more ...

code straight to the point.

1 solution

Did you try asking in the official Answers[^] forum? It's the equivalent of CodeProject's Quick Answers[^] (where this question is posted) except it is purely Matlab where you should get good support. Here is the link: MATLAB Answers - MATLAB Central[^]

If I was in your position, that is where I would have first asked the question.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900