comparing single image with many images

I have written a matlab function that enables me to get the name of image from user and compare it with the existing images and display if it matches or not..
function matchin
handles = guidata(gcbo);
set(handles.h_text,'String','performing matching...');
[image1, pathname]= uigetfile('*.bmp','Open An Fingerprint image');
Directory = fullfile ('F:','matlab','bin');
D = dir(fullfile(Directory,'*.bmp'));
set(handles.h_text,'String','matching complete....');
for i = 1:numel(D)
if strcmp(image1,D(i).name)
disp('matched');
else
disp('not matched');
end
end
the above code checks if the file name exists but i now want to compare the images itself instead of the file name. How can I do that?Please help..
Regards
Priya

9 Comments

What does "compare the images" mean to you in this context?
If you're looking for complete equality (of all pixels with no tolerance), use isequal(im1,im2)
Note that such comparisons are usually not useful in any meaningful image comparison.
@Walter it was just a try to see if I can check whether the file exists. Now i want to check if the image itself as a whole, exists or not
@Anand thanks a lot!! It really works!! Thanks for the timely help.
But there is some problem with the loop. If I select the second image it should display the output as
not matched
matched
not matched
But I am getting it as
not matched
not matched
matched
I don't know where I have gone wrong.
Is there any proper way to compare the images?
You can use http://www.mathworks.com/help/matlab/ref/imfinfo.html to test to see if the file contains an image, and check its sizes and some other information. For example if the contained image is a different size than your original then you can say that it is not the same image.
The prgrm shows error in line 4 ...what should we do??? Plz ans this...
You should start a new question of your own with all the source code, data, and error messages needed for us to help you.

Sign in to comment.

 Accepted Answer

How do you define a match? If all pixels are the same, and only one single pixel is different by only 1 gray level? Is that a match or not a match? If you say that's not a match, then first just compare the number of rows, columns, and color channels. Then if all those match, simply subtract the image (after converting to floating point) and look for any non-zero values with nnz();
diffImage = single(image1) - single(image2);
imagesMatch = nnz(diffImage(:)) == 0;

21 Comments

Or easier in this situation,
isequal(image1, image2)
@Image Analyst Your answer is very useful but, how do I include it in the loop?
Adapt the code in the FAQ for processing a bunch of files: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F. But you didn't answer my questions, and didn't define how you define a match. Look at my answer again, and try to respond to every sentence that ends in a question mark.
Sorry, by a match I meant that all the pixels in both the pictures must be of the same gray level.
Then using the loop code in the FAQ, and isequal() should work for you.
All the pixels must be of the same gray level (as in the other image, presumably). But what if the images are different RGB that happen to work out to the same gray level using the standard rgb2gray() transformation ?
I imagine she wants to compare pixel values on a channel by channel basis. If she leaves them as RGB then isequal should still work, no matter whether both are grayscale or RGB. Only problem comes in if she's comparing RGB to grayscale and in that case comparing the ndims(theImage) should be done first to detect a difference in the number of color channels. (I know Walter knows all this already.)
The comparison is always between two RGB images. Thanks
So to check, if in image1, a particular pixel has an RGB combination that gives a grayscale of (say) 158, and if in image2, the corresponding pixel has a different RGB combination that also give a grayscale of 158, then the comparison should consider them to be the same?
yes, even though the combination is different it should conclude it as a match.But, using isequal() does not provide the exact match.
diffImage = single(image1) - single(image2);
imagesMatch = nnz(diffImage(:)) == 0;
In the above code what does the following mean?
imagesMatch = nnz(diffImage(:)) == 0;
nnz() is the number of non-zeros. If they match, you want ALL zeros - in other words, you don't want any "non-zeros". So I check if the number of "non-zeros" is zero. If it is zero, there are no "non-zeros" and all pixels are zero, which means a perfect match, so that's why I compare nnz() to 0 with the statement nnz(diffImage(:)) == 0.
I am trying to incorporate your idea of matching but I am facing a problem that is, in the code "D{i}.name" reads the name of the file and then compares it with the "image1" may be that is the reason isequal() did not work..How can I get the image from a set of images and then compare. Hope I am not confusing you..Does D{i}.name read the image name alone or the image itself as a whole??
There is no imread() in your code. Your code compares filenames - strings - not image arrays. If you want to compare the images inside the files, then you need to call imread().
I included imread() and tried the snippet you suggested but, I am getting the following error
Array dimensions must match for binary array op.
Error in org (line 164)
diffImage = single(image1)- single(image2);
Error while evaluating uicontrol Callback
But both the images are of same dimensions. Where have I gone wrong?
do
whos image1
whos image2
Tell me what you see.
whos image1 is
Name Size Bytes Class Attributes
image1 227x227x3 1236696 double
whos image2 is
Name Size Bytes Class Attributes
image2 227x227x3 154587 uint8
How do I convert uint8 to double or viceversa??
I converted image1 to uint8 but still the matching is not performed. I don't know where I have made the mistake and I am still getting the following error
Array dimensions must match for binary array op.
Error in org (line 213)
diffImage=single(image12)-single(image13);
Error while evaluating uicontrol Callback
Do the same thing (whos) for image12 and image13 that you did for image1 and image2.
I used
D = dir(fullfile(Directory,'*.jpg'));
imcell = {D.name}';
to get the file names present in the folder and then used imread() to read the image. There are three images in the folder
'min.jpg'
'min1.jpg'
'min2.jpg'
But I am getting the error that the last image does not exist. Why is that?
not matched
not matched
Error using imread (line 369)
File "min2.jpg" does not exist.
Error in org (line 215)
image13=imread(imcell{i});
Well apparently it doesn't exist. Follow the second code example in the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F

Sign in to comment.

More Answers (2)

gagan deep
gagan deep on 15 Jun 2014
this code will not run can u send me full code !. gdeep90singh@gmail.com thanks
Umara Zafar
Umara Zafar on 9 Jun 2017
Edited: Image Analyst on 9 Jun 2017
I used the same code for my project
function matchin
handles = guidata(gcbo);
set(handles.h_text,'String','performing matching...');
[image1, pathname]= uigetfile('*.bmp','Open An Fingerprint image');
Directory = fullfile ('F:','matlab','bin');
D = dir(fullfile(Directory,'*.bmp'));
set(handles.h_text,'String','matching complete....');
for i = 1:numel(D)
if strcmp(image1,D(i).name)
disp('matched');
else
disp('not matched');
end
end
But i'm getting this error "H must be the handle to a figure or figure descendent." Anyone can help ? Thanks

1 Comment

There is no capital H in your code. Post a new, separate question and post all the red text so we can see the actual line of code that is throwing the error.

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!