How can I load and plot the difference of a set of images to a reference image?

Illustration
Lea Kerciku - 2023-02-23T10:48:56+00:00
Question: How can I load and plot the difference of a set of images to a reference image?

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

Profile picture of Kshitij Singh Kshitij Singh answered . 2025-11-20

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);

 


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!