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

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

APPM 2360 Lab #2: Facial Recognition

Image Compression Techniques

CS1114 Assignment 5, Part 1

Lab # 2 - ACS I Part I - DATA COMPRESSION in IMAGE PROCESSING using SVD

SD 575 Image Processing

x = 12 x = 12 1x = 16

Interlude: Solving systems of Equations

Summer 2017 ECE 3056

Lecture 8 JPEG Compression (Part 3)

Image Compression With Haar Discrete Wavelet Transform

Lecture 8 JPEG Compression (Part 3)

A quick Matlab tutorial

Ian Snyder. December 14, 2009

Robert Matthew Buckley. Nova Southeastern University. Dr. Laszlo. MCIS625 On Line. Module 2 Graphics File Format Essay

Matrix Inverse 2 ( 2) 1 = 2 1 2

Haar Wavelet Image Compression

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

AUDIOVISUAL COMMUNICATION

Variable Definition and Statement Suppression You can create your own variables, and assign them values using = >> a = a = 3.

CIS 121 Data Structures and Algorithms with Java Spring 2018

CS 135, Fall 2010 Project 4: Code Optimization Assigned: November 30th, 2010 Due: December 12,, 2010, 12noon

CS1114: Matlab Introduction

The 2D Fourier transform & image filtering

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

ROI Based Image Compression in Baseline JPEG

A Novel Approach for Deblocking JPEG Images

How to learn MATLAB? Some predefined variables

Introduction to Computer Graphics and Computer Vision Assignment 3: Due 7/27/2015

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

Beginner s Mathematica Tutorial

Assignment 02 (Due: Monday, February 1, 2016)

Introduction to Matlab to Accompany Linear Algebra. Douglas Hundley Department of Mathematics and Statistics Whitman College

Homework 1 Description CmpE 362 Spring Instructor : Fatih Alagoz Teaching Assistant : Yekta Said Can Due: 3 March, 23:59, sharp

Topic 3: Fractions. Topic 1 Integers. Topic 2 Decimals. Topic 3 Fractions. Topic 4 Ratios. Topic 5 Percentages. Topic 6 Algebra

MATLAB = MATrix LABoratory. Interactive system. Basic data element is an array that does not require dimensioning.

ITP 101 Project 2 - Photoshop

Homework 6: Pose Tracking EE267 Virtual Reality 2018

Homework 8: Matrices Due: 11:59 PM, Oct 30, 2018

CS4495 Fall 2014 Computer Vision Problem Set 5: Optic Flow

ILLUSTRATOR. Introduction to Adobe Illustrator. You will;

The Singular Value Decomposition: Let A be any m n matrix. orthogonal matrices U, V and a diagonal matrix Σ such that A = UΣV T.

Matrices. Chapter Matrix A Mathematical Definition Matrix Dimensions and Notation

2.1 Transforming Linear Functions

Introduction ti to JPEG

Day 1: Introduction to MATLAB and Colorizing Images CURIE Academy 2015: Computational Photography Sign-Off Sheet

FreeMat Tutorial. 3x + 4y 2z = 5 2x 5y + z = 8 x x + 3y = -1 xx

CS1114: Matlab Introduction

Matrices. Matrices. A matrix is a 2D array of numbers, arranged in rows that go across and columns that go down: 4 columns. Mike Bailey.

Lab 1: Elementary image operations

CIS 520, Machine Learning, Fall 2015: Assignment 7 Due: Mon, Nov 16, :59pm, PDF to Canvas [100 points]

The PackBits program on the Macintosh used a generalized RLE scheme for data compression.

IMAGE COMPRESSION USING FOURIER TRANSFORMS

CSE152a Computer Vision Assignment 1 WI14 Instructor: Prof. David Kriegman. Revision 0

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

I. This material refers to mathematical methods designed for facilitating calculations in matrix

Introduction to Computer Science (I1100) Data Storage

Does everyone have an override code?

Teaching Manual Math 2131

HMC CS 158, Fall 2017 Problem Set 3 Programming: Regularized Polynomial Regression

15 Data Compression 2014/9/21. Objectives After studying this chapter, the student should be able to: 15-1 LOSSLESS COMPRESSION

ECE 3793 Matlab Project 1

Computational Foundations of Cognitive Science. Inverse. Inverse. Inverse Determinant

Lab of COMP 406. MATLAB: Quick Start. Lab tutor : Gene Yu Zhao Mailbox: or Lab 1: 11th Sep, 2013

Index. 1. Motivation 2. Background 3. JPEG Compression The Discrete Cosine Transformation Quantization Coding 4. MPEG 5.

Homework 1: Getting Started with WebGL and Transformations EE267 Virtual Reality 2018

Chemistry 30 Tips for Creating Graphs using Microsoft Excel

Computer Vision, Laboratory session 1

Homework 4 Due Friday, 10/3/08

MATLAB Demo. Preliminaries and Getting Started with Matlab

Assignment 3 Functions, Graphics, and Decomposition

CSE/Math 485 Matlab Tutorial and Demo

Honors Computer Science Python Mr. Clausen Program 7A, 7B

3 Data Storage 3.1. Foundations of Computer Science Cengage Learning

DYADIC WAVELETS AND DCT BASED BLIND COPY-MOVE IMAGE FORGERY DETECTION

Visualizing Change with Colour Gradient Approximations

An introduction to plotting data

Redundant Data Elimination for Image Compression and Internet Transmission using MATLAB

ELE 201, Spring 2014 Laboratory No. 4 Compression, Error Correction, and Watermarking

15-110: Principles of Computing, Spring 2018

1

Programming Exercise 7: K-means Clustering and Principal Component Analysis

Matrices. Mike Bailey. matrices.pptx. Matrices

Today. Motivation. Motivation. Image gradient. Image gradient. Computational Photography

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab

Recognition, SVD, and PCA

MATH 2221A Mathematics Laboratory II

ELEC4042 Signal Processing 2 MATLAB Review (prepared by A/Prof Ambikairajah)

Clustering Images. John Burkardt (ARC/ICAM) Virginia Tech... Math/CS 4414:

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

CS1114 Section: SIFT April 3, 2013

Project # 3: Haar Wavelet Transform. Preliminaries

Performing Matrix Operations on the TI-83/84

1 2 (3 + x 3) x 2 = 1 3 (3 + x 1 2x 3 ) 1. 3 ( 1 x 2) (3 + x(0) 3 ) = 1 2 (3 + 0) = 3. 2 (3 + x(0) 1 2x (0) ( ) = 1 ( 1 x(0) 2 ) = 1 3 ) = 1 3

Introduction to MATLAB

Compression of Stereo Images using a Huffman-Zip Scheme

In math, the rate of change is called the slope and is often described by the ratio rise

Computer Vision, Laboratory session 1

Shading Techniques Denbigh Starkey

CPSC 535 Assignment 1: Introduction to Matlab and Octave

Transcription:

Image Manipulation in MATLAB Due Monday, July 17 at 5:00 PM 1 Instructions Labs may be done in groups of 2 or 3 (i.e., not alone). You may use any programming language you wish but MATLAB is highly suggested. One report must be turned in for each group and must be in PDF format. Labs must include each student s: Name Student number Section number This lab is due on Monday, July 17 at 5pm. Each lab must be turned in through D2L on the course page. When you submit the lab please include each group members information (name, student number, and section number) in the comments. This allows us to search for a students report. Once the labs are graded you need to specifically check that your grade was entered. If you are missing a grade please let Erik know within a week of grading. The report must be typed (including equations). Be sure to label all graph axes and curves so that, independent of the text, it is clear what is in the graph. Simply answering the lab questions will not earn you a good grade. Take time to write your report as up to 20% of your grade may be based on organization, structure, style, grammar, and spelling. Write a report! DO NOT submit a bulleted list of answers. If you have questions about what is expected, don t hesitate to ask. Note Include all of your code in an appendix. Make sure to comment your code clearly, so it s very obvious which problem the code was for. Output not supported by code in the appendix will not be counted. 2 Introduction Digital images are just matrices of pixels, and any type of matrix operation can be applied to a matrix containing image data. In this project you will explore some ways to manipulate images using MATLAB. We ll start off with transformation matrices, and move on to image compression. 1

3 Basic MATLAB commands MATLAB has some nice built in functions for reading and writing image files the first command we ll be using is imread, which reads in an image file saved in the current directory. imread works with most popular image file formats. To view an image in a matlab figure, use imagesc. imagesc is similar to image, but for our purposes will work more consistently. The following code will read in an image with file name photo1.jpg, save it as the variable X, and display the image in a matlab figure window. Make sure you run the code from the same folder that contains the image. After reading in an image like this, X(:,:,1) is a 2-D matrix with intensity values for the red channel, X(:,:,2) for the green and X(:,:,3) for the blue. When images are read in using imread, MATLAB stores the data as integers. If we want to perform mathematical operations on the image data using floating point numbers, the integers must be converted to floats as well. If you just read in an image as X, you can use X = double(x); to perform the floating point conversion. Note that at this point, imagesc will no longer work properly on X, since the values in X aren t integers anymore. If you want to write image data to an image file, you can use imwrite. Note that if you converted the image data to the double format, you ll need to convert back to integer values. The command to do this is uint8. The following code shows how to read in an image, convert to double, and write to a.jpg file. You ll want to build your image manipulation code around this template if you wish to write your output to an image file. 4 Image Manipulation Matrix multiplication allows us to transform a vector in many ways. The following matrix takes the entries of a vector and shifts them down one position, cycling the last entry around to the top. 0 0 0 0 1 1 5 1 0 0 0 0 2 0 1 0 0 0 3 0 0 1 0 0 4 = 1 2 3 0 0 0 1 0 5 4 If you notice, the rows in the matrix were transformed in the same way that the product was, if you consider it starting off as the identity matrix. Each of the rows were shifted down one, and the 2

last row cycled around to the top. This is called a Transformation Matrix, and it is obtained by performing the desired transformation on each column of the identity matrix. In this case, column 1 turns into column 2, column 2 turns into column 3 etc. This transformation matrix works on matrices too. 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 1 6 11 16 21 2 7 12 17 22 3 8 13 18 23 4 9 14 19 24 5 10 15 20 25 = 5 10 15 20 25 1 6 11 16 21 2 7 12 17 22 3 8 13 18 23 4 9 14 19 24 Notice how it cycled the rows around in a matrix the same way it did in the vector. Since an image is just a matrix, we can transform them using a linear transformation. The following image was transformed using a 256 256 version of the transformation matrix above, shifting the image down by 50 pixels. Here s the code that produced the shifted image above. Rows can be transformed too, just by multiplying by the transformation matrix on the right side. As an example: 0 0 0 0 1 [ ] 1 0 0 0 0 1 2 3 4 5 0 1 0 0 0 0 0 1 0 0 = [ 2 3 4 5 1 ] 0 0 0 1 0 3

However, the reordering is not the same as before the transpose of the transformation matrix must be used to shift the elements so that the last element is first. 0 1 0 0 0 [ ] 0 0 1 0 0 1 2 3 4 5 0 0 0 1 0 0 0 0 0 1 = [ 5 1 2 3 4 ] 1 0 0 0 0 Just for fun: this code preforms the shift above, but animates it through the entire image. 5 Image Compression 5.1 The Discrete Cosine Transform You can think of the discrete Cosine transform (DCT) as decomposing a vector into a linear combination of cosine functions with different frequencies. If the data in a vector are smooth, then the low frequency components will dominate the linear combination. If the data are not smooth (discontinuous, jagged, rapidly increasing or decreasing), then there will be more weight placed on the higher frequency components. If something is smooth, that is it doesn t wiggle around very much, then most of the information can be retained by only looking at the first few cosine modes. Think about Taylor series, smooth functions can be approximated locally pretty well by low order Taylor polynomials. The idea is very similar with a cosine transform, except instead of using increasingly higher order (wigglier) polynomial terms we re using increasingly higher frequency (wigglier) cosine functions to represent a given set of data. There are several ways to define the DCT matrix, for this assignment use: 1 n for i = 1 C i,j = ( ) 2 n cos π(2(j 1)+1)(i 1) 2n for i > 1 Where C is an n n matrix, and i and j represent the row and column index respectively. There are several ways to construct this matrix, the easiest way is to use nested for loops. If you get stuck, use the following code as a guideline. 4

To apply this transform matrix to a vector just multiply. So if y is the transformed version of x, we would obtain it by doing y = Cx. Since C is square, the 1-D DCT is an operation that takes in a vector of length n and returns another vector of length n. For 1-D data, the output is a vector containing weights for the different frequency components; the higher the weight the more important that frequency component. This is just the 1-D DCT matrix. To transform our image data, we will need the 2-D transform. Thankfully it s very easy to compute the 2-D DCT using the 1-D matrix. Let X g be the grayscale version of the image data, then the 2-D DCT for the image X g is: Y = CX g C 1 The DCT matrix has the special property that its inverse is the same as its transpose. So for our DCT matrix C, C 1 = C T This is useful since transposes are significantly easier to compute than inverses. Now we can define the 2-D transformed image as: Y = CX g C T Intuitively, you can think of CX g as applying applying the 1-D DCT to the columns of X g, and X g C T as applying the 1-D DCT to the rows of X g. So CX g C T applies the 1-D transform to both the rows and the columns of X g. X g is a matrix, who s (i, j) entry represents the grayscale level at pixel position (i, j). In our case, the values range from 0 to 255, with 0 being black and 255 being white. 5

Figure 1: DCT coefficients for an image (values in the matrix Y ). Values in the upper left are weights on low frequency cosine components while values in the lower right are weights on high frequency cosine components. Since the values in the upper left are significantly larger than those in the lower right, we can see that low frequencies dominate the overall image. 5.2 Compression JPEG is a type of lossy compression, which means that the compressed file contains less information than the original. Since human eyes are better at seeing lower frequency components, we can afford to toss out the highest frequency components of our transformed image. The more uniform an image, the more data we can throw away without causing a noticeable loss in quality. More complicated images can still be compressed, but heavy compression is more noticeable. Thankfully the DCT can sort out which components of the image are represented by low frequencies, which are the ones we need to keep. The information corresponding to the highest frequencies is stored in the lower right of the transformed matrix, while the lowest is stored in the upper left. Therefore, we want to save data in the upper left, and delete data in the lower right. The following code will zero out the matrix below the off diagonal. 6

Adjusting the value of p moves the diagonal up and down the matrix, affecting how much data are retained. This illustration shows how the off diagonal moves with changes in p. After deleting the high frequency data, the inverse 2-D DCT must be applied to return the transformed image back to normal space (right now it will look nothing like the original photograph). Since none of the zeros need to be stored, this process could allow for a significant reduction in file size. 6 Tasks Note: Write-up all answers, and save it as a pdf. There should be enough exposition to describe the problem to someone who has not read this assignment. 1. Write a MATLAB function to read in the files photo1.jpg and photo2.jpg and store them as matrices of doubles. Convert the color arrays into a grayscale matrices formed by the linear combination of 30% red, 59% green, and 11% blue. Include these grayscale images in your writeup. 2. Find a matrix that will perform a horizontal shift of 128 pixels to photo2.jpg and include the shifted image in your write up. 3. How could you perform a horizontal and vertical shift? That is, what matrix operations would need to be applied to to get an image to wrap around both horizontally and vertically? Apply transformations to the original matrix from photo2.jpg that result in both a horizontal and vertical shift. Shift the image 128 pixels horizontally and 128 pixels vertically. 4. Using what you learned about transformation matrices, determine what matrix would be required to flip an image upside down. Using that transformation, flip photo2.jpg upside down. 5. What should transposing an image matrix do? Try it with photo2.jpg. Does it look the way you expected? 6. Using your own DCT matrix code, make a plot of the determinant of C as a function of n for n from 1 to 32 (you will need to create 32 different DCT matrices). Do you notice anything interesting about the relationship? What can you say about the rank of C? 7. Given the dimensions of our DCT matrix, what restrictions must we impose on the aspect ratio of images we wish to transform? (Hint: the DCT matrix is square.) you may use MATLAB s det command 7

8. Determine what steps need to be taken to undo the 2-D DCT. Remember that our DCT is defined by Y = CX g C T, and also the special properties of C. You can easily check to see if your inverse transform works by applying it to Y and viewing it with imagesc. 9. Perform our simplified JPEG-type compression on the image of Albus the cat, photo2.jpg Read an image into MATLAB and store as a matrix of doubles Convert the 3-D RGB matrix to a 2-D grayscale matrix Perform the 2-D discrete cosine transform on the grayscale image data Delete some of the less important values in the transformed matrix using the included algorithm Perform the inverse discrete cosine transform View the image or write to a file. Compress the image with several different values of p. Include sample images for compression values that don t cause an obvious drop in quality, as well as some that do. (Something like the 4 images at the top of this project writeup) 10. You should be able to make p pretty small before noticing a significant loss in quality. Explain why you think this might be the case. The point of image compression is to reduce storage requirements; compare the number of non-zero entries in the transformed image matrix (Y, not X g ) to a qualitative impression of compressed image quality. What value of p do you think provides a good balance? (no correct answer, just explain) Note Include all of your code in an appendix. Make sure to comment your code clearly, so it s very obvious which problem the code was for. Output not supported by code in the appendix will not be counted. 8