티스토리 뷰
6.1 Introduction
1) Spatial Filtering
Spatial Filtering : applying a function to a neighborhood of each pixel
① Move a 'mask' : a rectangle (usually with sides of odd length) or other shape over given img
② Create a new img whose pixels have grey values calculated from grey values under the mask
2) Linear Filter
Filter : Combination of Mask and Function
Linear Filter : Combination of Mask and Linear function
Multiplying all elements in Mask
by corresponding elements in Neighborhood Position
→ Adding up all products
3) Process for performing spatial filtering (3 steps)
(1) Position the Mask over the current pixel
(2) Form all products of filter elements with corresponding elements of neighborhood
(3) Add up all the products
(1)~(3) must be repeated for every pixel in img
[Ex] Averging filtering
(1) 3x3 Average Mask
- e : grey value of the current pixel in the original img
- Average : grey value of the corresponding pixel in the new img
(2) 5x5 img
>> x=uint8(10*magic(5))
x =
5×5 uint8 행렬
170 240 10 80 150
230 50 70 140 160
40 60 130 200 220
100 120 190 210 30
110 180 250 20 90
(3) Result of filtering 5x5 img with 3x3 Averaging filter
6.2 Notation
1) Matrix of Linear Filter
[Ex] Another Filter
2) Edges of the image
(1) Obvious problem in applying a filter at the edge of img
: Lack of grey value to use in the filter function
(2) Approaches to solve this problem
① Ignoring the edges
: Applying the mask to those pixels in img for with the mask will lie fully within img
∴ Output img size < Original img size
+) If mask is very large, we may lose a significant amount of information
② Zero Padding
: Assuming that all necessary values outside th img are Zero
∴ Output img size = Original img size
+) But, Unwanted artifacts (ex. edges) around the img can happen
6.3 Filtering in MATLAB
1) How to make Filter
(1) Creating filters by hand
>> a=ones(3,3)/9
a =
0.1111 0.1111 0.1111
0.1111 0.1111 0.1111
0.1111 0.1111 0.1111
(2) Using MATLAB function : fspecial('<option>', <filter size>)
🍏 Many options to make for easy creation of many different filters
Ex. 'average' option for blurring img
>> f1=fspecial('average') # 3x3 size filter (default)
>> f2=fspecial('average',[5,7]) # 5x7 size filter
>> f3=fspecial('average',[11]) # 11x11 size filter
>> h = fspecial(type)
>> h = fspecial('average',hsize)
>> h = fspecial('disk',radius)
>> h = fspecial('gaussian',hsize,sigma)
>> h = fspecial('laplacian',alpha)
>> h = fspecial('log',hsize,sigma)
>> h = fspecial('motion',len,theta)
>> h = fspecial('prewitt')
>> h = fspecial('sobel')
'average' | 평균 필터 |
'disk' | 원형 평균 필터(필박스) |
'gaussian' | 가우스 저역통과 필터 (권장 : imgaussfilt 또는 imgaussfilt3) |
'laplacian' | 2차원 라플라시안 연산자 근사 필터 |
'log' | LoG(가우스-라플라시안) 필터 |
'motion' | 카메라의 선형 움직임 근사 필터 |
'prewitt' | 프루이트(Prewitt) 가로 방향 경계 강조 필터 |
'sobel' | 소벨(Sobel) 가로 방향 경계 강조 필터 |
미리 정의된 2차원 필터 생성 - MATLAB fspecial - MathWorks 한국
이 예제의 수정된 버전이 있습니다. 사용자가 편집한 내용을 반영하여 이 예제를 여시겠습니까?
kr.mathworks.com
🍏 3 different ways to display a matrix of data type double
① Transform it to a matrix of type uint8, for use with imshow
② Divide its values by 255 to obtain a matrix with values in 0.1~1.0, for use with imshow
③ Use mat2gray to scale the result for display
>> c=imread('cameraman.tif');
>> f1=fspecial('average');
>> cf1=filter2(f1,c);
>> figure,imshow(c),figure,imshow(cf1/255)
- Further blurred by using an averaging filter of larger size
2) Filtering MATLAB function
(1) filter2(filter, image, shape) for Linear filtering
- shape : optional parameter to describe method for dealing with the edges
- Result : A matrix of data type double
(2) Examples
▪ filter2(filter, image, 'same') ; default
- How : Using zero padding
- Result : A matrix of equal size to original
>> filter2(a,x,'same')
ans =
76.6667 85.5556 65.5556 67.7778 58.8889
87.7778 111.1111 108.8889 128.8889 105.5556
66.6667 110.0000 130.0000 150.0000 106.6667
67.7778 131.1111 151.1111 148.8889 85.5556
56.6667 105.5556 107.7778 87.7778 38.8889
▪ filter2(filter, image, 'valid')
- How : Applying the mask only to inside pixels
- Result : A matrix of smaller size than original
- Large filter 사용 시 : Zero padding used at the edges 에 의해 dark border appearing around tmg = unwanted artifact 생길 수 있음 (ex. changing the average brightness of img) → 'valid' shape option 사용하면 방지 가능 !
>> filter2(a,x,'valid')
ans =
111.1111 108.8889 128.8889
110.0000 130.0000 150.0000
131.1111 151.1111 148.8889
▪ filter2(filter, image, 'full')
- How : Using zero padding & Applying the filter at all places on and around img where the mask intersects img
- Result : A matrix of larger size than original
>> filter2(a,x,'full')
ans =
18.8889 45.5556 46.6667 36.6667 26.6667 25.5556 16.6667
44.4444 76.6667 85.5556 65.5556 67.7778 58.8889 34.4444
48.8889 87.7778 111.1111 108.8889 128.8889 105.5556 58.8889
41.1111 66.6667 110.0000 130.0000 150.0000 106.6667 45.5556
27.7778 67.7778 131.1111 151.1111 148.8889 85.5556 37.7778
23.3333 56.6667 105.5556 107.7778 87.7778 38.8889 13.3333
12.2222 32.2222 60.0000 50.0000 40.0000 12.2222 10.0000
6.4 Frequencies ; low and high pass filters
1) Frequencies
: the amount by which grey values change with distance
- one important aspect of an img which enables us to choose the most appropriate filter
(1) High freq components
: characterized by large changes in grey values over small distances
Ex. Edges, Noise
(2) Low freq components
: characterized by little changes in grey values
Ex. Background, skin textures
2) Filters
(1) High Pass Filter
: passing over the high freq components & reducing or eliminating low freq components
- The sum of the coefficients(all elements in filter matrix) = 0
- Applying HPF to Low freq part of img(where grey values are similar) : resulting grey values → 0
- Used for Edge Detection, Edge Enhancement
Ex. Laplacian filter, Laplacian of Gaussian ("log") filter
>> f=fspecial('laplacian')
f =
0.1667 0.6667 0.1667
0.6667 -3.3333 0.6667
0.1667 0.6667 0.1667
>> cf=filter2(f,c);
>> imshow(cf/100)
>> f1=fspecial('log')
f1 =
0.0448 0.0468 0.0564 0.0468 0.0448
0.0468 0.3167 0.7146 0.3167 0.0468
0.0564 0.7146 -4.9048 0.7146 0.0564
0.0468 0.3167 0.7146 0.3167 0.0468
0.0448 0.0468 0.0564 0.0468 0.0448
>> cf1=filter2(f1,c);
>> figure,imshow(cf1/100)
(2) Low Pass Filter
: passing over the low freq components & reducing or eliminating high freq components
Ex. 3x3 averaging filter (blurring edges)
3) How to deal with values outside 0-255
For img display, grey values of pixels to lie bw 0-255
But, result of linear filtering values lie outside 0-255
(1) Make Negative values Positive
- certainly dealing with negative values, but not with values greater than 255
- So, (1) can only be used in specific circumstances
(Ex. only a few negative values & these values are close to zero)
(2) Clip values
- applying thresholding type operation to resulting grey value x to obtain displayable value y :
- producing img with all pixel values in required range
- But, not suitable if there are many grey values outside 0-255 range
(In particular, equally spread over a large range)
→ Destroy the results of filtering
(3) Scaling transformatioins
- x축 : g_L : the lowest grey value produced by filter / g_H : the highest grey value
- y축 : transforming all value in g_L - g_H to 0 - 255 by linear transformation :
→ 0 ≤ y ≤ 255
>> f2=[1 -2 1;-2 4 -2;1 -2 1]; # HPF
>> cf2=filter2(f2,c); # cf2 : -541~593
>> figure,imshow(mat2gray(cf2)); # mat2gray -> double(0~1)
>> maxcf2=max(cf2(:));
>> mincf2=min(cf2(:));
>> cf2g=(cf2-mincf2)/(maxcf2-mncf2); # linear transformation -> uint8(0~255)
>> figure,imshow(cf2/60) # better result by dividing result by a constant
6.5 Gaussian filters
1) Gaussian filters
Gaussian filters : Low Pass Filters, all based on the Gaussian probability distribution function
- blurring effect similar to neighborhood averaging
- σ : standard deviation (large σ produces to a flatter curve, small σ leads to a pointier curve)
(1) One-dimensional Gaussian function
(2) Two-dimensional Gaussian function
2) MATLAB function
▪ fspecial('gaussian') : producing Discrete version of Gaussian function
- second parameter (default=3) : filter size (square이면 single number 가능)
- final parameter (default=0.5) : standard deviation σ (클수록 평평)
▪ surf(1:a, 1:a, g) : drawing pictures of Discrete Gaussian function
>> a=50;s=3;
>> g=fspecial('gaussian',[a a],s);
>> surf(1:a,1:a,g)
>> s=9;
>> g2=fspecial('gaussian',[a a],s);
>> figure,surf(1:a,1:a,g2)
>> g1=fspecial('gaussian',[5,5]);
>> g2=fspecial('gaussian',[5,5],2);
>> g3=fspecial('gaussian',[11,11],1);
>> g4=fspecial('gaussian',[11,11],5);
>> imshow(filter2(g1,c)/256)
>> figure,imshow(filter2(g2,c)/256)
>> figure,imshow(filter2(g3,c)/256)
>> figure,imshow(filter2(g4,c)/256)
3) Effect of Large Standard deviation without bound
- Result : Averaging filter as limiting values !
- Gaussian blurring과 Averaging 결과는 비슷해보임
- But, Gaussian filter : elegant mathematical properties 가져서 suitable for blurring
>> fspecial('gaussian.,3,100)
ans =
0.1111 0.1111 0.1111
0.1111 0.1111 0.1111
0.1111 0.1111 0.1111
6.6 Non-linear filters
non-linear filters can be used, if less efficiently
1) nlfilter(<img>, <filter size>, <function>)
: Applying a filter to an img according to a pre-defined function
(If funtion is not already defined, we have to create an m-file which defines it)
- 3 Arguments : img matrix, filter size, matrix function which returns a scalar value
- Result : img has lost some sharpness & been brightened by maximum filter and darkend by minimum filter
- Very slow → little call for non-linear filters
>>> cmax=nlfilter(c,[3,3],'max(x(:))');
>>> cmin=nlfilter(c,[3,3],'min(x(:))');
2) ordfilt2
: more efficient MATLAB function
'DIP > Matlab' 카테고리의 다른 글
[Ch8] Edges (0) | 2022.02.20 |
---|---|
[Ch7] Noise (0) | 2022.02.12 |
[Ch5] Pointing Processing (0) | 2022.02.07 |
[Ch4] Image Display (0) | 2022.01.29 |
[Ch3] Images and MATLAB (0) | 2022.01.28 |