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 commentaires

Walter Roberson
Walter Roberson le 30 Mar 2013
What does "compare the images" mean to you in this context?
Anand
Anand le 30 Mar 2013
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.
Padmapriya
Padmapriya le 30 Mar 2013
@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
Padmapriya
Padmapriya le 30 Mar 2013
@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.
Padmapriya
Padmapriya le 30 Mar 2013
Is there any proper way to compare the images?
Walter Roberson
Walter Roberson le 30 Mar 2013
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.
sirisha boddapati
sirisha boddapati le 15 Juil 2018
The prgrm shows error in line 4 ...what should we do??? Plz ans this...
Image Analyst
Image Analyst le 15 Juil 2018
You should start a new question of your own with all the source code, data, and error messages needed for us to help you.

Connectez-vous pour commenter.

 Réponse acceptée

Image Analyst
Image Analyst le 31 Mar 2013
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 commentaires

Or easier in this situation,
isequal(image1, image2)
Padmapriya
Padmapriya le 1 Avr 2013
@Image Analyst Your answer is very useful but, how do I include it in the loop?
Image Analyst
Image Analyst le 1 Avr 2013
Modifié(e) : Image Analyst le 1 Avr 2013
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.
Padmapriya
Padmapriya le 1 Avr 2013
Sorry, by a match I meant that all the pixels in both the pictures must be of the same gray level.
Image Analyst
Image Analyst le 1 Avr 2013
Then using the loop code in the FAQ, and isequal() should work for you.
Walter Roberson
Walter Roberson le 1 Avr 2013
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 ?
Image Analyst
Image Analyst le 1 Avr 2013
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.)
Padmapriya
Padmapriya le 1 Avr 2013
The comparison is always between two RGB images. Thanks
Walter Roberson
Walter Roberson le 1 Avr 2013
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?
Padmapriya
Padmapriya le 8 Avr 2013
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;
Image Analyst
Image Analyst le 8 Avr 2013
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.
Padmapriya
Padmapriya le 11 Avr 2013
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??
Image Analyst
Image Analyst le 11 Avr 2013
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().
Padmapriya
Padmapriya le 11 Avr 2013
Modifié(e) : Padmapriya le 11 Avr 2013
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??
Padmapriya
Padmapriya le 13 Avr 2013
Modifié(e) : Padmapriya le 13 Avr 2013
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
Image Analyst
Image Analyst le 13 Avr 2013
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});
Image Analyst
Image Analyst le 13 Avr 2013
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

Connectez-vous pour commenter.

Plus de réponses (2)

gagan deep
gagan deep le 15 Juin 2014

0 votes

this code will not run can u send me full code !. gdeep90singh@gmail.com thanks
Umara Zafar
Umara Zafar le 9 Juin 2017
Modifié(e) : Image Analyst le 9 Juin 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 commentaire

Image Analyst
Image Analyst le 9 Juin 2017
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Images dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by