Udacity

[Udacity][Computer Vision]Filter

2016-06-26  本文已影响192人  wxy325

该系列主要为上Udacity Computer Vision公开课所做的笔记,内容将会涉及一些公开课视频截图,如有侵权请联系删除

Image as A Funciton

Image as A Function

Impulse

面积为1,相当于图片中的基本单位

Correlation vs Convolution

Convolution的结果为Correlation作一次左右翻转再做一次上下翻转。
对于Impulse Image做Filter,Corelation将会得到一个左右、上下翻转后的Filter,而Convolution将会得到Filter本身。
对于对称的Filter,Correlation与Convolution的结果相同

Formula for Correlation and Convolution Correlation Convolution

Image Filter

Gaussian Filter

Influence of Sigma

Influence of Sigma in Gaussian Filter

Code

pkg load image;

img = imread('../../resource/sample_img.png');
imshow(img);


%% create a gaussian filter
filter_size = 21;
filter_sigma = 3;
filter = fspecial('gaussian', filter_size, filter_sigma);
smoothed = imfilter(img, filter, 0);

%0
%circular  
%replicate
%symmetric

smoothed = imfilter(img, filter, 'circular');

imshow(smoothed);

Edge Problem

clip filter wrap around copy edge

Unsharp Mask

Unsharp Mask

Salt & Pepper Noise and Median Filter

Salt & Pepper Noise

Salt & Pepper Noise

Median Filter

Median Filter is not linear

Median Filter

Code in Matlab or Octave

noisy_img = imnoise(img, 'salt & pepper', 0.02);
imshow(noisy_img);
%median filter
median_filtered = medfilt2(noisy_img);
imshow(median_filtered);

Filter as Template

2A-L4

Template Matching

从Image中寻找Template

Normalized Correlation

Finding Template Correlation Map

1-D Case

例如一维信号

onion = [1,2,3,4,5]
peppers = [2,3,4]

其normalized correlation为最大值处就是template所在位置

onion = [1,2,3,4,5]
peppers = [2,3,4]
normxcorr2(onion, peppers);
%-0.86603  -0.50000   1.00000   1.00000   1.00000  -0.50000  -0.86603
%其中结果第一位为
%  2, 3, 4
%        1,2,3,4,5
%第二位
% 2,3,4
%   1,2,3,4,5

2-D Case

function [yIndex xIndex] = find_template_2D(template, img)
    c = normxcorr2(template, img);
    [yRaw xRaw] = find(c == max(c(:)));
    yIndex = yRaw - size(template, 1) + 1;
    xIndex = xRaw - size(template, 2) + 1;
end

Edge Detection

Gradient

Image Gradient

Image Gradient

Gradient Matrix

Gradient Matrix

Well-Known Gradient Mask

Sobel Operator

因为大部分图片都是smooth的,所以在计算gradient时可以使用Sobel Operator

Sobel Operator Matrix
在Matlab中,Sobel Operator function没有系数1/8,因此结果为8倍
filt = fspecial('sobel')
% filt = [[1, 2, 1]; [0,0,0], [-1, -2, -1]];
[gx gy] = imgradientxy(img, 'sobel');
% gx gradient in x direction
% gy gradient in y direction
imshow((gx + 4) / 8);  %normalize
imshow(gx, [-4, 4]);

% magnitude and direction
[gmag, gdir] = imgradient(gx, gy);

Other

Well-Known Gradient Mask

Real World

现实世界中,由于图片一般有noise,所以在计算gradient之前需要先把图片通过filter来smooth。由于derivative和filter都是linear operation,可以使用以下公式

Real World Gradient

先对gradient mask使用filter,再将其apply至图片

filtered before gradient

2nd Derivative

对图片进行2nd Derivative of Gaussian Filter,0点即为Gradient的极值点,即为Edge

2nd Derivate of Gaussian Filter

Effect of Sigma of Gaussian Derivative

Sigma of Gaussian Derivative

Canny Edge Operator

Gradient的结果要成为边缘图,还需要将 Gradient图细化并进行线条连接。这时可以使用Canny Edge Operator。

Canny Edge Operator - 1 Canny Edge Operator -2
Steps
Original Image Magnitude of The Gradient Thresholding Thining Canny: Non-maximal suppression Canny threshold hysteresis
上一篇 下一篇

猜你喜欢

热点阅读