How to compare the features from an unknown image with the feature vectors I extracted from my database images and display the matched image name?

1 view (last 30 days)
I stored the features in a mat file:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc;
clear all;
close all;
warning off;
f = processEyeImagesInFolder("F:\8th SEM\Dataset\CASIA");
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function featureTable = processEyeImagesInFolder(folderPath)
% List all image files in the specified folder
imageFiles = dir(fullfile(folderPath, '*.jpg')); % Update the file extension if needed
% Initialize a cell array to store image names and feature vectors
featureData = cell(numel(imageFiles), 2);
% Iterate through each image file
for i = 1:numel(imageFiles)
% Obtain the full file path
imagePath = fullfile(folderPath, imageFiles(i).name);
% Call the function to get the feature vector
currentFeatureVector = getFeatures(imagePath);
disp(['Image Name: ', imageFiles(i).name]);
disp('Feature Vector:');
disp(currentFeatureVector);
% Store image name and feature vector in the cell array
featureData{i, 1} = imageFiles(i).name;
featureData{i, 2} = currentFeatureVector;
end
% Convert cell array to a table
featureTable = cell2table(featureData, 'VariableNames', {'ImageName', 'FeatureVector'});
% Save the table to a MAT file
save('Feature_Data.mat','featureTable');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
my MAT file looks like this:
It is a 9x2 table.
Now how to compare the feature vector obtained from an unknown image, compare it with the feature vectors in the mat file and display the image name with the closest match?

Accepted Answer

Balavignesh
Balavignesh on 11 Feb 2024
Hello Ridhima,
Based on what I've gathered, you're looking to compare an unknown image against a set of feature vectors that have been saved in your MAT file.
Assuming that the new image file is available in your current working directory, you should start by extracting its feature vectors. This can be done using the same 'getFeatures' function employed for your existing dataset. To measure the similarity between the feature vector of the unknown image and those stored in your dataset, the 'Euclidean distance' metric would be a suitable choice. You'll want to identify the feature vector in your dataset that has the minimum Euclidean distance to the one from the unknown image.
Here's a function crafted to carry out the above process:
% unknownImagePath is the path to your new image.
function closestImageName = findClosestFeatureMatch(unknownImagePath)
% Load the stored feature vectors from the MAT file
load('Feature_Data.mat', 'featureTable');
% Extract the feature vector from the unknown image
unknownFeatureVector = getFeatures(unknownImagePath);
% Initialize variables to store the minimum distance and the index of the closest match
minDistance = inf;
closestIndex = -1;
% Iterate over each stored feature vector to calculate distances
for i = 1:size(featureTable, 1)
% Extract the current stored feature vector
storedFeatureVector = featureTable.FeatureVector{i};
% Compute the distance between the unknown feature vector and the current stored vector
distance = norm(unknownFeatureVector - storedFeatureVector);
% Update the minimum distance and index if the current distance is smaller
if distance < minDistance
minDistance = distance;
closestIndex = i;
end
end
% Check if a closest match was found
if closestIndex == -1
error('No matches found.');
else
% Return the name of the image with the closest feature vector
closestImageName = featureTable.ImageName{closestIndex};
end
end
Kindly have a look at the following documentation links to have more information on:
Hope that helps!
Balavignesh

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!