Effacer les filtres
Effacer les filtres

My image is stored in struct format after loading a .mat image. How to convert it back into image form ? It load as a struct 1x1 format.

6 vues (au cours des 30 derniers jours)
I want to load .mat images from a folder and display it as an image.
After loading a .mat image using load function,. How to convert it back into image form ? It loads as a struct 1x1 format.
Please Help me.
clc;
clear all;
close all;
warning off;
%%%%%%%%%%%%%%%%%% FEATURE MATCHING USING HAMMING DISTANCE %%%%%%%%%%%
input_features = getFeatures("F:\8th SEM\Dataset\CASIA\file6.jpg");%Complex image
subplot(1,2,1);imshow(input_features);title("Input Unknown Image");
input_features = double(input_features);
% List all images in the folder
folderPath = "F:\8th SEM\Extracted Features";
imageFiles = dir(fullfile(folderPath, '*.mat')); % Assuming images are in jpg format
% Initialize variables to store minimum distance and closest image
minDistance = Inf;
closestImage = '';
k=0;
minData=0;
% Loop through each image in the folder
for i = 1:length(imageFiles)
% Load the image
img_variable = imageFiles(i).name;
currentFile = fullfile(folderPath, imageFiles(i).name);
data=load(currentFile);
data= struct2cell(data);
extracted_features = double(data);
% Calculate Hamming distance between target image and current image
% Make sure both images have the same size
difference =abs(input_features - extracted_features);
distance = (sum(difference(:)));
disp(distance);
% Update closest image if distance is smaller than current minimum
if distance < minDistance
minDistance = distance;
closestImage = imageFiles(i).name;
minData=extracted_features;
k=i;
end
end
if minDistance > 5
msgbox('Match NOT Found', 'Result', 'modal');
disp(['Hamming distance: ', num2str((minDistance))]);
else
% Display closest image and its Hamming distance
msgbox('Match Found', 'Result', 'modal');
disp(['Closest image: ', imageFiles(k).name]);
disp(['Hamming distance: ', num2str((minDistance))]);
subplot(1,2,2);imshow(minData);title("Matched with:");
end
The error i am getting is:
Error using double
Conversion to double from cell is not possible.
Error in testUnknownImage (line 30)
extracted_features = double(data);

Réponse acceptée

Walter Roberson
Walter Roberson le 28 Jan 2024
Déplacé(e) : Walter Roberson le 28 Jan 2024
data=load(currentFile);
The result of load() of a .mat file is a struct with one field for each variable loaded.
data= struct2cell(data);
You convert the struct to a cell... okay, that is valid. The cell will have one entry for each variable that was loaded.
extracted_features = double(data);
You cannot double() a cell. You can double(data{1}) ...
  4 commentaires
Walter Roberson
Walter Roberson le 28 Jan 2024
Modifié(e) : Walter Roberson le 28 Jan 2024
data{1} means the content of the first cell entry in the cell array data
In context, it would mean the content of the "first" variable loaded from the .mat file.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by