目录
T1:在 matlab 中显示一幅彩色图像,并分别显示其 R、G、B 分量。
我的代码:
clear all;
close all;
rgb=imread("coloredChips.png");
subplot(2,2,1);imshow(rgb);title('原图');
R=rgb(:,:,1);
G=rgb(:,:,2);
B=rgb(:,:,3);
subplot(2,2,2);imshow(R);title('R');
subplot(2,2,3);imshow(G);title('G');
subplot(2,2,4);imshow(B);title('B');
运行结果:
T2:将图像转换为 HSI 颜色模型,显示各 H、S、I 分量。
RGB转HSI公式:
我的代码:
clear all;
close all;
rgb=imread("coloredChips.png");
subplot(2,2,1);imshow(rgb);title('原图');
hsi=rgb2hsi(rgb);
H=hsi(:,:,1);
S=hsi(:,:,2);
I=hsi(:,:,3);
subplot(2,2,2);imshow(H);title('H');
subplot(2,2,3);imshow(S);title('S');
subplot(2,2,4);imshow(I);title('I');
function hsi=rgb2hsi(rgb)
rgb=im2double(rgb);
R=rgb(:,:,1);
G=rgb(:,:,2);
B=rgb(:,:,3);
num=0.5*((R-G)+(R-B));
den=sqrt((R-G).^2+(R-B).*(G-B));
theta=acos(num./den);
H=theta;
H(B>G)=2*pi-H(B>G);
% 色调H的角度范围为[0,2π]
% 有时候为了将色调H的值在[0,1]之间,会将H的值除以2*PI
H = H/(2*pi);
num=min(min(R,G),B);
den=R+G+B;
% eps是matlab中最小的正数,eps=2.22044604925031e-016
% 在matlab的数值计算中,当发现某个值小于eps时,就把这个数当做0来处理
den(den==0)=eps; %防止分母为0
S=1-3*num./den;
I=(R+G+B)/3;
hsi=cat(3,H,S,I);
运行结果:
T3:将一幅灰度图像转换为伪彩色图像
我的代码:
clear all;
close all;
I=imread('lennagray.bmp');
GS8=grayslice(I,8);
GS64=grayslice(I,64);
subplot(1,3,1), imshow(I), title('原始灰度图像');
subplot(1,3,2), imshow(GS8,hot(8)), title('分成8层伪彩色');
subplot(1,3,3), imshow(GS64,hot(64)), title('分成64层伪彩色');
运行结果:
T4:对一幅图像进行高频、中频、低频滤波,然后分别将高频赋予红色、中频绿色、低频蓝色,实际上也就是空间滤波实现伪彩色
过程见下图:
我的代码:
clear all;
close all;
img=imread('cameraman.tif');
img_l= lpfilter(img,5);
img_h= hpfilter(img,35);
img_b= bpfilter(img,20,30);
img_rgb=cat(3,img_h,img_b,img_l);
subplot(231),imshow(img);title('原图');
subplot(232),imshow(img_h,[]);title('高通滤波');
subplot(233),imshow(img_l,[]);title('低通滤波');
subplot(234),imshow(img_b,[]);title('带通滤波');
subplot(235),imshow(img_rgb,[]);title('伪色彩图像');
lpfilter.m
% 理想低通滤波器
function img_result = lpfilter(img,r)
% 参数说明:img:原图像 r:滤波器半径 img_result:滤波后图像
%生成距离矩阵D,D中每个元素代表该元素与中心点的距离
M=size(img,1);%img的行数
N=size(img,2);%img列数
u=1:M;
v=1:N;
%生成坐标格点矩阵,V每个元素值为所在的列号,U每个元素值为所在的行号
[V,U]=meshgrid(v,u);
%距离公式:D=((U-M/2)^2+(V-N/2)^2)^(1/2)
D=sqrt((U-M/2).^2+(V-N/2).^2);
f=fftshift(fft2(img));
H=zeros(size(img));%滤波器H大小和图像相同
H(D<r)=1;
G=f.*H;
img_result=ifft2(ifftshift(G));
img_result=real(img_result);
hpfilter.m
% 理想高通滤波器
function img_result = hpfilter(img,r)
% 参数说明:img:原图像 r:滤波器半径 img_result:滤波后图像
%生成距离矩阵D,D中每个元素代表该元素与中心点的距离
M=size(img,1);%img的行数
N=size(img,2);%img列数
u=1:M;
v=1:N;
%生成坐标格点矩阵,V每个元素值为所在的列号,U每个元素值为所在的行号
[V,U]=meshgrid(v,u);
%距离公式:D=((U-M/2)^2+(V-N/2)^2)^(1/2)
D=sqrt((U-M/2).^2+(V-N/2).^2);
f=fftshift(fft2(img));
H=zeros(size(img));%滤波器H大小和图像相同
H(:)=1;%H初始化为1
H(D<r)=0;
G=f.*H;
img_result=ifft2(ifftshift(G));
img_result=real(img_result);
bpfilter.m
% 理想带通滤波器
function img_result = bpfilter(img,D0,W)
% 参数说明:img:原图像 D0:截止频率 W:带宽 img_result:滤波后图像
%生成距离矩阵D,D中每个元素代表该元素与中心点的距离
M=size(img,1);%img的行数
N=size(img,2);%img列数
u=1:M;
v=1:N;
%生成坐标格点矩阵,V每个元素值为所在的列号,U每个元素值为所在的行号
[V,U]=meshgrid(v,u);
%距离公式:D=((U-M/2)^2+(V-N/2)^2)^(1/2)
D=sqrt((U-M/2).^2+(V-N/2).^2);
f=fftshift(fft2(img));
H=zeros(size(img));%滤波器H大小和图像相同
H(D<(D0+W/2) & D>(D0-W/2))=1;
G=f.*H;
img_result=ifft2(ifftshift(G));
img_result=real(img_result);
运行结果:
T5:对一幅彩色图像,实现饱和度增强和减弱的效果
HSI转RGB公式:
- 在RG区域,H范围为[0°,120°)
- 在GB区域,H范围为[120°,240°)
- 在BR区域,H范围为[240°,360°)
我的代码:
clear all;
close all;
rgb=imread('coloredChips.png');
subplot(2,2,1.5), imshow(rgb), title('原图');
hsi=rgb2hsi(rgb);
H=hsi(:,:,1);
S=hsi(:,:,2);
I=hsi(:,:,3);
hsi_down=cat(3,H,S*0.3,I);
rgb_down=hsi2rgb(hsi_down);
subplot(2,2,3), imshow(rgb_down), title('饱和度减弱');
hsi_up=cat(3,H,S*3,I);
rgb_up=hsi2rgb(hsi_up);
subplot(2,2,4), imshow(rgb_up), title('饱和度增强');
rgb2hsi.m
function hsi=rgb2hsi(rgb)
rgb=im2double(rgb);
R=rgb(:,:,1);
G=rgb(:,:,2);
B=rgb(:,:,3);
num=0.5*((R-G)+(R-B));
den=sqrt((R-G).^2+(R-B).*(G-B));
theta=acos(num./den);
H=theta;
H(B>G)=2*pi-H(B>G);
% 色调H的角度范围为[0,2π]
% 有时候为了将色调H的值在[0,1]之间,会将H的值除以2*PI
H = H/(2*pi);
num=min(min(R,G),B);
den=R+G+B;
% eps是matlab中最小的正数,eps=2.22044604925031e-016
% 在matlab的数值计算中,当发现某个值小于eps时,就把这个数当做0来处理
den(den==0)=eps; %防止分母为0
S=1-3*num./den;
I=(R+G+B)/3;
hsi=cat(3,H,S,I);
hsi2rgb.m
function rgb=hsi2rgb(hsi)
H=hsi(:,:,1)*2*pi;
S=hsi(:,:,2);
I=hsi(:,:,3);
%RGB矩阵大小与hsi相同
M=size(hsi,1);%img的行数
N=size(hsi,2);%img列数
R=zeros(M,N);
G=zeros(M,N);
B=zeros(M,N);
%RG区
idx=find((0<=H)&(H<2*pi/3));
B(idx)=I(idx).*(1-S(idx));
R(idx)=I(idx).*(1+S(idx).*cos(H(idx))./cos(pi/3-H(idx)));
G(idx)=3*I(idx)-(R(idx)+B(idx));
%GB区
idx=find((H<4*pi/3)&(H>=2*pi/3));
G(idx)=I(idx).*(1+S(idx).*cos(H(idx)-2*pi/3)./cos(pi-H(idx)));
B(idx)=3*I(idx)-(R(idx)+G(idx));
R(idx)=I(idx).*(1-S(idx));
%BR区
idx=find((H<=2*pi)&(H>=4*pi/3));
G(idx)=I(idx).*(1-S(idx));
B(idx)=I(idx).*(1+S(idx).*cos(H(idx)-4*pi/3)./cos(5*pi/3-H(idx)));
R(idx)=3*I(idx)-(G(idx)+B(idx));
rgb=cat(3,R,G,B);
rgb=max(min(rgb,1),0);
运行结果:
T6: RGB颜色空间对图像实现直方图均衡化
我的代码:
clear all;close all;
img=imread('lenna.bmp');
subplot(1,2,1), imshow(img), title('原图');
R=img(:,:,1);
G=img(:,:,2);
B=img(:,:,3);
R=histeq(R);
G=histeq(G);
B=histeq(B);
img_histeq=cat(3,R,G,B);
subplot(1,2,2), imshow(img_histeq), title('RGB均衡化');
运行结果:
T7: HSI颜色空间中I分量进行直方图均衡化
我的代码:
clear all;close all;
img=imread('lenna.bmp');
subplot(1,2,1), imshow(img), title('原图');
hsi=rgb2hsi(img);
H=hsi(:,:,1);
S=hsi(:,:,2);
I=hsi(:,:,3);
I=histeq(I);
hsi_histeq=cat(3,H,S,I);
rgb=hsi2rgb(hsi_histeq);
subplot(1,2,2), imshow(rgb), title('I均衡化');
运行结果: