数字图像处理-滤波&边缘检测&锐化&RGB转HSI(Matlab)

文章目录

数字图像处理-图像增强(Matlab)

1、对选定的灰度图像进行反色、线性变换、对数变换等基本处理。(线性变换函数自设)

1.1 灰度反转

  • 原理公式
    g ( x , y ) = L − 1 − f ( x , y ) g(x,y)=L-1-f(x,y) g(x,y)=L1f(x,y)
    其 中 L 为 图 像 的 灰 度 级 数 其中L为图像的灰度级数 L
  • Matlab代码块:
%-------------------------灰度反转(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为256figure('name','灰度反转');
subplot(1,2,1);imshow(gray_pic,[]);title('原灰度图');
subplot(1,2,2);imshow(gray_reversal,[]);title('灰度反转图');
  • 运行效果:

在这里插入图片描述

1.2 线性变换

  • 线性函数
    g ( x , y ) = f ( x , y ) ∗ t a n α g(x,y)=f(x,y)*tan\alpha g(x,y)=f(x,y)tanα
    { α > π 4 g ( x , y ) > f ( x , y ) 灰 度 拉 伸 α = π 4 g ( x , y ) = f ( x , y ) 灰 度 不 变 α < π 4 g ( x , y ) < f ( x , y ) 灰 度 压 缩 \begin{cases} \alpha>\dfrac{\pi}{4} & g(x,y)>f(x,y)&灰度拉伸 \\ \\ \alpha=\dfrac{\pi}{4} &g(x,y) =f(x,y)&灰度不变 \\ \\ \alpha<\dfrac{\pi}{4} & g(x,y)<f(x,y)&灰度压缩 \\ \end{cases} α>4πα=4πα<4πg(x,y)>f(x,y)g(x,y)=f(x,y)g(x,y)<f(x,y)
  • Matlab代码块:
%-------------------------线性变换(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('灰度拉伸');
  • 运行效果:

在这里插入图片描述

1.3 对数变换

  • 对数函数
    g ( x , y ) = C l o g [ 1 + f ( x , y ) ] g(x,y)=Clog[1+f(x,y)] g(x,y)=Clog[1+f(x,y)]
  • Matlab代码块:
%-------------------------对数变换(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
  • 运行效果:

在这里插入图片描述

2、对选定的灰度图像进行直方图均衡化处理,并显示处理前后的直方图。

2.1 直方图均衡化

  • Matlab代码块:
%-------------------------直方图均衡化(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的灰度空间,方便查看效果
  • 运行效果:

在这里插入图片描述

3、分别在两幅灰度图像中加入一定量的高斯噪声和椒盐噪声,噪声强度自定。然后分别采用3x3的均值滤波和3x3中值滤波对噪声进行处理,显示结果图像,并计算出两种处理方法的峰值信噪比(PSNR)。

3.1 添加高斯噪声和椒盐噪声

  • Matlab代码块:
%------------------------------添加噪声(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('加椒盐噪声');
  • 运行效果:

在这里插入图片描述

3.2 峰值信噪比PSNR

  • 原理公式:
    均 方 差 ( 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=0m1j=0n1I(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=1256MAXI=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

3.3 对高斯噪声进行滤波并计算PSNR

  • Matlab代码块:
%-----------------------------对高斯噪声进行滤波并计算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']);
  • 运行效果:

在这里插入图片描述

3.4 对椒盐噪声进行滤波并计算PSNR

  • Matlab代码块:
%-----------------------------对椒盐噪声进行滤波并计算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']);
  • 运行效果:

在这里插入图片描述

4、对选定的灰度图像进行锐化处理:先对原图像进行3*3均值滤波使其模糊,再分别通过Roberts算子、Sobel算子、拉普拉斯算子对其进行边缘增强处理,显示结果图像并对比各方法的结果

4.1 均值滤波

  • Matlab代码块:
%-----------------------------------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均值滤波');
  • 运行效果:

在这里插入图片描述

4.2 Roberts算子边缘增强

  • 原理公式
    ∇ f = ∣ f ( x , y ) − f ( x + 1 , y + 1 ) ∣ + ∣ f ( x + 1 , y ) − f ( x , y + 1 ) ∣ \nabla f=|f(x,y)-f(x+1,y+1)|+|f(x+1,y)-f(x,y+1)| f=f(x,y)f(x+1,y+1)+f(x+1,y)f(x,y+1)
    H 1 = [ 1 0 0 − 1 ]         H 2 = [ 0 1 − 1 0 ] H_1=\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} \ \ \ \ \ \ \ H_2=\begin{bmatrix} 0 & 1 \\ -1 & 0 \end{bmatrix} H1=[1001]       H2=[0110]
  • Matlab代码块:
% ------------------------------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算子锐化图像');
  • 运行效果:

在这里插入图片描述

4.3 Sobel算子边缘增强

  • 原理公式
    在这里插入图片描述

  • Matlab代码块:

% ------------------------------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算子锐化图像');
  • 运行效果:

在这里插入图片描述

4.4 拉普拉斯算子边缘增强

  • 原理公式
    ∇ 2 f = f ( x + 1 , y ) + f ( x − 1 , y ) + f ( x , y + 1 ) + f ( x , y − 1 ) − 4 f ( x , y ) \nabla^2f=f(x+1,y)+f(x-1,y)+f(x,y+1)+f(x,y-1)-4f(x,y) 2f=f(x+1,y)+f(x1,y)+f(x,y+1)+f(x,y1)4f(x,y)
    H = [ 0 1 0 1 − 4 1 0 1 0 ] H=\begin{bmatrix} 0 & 1 & 0 \\ 1 & -4 & 1 \\ 0 & 1 & 0 \end{bmatrix} H=010141010
  • Matlab代码块:
% ------------------------------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为负数,所以最后图像锐化是将两幅图相减
  • 运行效果:

在这里插入图片描述

5、对选定的灰度图像进行巴特沃斯高通滤波处理:要求设定多种不同(高、中、低)的截止频率进行滤波,显示其经滤波后的空域图像

  • 基本原理:

在这里插入图片描述

  • Matlab代码块:
%************************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、对选定的灰度图像进行理想低通滤波处理:要求设定多种不同(高、中、低)的截止频率进行滤波,显示其经滤波后的空域图像。

  • 基本原理:

在这里插入图片描述

  • Matlab代码块:
%************************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三个分量的图像。

  • RGB转HSI公式:

在这里插入图片描述

  • Matlab代码块:
%************************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);
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');
figure('name','R/G/B单色图像直方图');
subplot(3,2,1);imshow(R,[]);title('R');
subplot(3,2,2);imhist(R,128);title('R直方图');
subplot(3,2,3);imshow(G,[]);title('G');
subplot(3,2,4);imhist(G,128);title('G直方图');
subplot(3,2,5);imshow(B,[]);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图像,采用对每一彩色分量进行拉普拉斯算子滤波,完成图像锐化处理。

  • 原理公式
    ∇ 2 f = f ( x + 1 , y ) + f ( x − 1 , y ) + f ( x , y + 1 ) + f ( x , y − 1 ) − 4 f ( x , y ) \nabla^2f=f(x+1,y)+f(x-1,y)+f(x,y+1)+f(x,y-1)-4f(x,y) 2f=f(x+1,y)+f(x1,y)+f(x,y+1)+f(x,y1)4f(x,y)
    H = [ 0 1 0 1 − 4 1 0 1 0 ] H=\begin{bmatrix} 0 & 1 & 0 \\ 1 & -4 & 1 \\ 0 & 1 & 0 \end{bmatrix} H=010141010
  • Matlab代码块:
%************************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('拉普拉斯锐化');
  • 运行效果:

在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_23023937/article/details/109434584