zoukankan      html  css  js  c++  java
  • 让你弄明白高斯核是怎样进行滤波工作的

    function I=imgaussian(I,sigma,siz)
    % IMGAUSSIAN filters an 1D, 2D color/greyscale or 3D image with an
    % Gaussian filter. This function uses for filtering IMFILTER or if
    % compiled the fast mex code imgaussian.c . Instead of using a
    % multidimensional gaussian kernel, it uses the fact that a Gaussian
    % filter can be separated in 1D gaussian kernels.
    %
    % J=IMGAUSSIAN(I,SIGMA,SIZE)
    %
    % inputs,
    % I: The 1D, 2D greyscale/color, or 3D input image with
    % data type Single or Double
    %一维、二维或是三维的输入图像
    % SIGMA: The sigma used for the Gaussian kernel
    %标准差
    % SIZE: Kernel size (single value) (default: sigma*6)
    % 默认的高斯核大小是sigma*6
    % outputs,
    % J: The gaussian filtered image
    %输出滤波后的图像
    % note, compile the code with: mex imgaussian.c -v
    %
    % example,
    % I = im2double(imread('peppers.png'));
    % figure, imshow(imgaussian(I,10));
    %
    % Function is written by D.Kroon University of Twente (September 2009)

    if(~exist('siz','var')), siz=sigma*6; end

    if(sigma>0)
    % Make 1D Gaussian kernel
    x=-ceil(siz/2):ceil(siz/2);
    H = exp(-(x.^2/(2*sigma^2)));
    H = H/sum(H(:));%归一化的一步

    % Filter each dimension with the 1D Gaussian kernels\
    if(ndims(I)==1)
    I=imfilter(I,H, 'same' ,'replicate');
    elseif(ndims(I)==2)
    Hx=reshape(H,[length(H) 1]);
    Hy=reshape(H,[1 length(H)]);
    I=imfilter(imfilter(I,Hx, 'same' ,'replicate'),Hy, 'same' ,'replicate');
    elseif(ndims(I)==3)
    if(size(I,3)<4) % Detect if 3D or color image
    Hx=reshape(H,[length(H) 1]);
    Hy=reshape(H,[1 length(H)]);
    for k=1:size(I,3)
    I(:,:,k)=imfilter(imfilter(I(:,:,k),Hx, 'same' ,'replicate'),Hy, 'same' ,'replicate');
    end
    else
    Hx=reshape(H,[length(H) 1 1]);
    Hy=reshape(H,[1 length(H) 1]);
    Hz=reshape(H,[1 1 length(H)]);
    I=imfilter(imfilter(imfilter(I,Hx, 'same' ,'replicate'),Hy, 'same' ,'replicate'),Hz, 'same' ,'replicate');
    end
    else
    error('imgaussian:input','unsupported input dimension');
    end
    end

    先是对行进行卷积,再是对列进行卷积。

  • 相关阅读:
    Q15格式表示负小数
    音频算法处理笔试面试题
    有符号和无符号之间的转化
    PE5 Smallest multiple
    PE3 Largest prime factor(最大素数因子)
    PE2 Even Fibonacci numbers(最大菲波那列偶数)
    PE 4 Largest palindrome product(最大回文)
    PE1 Multiples of 3 and 5
    Codevs高精度入门(减法、加法和乘法)解题报告
    计算机网络学习笔记(二) 计算机网络结构
  • 原文地址:https://www.cnblogs.com/CBDoctor/p/2264087.html
Copyright © 2011-2022 走看看