在matlab中对hsv进行均匀量化和非均匀量化

首先,进行非均匀量化,H,S,V三通道分别量化为16,4,4级,返回一个向量。量化依据如下表:

function vec = getHsvHist(Image)
[M,N,O] = size(Image);
if O~= 3
    error('3 components are needed for histogram');
end
[h,s,v] = rgb2hsv(Image);
H = h; S = s; V = v;
h = h*360;

%将hsv空间非等间隔量化:
%  h量化成16级;
%  s量化成4级;
%  v量化成4级;
for i = 1:M
    for j = 1:N
        if h(i,j)<=15||h(i,j)>345
            H(i,j) = 0;
        end
        if h(i,j)<=25&&h(i,j)>15
            H(i,j) = 1;
        end
        if h(i,j)<=45&&h(i,j)>25
            H(i,j) = 2;
        end
        if h(i,j)<=55&&h(i,j)>45
            H(i,j) = 3;
        end
        if h(i,j)<=80&&h(i,j)>55
            H(i,j) = 4;
        end
        if h(i,j)<=108&&h(i,j)>80
            H(i,j) = 5;
        end
        if h(i,j)<=140&&h(i,j)>108
            H(i,j) = 6;
        end
        if h(i,j)<=165&&h(i,j)>140
            H(i,j) = 7;
        end
        if h(i,j)<=190&&h(i,j)>165
            H(i,j) = 8;
        end
        if h(i,j)<=220&&h(i,j)>190
            H(i,j) = 9;
        end
        if h(i,j)<=255&&h(i,j)>220
            H(i,j) = 10;
        end
        if h(i,j)<=275&&h(i,j)>255
            H(i,j) = 11;
        end
        if h(i,j)<=290&&h(i,j)>275
            H(i,j) = 12;
        end
        if h(i,j)<=316&&h(i,j)>290
            H(i,j) = 13;
        end
        if h(i,j)<=330&&h(i,j)>316
            H(i,j) = 14;
        end
        if h(i,j)<=345&&h(i,j)>330
            H(i,j) = 15;
        end
    end
end
for i = 1:M
    for j = 1:N
        if s(i,j)<=0.15&&s(i,j)>0
            S(i,j) = 0;
        end
        if s(i,j)<=0.4&&s(i,j)>0.15
            S(i,j) = 1;
        end
        if s(i,j)<=0.75&&s(i,j)>0.4
            S(i,j) = 2;
        end
        if s(i,j)<=1&&s(i,j)>0.75
            S(i,j) = 3;
        end
    end
end
for i = 1:M
    for j = 1:N
        if v(i,j)<=0.15&&v(i,j)>0
            V(i,j) = 0;
        end
        if v(i,j)<=0.4&&v(i,j)>0.15
            V(i,j) = 1;
        end
        if v(i,j)<=0.75&&v(i,j)>0.4
            V(i,j) = 2;
        end
        if v(i,j)<=1&&v(i,j)>0.75
            V(i,j) = 3;
        end
    end
end

%将三个颜色分量合成为一维特征向量:L = H*Qs*Qv+S*Qv+v;Qs,Qv分别是S和V的量化级数, L取值范围[0,255]
%取Qs = 4; Qv = 4
L=zeros(M,N);
for  i = 1:M
    for j = 1:N
        L(i,j) = H(i,j)*16+S(i,j)*4+V(i,j);
    end
end
%计算L的直方图
Hist=zeros(1,256);
for i = 0:255
    Hist(i+1) = size(find(L==i),1);
end
vec=Hist';

接着,进行均匀量化,H,S,V三通道分别量化为16,4,4级,返回一个向量。

function  vec= hsvHist(Image)
[M,N,O] = size(Image);
if O~= 3
    error('3 components are needed for histogram');
end
H_BITS = 4; S_BITS =2; V_BITS = 2;
hsv = uint8(255*rgb2hsv(Image));
%均匀量化
% bitshift(24,-3) 表示24除以2的3次方
H=bitshift(hsv(:,:,1),-(8-H_BITS));
S=bitshift(hsv(:,:,2),-(8-S_BITS));
V=bitshift(hsv(:,:,3),-(8-V_BITS));

%%
%先进行合成,然后再统计
L=zeros(M,N);
for i=1:M
    for j=1:N
        L(i,j)=16*H(i,j)+4*S(i,j)+V(i,j);
    end
end
%计算L的直方图
Hist=zeros(1,256);
for i = 0:255
    Hist(i+1) = size(find(L==i),1);
end
vec=Hist';
end

利用matlab实现对于图像的hsv颜色特征提取

以lena图像进行比较:

clc;clear;close all;
rgb=imread('d:/pic/lena.jpg');
h1=getHsvHist(rgb);
h2=hsvHist(rgb);
figure,
subplot(211),bar(h1),title('hsv非均匀量化直方图');
subplot(212),bar(h2),title('hsv均匀量化直方图');