Image gradients and edges April th, 28 Yong Jae Lee UC Davis PS due this Friday Announcements Questions? 2 Last time Image formation Linear filters and convolution useful for Image smoothing, removing noise Box filter Gaussian filter Impact of scale / width of smoothing filter Separable filters more efficient Median filter: a non-linear filter, edge-preserving 3
Filter f = /9 x [ ] Review f*g=? original image g filtered 4 Review Filter f = /9 x [ ] T f*g=? original image g filtered 5 Review How do you sharpen an image? 6 2
Practice with linear filters 2 - Original Sharpening filter: accentuates differences with local average 7 Slide credit: David Lowe Filtering examples: sharpening 8 Sharpening revisited What does blurring take away? = original Let s add it back: smoothed (5x5) detail + α = Slide credit: Svetlana Lazebnik original detail sharpened9 3
Unsharp mask filter f ( f f g) ( ) f f g f (( ) e g) image blurred image unit impulse (identity) unit impulse Slide credit: Svetlana Lazebnik Gaussian Laplacian of Gaussian Median filter f: Review Is f(a+b) = f(a)+f(b)? Example: a = [ 2 3 4 5] b = [55 2 3 4 5] Is f linear? Slide credit: Devi Parikh Recall: Image filtering Compute a function of the local neighborhood at each pixel in the image Function specified by a filter or mask saying how to combine values from neighbors Uses of filtering: Enhance an image (denoise, resize, increase contrast, etc) Extract information (texture, edges, interest points, etc) Detect patterns (template matching), Adapted from Derek Hoiem 2 4
Recall: Image filtering Compute a function of the local neighborhood at each pixel in the image Function specified by a filter or mask saying how to combine values from neighbors Uses of filtering: Enhance an image (denoise, resize, increase contrast, etc) Extract information (texture, edges, interest points, etc) Detect patterns (template matching), Adapted from Derek Hoiem 3 Edge detection Goal: map image from 2d array of pixels to a set of curves or line segments or contours. Why? Figure from J. Shotton et al., PAMI 27 Main idea: look for strong gradients, post-process 4 What causes an edge? Reflectance change: appearance information, texture Depth discontinuity: object boundary Cast shadows Change in surface orientation: shape 5 5
Edges/gradients and invariance 6 Derivatives and edges An edge is a place of rapid change in the image intensity function. image intensity function (along horizontal scanline) first derivative Slide credit: Svetlana Lazebnik edges correspond to extrema of derivative 7 Derivatives with convolution For 2D function, f(x,y), the partial derivative is: f ( x, y) f ( x, y) f ( x, y) lim x For discrete data, we can approximate using finite differences: f ( x, y) f ( x, y) f ( x, y) x To implement above as convolution, what would be the associated filter? 8 6
Partial derivatives of an image f ( x, y) x f ( x, y) y - Which shows changes with respect to x? (showing filters for correlation) -? or - 9 Assorted finite difference filters >> My = fspecial( sobel ); >> outim = imfilter(double(im), My); >> imagesc(outim); >> colormap gray; 2 Image gradient The gradient of an image: The gradient points in the direction of most rapid change in intensity The gradient direction (orientation of edge normal) is given by: The edge strength is given by the gradient magnitude Slide credit: Steve Seitz 2 7
Effects of noise Consider a single row or column of the image Plotting intensity as a function of position gives a signal Where is the edge? Slide credit: Steve Seitz 22 Effects of noise Difference filters respond strongly to noise Image noise results in pixels that look very different from their neighbors Generally, the larger the noise the stronger the response What can we do about it? 23 Source: D. Forsyth Solution: smooth first Where is the edge? Look for peaks in 24 8
Derivative theorem of convolution Differentiation property of convolution. 25 Slide credit: Steve Seitz Derivative of Gaussian filters ( I g) h I ( g h).3.33.29.33.3.33.596.983.596.33.29.983.62.983.29.33.596.983.596.33.3.33.29.33.3 26 Derivative of Gaussian filters x-direction y-direction 27 Slide credit: Svetlana Lazebnik 9
Laplacian of Gaussian Consider Laplacian of Gaussian operator Where is the edge? Slide credit: Steve Seitz Zero-crossings of bottom graph 28 2D edge detection filters Laplacian of Gaussian Gaussian derivative of Gaussian is the Laplacian operator: 29 Slide credit: Steve Seitz Smoothing with a Gaussian Recall: parameter σ is the scale / width / spread of the Gaussian kernel, and controls the amount of smoothing. 3
Effect of σ on derivatives σ = pixel σ = 3 pixels The apparent structures differ depending on Gaussian s scale parameter. Larger values: larger scale edges detected Smaller values: finer features detected 3 So, what scale to choose? It depends what we re looking for. 32 Mask properties Smoothing Values positive Sum to constant regions same as input Amount of smoothing proportional to mask size Remove high-frequency components; low-pass filter Derivatives signs used to get high response in regions of high contrast Sum to no response in constant regions High absolute value at points of high contrast 33
Seam carving: main idea [Shai & Avidan, SIGGRAPH 27] 34 Seam carving: main idea Content-aware resizing Traditional resizing [Shai & Avidan, SIGGRAPH 27] 35 Seam carving: main idea video 36 2
Seam carving: main idea Content-aware resizing Intuition: Preserve the most interesting content Prefer to remove pixels with low gradient energy To reduce or increase size in one dimension, remove irregularly shaped seams Optimal solution via dynamic programming. 37 Seam carving: main idea Energy( f ) Want to remove seams where they won t be very noticeable: Measure energy as gradient magnitude Choose seam based on minimum total energy path across image, subject to 8-connectedness. 38 Seam carving: algorithm s s 2 s 3 s 4 s 5 Let a vertical seam s consist of h positions that form an 8-connected path. h Let the cost of a seam be: Cost(s) Energy( f ( s i )) i Optimal seam minimizes this cost: s* min Cost( s) s Compute it efficiently with dynamic programming. Energy( f ) 39 3
How to identify the minimum cost seam? How many possible seams are there? height h, width w First, consider a greedy approach: 2 5 3 8 9 2 6 Energy matrix (gradient magnitude) Slide credit: Adapted from Kristen Grauman 4 Seam carving: algorithm Compute the cumulative minimum energy for all possible connected seams at each entry (i,j): M( i, j) Energy( i, j) min M( i, j ), M( i, j), M( i, j ) row i- row i j- j j+ j Energy matrix (gradient magnitude) M matrix: cumulative min energy (for vertical seams) Then, min value in last row of M indicates end of the minimal connected vertical seam. Backtrack up from there, selecting min of 3 above in M. 4 Example M( i, j) Energy( i, j) min M( i, j ), M( i, j), M( i, j ) 2 5 3 8 2 9 6 3 8 3 8 5 9 4 Energy matrix (gradient magnitude) M matrix (for vertical seams) 42 4
Example M( i, j) Energy( i, j) min M( i, j ), M( i, j), M( i, j ) 2 5 3 8 2 9 6 3 8 3 8 5 9 4 Energy matrix (gradient magnitude) M matrix (for vertical seams) 43 Real image example Original Image Energy Map Blue = low energy Red = high energy 44 Real image example 45 5
Other notes on seam carving Analogous procedure for horizontal seams Can also insert seams to increase size of image in either dimension Duplicate optimal seam, averaged with neighbors Other energy functions may be plugged in E.g., color-based, interactive, Can use combination of vertical and horizontal seams 46 Example seam carving results 47 48 6
49 Conventional resize Original image Seam carving result 5 Conventional resize Original image Seam carving result 5 7
Conventional resize (399 by 599) Original image (599 by 799) Seam carving (399 by 599) 52 Removal of a marked object 53 Removal of a marked object 54 8
Removal of a marked object 55 Failure cases with seam carving 56 Failure cases with seam carving 57 9
Questions? See you Thursday! 58 2