如图,脸部与白背景的hsv值几乎一样,无法区分
如下图,白图与baby图的hsv量化直方图余弦相似度为0.95,你信么?
而如下图,一个是纯白,一个是中间有红块,但余弦相似度为0.99
clear all; clc; close all;
%% Read image from file
p1='e:/img/wx.jpg';
p2='e:/img/rc.jpg';
%采用hsv颜色直方图+指数调亮,大幅度提高相似匹配准确率
%下一步考虑把梯度直方图加入进来、平均颜色、最多颜色、前3种颜色?
%以及显著度
inImg = imread(p1);
[m,n,c] = size(inImg);
ratio = 300/(m+n);
img = imresize(inImg, ratio);
[m,n,c] = size(img);
%img = im2double(img);
%img = img.^0.5;
im1=img;
hsv = rgb2hsv(img);
%hsv(:,:,2) = 0;
%hsv(:,:,3) = 0;
%imwrite(hsv, 'd:/hsv.jpg');
%figure; imshow(hsv); title('hsv');
%figure; imshow(img); title('img');
h1 = hsvquan(hsv);
inImg = imread(p2);
[m,n,c] = size(inImg);
ratio = 300/(m+n);
img = imresize(inImg, ratio);
img = im2double(img);
%img = img.^0.5;
im2=img;
[m,n,c] = size(img);
hsv = rgb2hsv(img);
%figure; imshow(img); title('img');
h2 = hsvquan(hsv);
figure;
subplot(221); imshow(im1);
%h1 = smooth(h1);
%h1 = filter(ones(1,2)/2, 1, h1);
subplot(222); bar(h1); title('hsv量化直方图1');
%h2 = filter(ones(1,2)/2, 1, h2);
subplot(223); imshow(im2);
subplot(224); bar(h2); title('hsv量化直方图2');
%figure; bar(h2); title('hsv量化直方图2');
s1 = sqrt(sum(h1.^2));
s2 = sqrt(sum(h2.^2));
s0 = sum(h1.*h2);
%相似度
sim = s0 / s1 / s2
function L=hsvquan(hsv)
%对HSV进行量化,把3个颜色分量合成为一维特征矢量:
h=hsv(:,:,1);
s=hsv(:,:,2);
v=hsv(:,:,3);
% 如果对HSV 空间进行适当的量化后再计算直方图, 则计算量要少得多. 我们将H , S ,V 3个分量按照人的颜色感知进行非等间隔的量化, 从对颜色模型的大量分析, 我们把
% 色调H 空间分成8份, 饱和度S 和亮度V 空间分别分成3份, 并根据色彩的不同范围进行量化, 量化后的色
% 调、饱和度和亮度值分别为H , S ,V.
h=h*360;
H=zeros(size(hsv,1),size(hsv,2));
H(h>=316|h<=20)=0;
H(h>=21&h<=40)=1;
H(h>=41&h<=75)=2;
H(h>=76&h<=155)=3;
H(h>=156&h<=190)=4;
H(h>=191&h<=270)=5;
H(h>=271&h<=295)=6;
H(h>=296&h<=351)=7;
S=zeros(size(hsv,1),size(hsv,2));
S(s>=0&s<=0.2)=0;
S(s>0.2&s<=0.7)=1;
S(s>0.7&s<=1)=2;
V=zeros(size(v));
V(v>=0&v<=0.2)=0;
V(v>0.2&v<=0.7)=1;
V(v>0.7&v<=1)=2;
l=9*H+3*S+V;
% L=mat2gray(l);
% histL=imhist(L,72);
for i=0:71
L(i+1)=numel(l(l==i));
end