数字图像处理实验4(HGD)—— 帮助文档

conv2:

 conv2 Two dimensional convolution.
    C = conv2(A, B) performs the 2-D convolution of matrices A and B.

    C = conv2(..., SHAPE) returns a subsection of the 2-D convolution with size specified by SHAPE:
      'full'  - (default) returns the full 2-D convolution,
      'same'  - returns the central part of the convolution that is the same size as A.
      'valid' - returns only those parts of the convolution that are computed without the zero-padded edges.
                size(C) = max([ma-max(0,mb-1),na-max(0,nb-1)],0).
conv2即实现矩阵A和B的二维卷积。实验中我们采用的是same参数,如果要使输出图像与输入图像的大小保持一致,就要用same。关于三个参数的具体解释,参照(详细的图解):https://blog.csdn.net/qq_27015403/article/details/52831062


对于filter2,conv2,imfilter的区别:
1、  filter2、conv2将输入转换为double类型,输出也是double的,输入总是补零(zero padded), 不支持其他的边界补充选项。
2、  imfilter:不将输入转换为double,输出只与输入同类型,有灵活的边界补充选项

来自 :<https://blog.csdn.net/yuanhuiling/article/details/79128116> 

fspecial:

 fspecial Create predefined 2-D filters.
    H = fspecial(TYPE) creates a two-dimensional filter H of the specified type. Possible values for TYPE are:
      'average'     averaging filter
      'disk'           circular averaging filter
      'gaussian'    Gaussian lowpass filter
      'laplacian'   filter approximating the 2-D Laplacian operator
      'log'            Laplacian of Gaussian filter
      'motion'      motion filter
      'prewitt'      Prewitt horizontal edge-emphasizing filter
      'sobel'         Sobel horizontal edge-emphasizing filter

  
    H = fspecial('prewitt') returns 3-by-3 filter that emphasizes
    horizontal edges by approximating a vertical gradient. If you need to
    emphasize vertical edges, transpose the filter H: H'.
 
        [1 1 1;0 0 0;-1 -1 -1].
 
    H = fspecial('sobel') returns 3-by-3 filter that emphasizes
    horizontal edges utilizing the smoothing effect by approximating a
    vertical gradient. If you need to emphasize vertical edges, transpose
    the filter H: H'.
 
        [1 2 1;0 0 0;-1 -2 -1].

fspecial 即创造一个预先定义好的二维过滤器,或者叫模板。其有八种模板可以选择。为什么称之为预先定义好的呢?原因如上述帮助文档可见,它已经给定了具体的模板数值,如果要自己定义的话,可以不采用该函数,可以自己定义一个算子(矩阵),z再利用conv2函数将图像和算子进行2维卷积运算。


imshow:

 imshow Display image in Handle Graphics figure.  
    imshow(I) displays the grayscale image I.
 
    imshow(I,[LOW HIGH]) displays the grayscale image I, specifying the display
    range for I in [LOW HIGH]. The value LOW (and any value less than LOW)
    displays as black, the value HIGH (and any value greater than HIGH) displays
    as white. Values in between are displayed as intermediate shades of gray,
    using the default number of gray levels. 
 
    imshow(I,[]) displays the grayscale image I scaling the display based
    on the range of pixel values in I. imshow uses [min(I(:)) max(I(:))] as
    the display range, that is, the minimum value in I is displayed as
    black, and the maximum value is displayed as white.

imshow,这个函数可以说是最简单的函数之一,为什么到了这个实验才提及呢?是因为在这个实验我不小心调用了imshow(I,[])函数,导致我的输出图像特别的怪异(跟课本或者实验教材对不上),查阅此函数,原来空值默认选取了有效的(有值的)像素范围,相当于进行了将一个范围小于[0,1](如[0.3,0.7])变换到了[0,1]的线性灰度变换。实验中只调用一个参数即可。

附上实验四代码(仅供参考):(matlab R2016a)

图像锐化算子:

I=imread('lena.jpg');
I=im2double(I);
figure('NumberTitle', 'off', 'Name', ' 图像锐化算子');
subplot(2,2,1),imshow(I,[]);title('原始图像');
h=[0 1 0,1 -4 1,0 1 0];%拉普拉斯算子
J=conv2(I,h,'same');
K=I-J;%增强的图像为原始图像减去拉普拉斯算子滤波的图像
subplot(2,2,2),imshow(K);title('laplace');
H=fspecial('sobel');
J1=imfilter(I,H);
J1=I-J1;
subplot(2,2,3),imshow(J1);title('sobel');
H1=fspecial('prewitt');
J2=imfilter(I,H1);
J2=I-J2;
subplot(2,2,4),imshow(J2);title('prewitt');

均值和中值滤波,低通滤波:

[I,map]=imread('lena.jpg');
I1 = imnoise(I,'salt & pepper',0.02);       %添加椒盐噪声   
I2 = double(I1)/255;
h1=[1/9 1/9 1/9;1/9 1/9 1/9;1/9 1/9 1/9];
J1=conv2(I2,h1);
J2=medfilt2(I2,[3 3 ]);
figure('NumberTitle', 'off', 'Name', '均值和中值滤波');
subplot(1,3,1);imshow(I2);title('椒盐噪声');
subplot(1,3,2);imshow(J1);title('均值滤波');
subplot(1,3,3);imshow(J2);title('中值滤波');

noisy = imnoise(I,'gaussian',0.02);     %添加高斯噪声
[M N]=size(I);
F=fft2(noisy);
fftshift(F);
Dcut=100;
D0=150;
D1=250;
for u=1:M
    for v=1:N
        D(u,v)=sqrt(u^2+v^2);
        BUTTERH(u,v)=1/(1+(sqrt(2)-1)*(D(u,v)/Dcut)^2);%巴特沃斯低通滤波器
        EXPOTH(u,v)=exp(log(1/sqrt(2))*(D(u,v)/Dcut)^2);%指数型
        if D(u,v)<D0
            TRAPEH(u,v)=1;       %梯形
        elseif D(u,v)<=D1
            TRAPEH(u,v)=(D(u,v)-D1)/(D0-D1);
        else
            TRAPEH(u,v)=0;
        end
    end
end
BUTTERG=BUTTERH.*F;
BUTTERfiltered=ifft2(BUTTERG);
EXPOTG=EXPOTH.*F;
EXPOTGfiltered=ifft2(EXPOTG);
TRAPEG=TRAPEH.*F;
TRAPEfiltered=ifft2(TRAPEG);
figure('NumberTitle', 'off', 'Name', '低通滤波器平滑处理');
subplot(2,2,1),imshow(noisy);title('高斯噪声');
subplot(2,2,2),imshow(BUTTERfiltered,map);title('巴特沃斯低通滤波后');
subplot(2,2,3),imshow(EXPOTGfiltered,map);title('指数型低通滤波后');
subplot(2,2,4),imshow(TRAPEfiltered,map);title('梯形低通滤波后');
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值