calculate Euclidean distance of two image in hsv color space in matlab

若如初见. 提交于 2019-12-06 06:33:44

问题


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:


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;



来源:https://stackoverflow.com/questions/17781345/calculate-euclidean-distance-of-two-image-in-hsv-color-space-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!