Matlab自带的图片所在路径为C:\Program Files\MATLAB\R2018b\toolbox\images\imdata,可以用其他图片。
代码:
%rgb2hsv
clear
clc
f=imread(‘onion.png’);
r=f(:,:,1);
g=f(:,:,2);
b=f(:,:,3);
[M,N]=size®;
r1=r/255;
g1=g/255;
b1=b/255;
uint8 cMax=zeros(M,N);
uint8 cMin=zeros(M,N);
for i=1:M
for j=1:N
cMax(i,j)=max([r1(i,j),g1(i,j),b1(i,j)]);
cMin(i,j)=min([r1(i,j),g1(i,j),b1(i,j)]);
end
end
delt=cMax-cMin;
v=cMax;
uint8 h=zeros(M,N);
uint8 s=zeros(M,N);
for i=1:M
for j=1:N
if cMax(i,j)==0
s(i,j)=0;
else
s(i,j)=delt(i,j)/cMax(i,j);
end
if delt(i,j)==0
h(i,j)=0;
elseif cMax(i,j)==r1(i,j)
h(i,j)=60*((g1(i,j)-b1(i,j))/delt(i,j));
elseif cMax(i,j)==g1(i,j)
h(i,j)=60*((b1(i,j)-r1(i,j))/delt(i,j)+2);
elseif cMax(i,j)==b1(i,j)
h(i,j)=60*((r1(i,j)-g1(i,j))/delt(i,j)+4);
end
end
end
subplot(2,2,1),imshow(f);
subplot(2,2,2),imshow(h);
subplot(2,2,3),imshow(s);
subplot(2,2,4),imshow(v);