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.
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

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.
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?
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 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.
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 Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

질문:

2013년 3월 30일

댓글:

2018년 7월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by