计算matlab中hsv颜色空间中两个图像的欧氏距离 [英] calculate Euclidean distance of two image in hsv color space in matlab
问题描述
我使用下面的代码来计算两个rgb图像的欧几里德距离:
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);
它给了我正确的答案
但现在我想使用在hsv颜色模式下的两个图像的代码,但它不会在它上面工作
导致所有上述代码都在2d空间而hsv是1d
是否有任何特定的计算代码hsv颜色空间中两个图像的欧几里德距离?
图像格式为jpeg
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
推荐答案
您需要单独为每个频道创建直方图
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
使用此函数可以计算图像以hsv空间成像距离
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 );
偷看 im2hsvHist
:
n = 256;
hsvImg = rgb2hsv(img);
hst = hist(reshape(hsvImg,[],3),255); %而不是循环!
hst = hst(:) / n;
n = 256;
hsvImg = rgb2hsv( img );
hst = hist( reshape(hsvImg, [], 3), 255 ); % instead of loop!
hst = hst(:) / n;
这篇关于计算matlab中hsv颜色空间中两个图像的欧氏距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!