数字图像处理(实验)-matlab-非线性平滑滤波,频域图像增强,直方图修正,彩色图像处理

实验使用matlab2018b版本

一.直方图修正

【实验内容】

1.

clc;

g=imread('lene.jpg');

I=rgb2gray(g);

subplot(2,2,1);

imshow(I);

title('原始图像');

i1=zeros(1,256);

i2=zeros(1,256);

new_i1=zeros(1,256);

new_i2=zeros(1,256);

[h w]=size(I);

new_i=zeros(h,w);

for x=1:h

    for y=1:w

        i1(1+I(x,y))=i1(1+I(x,y))+1;

    end

end

i2=i1/sum(i1);

subplot(2,2,2);

plot(i2);

title('原始图像直方图');

xlabel('灰度级');

ylabel('概率密度');

for i=2:256

    i2(1,i)=i2(1,i)+i2(1,i-1);

end

for i=1:256

    t(1,i)=floor(254*i2(1,i)+0.5);

end

for i=1:256

    new_i1(1+t(1,i)+1)=new_i1(1+t(1,i)+1)+t(1,i);

end

new_i2=new_i1/sum(new_i1);

for x=1:h

    for y=1:w

        new_i(x,y)=t(1+I(x,y));

    end

end

subplot(223),imshow(new_i,[]);

title('均衡化后的图像');

subplot(224),plot(new_i2);

xlabel('灰度级');

ylabel('概率密度');

title('均衡化后的直方图');

2.

clc;

g=imread('lene.jpg');

I=rgb2gray(g);

subplot(2,2,1);

imshow(I);

title('原始图像');

J=histeq(I);

subplot(223),imhist(I);

title('原始图像直方图');

subplot(222),imshow(uint8(J));

title('均衡化后的图像');

subplot(224),imhist(J);

title('均衡化后的直方图');

3.

clc;

I=imread('lene.jpg');

Imatch=imread('caise.jpg');

Jmatch=imhist(Imatch);

Iout=histeq(I,Jmatch);

figure;

subplot(131),imshow(I),title('原始图像');

subplot(132),imshow(Imatch),title('目标图像');

subplot(133),imshow(Iout),title('直方图匹配后的图像');

figure;

subplot(311),imhist(I);

title('原始图像直方图');

subplot(312),imhist(Jmatch);

title('目标图像的直方图');

subplot(313),imhist(Iout);

title('直方图匹配后的直方图');

【实验结果】

1.

 

2.

 

3.

 二.频域图像增强

【实验内容】

1.理想低通

clear;

g=imread('lene.jpg');

I=rgb2gray(g);

subplot(221),imshow(I),xlabel('原始图像');

f=double(I);

I1=fftshift(fft2(I));

F2=log(abs(I1));

subplot(222),imshow(F2,[],'InitialMagnification','fit'),

xlabel('原始图像的傅里叶变换图像')

[a,b]=size(I);

a0=round(a/2);

b0=round(b/2);

d=50;

for i=1:a

    for j=1:b

        dis=sqrt((i-a0)^2+(j-b0)^2);

        if dis<d

            h=1;

        else

            h=0;

        end

        result(i,j)=h*I1(i,j);

    end

end



F3=log(abs(result));

subplot(223),imshow(F3,'InitialMagnification','fit'),xlabel('理想低通滤波后的频域图');

result=ifftshift(result);

F4=ifft2(result);

F5=uint8(real(F4));

subplot(224),imshow(F5);

xlabel('理想低通滤波后的图像');

2.理想高通

clear;

g=imread('lene.jpg');

I=rgb2gray(g);

subplot(221),imshow(I),xlabel('原始图像');

f=double(I);

I1=fftshift(fft2(I));

F2=log(abs(I1));

subplot(222),imshow(F2,[],'InitialMagnification','fit'),

xlabel('原始图像的傅里叶变换图像')

[a,b]=size(I);

a0=round(a/2);

b0=round(b/2);

d=10;

for i=1:a

    for j=1:b

        dis=sqrt((i-a0)^2+(j-b0)^2);

        if dis<d

            h=0;

        else

            h=1;

        end

        result(i,j)=h*I1(i,j);

    end

end



F3=log(abs(result));

subplot(223),imshow(F3,'InitialMagnification','fit'),xlabel('理想高通滤波后的频域图');

result=ifftshift(result);

F4=ifft2(result);

F5=uint8(real(F4));

subplot(224),imshow(F5);

xlabel('理想高通滤波后的图像');

3.2阶巴特沃斯低通

clear;

g=imread('lene.jpg');

I=rgb2gray(g);

subplot(2,2,1);

imshow(I);

xlabel('原始图像');

f=double(I);

I1=fftshift(fft2(I));

F2=log(abs(I1));

subplot(222);

imshow(F2,[],'initialMagnification','fit');

xlabel('(b)傅里叶变换图像');

[a,b]=size(F2);

a0=fix(a/2);

b0=fix(b/2);

d=15;

n=2;

for i=1:a

    for j=1:b

        dis=sqrt((i-a0)^2+(j-b0)^2);

        if dis==0

            h=0;

        else

            h=1/(1+(dis/d)^(2*n));

        end

        result(i,j)=h*I1(i,j);

    end

end

F3=log(abs(result));

subplot(223);

imshow(F3,'initialMagnification','fit');

xlabel('(c)2阶巴特沃斯低通滤波后的频域图');

result=ifftshift(result);

F4=ifft2(result);

F5=uint8(real(F4));

subplot(224);

imshow(F5);

xlabel('(d)2阶巴特沃斯低通滤波后的图像');

4.2阶巴特沃斯高通

clear;

g=imread('lene.jpg');

I=rgb2gray(g);

subplot(2,2,1);

imshow(I);

xlabel('原始图像');

f=double(I);

I1=fftshift(fft2(I));

F2=log(abs(I1));

subplot(222);

imshow(F2,[],'initialMagnification','fit');

xlabel('(b)傅里叶变换图像');

[a,b]=size(F2);

a0=fix(a/2);

b0=fix(b/2);

d=15;

n=2;

for i=1:a

    for j=1:b

        dis=sqrt((i-a0)^2+(j-b0)^2);

        if dis==0

            h=0;

        else

            h=1/(1+(d/dis)^(2*n));

        end

        result(i,j)=h*I1(i,j);

    end

end

F3=log(abs(result));

subplot(223);

imshow(F3,'initialMagnification','fit');

xlabel('(c)2阶巴特沃斯高通滤波后的频域图');

result=ifftshift(result);

F4=ifft2(result);

F5=uint8(real(F4));

subplot(224);

imshow(F5);

xlabel('(d)2阶巴特沃斯高通滤波后的图像');

5.高斯低通

clear;

g=imread('lene.jpg');

I=rgb2gray(g);

subplot(2,2,1);

imshow(I);

xlabel('原始图像');

f=double(I);

I1=fftshift(fft2(I));

F2=log(abs(I1));

subplot(222);

imshow(F2,[],'initialMagnification','fit');

xlabel('(b)傅里叶变换图像');

[a,b]=size(F2);

a0=fix(a/2);

b0=fix(b/2);

d=50;

d1=2*(d^2);

for i=1:a

    for j=1:b

        dis=(i-a0)^2+(j-b0)^2;

        if dis==0

            h=0;

        else

            h=exp(-dis/d1);

        end

        result(i,j)=h*I1(i,j);

    end

end

F3=log(abs(result));

subplot(223);

imshow(F3,'initialMagnification','fit');

xlabel('(c)高斯低通滤波后的频域图');

result=ifftshift(result);

F4=ifft2(result);

F5=uint8(real(F4));

subplot(224);

imshow(F5);

xlabel('(d)高斯低通通滤波后的图像');

6.高斯高通

clear;

g=imread('lene.jpg');

I=rgb2gray(g);

subplot(2,2,1);

imshow(I);

xlabel('原始图像');

f=double(I);

I1=fftshift(fft2(I));

F2=log(abs(I1));

subplot(222);

imshow(F2,[],'initialMagnification','fit');

xlabel('(b)傅里叶变换图像');

[a,b]=size(F2);

a0=fix(a/2);

b0=fix(b/2);

d=5;

d1=2*(d^2);

for i=1:a

    for j=1:b

        dis=(i-a0)^2+(j-b0)^2;

        if dis==0

            h=0;

        else

            h=1-exp(-dis/d1);

        end

        result(i,j)=h*I1(i,j);

    end

end

F3=log(abs(result));

subplot(223);

imshow(F3,'initialMagnification','fit');

xlabel('(c)高斯高通滤波后的频域图');

result=ifftshift(result);

F4=ifft2(result);

F5=uint8(real(F4));

subplot(224);

imshow(F5);

xlabel('(d)高斯高通通滤波后的图像');

【实验结果】

1.

 

2.

 

3.

 

4.

 

5.

 

6.

 三.非线性平滑滤波器

【实验内容】

1.

clc;

x=imread('lene.jpg');

n=3;

[h,w]=size(x);

subplot(221),imshow(x);

title('原始图像');

x=padarray(x,[4,4]);

I=rgb2gray(x);

x=imnoise(x,'salt & pepper',0.2);

subplot(222),imshow(x);

title('加入椒盐噪声的图像');

x1=double(x);

x2=x1;

for i=1:h-n+1

    for j=1:w-n+1

        c=x1(i:i+(n-1),j:j+(n-1));

        e=c(1,:);

        for u=2:n

            e=[e,c(u,:)];

        end

        med=median(e);

        x2(i+(n-1)/2,j+(n-1)/2)=med;

    end

end

d=uint8(x2);

subplot(223),imshow(d);

title('中值滤波后图像');

x0=rgb2gray(x);

b=medfilt2(x0,[n,n]);

subplot(224),imshow(b);

title('中值滤波后灰度图像');

2.

clc;

g=imread('lene.jpg');

I=rgb2gray(g);

Inoise=imnoise(I, 'salt & pepper',0.3);

subplot(221),imshow(I),xlabel('原始图像');

subplot(222),imshow(Inoise);

xlabel('受椒盐噪声干扰的图像');

[h,w]=size(Inoise);

nmin=3;

nmax=9;

Imf=Inoise;

%边界扩充

I_ex=[zeros((nmax-1)/2,w+(nmax-1));zeros(h,(nmax-1)/2),Inoise,zeros(h,(nmax-1)/2);zeros((nmax-1)/2,w+(nmax-1))];

for x=1:h

    for y=1:w

        for n=nmin:2:nmax

            Sxy=I_ex(x+(nmax-1)/2-(n-1)/2:x+(nmax-1)/2+(n-1)/2,y+(nmax-1)/2-(n-1)/2:y+(nmax-1)/2+(n-1)/2);

            Smax=max(max(Sxy));

            Smin=min(min(Sxy));

            Smed=median(median(Sxy));

            if Smed>Smin && Smed<Smax

                if Imf(x,y)>Smin && Imf(x,y)<Smax

                    Imf(x,y)=Smed;

                end

                break

            end

        end

        Imf(x,y)=Smed;

    end

end

subplot(223),imshow(Imf),xlabel('最大尺寸为9的自适应中值滤波后的图像');



Imf1=medfilt2(Inoise,[3,3]);

subplot(224),imshow(Imf1),xlabel('尺寸为3*3的传统中值滤波后的图像');

【实验结果】

1.

 

2.

四彩色图像处理

 

1.

clc;

I=imread('lene.jpg');

F=rgb2gray(I);

H=grayslice(F,64);

subplot(311),imshow(F),xlabel('灰度图像');

subplot(312),imshow(H,jet(64)),xlabel('伪彩色处理-分层');

subplot(313),imshow(I),xlabel('原图像');

2.

clc;

I=imread('lene.jpg');

subplot(311),imshow(I),xlabel('原图像');

lapmask=[1,1,1;1,-8,1;1,1,1,];

L=imfilter(I,lapmask,'replicate');

subplot(312),imshow(L),xlabel('拉普拉斯锐化效果');

F=imsubtract(I,L);

subplot(313),imshow(F),xlabel('拉普拉斯增强');

3.

clc;

I=imread('lene.jpg');

F=rgb2hsv(I);

H=F(:,:,1);

S=F(:,:,2);

V=F(:,:,3);

subplot(311),imshow(I),xlabel('原图像');

subplot(312),imshow(F),xlabel('原图像的HSV模型');

w=fspecial('average',9);

H_filt=imfilter(H,w,'replicate');

h=cat(3,H_filt,S,V);

i=hsv2rgb(h);

subplot(313),imshow(i),xlabel('只平滑了色调的图像');

【实验结果】

1.

 

2.

 

3.

 

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值