Supervised Image Classification using Statistical Features

Supervised Image Classification using Statistical Features

Spread the love

The provided code implements a supervised image classification system using statistical features. The goal of this system is to classify input images into predefined classes. The code consists of two main parts: training and testing.

During the training phase, the user selects an input image and assigns it a class label representing its category. Statistical features, specifically the mean and standard deviation of the image pixels, are extracted from the image. These features, along with the assigned class label, are stored in a database.

In the testing phase, a separate test image is selected. Similar to the training phase, statistical features are extracted from the test image. The code then compares the features of the test image with those of the training images stored in the database. The image in the database with the minimum difference in feature values is considered the closest match. The corresponding class label of the closest match is assigned to the test image, indicating its predicted category.

This classification system operates under the assumption that images belonging to the same class exhibit similar statistical characteristics. By comparing the statistical features, the system attempts to classify the test image into one of the predefined classes based on its similarity to the training images.

The code utilizes a supervised learning approach, relying on labeled training data, to train a model and make predictions on unseen test data. The extracted statistical features serve as a representation of the images in the training and testing stages.

Overall, this code provides a basic framework for performing supervised image classification using statistical features, offering a starting point for more advanced image recognition and classification tasks.

Train code

clc;
clear all;
close all;
%% Taking an Image
[fname, path]=uigetfile(‘.png’,’Open an Image as input for training’);
fname=strcat(path, fname);
im=imread(fname);
im=im2bw(im);
imshow(im);
title(‘Input Image’);
c=input(‘Enter the Class(Number from 1-12)’);
%% Feature Extraction
F=FeatureStatistical(im);
try
load db;
F=[F c];
db=[db; F];
save db.mat db
catch
db=[F c]; % 10 12 1
save db.mat db
end

Test code

%% Test Image
clc;
clear all;
close all;
[fname, path]=uigetfile(‘.png’,’provide an Image for testing’);
fname=strcat(path, fname);
im=imread(fname);
im=im2bw(im);
imshow(im);
title(‘Test Image’);
%% Find the class the test image belongs
Ftest=FeatureStatistical(im);
%% Compare with the feature of training image in the database
load db.mat
Ftrain=db(:,1:2);
Ctrain=db(:,3);
for (i=1:size(Ftrain,1));
dist(i,:)=sum(abs(Ftrain(i,:)-Ftest));
end
m=find(dist==min(dist),1);
det_class=Ctrain(m);
msgbox(strcat(‘Detected Class=’,num2str(det_class)));

FeatureStatistical Code

function [F]=FeatureStatistical(im)
% Summary of this function goes here
im=double(im);
m=mean(mean(im));
s=std(std(im));
F=[m s];
end

Code explanation

  1. The first section of the code is for training. It starts by prompting the user to select an input image (assumed to be in PNG format). The selected image is then converted to black and white (binary) using im2bw function and displayed using imshow.
  2. The user is then asked to enter a class number ranging from 1 to 12, representing the class to which the image belongs.
  3. The code calls a function FeatureStatistical to extract features from the image. We’ll discuss this function later.
  4. The code attempts to load a file named “db.mat” which is assumed to contain a database of previously trained images and their corresponding class labels. If the file doesn’t exist, it creates a new database with the current image’s features and class label and saves it as “db.mat”. If the file exists, it appends the current image’s features and class label to the existing database and saves it.
  1. The second section of the code is for testing. It prompts the user to select a test image (also assumed to be in PNG format) and performs the same steps as in the training section to convert it to binary and display it.
  2. The code loads the “db.mat” file containing the database of trained images and their class labels.
  3. It calls the FeatureStatistical function again to extract features from the test image.
  4. The code compares the features of the test image with the features of the training images in the database. It calculates the absolute differences between the feature vectors of each training image and the test image using the sum(abs(Ftrain(i,:)-Ftest)) expression.
  5. The index (m) of the minimum difference is found using find(dist==min(dist),1).
  6. The corresponding class label (det_class) of the training image with the minimum difference is obtained from Ctrain(m).
  7. Finally, a message box is displayed showing the detected class label of the test image.

The purpose of this function is to extract statistical features from the input image. Here are the steps it performs:

  1. The input image (im) is converted to double precision.
  2. The mean (m) and standard deviation (s) of the image pixels are computed using the mean and std functions, respectively.
  3. The mean and standard deviation values are stored in an array F.
  4. The function returns the array F as the output.

The FeatureStatistical function essentially computes the statistical features of the image, which can be used as a representation of the image in the training and testing stages. In this case, the features used are the mean and standard deviation of the pixel values, but you can modify this function to extract other statistical features based on your requirements.

Alexnet code: How to use Alexnet to train images & calculate its accuracy.

Falana William is a passionate writer who has a keen interest in various topics. With expertise as a certified Google digital marketing expert, Falana William possesses the skills and knowledge to navigate the ever-evolving digital landscape. Combining a love for writing with proficiency in digital marketing, Falana William is equipped to create engaging and effective content that resonates with target audiences.
Back To Top