[Udacity][Computer Vision]Filter
2016-06-26 本文已影响192人
wxy325
该系列主要为上Udacity Computer Vision公开课所做的笔记,内容将会涉及一些公开课视频截图,如有侵权请联系删除
Image as A Funciton
Image as A FunctionImpulse
面积为1,相当于图片中的基本单位
Correlation vs Convolution
Convolution的结果为Correlation作一次左右翻转再做一次上下翻转。
对于Impulse Image做Filter,Corelation将会得到一个左右、上下翻转后的Filter,而Convolution将会得到Filter本身。
对于对称的Filter,Correlation与Convolution的结果相同
Image Filter
Gaussian Filter
Influence of Sigma
Influence of Sigma in Gaussian FilterCode
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(black) 0
- wrap around Circular
- copy edge Replicate
- reflect across edge Symmetric
reflect across edge
Unsharp Mask
Unsharp MaskSalt & Pepper Noise and Median Filter
Salt & Pepper Noise
Salt & Pepper NoiseMedian Filter
Median Filter is not linear
Median FilterCode 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 Map1-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 GradientGradient Matrix
Gradient MatrixWell-Known Gradient Mask
Sobel Operator
因为大部分图片都是smooth的,所以在计算gradient时可以使用Sobel Operator
在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 MaskReal World
现实世界中,由于图片一般有noise,所以在计算gradient之前需要先把图片通过filter来smooth。由于derivative和filter都是linear operation,可以使用以下公式
Real World Gradient先对gradient mask使用filter,再将其apply至图片
filtered before gradient2nd Derivative
对图片进行2nd Derivative of Gaussian Filter,0点即为Gradient的极值点,即为Edge
2nd Derivate of Gaussian FilterEffect of Sigma of Gaussian Derivative
Sigma of Gaussian DerivativeCanny Edge Operator
Gradient的结果要成为边缘图,还需要将 Gradient图细化并进行线条连接。这时可以使用Canny Edge Operator。
Canny Edge Operator - 1 Canny Edge Operator -2