I am new to Matlab. I have this function which I want to calculate the Euclidean distance between two pixels(RGB).
function[distance]=calc_dist(R1, R2, G1, G2, B1, B2)
if (R1>R2)
dR=R1-R2;
else
dR=R2-R1;
end
if (G1>G2)
dG=G1-G2;
else
dG=G2-G1;
end
if (B1>B2)
dB=B1-B2;
else
dB=B2-B1;
end
sum=uint64(3*dR*dR+4*dG*dG+2*dB*dB);
disp(sum);
distance=(sqrt(double(3*dR*dR+4*dG*dG+2*dB*dB));
end
The problem is the displayed value for sum is 255 each time. This must be happening because the variables are of type uint8. How do I change them? I tried to do some sort of casting
sum=uint64(3*dR*dR+4*dG*dG+2*dB*dB);
but I get this error: 'Undefined function 'uit64' for input arguments of type 'uint8'. How should I display the right value for the sum? Thanks. '
dist()which should be able to calculate distances using several metrics - Benuint64; you were just missing then. Be that as it may, the casting is occurring too late - after you have done the multiplication and not before. You may want to try something likeR1=double(R1)etc. for each input variable (tedious) and then set the Euclidean distance asdist = sqrt((R1-R2)^2 + (B1-B2)^2 + (G1-G2)^2). (Not sure where your 2,3 and 4 come in.) - Geoff