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 件のコメント

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

サインインしてコメントする。

 採用された回答

Image Analyst
Image Analyst 2013 年 3 月 31 日

1 投票

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 件のコメント

Walter Roberson
Walter Roberson 2013 年 3 月 31 日
Or easier in this situation,
isequal(image1, image2)
Padmapriya
Padmapriya 2013 年 4 月 1 日
@Image Analyst Your answer is very useful but, how do I include it in the loop?
Image Analyst
Image Analyst 2013 年 4 月 1 日
編集済み: Image Analyst 2013 年 4 月 1 日
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 2013 年 4 月 1 日
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 2013 年 4 月 1 日
Then using the loop code in the FAQ, and isequal() should work for you.
Walter Roberson
Walter Roberson 2013 年 4 月 1 日
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 2013 年 4 月 1 日
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 2013 年 4 月 1 日
The comparison is always between two RGB images. Thanks
Walter Roberson
Walter Roberson 2013 年 4 月 1 日
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 2013 年 4 月 8 日
yes, even though the combination is different it should conclude it as a match.But, using isequal() does not provide the exact match.
Padmapriya
Padmapriya 2013 年 4 月 8 日
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 2013 年 4 月 8 日
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 2013 年 4 月 11 日
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 2013 年 4 月 11 日
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 2013 年 4 月 11 日
編集済み: Padmapriya 2013 年 4 月 11 日
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?
Image Analyst
Image Analyst 2013 年 4 月 11 日
do
whos image1
whos image2
Tell me what you see.
Padmapriya
Padmapriya 2013 年 4 月 13 日
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 2013 年 4 月 13 日
編集済み: Padmapriya 2013 年 4 月 13 日
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 2013 年 4 月 13 日
Do the same thing (whos) for image12 and image13 that you did for image1 and image2.
Padmapriya
Padmapriya 2013 年 4 月 13 日
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 2013 年 4 月 13 日
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

サインインしてコメントする。

その他の回答 (2 件)

gagan deep
gagan deep 2014 年 6 月 15 日

0 投票

this code will not run can u send me full code !. gdeep90singh@gmail.com thanks
Umara Zafar
Umara Zafar 2017 年 6 月 9 日
編集済み: Image Analyst 2017 年 6 月 9 日

0 投票

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 件のコメント

Image Analyst
Image Analyst 2017 年 6 月 9 日
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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by