哪位大神可以解释一下,不懂啊,尤其是后面,谢谢了,很急!!! 20
F=imread('F:\166.jpg');F=im2double(F);r=F(:,:,1);g=F(:,:,2);b=F(:,:,3);th=acos((0.5*(...
F=imread('F:\166.jpg');
F=im2double(F);
r=F(:,:,1);
g=F(:,:,2);
b=F(:,:,3);
th=acos((0.5*((r-g)+(r-b)))./((sqrt((r-g).^2+(r-b).*(g-b)))+eps));
H=th;
H(b>g)=2*pi-H(b>g);
H=H/(2*pi);
S=1-3.*(min(min(r,g),b))./(r+g+b+eps);
I=(r+g+b)/3;
hsi=cat(3,H,S,I);
IE=histeq(I);
RV=cat(3,H,S,IE);
HV=RV(:,:,1)*2*pi;
SV=RV(:,:,2);
IV=RV(:,:,3);
R=zeros(size(HV));
G=zeros(size(HV));
B=zeros(size(HV));
%RG Sector;判断H所在范围
id=find((0<=HV)& (HV<2*pi/3));%find返回所需元素的所在位置
%位置的判定:在矩阵中,第一列开始,自上而下,依次为1,2,3...,然后再从第二列,第三列依次往%后数
B(id)=IV(id).*(1-SV(id));
R(id)=IV(id).*(1+SV(id).*cos(HV(id))./cos(pi/3-HV(id)));
G(id)=3*IV(id)-(R(id)+B(id));
%BG Sector
id=find((2*pi/3<=HV)& (HV<4*pi/3));
R(id)=IV(id).*(1-SV(id));
G(id)=IV(id).*(1+SV(id).*cos(HV(id)-2*pi/3)./cos(pi-HV(id)));
B(id)=3*IV(id)-(R(id)+G(id));
%BR Sector
id=find((4*pi/3<=HV)& (HV<2*pi));
G(id)=IV(id).*(1-SV(id));
B(id)=IV(id).*(1+SV(id).*cos(HV(id)-4*pi/3)./cos(5*pi/3-HV(id)));
R(id)=3*IV(id)-(G(id)+B(id));
Im3=cat(3,R,G,B);
Im3=max(min(Im3,1),0);
figure;
subplot(2,2,1);
imshow(F);
subplot(2,2,2);
imshow(Im3);
Im_3=rgb2gray(Im3);
F1=rgb2gray(F);
subplot(2,2,3);
imhist(F1,64);
subplot(2,2,4);
imhist(Im_3,64); 展开
F=im2double(F);
r=F(:,:,1);
g=F(:,:,2);
b=F(:,:,3);
th=acos((0.5*((r-g)+(r-b)))./((sqrt((r-g).^2+(r-b).*(g-b)))+eps));
H=th;
H(b>g)=2*pi-H(b>g);
H=H/(2*pi);
S=1-3.*(min(min(r,g),b))./(r+g+b+eps);
I=(r+g+b)/3;
hsi=cat(3,H,S,I);
IE=histeq(I);
RV=cat(3,H,S,IE);
HV=RV(:,:,1)*2*pi;
SV=RV(:,:,2);
IV=RV(:,:,3);
R=zeros(size(HV));
G=zeros(size(HV));
B=zeros(size(HV));
%RG Sector;判断H所在范围
id=find((0<=HV)& (HV<2*pi/3));%find返回所需元素的所在位置
%位置的判定:在矩阵中,第一列开始,自上而下,依次为1,2,3...,然后再从第二列,第三列依次往%后数
B(id)=IV(id).*(1-SV(id));
R(id)=IV(id).*(1+SV(id).*cos(HV(id))./cos(pi/3-HV(id)));
G(id)=3*IV(id)-(R(id)+B(id));
%BG Sector
id=find((2*pi/3<=HV)& (HV<4*pi/3));
R(id)=IV(id).*(1-SV(id));
G(id)=IV(id).*(1+SV(id).*cos(HV(id)-2*pi/3)./cos(pi-HV(id)));
B(id)=3*IV(id)-(R(id)+G(id));
%BR Sector
id=find((4*pi/3<=HV)& (HV<2*pi));
G(id)=IV(id).*(1-SV(id));
B(id)=IV(id).*(1+SV(id).*cos(HV(id)-4*pi/3)./cos(5*pi/3-HV(id)));
R(id)=3*IV(id)-(G(id)+B(id));
Im3=cat(3,R,G,B);
Im3=max(min(Im3,1),0);
figure;
subplot(2,2,1);
imshow(F);
subplot(2,2,2);
imshow(Im3);
Im_3=rgb2gray(Im3);
F1=rgb2gray(F);
subplot(2,2,3);
imhist(F1,64);
subplot(2,2,4);
imhist(Im_3,64); 展开
2个回答
展开全部
%将RGB图像转换为HSI图像
% 显示HSI图像
%直方图均衡化HSI图像
%将HSI图像转换回RGB图像
function rgbtohsi(x)
F=imread(x);
F=im2double(F);
r=F(:,:,1);
g=F(:,:,2);
b=F(:,:,3);
th=acos((0.5*((r-g)+(r-b)))./((sqrt((r-g).^2+(r-b).*(g-b)))+eps));
H=th;
H(b>g)=2*pi-H(b>g);
H=H/(2*pi);
S=1-3.*(min(min(r,g),b))./(r+g+b+eps);
I=(r+g+b)/3;
hsi=cat(3,H,S,I);
HE=H*2*pi;
HE=histeq(HE);
HE=HE/(2*pi);
SE=histeq(S);
IE=histeq(I);
choice=input('1:RGB 转换为 HSI\n2:显示HSI图像\n3:HSI转换为RGB图像\n4:色调均衡\n5:饱和度均衡\n6:亮度均衡 \n7:HSI均衡\n 输入您的选择 :');
switch choice
case 1
figure,imshow(F),title('RGB 图像');
figure, imshow(hsi),title('HSI 图像');
case 2
figure,imshow(F),title('RGB 图像');
figure, imshow(H),title('Hue 图像');
figure, imshow(S),title('饱和度图像');
figure, imshow(I),title('亮度图像');
case 3
C=hsitorgb(hsi);
figure,imshow(hsi),title('HSI 图像');
figure, imshow(C),title('RGB 图像');
case 4
RV=cat(3,HE,S,I);
C=hsitorgb(RV);
figure,imshow(hsi),title('HSI 图像');
figure,imshow(F),title('RGB 图像');
figure, imshow(C),title('RGB Image-Hue Equalized');
case 5
RV=cat(3,H,SE,I);
C=hsitorgb(RV);
figure,imshow(hsi),title('HSI 图像');
figure,imshow(F),title('RGB 图像');
figure, imshow(C),title('RGB 饱和度均衡');
case 6
RV=cat(3,H,S,IE);
C=hsitorgb(RV);
figure,imshow(hsi),title('HSI 图像');
figure,imshow(F),title('RGB 图像');
figure, imshow(C),title('RGB 亮度均衡');
case 7
RV=cat(3,HE,SE,IE);
C=hsitorgb(RV);
figure,imshow(hsi),title('HSI 图像');
figure,imshow(F),title('RGB 图像');
figure, imshow(C),title('RGB HSI 均衡');
otherwise
display('选择错误');
end
end
%函数,将HSI图像转换为RGB图像
function C=hsitorgb(hsi)
HV=hsi(:,:,1)*2*pi;
SV=hsi(:,:,2);
IV=hsi(:,:,3);
R=zeros(size(HV));
G=zeros(size(HV));
B=zeros(size(HV));
%RG Sector
id=find((0<=HV)& (HV<2*pi/3));
B(id)=IV(id).*(1-SV(id));
R(id)=IV(id).*(1+SV(id).*cos(HV(id))./cos(pi/3-HV(id)));
G(id)=3*IV(id)-(R(id)+B(id));
%BG Sector
id=find((2*pi/3<=HV)& (HV<4*pi/3));
R(id)=IV(id).*(1-SV(id));
G(id)=IV(id).*(1+SV(id).*cos(HV(id)-2*pi/3)./cos(pi-HV(id)));
B(id)=3*IV(id)-(R(id)+G(id));
%BR Sector
id=find((4*pi/3<=HV)& (HV<2*pi));
G(id)=IV(id).*(1-SV(id));
B(id)=IV(id).*(1+SV(id).*cos(HV(id)-4*pi/3)./cos(5*pi/3-HV(id)));
R(id)=3*IV(id)-(G(id)+B(id));
C=cat(3,R,G,B);
C=max(min(C,1),0);
end
程序出错提示:??? function rgbtohsi(x)
|
Error: Function definitions are not permitted at the prompt or in scripts.
% 显示HSI图像
%直方图均衡化HSI图像
%将HSI图像转换回RGB图像
function rgbtohsi(x)
F=imread(x);
F=im2double(F);
r=F(:,:,1);
g=F(:,:,2);
b=F(:,:,3);
th=acos((0.5*((r-g)+(r-b)))./((sqrt((r-g).^2+(r-b).*(g-b)))+eps));
H=th;
H(b>g)=2*pi-H(b>g);
H=H/(2*pi);
S=1-3.*(min(min(r,g),b))./(r+g+b+eps);
I=(r+g+b)/3;
hsi=cat(3,H,S,I);
HE=H*2*pi;
HE=histeq(HE);
HE=HE/(2*pi);
SE=histeq(S);
IE=histeq(I);
choice=input('1:RGB 转换为 HSI\n2:显示HSI图像\n3:HSI转换为RGB图像\n4:色调均衡\n5:饱和度均衡\n6:亮度均衡 \n7:HSI均衡\n 输入您的选择 :');
switch choice
case 1
figure,imshow(F),title('RGB 图像');
figure, imshow(hsi),title('HSI 图像');
case 2
figure,imshow(F),title('RGB 图像');
figure, imshow(H),title('Hue 图像');
figure, imshow(S),title('饱和度图像');
figure, imshow(I),title('亮度图像');
case 3
C=hsitorgb(hsi);
figure,imshow(hsi),title('HSI 图像');
figure, imshow(C),title('RGB 图像');
case 4
RV=cat(3,HE,S,I);
C=hsitorgb(RV);
figure,imshow(hsi),title('HSI 图像');
figure,imshow(F),title('RGB 图像');
figure, imshow(C),title('RGB Image-Hue Equalized');
case 5
RV=cat(3,H,SE,I);
C=hsitorgb(RV);
figure,imshow(hsi),title('HSI 图像');
figure,imshow(F),title('RGB 图像');
figure, imshow(C),title('RGB 饱和度均衡');
case 6
RV=cat(3,H,S,IE);
C=hsitorgb(RV);
figure,imshow(hsi),title('HSI 图像');
figure,imshow(F),title('RGB 图像');
figure, imshow(C),title('RGB 亮度均衡');
case 7
RV=cat(3,HE,SE,IE);
C=hsitorgb(RV);
figure,imshow(hsi),title('HSI 图像');
figure,imshow(F),title('RGB 图像');
figure, imshow(C),title('RGB HSI 均衡');
otherwise
display('选择错误');
end
end
%函数,将HSI图像转换为RGB图像
function C=hsitorgb(hsi)
HV=hsi(:,:,1)*2*pi;
SV=hsi(:,:,2);
IV=hsi(:,:,3);
R=zeros(size(HV));
G=zeros(size(HV));
B=zeros(size(HV));
%RG Sector
id=find((0<=HV)& (HV<2*pi/3));
B(id)=IV(id).*(1-SV(id));
R(id)=IV(id).*(1+SV(id).*cos(HV(id))./cos(pi/3-HV(id)));
G(id)=3*IV(id)-(R(id)+B(id));
%BG Sector
id=find((2*pi/3<=HV)& (HV<4*pi/3));
R(id)=IV(id).*(1-SV(id));
G(id)=IV(id).*(1+SV(id).*cos(HV(id)-2*pi/3)./cos(pi-HV(id)));
B(id)=3*IV(id)-(R(id)+G(id));
%BR Sector
id=find((4*pi/3<=HV)& (HV<2*pi));
G(id)=IV(id).*(1-SV(id));
B(id)=IV(id).*(1+SV(id).*cos(HV(id)-4*pi/3)./cos(5*pi/3-HV(id)));
R(id)=3*IV(id)-(G(id)+B(id));
C=cat(3,R,G,B);
C=max(min(C,1),0);
end
程序出错提示:??? function rgbtohsi(x)
|
Error: Function definitions are not permitted at the prompt or in scripts.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询