CS376 Lecture 2
CS376 Lecture 2
Qixing Huang
January 28th 2019
Announcements
• Piazza for assignment questions
• Linear filters
– Examples: smoothing filters
Images as matrices
Result of averaging 100 similar snapshots
Source: https://slideplayer.com/slide/13474214/
Images as functions
Digital camera
10 10 10 10 24 20
21 21 70 10 30 40
00 05 10 15 30 10 2D
05 15 10 16 17 28
75 50 30 20 25 30
98 78 77 77 75 46
1D
Digital color images
Slide Credit:
Digital color images Kristen Grauman
Color images,
RGB color
space
R G B
Images in Matlab
• Images represented as a matrix
• Suppose we have a NxM RGB image called “I”
– I(1,1,1) = top-left pixel value in R-channel
– I(y, x, b) = y pixels down, x pixels to right in the bth channel
– I(N, M, 3) = bottom-right pixel in B-channel
• imread(filename) returns a uint8 image (values 0 to 255)
– Convert to double format (values 0 to 1) with im2double
Main idea: image filtering
• Aggregate the local neighborhood at each pixel in the
image
– Function specified a pattern saying how to aggregate
values from neighbors
• Uses of filtering:
– Enhance an image (denoise, resize, level-of-details, etc)
– Extract information (texture, edges, features, etc)
– Detect patterns (template matching)
Motivation: noise reduction
Slide credit:
Kristen Grauman
Common types of noise
– Salt and pepper noise:
random occurrences of
black and white pixels
Source: S. Seitz
Gaussian noise
Image
shows the
noise values
themselves.
Effect of
sigma on
Gaussian
noise:
Image
shows the
noise values
themselves.
Effect of
sigma on
Gaussian
noise:
Image
shows the
noise values
themselves.
Effect of
sigma on
Gaussian
noise
Slide credit:
Kristen Grauman
First attempt at a solution
• Let’s replace each pixel with an average of all
the values in its neighborhood
• Assumptions:
– Expect pixels to be like their neighbors
– Expect noise processes to be independent from
pixel to pixel
First attempt at a solution
• Let’s replace each pixel with an average of all
the values in its neighborhood
• Moving average in 1D:
Source: S. Marschner
Weighted Moving Average
• Can add weights to our moving average
• Weights [1, 1, 1, 1, 1] / 5
Source: S. Marschner
Weighted Moving Average
• Non-uniform weights [1, 4, 6, 4, 1] / 16
Source: S. Marschner
Moving Average In 2D
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 90 0 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Source: S. Seitz
Moving Average In 2D
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 10
0 0 0 90 90 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 90 0 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Source: S. Seitz
Moving Average In 2D
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 10 20
0 0 0 90 90 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 90 0 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Source: S. Seitz
Moving Average In 2D
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 10 20 30
0 0 0 90 90 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 90 0 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Source: S. Seitz
Moving Average In 2D
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 10 20 30 30
0 0 0 90 90 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 90 0 90 90 90 0 0
0 0 0 90 90 90 90 90 0 0
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Source: S. Seitz
Moving Average In 2D
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 10 20 30 30 30 20 10
0 0 0 90 90 90 90 90 0 0 0 20 40 60 60 60 40 20
0 0 0 90 90 90 90 90 0 0 0 30 60 90 90 90 60 30
0 0 0 90 90 90 90 90 0 0 0 30 50 80 80 90 60 30
0 0 0 90 0 90 90 90 0 0 0 30 50 80 80 90 60 30
0 0 0 90 90 90 90 90 0 0 0 20 30 50 50 60 40 20
0 0 0 0 0 0 0 0 0 0 10 20 30 30 30 30 20 10
0 0 90 0 0 0 0 0 0 0 10 10 10 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Source: S. Seitz
Correlation filtering
Say the averaging window size is 2k+1 x 2k+1:
Non-uniform weights
Correlation filtering
0 0 0 0 0 0 0 0 0 0
0
0
0
0
0
0
0
90
0
90
0
90
0
90
0
90
0
0
0
0
1 1 1 0 20 40 60 60
0
0
0
0
0
0
90
90
90
90
90
90
90
90
90
90
0
0
0
0 ?
1 1 1
0 0 0 90 0 90 90 90 0 0
1 1 1
0 0 0 90 90 90 90 90 0 0
“box filter”
0 0 0 0 0 0 0 0 0 0
0 0 90 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Boundary Issues
• What is the size of the output?
imfilter
Boundary Issues
• What is the size of the output?
filter2
Smoothing with a Gaussian
Gaussian filters
• What parameters matter here?
• Size of kernel or mask
– Note, Gaussian function has infinite support, but discrete filters
use finite kernels
σ = 5 with σ = 5 with
10 x 10 30 x 30
kernel kernel Slide credit:
Kristen Grauman
Gaussian filters
• What parameters matter here?
• Variance of Gaussian: determines extent of
smoothing
σ = 2 with σ = 5 with
30 x 30 30 x 30
kernel kernel
Slide credit:
Kristen Grauman
Matlab
>> hsize = 10;
>> sigma = 5;
>> h = fspecial(‘gaussian’ hsize, sigma);
>> mesh(h);
>> imagesc(h);
for sigma=1:3:10
h = fspecial('gaussian‘, fsize, sigma);
out = imfilter(im, h);
imshow(out);
pause;
Slide credit:
end Kristen Grauman
Keeping the two Gaussians in play straight…
More noise →
Slide credit:
Kristen Grauman
Predict the outputs using correlation
filtering
0 0 0 0 0 0
* 0 1 0 =? * 0 0 1 =?
0 0 0 0 0 0
-
0 0 0 1 1 1
* 0 2 0 1 1 1 =?
0 0 0 1 1 1
Slide credit:
Kristen Grauman
Practice with linear filters
?
0 0 0
0 1 0
0 0 0
Original
Source: D. Lowe
Practice with linear filters
0 0 0
0 1 0
0 0 0
Original Filtered
(no change)
Source: D. Lowe
Practice with linear filters
?
0 0 0
0 0 1
0 0 0
Original
Source: D. Lowe
Practice with linear filters
0 0 0
0 0 1
0 0 0
1 1 1
1 1 1 ?
1 1 1
Original
Source: D. Lowe
Practice with linear filters
1 1 1
1 1 1
1 1 1
Source: D. Lowe
Practice with linear filters
-
0 0 0 1 1 1
0 2 0
0 0 0
1 1 1
1 1 1
?
Original
Source: D. Lowe
Practice with linear filters
-
0 0 0 1 1 1
0 2 0 1 1 1
0 0 0 1 1 1
Source: D. Lowe
Filtering examples: sharpening
More Examples
1 1 1
1 1 1
1 1 1
-
0 0 0 1 1 1
0 2 0 1 1 1
0 0 0 1 1 1
Convolution
• Convolution:
– Flip the filter in both dimensions (bottom to top, right to left)
– Then apply cross-correlation
Notation for H
convolution
operator
Aude Oliva & Antonio Torralba & Philippe G Schyns, SIGGRAPH 2006
Application: Hybrid Images
Gaussian Filter A. Oliva, A. Torralba, P.G. Schyns,
“Hybrid Images,” SIGGRAPH 2006
Laplacian Filter
Aude Oliva & Antonio Torralba & Philippe G Schyns, SIGGRAPH 2006
Summary
• Image “noise”
• Linear filters and convolution useful for
– Enhancing images (smoothing, removing noise)
• Box filter
• Gaussian filter
• Impact of scale / width of smoothing filter
– Detecting features (next time)
• Separable filters more efficient
• Median filter: a non-linear filter, edge-preserving
Coming up
• Wednesday:
– Filtering part 2: filtering for features (edges,
gradients, seam carving application)
• Tomorrow:
– Assignment 0 is due on Canvas 11:59 PM
•