YOU ARE DOWNLOADING DOCUMENT

Please tick the box to continue:

Transcript
Page 1: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

Computer Science 121

Scientific ComputingWinter 2014Chapter 14

Images

Page 2: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

Background: Images

● Signal (sound, last chapter) is a single (one-dimensional) quantity that varies over time.

● Image (picture) can be thought of as a two-dimensional quantity that varies over space.

● Otherwise, the computations for sounds and images are about the same!

Page 3: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.1 Black-and-White Images

Page 4: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.1 Black-and-White Images

Page 5: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.1 Black-and-White Images

Page 6: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.1 Black-and-White Images

● Eventually we get down to a single point (pixel, “picture element”) whose color we need to represent.

● For black-and-white images, two basic options– Halftone: Black (0) or white (1); having lots of

points makes a region look gray– Grayscale: Some set of values (2N, N typically = 8)

representing shades of gray.

Page 7: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.1 Black-and-White Images● To turn a b/w image into a grid of pixels, imagine

running a stylus horizontally along the image:

Page 8: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.1 Black-and-White Images● This gives us a one-dimensional function showing

gray level at each point along the horizontal axis:

Page 9: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.1 Black-and-White Images● Doing this repeatedly with lots of evenly-spaced scan

lines gives us a grayscale map of the image:

● Height = grayscale value

● Spacing between lines =

spacing within line

Page 10: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.2 Color● Light comes in difference wavelengths (frequencies):

Page 11: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.2 Color● Like a sound, energy from a given light source (e.g.,

star) can be described by its wavelength (frequency) spectrum : the amount of each wavelength present in the source.

Spectrum of vowel “ee”Frequency (Hz)

Ene

rgy

(dB

)

Spectrum of star ICR3287

Wavelength (nm)

Inte

nsity

Page 12: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.2 Color● Light striking our eyes is focused by a lens and

projected onto the retina.● The retina contains photoreceptors (sensors)

called cones that respond to different wavelengths of light: red, green, and blue.

● So retina can be thought of as a transducer that inputs light and outputs a three-dimensional value for each point in the image: [R, G, B], where R, G, and B each fall in the interval (0,1).

● So we can represent any color image as three “grayscale” images:

Page 13: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.2 Color

=

,R

,G B

Page 14: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.3 Digital Sampling of Images● Same questions arise for sampling images as for

sound:–Sampling Frequency (how often to sample)–Quantization (# of bits per sample)

● With images, “how often” means “how many times per linear unit (inch, cm) – a.k.a. resolution

● Focus on quantization–Each sample is an index into a color map–With too few bits, we lose gradual shading

Page 15: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

Color Maps

● With 8 bits per color, 3 colors per pixel, we get 24 bits per pixel: 224 ≈ 100,000,000 distinct colors

● We can actually get away with far fewer, using color maps● Each pixel has a value that tells us what row in the color map to use● Color map rows are R, G, B values:

Page 16: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

Color Maps>> colormap

ans =

0 0 0.5625 0 0 0.6250 ... 0.4375 1.0000 0.5625 0.5000 1.0000 0.5000 ...

0.5625 0 00.5000

0 0>> size(colormap)

ans = 64 3

Page 17: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

Color MapsMatlab provides us with some default color maps:>> drawMandelbrot([-2.5 2.5], [-2 2])

Page 18: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

Color Maps

>> colormap(hot)

Page 19: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

Color Maps

>> colormap(flag)

Page 20: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

Quantization ProblemsWith too few bits, we lose gradual shading:

5 bits

1 bit

2 bits

3 bits

4 bits

6 bits

7 bits

8 bits

Page 21: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.4 Sampling and Storing Images in Files

• Scanners usually output images in one of several formats– JP(E)G (Joint Photographic Experts Group)– GIF (Graphics Interchange Format)– PNG (Portable Network Graphics)– TI(F)F (Tagged Image File Format)

• As with sound formats, main issues in image formats are compression scheme (how images are stored and transmitted to save space/time) and copyright

• We'll focus on Matlab and mathematical issues....

Page 22: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.4 Sampling and Storing Images in Files

• Matlab imread commands lets us read in files in several formats (automagically figures out which):

>> a = imread('sakidehli.png'); % b/w>> size(a)ans = 269 176>> b = imread('monaLisaLouvre.jpg'); % color>> size(b)ans = 864 560 3

Page 23: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.4 Sampling and Storing Images in Files

• Then use image to display image:

>> image(b)

Page 24: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.4 Sampling and Storing Images in Files

Page 25: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.4 Sampling and Storing Images in Files

• Then use image to display image:

>> image(b)

• imwrite writes it back out:

>> imwrite(b, 'monaLisaCopy.jpg')

Page 26: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.4 Sampling and Storing Images in Files

• Recall formula for sound signal:

P(t) = Σi Ai sin(2πfi t + φi)

• For images, we just add another dimension:

P(x, y) = Σj,k Aj,k sin(2π( fj x + fk y + φj,k))

• This means that the issues/algorithms for images are similar to those for sounds• Aliasing• Frequency transforms, filters (next lecture)

Page 27: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

Spatial Frequency: Intuitive Version

x : ~18 color changes

y : ~

6 co

lor

chan

ges

Page 28: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.4 Sampling and Storing Images in Files

• As with sound, aliasing becomes an issue if sampling frequency is inadequate:

Page 29: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.4 Sampling and Storing Images in Files

• As with sound, aliasing becomes an issue if sampling frequency is inadequate:

Page 30: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.4 Sampling and Storing Images in Files

• As with sound, aliasing becomes an issue if sampling frequency is inadequate:

Page 31: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.4 Sampling and Storing Images in Files

• As with sound, aliasing becomes an issue if sampling frequency is inadequate:

0.04 mm samples

Page 32: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.4 Sampling and Storing Images in Files

• As with sound, aliasing becomes an issue if sampling frequency is inadequate:

0.04 mm samples

0.25 mm samples

Page 33: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.5 Manipulating and Synthesizing Images

• Probably want to use PhotoShop or another image-processing package instead of Matlab.

• But Matlab allows us to see the math behind the processing:

>> b = imread('monaLisaLouvre.jpg');>> red = b(:,:,1);>> green = b(:,:,2);>> blue = b(:,:,3);>> image(red)>> colormap(gray) % just show intensity

Page 34: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.5 Manipulating and Synthesizing Images

>> image(red)

Page 35: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.5 Manipulating and Synthesizing Images

>> image(green)

Page 36: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.5 Manipulating and Synthesizing Images

>> image(blue)

Page 37: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.5 Manipulating and Synthesizing Images

• Put them back together with cat:>> image(cat(3, red, green, blue))

Page 38: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.5 Manipulating and Synthesizing Images

• Put them back together with cat:>> image(cat(3, red, green, blue))

Page 39: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.5 Manipulating and Synthesizing Images

>> max(max(a/3))ans = 68>> image(68-a/3), colormap(gray)

• Make a negative:

Page 40: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.5 Manipulating and Synthesizing Images

>> max(max(a/3))ans = 68>> image(68-a/3), colormap(gray)

• Make a negative:

Page 41: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

14.6 Example: Image Restoration

• Mona Lisa has faded and yellowed over 500 years.

• Can we get an idea of what it looked like originally?

• Basic idea: Balance out R, G, B

Page 42: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

>> a = imread('monaLisaLouvre.jpg');>> mona = image2double(a); % kaplan>> min(min(min(mona)))ans = 0>> max(max(max(mona)))ans = 1>> r = mona(:,:,1);>> hist(r(:), 50) % histogram

Page 43: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

>> a = imread('monaLisaLouvre.jpg');>> mona = image2double(a); % kaplan>> min(min(min(mona)))ans = 0>> max(max(max(mona)))ans = 1>> r = mona(:,:,1);>> hist(r(:), 50) % histogram

Page 44: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.
Page 45: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

>> newr = equalize(r); % kaplan>> hist(newr(:), 50)

Page 46: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

>> newr = equalize(r); % kaplan>> hist(newr(:), 50)

Page 47: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

>> newr = equalize(r); >> newg = equalize(g);>> newb = equalize(b);>> newmona = cat(3, newr, newg, newb);>> image(newmona)

Page 48: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

>> newr = equalize(r); >> newg = equalize(g);>> newb = equalize(b);>> newmona = cat(3, newr, newg, newb);>> image(newmona)

Page 49: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

>> image(newmona*.5 + mona*.5);

Page 50: Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.

Related Documents