Last Time Dithering Digital Halftoning Ordered dithering compares

  • Slides: 31
Download presentation
Last Time • Dithering (Digital Halftoning) – Ordered dithering compares each pixel to a

Last Time • Dithering (Digital Halftoning) – Ordered dithering compares each pixel to a different threshold, designed to reduce sharp-edge artifacts – Pattern dithering computes sub-block brightnesses and replaces the block with a fixed output of the same brightness – Floyd-Steinberg (error diffusion) gives very good results but still has some artifacts – You can dither color images too • Signal processing – Images can be viewed as sampled functions – All functions can be represented in the frequency domain as a sum of sin’s and cos’s of varying frequency – The Fourier Transform computes the frequency domain representation – Sharp edges result in high frequency components – Smooth areas result in low frequency components – A band-limited function has an upper bound on the highest frequency – Filters emphasize or diminish particular frequencies – There are several functions we will be particularly concerned with 2/10/04 © University of Wisconsin, CS 559 Spring 2004

Today • Project 1 • Filters • Reconstruction basics 2/10/04 © University of Wisconsin,

Today • Project 1 • Filters • Reconstruction basics 2/10/04 © University of Wisconsin, CS 559 Spring 2004

Project 1 • Due March 1 • Implement a basic image editing program –

Project 1 • Due March 1 • Implement a basic image editing program – Photoshop lite • The aim is to implement 100 points worth of options – Different options have different point values – Some options are harder than others – The value of some options depends on whether you do other, similar options • You may work in pairs, in which case the aim is 200 points – Both people must independently email cs 559 -1 • Start early: you have enough information to implement several operations already 2/10/04 © University of Wisconsin, CS 559 Spring 2004

More Project 1 • Read very carefully the requirements for grading – We will

More Project 1 • Read very carefully the requirements for grading – We will compile and run your program automatically – We will look at the output individually – We will not tolerate programs that don’t compile or run • Keep an eye out for hints and clarifications 2/10/04 © University of Wisconsin, CS 559 Spring 2004

Box Filter • Box filters smooth by averaging neighbors • In frequency domain, keeps

Box Filter • Box filters smooth by averaging neighbors • In frequency domain, keeps low frequencies and attenuates (reduces) high frequencies, so clearly a low-pass filter Spatial: Box 2/10/04 Frequency: sinc © University of Wisconsin, CS 559 Spring 2004

Box Filter 2/10/04 © University of Wisconsin, CS 559 Spring 2004

Box Filter 2/10/04 © University of Wisconsin, CS 559 Spring 2004

Handling Boundaries • At (0, 0) for instance, you might need pixel data for

Handling Boundaries • At (0, 0) for instance, you might need pixel data for (-1, -1), which doesn’t exist • Option 1: Make the output image smaller – don’t evaluate pixels you don’t have all the input for • Option 2: Replicate the edge pixels – Equivalent to: posn = x + i; if ( posn < 0 ) posn = 0; and so on for other indices • Option 3: Reflect image about edge – Equivalent to: posn = x + i; if ( posn < 0 ) posn = -posn; and similar for others 2/10/04 © University of Wisconsin, CS 559 Spring 2004

Bartlett Filter • Triangle shaped filter in spatial domain • In frequency domain, product

Bartlett Filter • Triangle shaped filter in spatial domain • In frequency domain, product of two box filters, so attenuates high frequencies more than a box Spatial: Triangle (Box Box) 2/10/04 Frequency: sinc 2 © University of Wisconsin, CS 559 Spring 2004

Constructing Masks: 1 D • Sample the filter function at matrix “pixels”, then normalize

Constructing Masks: 1 D • Sample the filter function at matrix “pixels”, then normalize • eg 2 D Bartlett 1 5 0 1 1 3 1 2 • Can go to edge of pixel or middle of next: results are slightly different 0 2/10/04 1 2 1 4 © University of Wisconsin, CS 559 Spring 2004 1 2 1

Constructing Masks: 2 D • Multiply 2 1 D masks together using outer product

Constructing Masks: 2 D • Multiply 2 1 D masks together using outer product 0. 2 0. 6 0. 2 • M is 2 D mask, m is 1 D mask 0. 2 0. 04 0. 12 0. 04 0. 6 0. 12 0. 36 0. 12 0. 04 2/10/04 © University of Wisconsin, CS 559 Spring 2004

Bartlett Filter 2/10/04 © University of Wisconsin, CS 559 Spring 2004

Bartlett Filter 2/10/04 © University of Wisconsin, CS 559 Spring 2004

Guassian Filter • Attenuates high frequencies even further • In 2 d, rotationally symmetric,

Guassian Filter • Attenuates high frequencies even further • In 2 d, rotationally symmetric, so fewer artifacts 2/10/04 © University of Wisconsin, CS 559 Spring 2004

Gaussian Filter 2/10/04 © University of Wisconsin, CS 559 Spring 2004

Gaussian Filter 2/10/04 © University of Wisconsin, CS 559 Spring 2004

Constructing Gaussian Mask • Use the binomial coefficients – Central Limit Theorem (probability) says

Constructing Gaussian Mask • Use the binomial coefficients – Central Limit Theorem (probability) says that with more samples, binomial converges to Gaussian 1 64 2/10/04 1 1 16 1 4 6 4 1 1 6 15 20 15 6 2 1 1 3 3 1 1 4 6 4 1 1 © University of Wisconsin, CS 559 Spring 2004

High-Pass Filters • A high-pass filter can be obtained from a low-pass filter –

High-Pass Filters • A high-pass filter can be obtained from a low-pass filter – If we subtract the smoothed image from the original, we must be subtracting out the low frequencies – What remains must contain only the high frequencies • High-pass masks come from matrix subtraction: • eg: 3 x 3 Bartlett 2/10/04 © University of Wisconsin, CS 559 Spring 2004

High-Pass Filter 2/10/04 © University of Wisconsin, CS 559 Spring 2004

High-Pass Filter 2/10/04 © University of Wisconsin, CS 559 Spring 2004

Edge Enhancement • High-pass filters give high values at edges, low values in constant

Edge Enhancement • High-pass filters give high values at edges, low values in constant regions • Adding high frequencies back into the image enhances edges • One approach: – Image = Image + [Image – smooth(Image)] Low-pass High-pass 2/10/04 © University of Wisconsin, CS 559 Spring 2004

Edge-Enhance Filter 2/10/04 © University of Wisconsin, CS 559 Spring 2004

Edge-Enhance Filter 2/10/04 © University of Wisconsin, CS 559 Spring 2004

Edge Enhancement 2/10/04 © University of Wisconsin, CS 559 Spring 2004

Edge Enhancement 2/10/04 © University of Wisconsin, CS 559 Spring 2004

Fixing Negative Values • The negative values in high-pass filters can lead to negative

Fixing Negative Values • The negative values in high-pass filters can lead to negative image values – Most image formats don’t support this • Solutions: – Truncate: Chop off values below min or above max – Offset: Add a constant to move the min value to 0 – Re-scale: Rescale the image values to fill the range (0, max) 2/10/04 © University of Wisconsin, CS 559 Spring 2004

Filtering and Color • To filter a color image, simply filter each of R,

Filtering and Color • To filter a color image, simply filter each of R, G and B separately • Re-scaling and truncating are more difficult to implement: – Adjusting each channel separately may change color significantly – Adjusting intensity while keeping hue and saturation may be best, although some loss of saturation is probably OK 2/10/04 © University of Wisconsin, CS 559 Spring 2004

Resampling • Making an image larger is like sampling the original function at higher

Resampling • Making an image larger is like sampling the original function at higher density – You need more pixels to represent the same thing, so a higher pixel density Original More samples Original spacing = bigger • Reducing an image in size is like sampling at lower density • Generating new samples of the “same” function is called resampling – In theory, 2 steps: Reconstruction and sampling – but not in practice • Many other image manipulation tasks require resampling 2/10/04 © University of Wisconsin, CS 559 Spring 2004

General Scenario • You are trying to create a new image of some form,

General Scenario • You are trying to create a new image of some form, and you need data from a particular place in the existing image – Always: Figure out where the new sample comes from in the original image Original 2/10/04 © University of Wisconsin, CS 559 Spring 2004 New

Resampling at a Point • We want to reconstruct the original “function” at the

Resampling at a Point • We want to reconstruct the original “function” at the required point • We will use information from around the point to do this • We do it using a filter – We will justify this shortly • Which filter? ? – We’ll look at Bartlett (triangular) – Other filters also work • You might view this as interpolation, but to understand what’s happening, we’ll view it as filtering Use these to reconstruct 2/10/04 © University of Wisconsin, CS 559 Spring 2004

Resampling at a Point • Place a Bartlett filter at the required point •

Resampling at a Point • Place a Bartlett filter at the required point • Multiply the value of the neighbor by the filter at that point, and add them – Convolution with discrete samples • The filter size is a parameter • Say the filter is size 3, and you need the value at x=5. 75 – You need the image samples, I(x), from x=5, x=6 and x=7 – You need the filter value, H(s), at s=-0. 75, s=0. 25 and s=1. 25 – Compute: I(5)*H(-0. 75)+I(6)*H(0. 25)+I(7)*H(1. 25) 2/10/04 © University of Wisconsin, CS 559 Spring 2004

Functional Form for Filters • Consider the Bartlett in 1 D: • To apply

Functional Form for Filters • Consider the Bartlett in 1 D: • To apply it at a point xorig and find the contribution from point x where the image has value I(x) • Extends naturally to 2 D: 2/10/04 -w/2 © University of Wisconsin, CS 559 Spring 2004 0 w/2

Common Operations • Image scaling by a factor k (e. g. 0. 5 =

Common Operations • Image scaling by a factor k (e. g. 0. 5 = half size): – To get xorig given xnew, divide by k: • Image rotation by an angle : – This rotate around the bottom left (top left? ) corner. It’s up to you to figure out how to rotate about the center – Be careful of radians vs. degrees: all C++ standard math functions take radians, but Open. GL functions take degrees 2/10/04 © University of Wisconsin, CS 559 Spring 2004

Ideal Image Size Reduction • To do ideal image resampling, we would reconstruct the

Ideal Image Size Reduction • To do ideal image resampling, we would reconstruct the original function based on the samples • A requirement for perfect enlargement or size reduction – Almost never possible in practice, and we’ll see why 2/10/04 © University of Wisconsin, CS 559 Spring 2004

An Reconstruction Example • Say you have a sine function of a particular frequency

An Reconstruction Example • Say you have a sine function of a particular frequency • And you sample it too sparsely • You could draw a different sine curve through the samples 2/10/04 © University of Wisconsin, CS 559 Spring 2004

Some Intuition • To reconstruct a function, you need to reconstruct every frequency component

Some Intuition • To reconstruct a function, you need to reconstruct every frequency component that’s in it – This is in the frequency domain, but that’s because it’s easy to talk about “components” of the function • But we’ve just seen that to accurately reconstruct high frequencies, you need more samples • The effect on the previous slide is called aliasing – The correct frequency is aliased by the longer wavelength curve 2/10/04 © University of Wisconsin, CS 559 Spring 2004

Nyquist Frequency • Aliasing cannot happen if you sample at a frequency that is

Nyquist Frequency • Aliasing cannot happen if you sample at a frequency that is twice the original frequency – the Nyquist sampling limit – You cannot accurately reconstruct a signal that was sampled below its Nyquist frequency – you do not have the information – There is no point sampling at higher frequency – you do not gain extra information • Signals that are not bandlimited cannot be accurately sampled and reconstructed – They would require an infinite sampling frequency – Can you reconstruct something with a sharp edge in it? 2/10/04 © University of Wisconsin, CS 559 Spring 2004