在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均匀量化直方图');
相关推荐
zizisuo11 分钟前
面试篇:Spring Security
网络·数据库·安全
玉笥寻珍12 分钟前
Web安全渗透测试基础知识之HTTP参数污染篇
网络·网络协议·安全·web安全·http
GCKJ_08241 小时前
观成科技:加密C2框架Vshell流量分析
网络·科技·信息与通信
大蚂蚁2号2 小时前
windows文件共享另一台电脑资源管理器网络文件夹无法找到机器
运维·服务器·网络
LetsonH3 小时前
Home Assistant 米家集成:开启智能家居新体验
网络·智能家居
ghie90903 小时前
x-IMU matlab zupt惯性室内定位算法
人工智能·算法·matlab
欧先生^_^3 小时前
Docker 的各种网络模式
网络·docker·容器
彬彬醤3 小时前
查询电脑伪装IP,网络安全速查攻略!
网络·网络协议·tcp/ip·安全·web安全·http·https
兴达易控5 小时前
Profibus DP主站转Modbus TCP网关接E+H流量计通讯案例
网络
瑞雪兆丰年兮5 小时前
数学实验(Matlab符号运算)
开发语言·算法·matlab·数学实验