%-------------------------灰度反转(Matlab代码)-----------------------
clc; %清空控制台
clear; %清空工作区
close all; %关闭已打开的figure图像窗口
color_pic=imread('lena512color.bmp'); %读取彩色图像
gray_pic=rgb2gray(color_pic); %将彩色图转换成灰度图
gray_reversal=256-1-gray_pic; % g(x,y)=L-1-f(x,y) 此图灰度级数L为256级
figure('name','灰度反转');
subplot(1,2,1);imshow(gray_pic,[]);title('原灰度图');
subplot(1,2,2);imshow(gray_reversal,[]);title('灰度反转图');
%-------------------------线性变换(Matlab代码)------------------------
clc; %清空控制台
clear; %清空工作区
close all; %关闭已打开的figure图像窗口
color_pic=imread('lena512color.bmp'); %读取彩色图像
gray_pic=rgb2gray(color_pic); %将彩色图转换成灰度图
double_gray_pic=im2double(gray_pic);
linear_transform_reduce=double_gray_pic*tan(pi/6); %g(x,y)=f(x,y)*tan(a) 线性变换函数
linear_transform_increase=double_gray_pic*tan(pi/3); %当a<pi/4时,灰度压缩,a>pi/4时,灰度拉伸
figure('name','线性变换');
subplot(1,3,1);imshow(gray_pic);title('原灰度图');
subplot(1,3,2);imshow(im2uint8(linear_transform_reduce),[]);title('灰度压缩'); %im2double后的图像用im2uint8转换成uint8
subplot(1,3,3);imshow(im2uint8(linear_transform_increase),[]);title('灰度拉伸');
%-------------------------对数变换(Matlab代码)---------------------------
clc;
clear;
close all;
gray_pic=imread('log_picture.bmp'); %读取灰度图像
double_gray_pic=im2double(gray_pic); %进行log运算得把uint8转换成double型
log_transform=3*log(1+double_gray_pic); %对数变换g(x,y)=clog[1+f(x,y)],c=3
figure('name','对数变换');
subplot(1,2,1);imshow(gray_pic,[]);title('原灰度图');
subplot(1,2,2);imshow(im2uint8(log_transform),[]);title('对数变换'); %im2double后的图像用im2uint8转换成uint8
%-------------------------直方图均衡化(Matlab代码)-----------------------
clc; %清空控制台
clear; %清空工作区
close all; %关闭已打开的figure图像窗口
color_pic=imread('lena512color.bmp'); %读取彩色图像
gray_pic=rgb2gray(color_pic); %将彩色图转换成灰度图
histogram_equalization=histeq(gray_pic); %调用histeq函数进行直方图均衡化
figure('name','直方图均衡化');
subplot(2,2,1);imshow(gray_pic,[]);title('原灰度图');
subplot(2,2,2);imshow(histogram_equalization,[]);title('直方图均衡化后灰度图');
subplot(2,2,3);imhist(gray_pic,64);title('原直方图'); %直方图划分成64个长度为4的灰度空间,方便查看效果
subplot(2,2,4);imhist(histogram_equalization,64);title('直方图均衡化'); %直方图划分成64个长度为4的灰度空间,方便查看效果
%------------------------------添加噪声(Matlab代码)-----------------------------
clc; %清空控制台
clear; %清空工作区
close all; %关闭已打开的figure图像窗口
color_pic=imread('lena512color.bmp'); %读取彩色图像
gray_pic=rgb2gray(color_pic); %将彩色图转换成灰度图
double_gray_pic=im2double(gray_pic); %将uint8转成im2double型便于后期计算
Gaussian_noise=imnoise(double_gray_pic,'gaussian',0,0.01); %给灰度图添加均值为0,方差为0.01的高斯噪声
Salt_pepper_noise=imnoise(double_gray_pic,'salt & pepper',0.05); %给灰度图添加噪声密度为0.05的椒盐噪声
figure('name','加噪');
subplot(1,3,1);imshow(double_gray_pic,[]);title('原灰度图');
subplot(1,3,2);imshow(Gaussian_noise,[]);title('加高斯噪声');
subplot(1,3,3);imshow(Salt_pepper_noise,[]);title('加椒盐噪声');
原理公式:
均 方 差 ( M S E ) = 1 m n ∑ i = 0 m − 1 ∑ j = 0 n − 1 ∣ ∣ I ( i , j ) − K ( i , j ) ∣ ∣ 2 均方差(MSE)=\dfrac{1}{mn}\sum_{i=0}^{m-1}\sum_{j=0}^{n-1}|| I(i,j)-K(i,j)||^{2} \\ 均方差(MSE)=mn1i=0∑m−1j=0∑n−1∣∣I(i,j)−K(i,j)∣∣2
峰 值 信 噪 比 ( P S N R ) = 10 l o g 10 ( M A X I 2 M S E ) 峰值信噪比(PSNR)=10log_{10}\bigg(\dfrac{MAX_I^2}{MSE}\bigg) 峰值信噪比(PSNR)=10log10(MSEMAXI2)
其 中 M A X I = 图 像 灰 度 级 数 − 1 , 此 处 图 片 灰 度 级 为 256 , 所 以 M A X I = 255 其中MAX_I=图像灰度级数-1,此处图片灰度级为256,所以MAX_I=255 其中MAXI=图像灰度级数−1,此处图片灰度级为256,所以MAXI=255
Matlab代码块:
%------------------------matlab新建函数PSNR------------------------
function cal_PSNR = PSNR(img1,img2)
[width,height]=size(img1);
double_img1=im2double(img1); %计算图像峰值信噪比,需将2张图像从uint8型转成im2double型
double_img2=im2double(img2);
matrix_subtraction=double_img1-double_img2;
MSE=sum(sum(matrix_subtraction.^2))/(width*height);
cal_PSNR=10*log10(255^2/MSE);
end
%-----------------------------对高斯噪声进行滤波并计算PSNR (Matlab代码)-------------------------------
gaussian_mean_filter=filter2(fspecial('average',3),Gaussian_noise); %均值滤波
gau_mean_filter_snr=PSNR(double_gray_pic,gaussian_mean_filter); %均值滤波后的峰值信噪比
gaussian_median_filter=medfilt2(Gaussian_noise,[3,3]); %中值滤波
gau_median_filter_snr=PSNR(double_gray_pic,gaussian_median_filter); %中值滤波后的峰值信噪比
figure('name','对高斯噪声进行滤波');
subplot(1,2,1);imshow(gaussian_mean_filter,[]);title(['均值滤波,PSNR:',num2str(gau_mean_filter_snr),'dB']);
subplot(1,2,2);imshow(gaussian_median_filter,[]);title(['中值滤波,PSNR:',num2str(gau_median_filter_snr),'dB']);
%-----------------------------对椒盐噪声进行滤波并计算PSNR (Matlab代码)-------------------------------
salt_mean_filter=filter2(fspecial('average',3),Salt_pepper_noise); %均值滤波
salt_mean_filter_snr=PSNR(double_gray_pic,salt_mean_filter); %均值滤波后的峰值信噪比
salt_median_filter=medfilt2(Salt_pepper_noise,[3,3]); %中值滤波
salt_median_filter_snr=PSNR(double_gray_pic,salt_median_filter); %中值滤波后的峰值信噪比
figure('name','对椒盐噪声进行滤波');
subplot(1,2,1);imshow(salt_mean_filter,[]);title(['均值滤波,PSNR:',num2str(salt_mean_filter_snr),'dB']);
subplot(1,2,2);imshow(salt_median_filter,[]);title(['中值滤波,PSNR:',num2str(salt_median_filter_snr),'dB']);
%-----------------------------------3*3均值滤波模糊图像(Matlab代码)----------------------------
clc; %清空控制台
clear; %清空工作区
close all; %关闭已打开的figure图像窗口
color_pic=imread('lena512color.bmp'); %读取彩色图像
gray_pic=rgb2gray(color_pic); %将彩色图转换成灰度图
double_gray_pic=im2double(gray_pic); %将uint8转成im2double型便于后期计算
mean_filter=filter2(fspecial('average',3),double_gray_pic); %均值滤波
[width,height]=size(mean_filter);
figure('name','3*3均值滤波');
subplot(1,2,1);imshow(double_gray_pic,[]);title('原灰度图');
subplot(1,2,2);imshow(mean_filter,[]);title('3*3均值滤波');
% ------------------------------Roberts算子边缘增强(Matlab)--------------------------------
roberts_img = zeros(width,height); %预先分配内存空间,提高运行速率
for i=1:width-1
for j=1:height-1
roberts_img(i,j)=abs(mean_filter(i+1,j)-mean_filter(i,j+1))+abs(mean_filter(i,j)-mean_filter(i+1,j+1));
end
end
figure('name','roberts算子');
subplot(2,2,1);imshow(double_gray_pic,[]);title('原灰度图');
subplot(2,2,2);imshow(mean_filter,[]);title('3x3均值滤波');
subplot(2,2,3);imshow(im2uint8(roberts_img),[]);title('roberts算子的图像');
subplot(2,2,4);imshow(im2uint8(mean_filter+roberts_img),[]);title('roberts算子锐化图像');
% ------------------------------Sobel算子边缘增强(Matlab代码)--------------------------------
sobel_img=zeros(width,height); %预先分配内存空间,提高运行速率
sobel_x=zeros(width,height); %预先分配内存空间,提高运行速率
sobel_y=zeros(width,height); %预先分配内存空间,提高运行速率
for i=2:width-1
for j=2:height-1
sobel_y(i,j)=abs(mean_filter(i-1,j+1)+2*mean_filter(i,j+1)+mean_filter(i+1,j+1)-mean_filter(i-1,j-1)-2*mean_filter(i,j-1)-mean_filter(i+1,j-1));
sobel_x(i,j)=abs(mean_filter(i+1,j+1)+2*mean_filter(i+1,j)+mean_filter(i+1,j-1)-mean_filter(i-1,j+1)-2*mean_filter(i-1,j)-mean_filter(i-1,j-1));
sobel_img(i,j)=0.3*sobel_x(i,j)+0.3*sobel_y(i,j);
end
end
figure('name','sobel算子');
subplot(2,2,1);imshow(double_gray_pic,[]);title('原灰度图');
subplot(2,2,2);imshow(mean_filter,[]);title('3x3均值滤波');
subplot(2,2,3);imshow(im2uint8(sobel_img),[]);title('sobel算子的图像');
subplot(2,2,4);imshow(im2uint8(mean_filter+sobel_img),[]);title('sobel算子锐化图像');
% ------------------------------Laplace算子边缘增强(Matlab代码)----------------------------------
laplace_img = zeros(width,height); %预先分配内存空间,提高运行速率
for i=2:width-1
for j=2:height-1
laplace_img(i,j)=mean_filter(i+1,j)+mean_filter(i-1,j)+mean_filter(i,j+1)+mean_filter(i,j-1)-4*mean_filter(i,j);
end
end
figure('name','laplace算子');
subplot(2,2,1);imshow(double_gray_pic,[]);title('原灰度图');
subplot(2,2,2);imshow(mean_filter,[]);title('3x3均值滤波');
subplot(2,2,3);imshow(im2uint8(laplace_img),[]);title('laplace算子的图像');
subplot(2,2,4);imshow(im2uint8(mean_filter-laplace_img),[]);title('laplace算子锐化图像');
%由于采用的拉普拉斯算子中心是-4为负数,所以最后图像锐化是将两幅图相减
推 导 : H ( u , v ) = 1 − H l p ( u , v ) = 1 − 1 1 + [ D ( u , v ) / D 0 ] 2 n 推导:H(u,v)=1-H_{lp}(u,v)=1-\dfrac{1}{1+[D(u,v)/D_0]^{2n}} 推导:H(u,v)=1−Hlp(u,v)=1−1+[D(u,v)/D0]2n1
= [ D ( u , v ) / D 0 ] 2 n 1 + [ D ( u , v ) / D 0 ] 2 n = 1 1 + [ D 0 / D ( u , v ) ] 2 n \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ =\dfrac{[D(u,v)/D_0]^{2n}}{1+[D(u,v)/D_0]^{2n}}=\dfrac{1}{1+[D_0/D(u,v)]^{2n}} =1+[D(u,v)/D0]2n[D(u,v)/D0]2n=1+[D0/D(u,v)]2n1
%************************5、对选定的灰度图像进行巴特沃斯高通滤波处理:要求设定多种不同(高、中、低)的截止频率进行滤波,显示其经滤波后的空域图像**************************
clc; %清空控制台
clear; %清空工作区
close all; %关闭已打开的figure图像窗口
color_pic=imread('lena512color.bmp'); %读取彩色图像
gray_pic=rgb2gray(color_pic); %将彩色图转换成灰度图
double_gray_pic=im2double(gray_pic); %将uint8转成im2double型便于后期计算
[width,height]=size(double_gray_pic);
mid_w=width/2; %图像中心点横坐标
mid_h=height/2; %图像中心点纵坐标
fourier_pic=fft2(double_gray_pic); %对灰度图进行傅里叶变换
fourier_shift=fftshift(fourier_pic); %将频谱图中零频率成分移动至频谱图中心
level=2; %二阶巴特沃兹
end_radius=[5,30,83]; %设置截止频率
result1=zeros(width,height); %预先分配内存空间,提高运行速率
result2=zeros(width,height); %预先分配内存空间,提高运行速率
result3=zeros(width,height); %预先分配内存空间,提高运行速率
for i=1:width
for j=1:height
distance=sqrt((i-mid_w)^2+(j-mid_h)^2); %计算点(x,y)到中心点的距离
h1=1/(1+(end_radius(1)/distance)^(2*level)); %计算巴特沃斯滤波器
h2=1/(1+(end_radius(2)/distance)^(2*level));
h3=1/(1+(end_radius(3)/distance)^(2*level));
result1(i,j)=fourier_shift(i,j)*h1; %用滤波器乘以主函数
result2(i,j)=fourier_shift(i,j)*h2;
result3(i,j)=fourier_shift(i,j)*h3;
end
end
output1=im2uint8(real(ifft2(ifftshift(result1)))); %最终输出要记得频谱搬移回去
output2=im2uint8(real(ifft2(ifftshift(result2))));
output3=im2uint8(real(ifft2(ifftshift(result3))));
figure('name','巴特沃兹高通滤波器');
subplot(2,2,1);imshow(double_gray_pic);title('原灰度图');
subplot(2,2,2);imshow(output1,[]);title(['巴特沃兹高通滤波 D0=',num2str(end_radius(1))]);
subplot(2,2,3);imshow(output2,[]);title(['巴特沃兹高通滤波 D0=',num2str(end_radius(2))]);
subplot(2,2,4);imshow(output3,[]);title(['巴特沃兹高通滤波 D0=',num2str(end_radius(3))]);
%************************6、对选定的灰度图像进行理想低通滤波处理:要求设定多种不同(高、中、低)的截止频率进行滤波,显示其经滤波后的空域图像**************************
clc; %清空控制台
clear; %清空工作区
close all; %关闭已打开的figure图像窗口
color_pic=imread('lena512color.bmp'); %读取彩色图像
gray_pic=rgb2gray(color_pic); %将彩色图转换成灰度图
double_gray_pic=im2double(gray_pic); %将uint8转成im2double型便于后期计算
[width,height]=size(double_gray_pic);
mid_w=width/2; %图像中心点横坐标
mid_h=height/2; %图像中心点纵坐标
fourier_pic=fft2(double_gray_pic); %对灰度图进行傅里叶变换
fourier_shift=fftshift(fourier_pic); %将频谱图中零频率成分移动至频谱图中心
end_radius=[5,30,83]; %设置截止频率
Result=zeros(width,height); %预先分配内存空间,提高运行速率
figure('name','理想低通滤波器');
subplot(2,2,1);imshow(double_gray_pic,[]);title('原灰度图');
for k=1:3
Result=fourier_shift;
for i=1:width
for j=1:height
distance=sqrt((i-mid_w)^2+(j-mid_h)^2); %计算点(x,y)到中心点的距离
if distance>end_radius(k) %如果距离大于截止频率,则滤除分量,直接置0
Result(i,j)=0;
end
end
end
output=im2uint8(real(ifft2(ifftshift(Result)))); %最终输出要记得频谱搬移回去
subplot(2,2,k+1);imshow(output,[]);title(['理想低通滤波器 D0=',num2str(end_radius(k))]);
end
%************************7、对选定的一幅RGB彩色图像(BMP格式),分别显示该图的R/G/B单色图像,绘制R/G/B单色图像的直方图;将RGB彩色模式转换为HIS模式,再显示该图的H/I/S三个分量的图像**************************
clc; %清空控制台
clear; %清空工作区
close all; %关闭已打开的figure图像窗口
color_pic=imread('lena512color.bmp'); %读取彩色图像
double_color_pic=im2double(color_pic); %将uint8转成im2double型便于后期计算
%----------------------------分别提取R/G/B三个通道图像---------------------------
R=double_color_pic(:,:,1); %R,G,B二维用于计算
G=double_color_pic(:,:,2);
B=double_color_pic(:,:,3);
figure('name','提取R/G/B图像,二维灰度图')
subplot(2,2,1);imshow(double_color_pic,[]);title('原彩色图像');
subplot(2,2,2);imshow(R,[]);title('R');
subplot(2,2,3);imshow(G,[]);title('G');
subplot(2,2,4);imshow(B,[]);title('B');
zero=zeros(512,512); %此彩色图单层为512*512,生成512*512的0矩阵,用于R1,G1,B1的三维构成,显示有颜色的通道
R1=cat(3,R,zero,zero); %R1三维 三维能显示红绿蓝背景色
G1=cat(3,zero,G,zero); %G1三维
B1=cat(3,zero,zero,B); %B1三维
figure('name','提取R/G/B图像,三维彩色图');
subplot(2,2,1);imshow(double_color_pic,[]);title('原彩色图像');
subplot(2,2,2);imshow(R1,[]);title('R');
subplot(2,2,3);imshow(G1,[]);title('G');
subplot(2,2,4);imshow(B1,[]);title('B');
figure('name','R/G/B单色图像直方图');
subplot(3,2,1);imshow(R1,[]);title('R');
subplot(3,2,2);imhist(R,128);title('R直方图');
subplot(3,2,3);imshow(G1,[]);title('G');
subplot(3,2,4);imhist(G,128);title('G直方图');
subplot(3,2,5);imshow(B1,[]);title('B');
subplot(3,2,6);imhist(B,128);title('B直方图');
%----------------------------RGB转HSI------------------------------
num=0.5*((R-G)+(R-B));
den=sqrt((R-G).^2+(R-B).*(G-B));
theta=acos(num./(den+eps)); %eps=2.2204e-16 防止分母为0
H=theta;
H(B>G)=2*pi-H(B>G);
H=H/(2*pi); %归一化处理
num=min(min(R,G),B);
den=R+G+B;
den(den==0)=eps; %eps=2.2204e-16 防止分母为0
S=1-3*num./den;
H(S==0)=0; %完全不饱和的颜色根本没有色调 即饱和度为0时色调无定义置0
I=(R+G+B)/3;
HSI=cat(3,H,S,I); %合并三个图层
figure('name','HSI图像');
subplot(2,2,1);imshow(HSI,[]);title('HSI图像');
subplot(2,2,2);imshow(H,[]);title('H分量,色调');
subplot(2,2,3);imshow(S,[]);title('S分量,饱和度');
subplot(2,2,4);imshow(I,[]);title('I分量,亮度');
%************************8、对一幅彩色RGB图像,采用对每一彩色分量进行拉普拉斯算子滤波,完成图像锐化处理**************************
clc; %清空控制台
clear; %清空工作区
close all; %关闭已打开的figure图像窗口
color_pic=imread('lena512color.bmp'); %读取彩色图像
double_color_pic=im2double(color_pic); %将uint8转成im2double型便于后期计算
%-----先提取三个图层------
R=double_color_pic(:,:,1); %提取R图层
G=double_color_pic(:,:,2); %提取G图层
B=double_color_pic(:,:,3); %提取B图层
[width,height]=size(R);
laplace_imgR = zeros(width,height); %预先分配内存空间,提高运行速率
laplace_imgG = zeros(width,height); %预先分配内存空间,提高运行速率
laplace_imgB = zeros(width,height); %预先分配内存空间,提高运行速率
%--------分别对R/G/B图层进行拉普拉斯算子滤波-------
for i=2:width-1
for j=2:height-1
laplace_imgR(i,j)=R(i+1,j)+R(i-1,j)+R(i,j+1)+R(i,j-1)-4*R(i,j);
laplace_imgG(i,j)=G(i+1,j)+G(i-1,j)+G(i,j+1)+G(i,j-1)-4*G(i,j);
laplace_imgB(i,j)=B(i+1,j)+B(i-1,j)+B(i,j+1)+B(i,j-1)-4*B(i,j);
end
end
output=cat(3,laplace_imgR,laplace_imgG,laplace_imgB); %合并三个经拉普拉斯算子滤波后的图层
figure('name','RGB拉普拉斯算子锐化');
subplot(2,2,1);imshow(laplace_imgR,[]);title('R分量拉普拉斯滤波');
subplot(2,2,2);imshow(laplace_imgG,[]);title('G分量拉普拉斯滤波');
subplot(2,2,3);imshow(laplace_imgB,[]);title('B分量拉普拉斯滤波');
subplot(2,2,4);imshow(output,[]);title('合成图像拉普拉斯滤波');
figure('name','三通道锐化后的图像');
subplot(1,2,1);imshow(color_pic,[]);title('原彩色图');
subplot(1,2,2);imshow(im2uint8(double_color_pic-output));title('拉普拉斯锐化');