CS1114 Section 8: The Fourier Transform March 13th, 2013

Similar documents
APPM 2360 Project 2 Due Nov. 3 at 5:00 PM in D2L

Fourier transforms and convolution

Texture. Outline. Image representations: spatial and frequency Fourier transform Frequency filtering Oriented pyramids Texture representation

Lecture 5: Frequency Domain Transformations

Fourier Transforms and Signal Analysis

Computer Vision and Graphics (ee2031) Digital Image Processing I

The 2D Fourier transform & image filtering

Table of Laplace Transforms

Chapter 1. Math review. 1.1 Some sets

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

Spatial Enhancement Definition

Matlab for FMRI Module 1: the basics Instructor: Luis Hernandez-Garcia

A/D Converter. Sampling. Figure 1.1: Block Diagram of a DSP System

1.7 Limit of a Function

Image processing in frequency Domain

A Basic Guide to Using Matlab in Econ 201FS

Here are some of the more basic curves that we ll need to know how to do as well as limits on the parameter if they are required.

Paul's Online Math Notes. Online Notes / Algebra (Notes) / Systems of Equations / Augmented Matricies

Scaled representations

Digital Image Processing. Prof. P. K. Biswas. Department of Electronic & Electrical Communication Engineering

Math 144 Activity #4 Connecting the unit circle to the graphs of the trig functions

1 Connections: Example Applications of Linear Algebra in Computer Science

Introduction to Digital Image Processing

Digital Image Processing. Image Enhancement - Filtering

UV Mapping to avoid texture flaws and enable proper shading

Sampling, Aliasing, & Mipmaps

Computer Vision. Fourier Transform. 20 January Copyright by NHL Hogeschool and Van de Loosdrecht Machine Vision BV All rights reserved

Image Manipulation in MATLAB Due Monday, July 17 at 5:00 PM

The Addition Formulas in Trigonometry. Scott Fallstrom Faculty Director, Math Learning Center

Digital Image Processing. Lecture 6

Final Review CMSC 733 Fall 2014

Computer Vision: 4. Filtering. By I-Chen Lin Dept. of CS, National Chiao Tung University

Image Compression With Haar Discrete Wavelet Transform

Applied Calculus. Lab 1: An Introduction to R

,!7IA3C1-cjfcei!:t;K;k;K;k ISBN Graphing Calculator Reference Card. Addison-Wesley s. Basics. Created in conjuction with

Theoretically Perfect Sensor

Grade 6 Math Circles November 6 & Relations, Functions, and Morphisms

Generating Functions

Here are a couple of warnings to my students who may be here to get a copy of what happened on a day that you missed.

Matrix Multiplication Studio April 20, 2007

FUNCTIONS AND MODELS

1 Introduction to Matlab

CS534: Introduction to Computer Vision Edges and Contours. Ahmed Elgammal Dept. of Computer Science Rutgers University

Biomedical Image Analysis. Spatial Filtering

Limits. f(x) and lim. g(x) g(x)

CS334: Digital Imaging and Multimedia Edges and Contours. Ahmed Elgammal Dept. of Computer Science Rutgers University

Sampling, Aliasing, & Mipmaps

CS4495 Fall 2014 Computer Vision Problem Set 5: Optic Flow

The first thing we ll need is some numbers. I m going to use the set of times and drug concentration levels in a patient s bloodstream given below.

ACT Math test Trigonometry Review

Spectroscopic Analysis: Peak Detector

sin(a+b) = sinacosb+sinbcosa. (1) sin(2πft+θ) = sin(2πft)cosθ +sinθcos(2πft). (2)

Computational Aspects of MRI

Image processing. Reading. What is an image? Brian Curless CSE 457 Spring 2017

Paul's Online Math Notes Calculus III (Notes) / Applications of Partial Derivatives / Lagrange Multipliers Problems][Assignment Problems]

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

AMS 27L LAB #2 Winter 2009

Theoretically Perfect Sensor

Rec 8 Notes. 1 Divide-and-Conquer Convex Hull. 1.1 Algorithm Idea. 1.2 Algorithm. Here s another random Divide-And-Conquer algorithm.

n! = 1 * 2 * 3 * 4 * * (n-1) * n

Tracking Computer Vision Spring 2018, Lecture 24

Sampling, Aliasing, & Mipmaps

CSE/NEUBEH 528 Homework 0: Introduction to Matlab

Math.1330 Section 5.2 Graphs of the Sine and Cosine Functions

Affine Transformations Computer Graphics Scott D. Anderson

Section 7.6 Graphs of the Sine and Cosine Functions

Deconvolution and Imaging ASTR 240: In-class activity, April 1, 2013

The following is a table that shows the storage requirements of each data type and format:

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

CS1114 Assignment 5, Part 1

Lesson 29: Fourier Series and Recurrence Relations

Image Processing. Filtering. Slide 1

Image Compression Techniques

COMP : Practical 8 ActionScript II: The If statement and Variables

Getting Started Guide

Matlab Introduction. Scalar Variables and Arithmetic Operators

Matrices. A Matrix (This one has 2 Rows and 3 Columns) To add two matrices: add the numbers in the matching positions:

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi.

3. (a) Prove any four properties of 2D Fourier Transform. (b) Determine the kernel coefficients of 2D Hadamard transforms for N=8.

Intermediate Excel 2013

Matlab Tutorial. The value assigned to a variable can be checked by simply typing in the variable name:

Image Processing. Application area chosen because it has very good parallelism and interesting output.

Tutorial on Fourier Theory

4.7 Approximate Integration

CS1114: Study Guide 2

XP: Backup Your Important Files for Safety

Lab 1: Elementary image operations

Section Graphs of the Sine and Cosine Functions

An Intuitive Explanation of Fourier Theory

Fall 2014 MAT 375 Numerical Methods. Introduction to Programming using MATLAB

Basics. Sampling and Reconstruction. Sampling and Reconstruction. Outline. (Spatial) Aliasing. Advanced Computer Graphics (Fall 2010)

Introduction to Matlab

Computer Vision I. Announcements. Fourier Tansform. Efficient Implementation. Edge and Corner Detection. CSE252A Lecture 13.

Part 6b: The effect of scale on raster calculations mean local relief and slope

APPM 2460 PLOTTING IN MATLAB

A quick Matlab tutorial

Aliasing. Can t draw smooth lines on discrete raster device get staircased lines ( jaggies ):

Computer Vision 2. SS 18 Dr. Benjamin Guthier Professur für Bildverarbeitung. Computer Vision 2 Dr. Benjamin Guthier

Outline. Sampling and Reconstruction. Sampling and Reconstruction. Foundations of Computer Graphics (Fall 2012)

2. Use elementary row operations to rewrite the augmented matrix in a simpler form (i.e., one whose solutions are easy to find).

Transcription:

CS1114 Section 8: The Fourier Transform March 13th, 2013 http://xkcd.com/26 Today you will learn about an extremely useful tool in image processing called the Fourier transform, and along the way get more practice with manipulating matrices in Matlab. In class, we ve learned about transformations that take one image to another image. The Fourier transform takes in an image (or, in 1D, a signal), and maps it to a different kind of space frequency space. What are frequencies? Well, a frequency is essentially how fast a signal varies spatially across the domain. The figure below shows two sine waves of two different frequencies, one set in 1D and one set in 2D: 2 sin(x) 2 sin(4*x) 1.5 1.5 1 1 0.5 0.5 0 0-0.5-0.5-1 -1-1.5-1.5-2 0 2 4 6 8 10 12 14 (a) Lower-frequency signal (1D) -2 0 2 4 6 8 10 12 14 (b) Higher-frequency signal (1D) (a) Lower-frequency signal (2D) (b) Higher-frequency signal (2D) 1

The frequency of a sine function is just the inverse of its period. A low-frequency function, like the ones on the left of the figure above above, oscillates very slowly, whereas a higher frequency function (like the ones on the right), oscillates much more quickly, with lots of variation in the same amount of space. For instance, the sines waves on the left of the figure might represent sin(2x), while the right signals might represent sin(8x). We say a sine wave sin(kx) (or a cosine wave cos(kx)) has frequency k. 1 1D Fourier Transforms Why are these sine waves of different frequencies useful? It turns out that we can represent any signal (or image) as a sum of sine and cosine functions with different frequencies, weighted by different coefficients. You might remember from calculus that you can take any function f(x) and approximate it around some center point with a power series f(x) a 0 + a 1 x + a 2 x 2 + a 3 x 4 +... (this is called a Taylor series). For instance: sin(x) = x x3 3! + x5 5! x7 7! +... You can think of computing the Taylor series of a function as computing a Taylor transform of the function transforming the function into its coefficients in Taylor space. We can represent the function in this new space as an array of the coefficients of the Taylor series; in other words, the sin function, under this Taylor transform becomes an (infinite) array of numbers: [ 0, 1, 0, -3, 0, 5, 0, -7,... ] (In practice, we often approximate the function by only keeping the first few numbers in the series.) Along the same lines, Joseph Fourier came up with the idea of representing an arbitrary function as an infinite sum of weighted sine and cosine functions with different frequencies: or, in other words: f(x) = a 0 + a 1 cos(x) + b 1 sin(x) + a 2 cos(2x) + b 2 sin(2x) +... f(x) = a 0 + [a k cos(kx) + b k sin(kx)]. k=1 This summation is called the Fourier series of f. The sequence of (complex) numbers a 0, a 1 + ib 1, a 2 + ib 2 is called the (discrete) Fourier transform of f. We won t worry about the fact that these are complex, not real numbers for now. The important thing is that the Fourier transform takes one function and transforms it into another representation, this new set of Fourier coefficients. Here s a simple example. Suppose our function is simply f(x) = 1 + 2 cos(x) + cos(4x). This function looks like this: 2

-2 4 1+2*cos(x)+cos(4*x) 3 2 1 0-1 0 2 4 6 8 10 and the (discrete) Fourier transform of f is just 1, 2, 0, 0, 1, 0, 0,.... The Fourier transform of a 1D function (i.e., a signal) tells us something about the frequency content of that signal. If only the first few coefficients are large, then that means the signal is fairly low-frequency it doesn t oscillate very rapidly. On the other hand, if there are coefficients far along in the series that are large, then that means that the signal changes very rapidly, at least in some places. Here s an example of a signal somewhere in between: This is a plot of the opening stock price of Google (ticker symbol GOOG on NASDAQ). We can compute the Fourier transform of this signal, and plot it as another signal. In Matlab, the function fft computes the Fourier transform of a signal (FFT stands for Fast Fourier Transform, incidentally). Here s what the Fourier transform of the Google opening price signal looks like: 3

Actually, this Fourier transform is complex (i.e., there is an imaginary component), so this plot shows the magnitude of the complex numbers (we won t worry too much about that, though we have to save something for other classes). Also, this plot shows the logarithm of the magnitudes, as the difference between different Fourier coefficients can be large. As you can see, this function has large coefficients for low frequencies (numbers on the x-axis close to zero), relatively low coefficients for medium frequencies (x between around 50 and 200), and larger coefficients again for the highest frequencies (x > 200). This makes some sense if you look at the original function over a long period of time, the overall shape of the stock price is relatively smooth (low frequency), but on very short time scales a single day the function can jump quite a bit (high frequency). So it is a mixture of low frequency and high frequency behavior, with some lesser amount of medium frequencies mixed in. Because this plot shows the log of the Fourier transform, the largest coefficients are actually many times bigger than the smallest coefficients. Now it s your turn to try this out. Copy the files from /courses/cs1114/section/fourier/ and open up Matlab. Load the data from the file price goog.txt by typing: >> goog = load( price_goog.txt ); This stores the data as an array called goog. You can then do things like plot(goog) to see a plot of the prices. To compute the Fourier transform, we do: >> goog_ft = fft(goog); >> plot(log(abs(goog_ft))); Fourier transforms contain complex numbers and often have huge ranges between the smallest and largest elements. In order to display these values usefully, we use abs to compute the (real-valued) magnitude of these numbers, then log to compute the logarithm. Note that like most math functions in Matlab, when you call abs or log on an array or matrix, the operation is applied individually (element-wise) to each element of the array or matrix. Now try plotting the Fourier transform of Apple s historical stock data, which is in price aapl.txt. Remember how to use the hold on command to put two plots on top of each other (hold off turns this off), and plot the Fourier transform of Google s and Apple s stock data together. Do they look similar? 2 2D Fourier Transforms We can also take the Fourier transform of a 2D signal, i.e., an image. Just as the Fourier transform of a 1D signal gives a set of numbers that we can think of as another signal, the Fourier transform of a 2D image gives us a 2D array that we can also think of as an image (although it will look nothing like the original image). In Matlab, we do this with the fft2 function. Let s try this out. Read in the image called llama.png. This image looks like a llama: 4

You ll want to convert this (and all other images in this lab) to a double image: >> llama = im2double(imread( llama.png )); If you do an imshow(llama), you will see a llama. Now take the Fourier transform of this image, using the fft2 function, storing the result in llama ft. Use imagesc to display llama ft. Whoops! If something went wrong, you might have forgotten to put in the abs function, as Matlab doesn t like displaying complex-valued images. Also, if you forget to put in the call to log, you ll probably get an image that looks almost completely black, as some coefficients are much larger than others. You may also want to tell imagesc to display values using a gray color map (instead of the false-color jet theme) using the colormap gray command. You should get an image that looks like this: 5

This image looks nothing like the original llama. It looks a lot like noise, except that the corners look somewhat brighter than the middle. In this case, the corners actually represent the low-frequency image content, and the middle represents higher frequency content. Amazingly, though this looks nothing like the original image, all of the original image data can be recovered from the Fourier transform. This can be done with the inverse Fourier transform, which in Matlab can be done with the ifft2 function. This takes a Fourier-transformed images, and reconstructs the original image. To see this, try: >> llama2 = ifft2(llama_ft); If you view this image, the llama has reappeared! Let s look at the Fourier transform of another image. Read in the image ceiling.png, and take a look at it. You ll notice that it has more repeated patterns than the llama image. Now compute the Fourier transform, and take a look at that as well. You should see that there is more evident structure in this Fourier transform than in the llama one. That has to do with the quasi-periodicity in the ceiling image. Now this is where things start to get interesting. The neat thing about Fourier transforms is that certain operations which are complicated in image space become extremely simple in Fourier space. Let s see a few examples. 2.1 Blurring One way to think of blurring an image is that we want to remove all of the high-frequency content, leaving only the smooth low frequency content. Earlier, we did this by convolving the original image with a box (or average) filter. But we can also do this very easily to the Fourier-transformed image, by simply setting all of the high-frequency coefficients to zero. Let s try this with the llama image. It turns out that this image is a 500 500 pixel, and the Fourier transform is also 500 500 (the size of the Fourier transform will always be the same as the original image, for now). Remember that the corners of the Fourier transform represent the low-frequency coefficients let s set everything else to zero, everything except for, say, a 50 50 region around each corner. To do that, let s create an image called blocker, which is 0 everywhere except for around the four corners: >> blocker = zeros(500,500); >> blocker(1:50,1:50) = 1; >> blocker(1:50,end-50:end) = 1;... If you haven t seen this before, end is a special name in Matlab that refers to the index of the last column (or row) of the matrix being indexed into this can be very handy. Try out the above you ll need a couple more lines to finish up the matrix blocker. Now we can just multiply this matrix by the Fourier-transformed llama image to get a new Fourier transform: >> llama_ft2 = llama_ft.* blocker; 6

Note that we use.* instead of the regular matrix multiply operator (*). The reason is that we want to just multiply each element of llama ft with the corresponding element of blocker, and not do a full matrix multiplication (this operation is called the entrywise product or Hadamard product, and is much simpler than the regular matrix multiply). llama ft2 should look like this: What happens when we take the inverse Fourier transform of this new image? Try it out! Note that we may have introduced some small imaginary terms in the reconstructed image, so you may need to add in a call to abs. You should see a blurred version of the llama. You might also see some strange artifacts around edges in the image. These are called ringing artifacts, and they occur because simply setting a bunch of frequencies to 0 isn t actually the best thing to do. Feel free to try and figure out a better blurring operator in Fourier space. 2.2 Removing artifacts Now for an even cooler application. Consider the following image, taken from a satellite: 7

There are a bunch of striping artifacts that are a result of the special high-resolution cameras used in satellites. How in the world can we remove the striping artifacts? It seems like a difficult task when we look at the image itself. However, let s take the Fourier transform of this image (you ll find the original image in striping.png). Do you notice anything interesting about the Fourier-transformed image? You should notice some small bright lines that look a bit out of place. It turns out that these are related to the striping artifacts in the image. What would happen if we set just those frequencies to zero? To do this, we ll create another blocker image, that just sets a few entries of the Fourier-transformed image to zero. In fact, we ve already created that image for you. Load the image blocker.png (remembering to do im2double), and have a look at it. You ll see that it s mostly ones, except for a few zeros, which are in key locations (which ones?). Try multiplying this blocker image with the Fourier transform, then do an inverse Fourier transform. If everything went according to plan, you should see a big difference... 2.3 If you have time... We ve seen how to easily blur an image and how to remove periodic artifacts in the Fourier domain. Here are a couple of things to think about if you still have time: 1. When we talked about convolution, we had blur kernels and also sharpening kernels that accentuated edges. Given that edges are by definition high-frequency, can you think of a way to do sharpening in the Fourier domain? Try it out on the llama image. 2. See if you can figure out how to do more even blurring without ringing artifacts that we saw with our corner blocker 8