hello i have problem displaying the lowest value from comparation process.

i want to do comparation one by one between an iris image template on axes1 and a folder filled with iris image templates. a hammingdist.m used to count the value. the output should be a template from the folder that has lowest value amongs other templates in that folder while compared to the template on axes1 and will be displayed on axes2. but instead of displaying the template with the lowest value, it displays the last file in the folder. how to fix it? here is the code:
function buttonpencocokan_Callback(hObject, eventdata, handles)
TestImage=getappdata(handles.axes1, 'templateVVV');
gettraining=get(handles.txtlokasilatih, 'String');
datapath = (gettraining);
templatefiles = dir(fullfile(datapath, '*.bmp'));
hammingdistances = zeros(size(templatefiles));
for fileidx = 1:numel(templatefiles)
templateimage = imread(fullfile(datapath, templatefiles(fileidx).name));
hammingdistances(fileidx) = hammingdist(TestImage, templateimage);
axes(handles.axes2);imshow(templateimage);
end

 Accepted Answer

Because you never found the minimum...if you want the global minimum in the end, then
for fileidx = 1:numel(templatefiles)
templateimage = imread(fullfile(datapath, templatefiles(fileidx).name));
hammingdistances(fileidx) = hammingdist(TestImage, templateimage);
end
[minhammingdist,idx]=min(hammingdistances); % find the min, location in array
axes(handles.axes2);
imshow(imread(fullfile(datapath, templatefiles(idx).name))); % reload/display the one
If you wanted to keep a running display of which was the current closest as processed, then
templateimage = imread(fullfile(datapath, templatefiles(1).name));
minhammingdistance = hammingdist(TestImage, templateimage);
minIndex=1;
imshow(templateimage,'parent',handles.axes2);
for fileidx = 2:numel(templatefiles)
templateimage = imread(fullfile(datapath, templatefiles(fileidx).name));
hammingdistance = hammingdist(TestImage, templateimage);
if hammingdistance < minhammingdistance
minhammingdistance = minhammingdistance;
minIndex=fileidx;
imshow(templateimage,'parent',handles.axes2);
end
end

18 Comments

thank you so much sir, it works!! by the way can we also displaying the value of the lowest hamming distance in a textfield and also display the filename of the choosen template(in axes2)in other textfield too? its just for additional information or as the comparation result details. sorry for my terrible english. thanks again sir, your help means alot to me! really. God bless you!
Sure: (almost) nothing to it! :)
fmt='Min Hamming distance file: %s: %.1f';
text(handles.axes2,xpos,ypos, ...
sprintf(fmt,templatefiles(idx).name,minhammingdist));
You'll have to experiment to find where is a good position for them, this writes text to the axes.
If you have other text boxes, use their handles and just set the 'String' property to the desired field, either value or filename.
ah my bad. :D not in axes sir, i mean i want to display them in text boxes. thank you sir. :D
Then, just use the sprintf command to build the text you wish to write following the above as a template to modify (and "doc fprintf" as the bible ;) ) and set the target controls' handles' 'String' property--at least I think that's the property name isn't it; "I don't do windows!" so I really don't know much at all about the controls property names and you can look them up as easy as I! VBG
hahahah yes, i did it sir. thank you again. :))
No problem, glad to help (up to a point; I figure everybody's got to learn at some point! :) ). I do try to put tutorial stuff in; I've been told I'm "preachy"...I tend to take it as a compliment. :)
hahahah, i and maybe also other beginners need the preachy stuff more. so its not bad at all. well im bad at making compliment. but im so grateful that u answered my question. whatever, you are my hero. have a nice day, sir! :D
Hi, how to display the 10 lowest values of it?
ok=sort(hammingdistances)? how to make it just lowest 10? ok=sort(hammingdistances(:10))?
how about the filename? how to get it along with the sorted value?
ok my sorting code doesn't work. lol any other hint?
Think!!! :)
Where are the lowest N values in the result of the sort????
For the other question, look at alternate return from sort -- what could you do if you knew which of the original locations was which in the smallest N?
Read
doc sort % for details of inputs/return values
Try
>> x=randperm(10) % some random data
x =
6 3 7 8 5 1 2 4 9 10
>> [xs,idx]=sort(x); % sort, keep indexing vector
>> N=3; % set some magic N
>> [xs(1:N);idx(1:N)].' % what do we have???
ans =
1 6
2 7
3 2
>> str=['a':'z'].'; % also have some other data (a str arrray)
What if use idx with it, mayhaps????
[minhammingdistances,idx]=sort(hammingdistances);
N=10;
[hamingdistances(1:N); idx(1:N)]
Like that then?
Did you try??? Do you not see what that is doing from the previous example???

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 18 Jun 2017

Commented:

on 21 Oct 2017

Community Treasure Hunt

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

Start Hunting!