필터 지우기
필터 지우기

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 (최근 30일)
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);

채택된 답변

Walter Roberson
Walter Roberson 2024년 1월 28일
이동: Walter Roberson 2024년 1월 28일
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
Walter Roberson
Walter Roberson 2024년 1월 28일
편집: Walter Roberson 2024년 1월 28일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by