how to set threshold value for comparing images in matlab?

how do I compare one image for example Z with multiple images in a folder and find out the one which matches closely to the image Z.

Answers (2)

You develop an algorithm. That's about all I can say, given the (lack of) information you've provided. Please read this and provide more info.

4 Comments

this is the code I have but I need to make changes in this one. this works only when two images are exactly the same. But I need to modify this to make it acceptable when two images are nearly the same. like suppose if I get some images scanned, not necessarily the scanned results would be the same, so it should work there. clear all; close all; clc;
path='d:\pic'; list=dir([path, '*.jpg']); img=imread('d:\pic'); % image to compare with X1=rgb2ind(img,16); for x=1:length(list) images{x}=imread([path, list(x).name]); X2=rgb2ind(images{x},16); c=corr2(X1,X2); figure(x); subplot(1,2,1),imshow(images{x}), xlabel(['Image ',num2str(x)]); subplot(1,2,2),imshow(img); if c==1 xlabel('The images are same.'); else xlabel('The images are not same'); end end
You can use psnr(), or ssim(). If you don't have a modern version of the Image Processing Toolbox, you can see the attached psnr code. You can also computer the RMS, MSE, or Median Absolute Difference. There are lots of ways to compare images so try them all - they're simple - and pick the one you like best.
thanks a lot. Is there any way of interfacing a scanner to MATLAB?? So that the images are directly read from the scanner without human intervention??
Not that I'm aware of. You might be able to use a twain interface or java robot but I can't help you there.

Sign in to comment.

close all;
clc;
path='C:\Users\Vinayak Shukla\Documents\MATLAB\MATLAB PROJECTS\MASTER THESIS FOLDER 2023\happy15_encrpted4.bmp';
list=dir([path, '*.jpg']);
img=imread('C:\Users\Vinayak Shukla\Documents\MATLAB\MATLAB PROJECTS\MASTER THESIS FOLDER 2023\happy15_encrpted4.bmp');
%image to compare with X1=rgb2ind(img,16);
forx=1:length(list);
images{x}=imread([path, list(x).name]);
X2=rgb2ind(images{x},16);
c=corr2(X1,X2);
figure(x);
subplot(1,2,1),imshow(images{x}), xlabel(['Image ',num2str(x)]);
subplot(1,2,2),imshow(img);
if c==1
xlabel('The images are same.');
else
xlabel('The images are not same');
end
This is the error i am getting
Unrecognized function or variable 'x'.
Error in pranav17 (line 8)
images{x}=imread([path, list(x).name]);

6 Comments

@Pranav Shukla That's right. You have not defined x before you passed it in to figure because you have this
forx=1:length(list);
instead of this
for x = 1 : numel(list)
There is no "forx" command or function.
Why are you trying to pass something to figure? Just simply do this:
figure;
And there is no need to do correlation, which takes time, when you can simply do
theAreEqual = isequal(X1, X2);
images{x}=imread([path, list(x).name]);
in this line a warning is coming and it says variable appears to change size on every loop iteration(within a script. consider preallocating for speed.
how do i preallocate for speed this above line?
Before the loop, do this to aviod the warning:
images = cell(numel(list), 1);
Here. This is cleaned up.
I don't know why you were converting things to indexed color and then trying to compare the index arrays. The mapping is not continuous, so there would be no point in finding the correlation coefficients. They'd either be equal or not; coefficients less than 1 would be meaningless as a measure of image similarity. I simply omitted that.
If you need the images for later, preallocate by creating a cell array. If you don't need them outside the loop, don't bother storing them.
% read from a set of IPT test images
basepath = fullfile(matlabroot,'toolbox/images/imdata');
filenameexpr = 'AT3*.tif';
matchingfiles = dir(fullfile(basepath,filenameexpr));
% reference image
% doing rgb2ind() on images and then comparing them does not
% yield a comparison of the actual image values.
% i'm just going to grab the first image in the sequence
Xref = imread(fullfile(matchingfiles(1).folder,matchingfiles(1).name));
numimages = numel(matchingfiles);
imagestack = cell(numimages,1); % preallocate
for k = 1:numimages
% read a file to test
thispath = fullfile(matchingfiles(k).folder,matchingfiles(k).name);
Xtest = imread(thispath);
% store it if necessary
% if you don't need to keep them for later, don't bother
imagestack{k} = Xtest;
% test it somehow (corr2, immse, isequal)
c = immse(Xref,Xtest); % mean-square error
% generate a pile of figures
figure(k);
subplot(1,2,1),imshow(Xtest), xlabel(['Image ',num2str(k)]);
subplot(1,2,2),imshow(Xref);
if c==0 % if error is zero
xlabel('The images are same.');
else
xlabel('The images are not same');
end
end
basepath = fullfile(matlabroot,'toolbox/images/imdata');
filenameexpr = '(C:\Users\Vinayak Shukla\Documents\MATLAB\MATLAB PROJECTS\MASTER THESIS FOLDER 2023\happy15_encrpted4.bmp)';
matchingfiles = dir(fullfile(basepath,filenameexpr));
Xref = imread(fullfile(matchingfiles().folder,matchingfiles().name));
numimages = numel(matchingfiles);
imagestack = cell(numimages,1); % preallocate
for k = 1:numimages
thispath = fullfile(matchingfiles(k).folder,matchingfiles(k).name);
Xtest = imread(thispath);
% store it if necessary
% if you don't need to keep them for later, don't bother
imagestack{k} = Xtest;
% test it somehow (corr2, immse, isequal)
c = immse(Xref,Xtest); % mean-square error
% generate a pile of figures
figure(k);
subplot(1,2,1),imshow(Xtest), xlabel(['Image ',num2str(k)]);
subplot(1,2,2),imshow(Xref);
if c==0 % if error is zero
xlabel('The images are same.');
else
xlabel('The images are not same');
end
end
>> pranav17
Error using fullfile
Not enough input arguments.
Error in pranav17 (line 26)
Xref = imread(fullfile(matchingfiles().folder,matchingfiles().name));
could some please rewrite the whole code again in a simplified manner, and post it here.
the code should be how to compare the threshold value of two images?
please help me out.
You have to give it an index of course. Is the reference your first image? If so,
Xref = imread(fullfile(matchingfiles(1).folder,matchingfiles(1).name));

Sign in to comment.

Categories

Asked:

on 15 Feb 2015

Commented:

on 30 Mar 2023

Community Treasure Hunt

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

Start Hunting!