CS101 Lecture 12: Image Compression Vector Graphics Compression - - PDF document

cs101 lecture 12 image compression
SMART_READER_LITE
LIVE PREVIEW

CS101 Lecture 12: Image Compression Vector Graphics Compression - - PDF document

10/11/12 CS101 Lecture 12: Image Compression Vector Graphics Compression Techniques Aaron Stevens (azs@bu.edu) 11 October 2012 Computer Science What Youll Learn Today Computer Science Review: how big are image files? How can we


slide-1
SLIDE 1

10/11/12 1

Computer Science

CS101 Lecture 12: Image Compression

Vector Graphics Compression Techniques

Aaron Stevens (azs@bu.edu)

11 October 2012

Computer Science

What You’ll Learn Today

  • Review: how big are image files?
  • How can we make image files smaller?
  • What are the main image file formats, and how

are they different from each other?

  • What are the main techniques for compressing

images?

slide-2
SLIDE 2

10/11/12 2

Computer Science

Image File Size: HUGE!

Raster/Bitmap Graphics Storage of data on a pixel-by-pixel basis

  • Bitmap (BMP), GIF, JPEG, and PNG, for example

How much data is required to represent a picture?

  • Typical size might be 1024 by 768 pixels (~ 800,000)
  • At 3 bytes per pixel, about 2,400,000 bytes for one picture.
  • A 10Mpixel picture would be 30,000,000 bytes per picture.

Computer Science

Consider download times…

Example:

  • A 10Mpixel picture would be 30,000,000 bytes
  • This is 240,000,000 bits.
  • Typical home cable modem downloads at 5 Mbits per second

(5,000,000 bits per second).

How long to download this picture?

  • 240,000,000 / 5,000,000 = 48 seconds

Average download speed on 3G networks is closer to 1MB/second.

http://www.cnn.com/2011/TECH/mobile/02/18/att.verizon.3g.speed.wired/index.html

slide-3
SLIDE 3

10/11/12 3

Computer Science

Describe this picture…

This image was a homework assignment by a CS108 student, Spring 2008.

Vector Graphics describes an image by coordinates, lines, geometric shapes and colors.

Computer Science

Vector Graphics Example

Instructions written in Python by CS108 student (Spring 2010). File size: 29743 bytes.

slide-4
SLIDE 4

10/11/12 4

Computer Science

Vector Graphics

Advantages:

  • Small file sizes (instructions take much less space than

sampling pixels)

  • Easy to resize mathematically without loss of detail

Disadvantages:

  • Image shapes are not lifelike
  • Color contrast and texturing are rigid, not lifelike
  • Requires software to interpret the drawing instructions

Computer Science

Raster vs. Vector Graphics

When would you use each?

Drawings, Diagrams, Games, etc. Photography, video, etc.

slide-5
SLIDE 5

10/11/12 5

Computer Science

Storing an image in fewer bytes

What are our choices? What are reasonable tradeoffs?

  • Raster Graphics vs. Vector Graphics?
  • Decrease sampling rate (fewer pixels)
  • Decrease quantization (fewer colors)
  • Compression

Computer Science

What is the output resolution?

  • iPhone 3 has a resolution of 640 X 960 pixels.
  • Evo has 480 X 800 pixels.
  • Facebook pictures (in web browser) are 560 X 750

pixels.

slide-6
SLIDE 6

10/11/12 6

Computer Science

Lower Image Resolution?

Original bitmap image dimensions:

  • 1524 * 2034 = 3,099,816 pixels
  • 3,099,816 pixels * 3 bytes = 9,299,448 bytes

How about 300 * 400 pixels?

  • 300 * 400 pixels = 120,000 pixels
  • 120,000 pixels * 3 bytes = 360,000 bytes

A compression ratio of 0.038. What’s the catch?

Computer Science

Fewer Colors: GIF Images

Graphics Interchange Format (GIF) Each image is made up of any 256 (or 16) RGB colors, but only those colors.

  • 256 colors: 8 bits per pixel
  • 16 colors: 4 bits per pixel
slide-7
SLIDE 7

10/11/12 7

Computer Science

16 Color GIF 120,000 bytes 256 Color GIF 200,000 bytes Original BMP 360,000 bytes

Fewer Colors: GIF Images

Computer Science

Compress them! How do you fit more cars on this lot?

Data compression Reduction in the amount of space needed to store a piece of data.

slide-8
SLIDE 8

10/11/12 8

Computer Science

Data Compression (saving space)

Data compression Reduction in the amount of space needed to store a piece of data. Data compression techniques can be:

lossless, which means the data can be retrieved without any loss of the original information lossy, which means some information may be lost in the process of compaction

Computer Science

Example: Text Compression

Problem: Unicode assigns 16 bits to each character in a document; uses a heck of a lot of space. We need ways to store and transmit text efficiently. Why? Common lossless compression techniques:

keyword encoding run-length encoding Huffman encoding

slide-9
SLIDE 9

10/11/12 9

Computer Science

Keyword Encoding

Replace frequently used words with a single character

Computer Science

Keyword Encoding Example

Given the following paragraph,

We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit

  • f Happiness. That to secure these rights, Governments are

instituted among Men, deriving their just powers from the consent of the governed, That whenever any Form of Government becomes destructive of these ends, it is the Right of the People to alter or to abolish it, and to institute new Government, laying its foundation on such principles and organizing its powers in such form, as to them shall seem most likely to effect their Safety and Happiness.

slide-10
SLIDE 10

10/11/12 10

Computer Science

Keyword Encoding Example

The encoded paragraph is

We hold # truths to be self-evident, $ all men are created equal, $ ~y are endowed by ~ir Creator with certain unalienable Rights, $ among # are Life, Liberty + ~ pursuit of Happiness. — $ to secure # rights, Governments are instituted among Men, deriving ~ir just powers from ~ consent of ~ governed, — $ whenever any Form of Government becomes destructive of # ends, it is ~ Right of ~ People to alter or to abolish it, + to institute new Government, laying its foundation on such principles + organizing its powers in such form, ^ to ~m shall seem most likely to effect ~ir Safety + Happiness.

Computer Science

Keyword Encoding

Compression ratio The size of the compressed data divided by the size of the

  • riginal data (0 < c.r. <= 1)

What did we save? Original paragraph: 656 characters Encoded paragraph: 596 characters Characters saved: 60 characters Compression ratio: 596/656 = 0.9085 Could we use this substitution chart for all text? How could we apply keyword encoding to images?

slide-11
SLIDE 11

10/11/12 11

Computer Science

Run-Length Encoding

Consider a single character which is repeated over and over again in a long sequence. Replace a repeated sequence with

  • a flag character
  • repeated character
  • number of repetitions

Example: *n8

  • * is the flag character
  • n is the repeated character
  • 8 is the number of times n is repeated

Computer Science

Run-Length Encoding Example

Original text bbbbbbbbjjjkllqqqqqq+++++ Encoded text *b8jjjkll*q6*+5 (Why isn't l encoded? J?) The compression ratio is 15/25 or .6 Encoded text *x4*p4l*k7 Original text xxxxpppplkkkkkkk

This type of repetition isn’t very helpful for English text. Can you think of a situation where it might be helpful?

slide-12
SLIDE 12

10/11/12 12

Computer Science

Run-Length Encoding Example

How would you describe the colors in this image?

Computer Science

Which letters occur most?

slide-13
SLIDE 13

10/11/12 13

Computer Science

Huffman Encoding

Why should the character “X" and "z" take up the same number of bits as "e" or " "? Huffman codes use variable-length bit strings to represent each character. More frequently used letters have fewest bits.

Computer Science

Huffman Encoding Example

ballboard would be

1010001001001010110001111011

  • Encoded is 28 bits vs 144 bits

with Unicode; The compression ratio is 28/144 or 0.39 Try to encode roadbed

slide-14
SLIDE 14

10/11/12 14

Computer Science

Huffman Encoding

Prefix Property No character's bit string is the prefix of any

  • ther character's bit string.

To decode look for match left to right, bit by bit

record letter when a match is found begin next character where you left off

Computer Science

Huffman Encoding Example

Decode

  • 1011111001010

Try it!

slide-15
SLIDE 15

10/11/12 15

Computer Science

Huffman Encoding

The technique for creating codes guarantees the prefix property of the codes. There is no single “Huffman code” -- each depends

  • n the application. Two types of Huffman codes:
  • general, based on use of letters in English, Spanish, ….
  • specialized, based on text itself or specific types of text

Computer Science

JPEG Images

The Joint Photographic Experts Group created the standard codec in 1992.

  • Default format on most digital cameras
  • Compression reduces file size up to 90% with

little loss in visible quality

  • Great for life-like images the web

Is JPEG compression lossy or lossless?

slide-16
SLIDE 16

10/11/12 16

Recall: RGB Colorspace

=

RGB has a lot of redundant information.

JPEG uses the YCbCr Colorspace

Y (luminance) Cb (chroma blue) Cr (chroma red)

slide-17
SLIDE 17

10/11/12 17

Computer Science

YCbCr Colorspace

YCbCr separates luminance (brightness) from chroma (color differences).

  • Our eyes are more sensitive to changes in luma

than to changes in chroma.

JPEG thus stores a high resolution Y, and lower-resolution CbCr.

  • Smoothing colors among adjacent pixels.

Computer Science

JPEG Color Smoothing

This image is progressively more less smoothed from left to right (lowest to highest quality).

slide-18
SLIDE 18

10/11/12 18

Computer Science

JPEG Compression

  • Convert color space from RGB to YCbCr
  • Smooth adjacent colors in Cb/Cr
  • Apply Huffman compression after smoothing

Which part of this is lossy compression?

Computer Science

PNG Image Format

Portable Network Graphics (PNG) 24-bit bitmapped color. Uses lossless compression.

2-stage compression process:

  • 1. Filter image by adjacent pixels,

record the differences

  • 2. Use the DEFLATE algorithm

(Huffman encoding scheme)

slide-19
SLIDE 19

10/11/12 19

Computer Science

Same Picture, Many Formats

Original image was 2048 * 1536 pixels All others are 300 * 400 pixels

Computer Science

slide-20
SLIDE 20

10/11/12 20

Computer Science

What You Learned Today

  • Vector graphics vs raster graphics
  • Factors in image file size
  • Resolution, color depth
  • Compression strategies
  • Reduce colors
  • Reduce resolution
  • Lossless compression
  • Lossy compression
  • GIF, PNG, and JPEG Format Images

Computer Science

Announcements and To Do

  • HW06 due Tuesday 10/16
  • Readings:
  • Wong ch 3, pp 66-86 (today)
  • Wong ch 4, pp 102-117 (next week)
slide-21
SLIDE 21

10/11/12 21

Computer Science

Image Manipulation Software

There are many different image manipulation software packages, e.g.:

  • Paint – Built in to windows
  • Photoshop – Commercial software
  • Irfanview – Free software
  • Paintshop Pro – Shareware/Commercial
  • GIMP – Free software