calculate Euclidean distance of two image in hsv c

2020-07-23 06:07发布

i use the code below to calculate the Euclidean distance for two rgb images:

Im1 = imread(filename1);
Im1 = rgb2gray(Im1);
hn1 = imhist(Im1)./numel(Im1);
Im2 = imread(filename2);
Im2 = rgb2gray(Im2);
hn2 = imhist(Im2)./numel(Im2);
 f = norm(hn1-hn2);

and it gives me the correct answer
but now i want to use the code for two images in hsv color mode but it wont work on it
cause all of the above code is in a 2d space while hsv is 1d
is there any specific code for calculating Euclidean distance of two image in hsv color space? the images format are jpeg

1条回答
走好不送
2楼-- · 2020-07-23 06:27

You need to create a histogram for each channel seperatetly

function hst = im2hsvHist( img )
% 
% computes three channels histogram in HSV color space
%
n = 256; % number of bins per hist (per channel)
hsvImg = rgb2hsv( img );
hst = zeros(n,3);
for ci = 1:3 
    hst(:,ci) = imhist( hsvImg(:,:,ci ) , n );
end
hst = hst(:) ./ n; % to 3*n vector, normalize by n and not 3n

Using this function you can compute the image to image distance in hsv space

Im1 = imread(filename1);
hst1 = im2hsvHist(Im1);
Im2 = imread(filename2);
hst2 = im2hsvDist(Im2);
f = norm( hst1 - hst2 );

Sneak a peek for a vectorized version of im2hsvHist:

n = 256;
hsvImg = rgb2hsv( img );
hst = hist( reshape(hsvImg, [], 3), 255 ); % instead of loop!
hst = hst(:) / n;

查看更多
登录 后发表回答