Why am I getting same Euclidean Distance for different images ?

I am trying to write a code that will indicate which two images are similar out of three.
% OBJECT: Comparing sift features by euclidean distance
%%Reading Images
img1=imread('106aa.jpg');
img2=imread('107aa.jpg');
img3=imread('107bb.jpg');
%%Resizing Images
resize1=imresize(img1, 0.25) ;
resize2=imresize(img2, 0.25) ;
resize3=imresize(img3, 0.25);
%%Converting rgb images to gray scale images
gray1=rgb2gray(resize1);
gray2=rgb2gray(resize2);
gray3=rgb2gray(resize3);
%%Converting to single precision
% because sift only work on single precision
Ia=single(gray1);
Ib=single(gray2);
Ic=single(gray3);
%%Extracting sift features
[fa, da] = vl_sift(Ia) ;
[fb, db] = vl_sift(Ib) ;
[fc, dc] = vl_sift(Ic) ;
[matches1, scores1] = vl_ubcmatch(da, db) ;
[matches2, scores2] = vl_ubcmatch(db, dc) ;
%%Comparing features vectors by Euclidean distance
fv1= [ 'fa' 'da' ] ;
fv2= [ 'fb' 'db' ] ;
fv3= [ 'fc' 'dc' ] ;
diff1 = fv1 - fv2;
distance1 = sqrt(diff1 * diff1')
diff2= fv2 - fv3 ;
distance2 = sqrt(diff2 * diff2')

 Accepted Answer

You can not reference variables by strings like that. If that is what you are trying to do. Perhaps you want [fa da]

10 Comments

fv1= [ 'fa' 'da' ]
You must be talking about above line in the code. I did so to construct feature vector. Is it not the correct way to construct feature vector?
I am getting below mentioned error if I don't reference variables by string.
Error using horzcat Dimensions of matrices being concatenated are not consistent.
Error in sift_feature_comparision (line 33) fv1= [ fa da ] ;
Remember 'fa' is a string, and [] around strings means to concatenate them, so ['fa' 'da'] gives the result 'fada' (the string)
Okay, I got your point. But do you know how to construct feature vector?
By doing so, the code to calculate Euclidean Distance (mentioned below) won't work because Matrix dimensions must agree.
diff1 = fv1 - fv2; distance1 = sqrt(diff1 * diff1')
diff2= fv2 - fv3 ; distance2 = sqrt(diff2 * diff2')
Shrug. Your formula for calculating Euclidean distance is wrong.
distance1 = sqrt(sum(diff1.^2));
Okay but before that "diff= fv1 - fv2" would not work because both have different dimensions.
Is it possible in MATLAB to subtract two row vectors if they have different size?
No it is not possible to subtract vectors of different size. You should be designing your feature vectors so that they are all the same length, and within the feature vectors each sub-group of values should have the same relative offset. You can pad subgroups within feature vectors.
For example, if DCT coefficients start at index 3143 in the first feature vector and extend for 128 values, then the second feature vector should store its DCT coefficients at index 3143 for length 128 as well.
Okay thanks a lot for guiding.

Sign in to comment.

More Answers (1)

21.4.2.7 American Sign Language, ASL Recognition
21.4.2.7.1 Sign Language, Other Languages, Chinese, Arabic
21.4.2.7.2 Sign Language, Fingerspelling

Community Treasure Hunt

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

Start Hunting!