Question
Hello, my task is to create a graph showing how a set a of images change in relation to the reference image. For defining the difference I am using the Structural Similarity (SSIM) index. Now my problem is that my loop does not go through all image in the folder directory but only takes the first one. In order to make the graph I need to assign to each image the value of the SSIM index (ssimval) and plot it. It would be very helpful if someone could show me what I am doing wrong in my code. Thank you! Here is my code so far: reference_img = imread('rock01b_noback.png'); %read reference img folder = 'C:\Users\...'; morphimages = dir(fullfile(folder,'*.png')); %specify pattern of files in folder for i = 1:length(morphimages) imagename = morphimages(i).name; fullimagename = fullfile(folder,imagename); fprintf(1, 'Reading %s.\n', fullimagename); imagearray = imread(fullimagename); [ssimval,ssimmap] = ssim(imagearray,reference_img) %find ssim index for each img in folder end
Expert Answer
Kshitij Singh
PhD Expert
Answered Aug 13, 2026
Try this:
% Demo by Image Analyst.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
folder = 'C:\Users\lea\documents\Matlab\work\Images'; % Change to wherever your images are.
baseFileName = 'rock01b_noback.webp';
fullFileName = fullfile(folder, baseFileName);
reference_img = imread(fullFileName); % read reference img
subplot(2, 2, 1);
imshow(reference_img);
title(baseFileName);
morphimages = dir(fullfile(folder,'*.PNG')); %specify pattern of files in folder
numImages = length(morphimages)
ssimval = zeros(1, numImages);
for k = 1 : length(morphimages)
baseImageName = morphimages(k).name;
fullImageName = fullfile(folder,baseImageName);
fprintf(1, 'Reading %d of %d : %s.\n', k, numImages, fullImageName);
subplot(2, 2, 2);
imageArray = imread(fullImageName);
if ~isequal(size(imageArray), size(imageArray))
fprintf('Skipping %d because it is not the same size as the reference image\n', baseImageName);
continue;
end
imshow(imageArray, []);
title(baseImageName);
ssimval(k) = ssim(imageArray, reference_img) %find ssim index for each img in folder
subplot(2, 2, 3:4);
plot(ssimval(1:k), 'b.-', 'LineWidth', 2, 'MarkerSize', 18);
drawnow;
grid on;
end
fprintf('Done running %s.m\n', mfilename);
100% Run Guarantee
3-Hour Fast-Track Delivery
Need a Custom Version or Complete Simulation for This Problem?
Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.
Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here
Related imageprocessing Questions & Solutions
Browse All →
Explore similar technical troubleshooting questions and verified MATLAB solutions:
Ready-to-Run MATLAB & Simulink Projects
Browse All Projects →