matlab常见的图像增强技术(包括基于幂次变换,对图像进行均衡化处理,巴特沃斯低通,理想低通,梯形低通滤波, 均值滤波,中值滤波,最大,最小值滤波,修正后的阿尔法滤波器)

1.基于幂次变换中的r值,比较不同r 值下图像增强的效果

代码 :

I = imread('D:\图片\TH.JFIF');

subplot (1,4,1);

imshow(I);title('原始图像','fontsize', 9);

subplot(1,4,2);

imshow(imadjust(I,[],[],0.5));title('Gamma=0.5');

subplot(1,4,3);

imshow(imadjust(I,[],[],1));title('Gamma=1');

subplot (1,4,4);

imshow(imadjust(I,[],[],1.5));title('Gamma=1.5');

2.对图像进行均衡化处理,灰度等级为64,输出均衡化前的图像,直方图和均衡化后的图像和直方图

代码 :

I=imread('D:\图片\TH.JFIF');
G=rgb2gray(I);
imhist(G);
K=16;H=histeq(G,K);
figure,subplot(2,2,1),imshow(G,[])
subplot(2,2,2),imshow(G,[]),hold on
subplot(2,2,3),hist(double(G),64),subplot(2,2,4),hist(double(H),64)....

 1巴特沃斯低通

 代码 :

I=rgb2gray(imread('C:\Users\Desktop\1.jpg'));
I=imnoise(I,'salt & pepper',0.001);
figure,subplot(1,2,1);imshow(I);
I1=fftshift(fft2(I));
[M,N]=size(I1);
n=2;d0=80;
n1=floor(M/2);
n2=floor(N/2);
for i=1:M
  for j=1:N
      d=sqrt((i-n1)^2+(j-n2)^2);
      H=1/(1+(d/d0)^2*n);
     I2(i,j)=H*I1(i,j);
  end
end
I2=ifftshift(I2);
I3=real(ifft2(I2));
subplot(1,2,2);imshow(I3,[]);

 2理想低通


%理想低通
I = imread('C:\Users\Desktop\1.jpg');
I=rgb2gray(I);
figure(1);
subplot(221),imshow(I);
title('原图像');
I=imnoise(I,'gaussian');%%加入高斯白噪声
subplot(222),imshow(I);
title('加入噪声后的图像');
s=fftshift(fft2(I));
subplot(223), imshow(log(abs(s)),[]); 
title('图像傅里叶变换取对数所得频谱');
[a,b]=size(s);
a0=round(a/2);
b0=round(b/2);
d=50;
for i=1:a 
    for j=1:b 
        distance=sqrt((i-a0)^2+(j-b0)^2);
        if distance<=d
            h=1;
        else
            h=0;
        end
        s(i,j)=h*s(i,j);
    end
end
s=uint8(real(ifft2(ifftshift(s))));
subplot(224),imshow(s);
title('理想低通滤波所得图像');

梯形低通滤波:

I = imread('C:\Users\Desktop\1.jpg');
I=rgb2gray(I);
figure(1);
subplot(221),imshow(I);
title('原图像');
I=imnoise(I,'gaussian');%%加入高斯白噪声
subplot(222),imshow(I);
title('加入噪声后的图像');
s=fftshift(fft2(I));
%subplot(223), imshow(log(abs(s)),[]); 
%title('图像傅里叶变换取对数所得频谱');
[a,b]=size(s);
a0=round(a/2);
b0=round(b/2);
d=50;d1=100;
for i=1:a 
    for j=1:b 
        distance=sqrt((i-a0)^2+(j-b0)^2);
        if distance<=d
            h=1;
        else if    d<=distance&distance<=d1
           h= (distance-d)/(d1-d);
      else
     h=0;
        s(i,j)=h*s(i,j);
         end
       end
    end
end
s=uint8(real(ifft2(ifftshift(s))));
subplot(224),imshow(s);
title('梯形低通滤波所得图像');

 均值滤波

clear;
I=imread('C:\Users\Desktop\1.jpg');
I=im2double(I);
I=rgb2gray(I);
%MappedData = mapminmax(I ,0, 255)
h=figure(1);
%显示原图
subplot(2,2,1);
imshow(I,[]);%[]自动产生适当的比例显示图像
title('原图');
%添加高斯噪声
subplot(2,2,2);
I_noise=double(imnoise(I,'gaussian',0.02));%salt & pepper注意中间的空格 无空格报错
imshow(I_noise,[]);title('高斯噪声');
%均值滤波
subplot(2,2,3);
I_3=fspecial('average',[3,3]);%3*3均值滤波    建立预定义的滤波算子
I_3=imfilter(I_noise,I_3);%(待处理矩阵,滤波器)
imshow(I_3,[]);title('3*3均值滤波');
subplot(2,2,4);
I_=exp(imfilter(log(I_noise),fspecial('average',3)));%算数均值滤波
imshow(I_,[]);title('3*3几何均值滤波');

最大值,最小值滤波:

clear;
clc;
img = imread('C:\Users\Desktop\1.jpg');
[M,N] = size(img);
% 最大值滤波
for i = 2:M-1
    for j=2:N-1
        t = img(i-1:i+1,j-1:j+1);
        new_img_max(i,j)  =max(t(:));
    end
end
% 最小值滤波
for i = 2:M-1
    for j=2:N-1
        t = img(i-1:i+1,j-1:j+1);
        new_img_min(i,j)  =min(t(:));
    end
end
subplot(1,3,1);
imshow(img);
title('原图像');
subplot(1,3,2)
imshow(new_img_max)
title('最大值滤波')
subplot(1,3,3)
imshow(new_img_min)
title('最小值滤波')

 

中值滤波:

%% 滤波窗口选择展示
clear; clc; close all;
rawimg = imread('C:\Users\Desktop\1.jpg');
[~,~,index] = size(rawimg);
if index ~= 1
   rawimg = rgb2gray(rawimg);  % 转化为灰度图 
end
% 添加噪声
salt_img=imnoise(rawimg,'salt & pepper',0.04);     % 叠加密度为0.04的椒盐噪声
figure;subplot(1,2,1);imshow(rawimg);title('原图');
subplot(1,2,2);imshow(salt_img);title('添加椒盐噪声');
% 进行滤波
min_fit = medfilt2(salt_img,[3 3]);   % 采用二维中值滤波函数对图像滤波,滤波窗口是3*3
max_fit = medfilt2(salt_img,[9,9]);  % 滤除高斯噪声
% 滤波效果:
figure;subplot(1,2,1);imshow(min_fit);title('3*3滤波窗口效果');
subplot(1,2,2);imshow(max_fit);title('9*9滤波窗口效果');

 

修正后的阿尔法滤波器:

clc;
img = imread('C:\Users\Desktop\1.jpg');
[M,N] = size(img);
% 最大值滤波
for i = 2:M-1
    for j=2:N-1
        t = img(i-1:i+1,j-1:j+1);
        new_img_max(i,j)  =max(t(:));
    end
end
% 最小值滤波
for i = 2:M-1
    for j=2:N-1
        t = img(i-1:i+1,j-1:j+1);
        new_img_min(i,j)  =min(t(:));
    end
end
subplot(1,3,1);
imshow(img);
title('原图像');
subplot(1,3,2)
imshow(new_img_max)
title('最大值滤波')
subplot(1,3,3)
imshow(new_img_min)
title('最小值滤波')

 

猜你喜欢

转载自blog.csdn.net/m0_53394907/article/details/124357612