1999 bg mobasseri1 1/5/2016 june 14, ‘99 colors in matlab

37
1999 BG Mobasseri 1 07/04 June 14, ‘99 COLORS IN MATLAB

Upload: philomena-quinn

Post on 19-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 1 04/21/23

June 14, ‘99

COLORS

IN

MATLAB

Page 2: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 2 04/21/23

UNDERSTANDING COLOR

To understand color we must first understand color spaces

We come in contact with color through:– Video(computers,TV’s)– Printed matter(magazines,

brochures)– Real World

Page 3: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 3 04/21/23

ADDITIVE COLOR MODEL

Formation of colors on video monitors is through an additive process of 3 color components– Red– Green – Blue

Concern is white balance, meaning how white is the white displayed on the monitor

Color=w1*red+w2*green+w3*blue

Page 4: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 4 04/21/23

RGB COLOR CUBE

Red

Blue

Green1

1

1

White

Yellow

Cyan

Magenta

Color isrepresentedby a point inthe RGB cube

Page 5: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 5 04/21/23

RGB CUBE

Page 6: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 6 04/21/23

SUBTRACTIVE COLOR MODEL

Color formation in printing industry is through the subtractive CMY model– Cyan– Magenta– Yellow

Challenge is black balance;to get real black

Often black is added as the 4th color, CMYK

Page 7: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 7 04/21/23

HUMAN COLOR MODEL

The most accurate model here is hue, saturation and value(brightness) HSV

white

black

hue

saturation

redgreen

blue

brightness

Page 8: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 8 04/21/23

NET REFERENCE ON COLOR

There is a comprehensive FAQ on color. You can reach it at

http://www.inforamp.net/~poynton/Poynton-colour.html

Page 9: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 9 04/21/23

UNDERSTANDING COLORMAPS

At the heart of color control in MATLAB is the concept of colormap

Colormap is a 3-column matrix with entries between 0 and 1. Each row defines a particular combination of (R,G,B) to define a color.

0 0 0

1 0 1

black

magenta

Page 10: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 10 04/21/23

FEW MATLAB’s COLORMAPS

1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2

5

10

15

20

25

30

HSV(32)

1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2

5

10

15

20

25

30

hot(32)

1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2

5

10

15

20

25

30

flag(32)

1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2

5

10

15

20

25

30

copper(32)

Imag

e in

ten

sit

y m

ap

s t

o c

olo

r

Page 11: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 11 04/21/23

Try it!

MATLAB comes with few images. To load one of them type– load clown

Now check your workspace. You will see image data stored in X and colormap stored in map

To display the image type– image(X)– What’s wrong?

Page 12: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 12 04/21/23

Digital image

Before going on, let’s define a digital image.

A digital image is simply a 2D matrix. Each element of the matrix, pixel, is an integer. For a black and white image pixels are between 0 (black) and 255(white)

MATLAB’s matrix oriented structure makes minced meat out of images and image processing

Page 13: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 13 04/21/23

Role of colormap

Type out a few values of X. You will see integers. But what is the relationship between these numbers and their actual color?

Enter colormap. Colormap is lookup table. It takes an intensity value, say 25, and connects it with some color.

To display true colors, you must set monitor’s colormap to the map used at the time the image was created

Page 14: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 14 04/21/23

WORKING WITH COLORMAPS

Command colormap(map) sets the colormap to one of preset or user defined values

For clown image ,type colormap(map) and watch the result.

Now try these– colormap(hot)– colormap(hsv)– colormap(gray)

Page 15: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 15 04/21/23

RECALLING AND SETTING COLORMAPS

Current colormap in effect can be found by

map=colormap

Any (3x1) matrix can be defined by the user and defined as the current colormap by

colormap(usermap)

Page 16: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 16 04/21/23

COLOR ASSIGNMENT

How is intensity connected to color?

Data Values

Colormap

lowest first row

highest last row

maps to

Page 17: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 17 04/21/23

COLOR MAPPING RULE

Let data range between Zmin and Zmax. Let the colormap rows k run from 1 to m.

Then

Zmin k=1, row 1

Zmax k=m, row m

The middle Z’s follow a linear mapping rule.

Page 18: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 18 04/21/23

Defining your first colormap

Plot the peaks function using – surf(peaks)

Define a colormap, mymap, so that the first row is blue, the second row is green, third row is white and fourth row red.

Then set MATLAB’s colormap to mymap. See code next

Page 19: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 19 04/21/23

Try it!

Interpret colors you see

Page 20: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 20 04/21/23

ARBITRARY COLOR ASSIGNMENT

Color mapping rule is simple:– take the minimum of the function and

assign it the color of the first row of colormap

– take the maximum of the function and assign it the color of last row of colormap

– in-between values take shades of color obtained by a linear mapping of the above two colors

Next slide shows you more

Page 21: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 21 04/21/23

ASSIGNING COLORS TO RANGES OF DATA

This is what we want to do:– assign blue to z<a– assign red to z>b– assign a continuous color tone(blue

to red) for a<z<b

a

b

Z

Page 22: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 22 04/21/23

How to do it? clim property

clim is a two element vector [cmin,cmax]. It can be set as follows– set(gca, ‘clim’,[cmin cmax])

Colormapping is done as follows– z<cmin ---->first row of colormap– z>cmax ---->last row of colormap

Page 23: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 23 04/21/23

Try it!

Plot the peaks function and display it with view(90,0)

Set cmin and cmax to [-2 2],[-3 3] and [-4 4] and see if you can explain the results?

0 5 10 15 20 25 30 35 40 45 50-8

-6

-4

-2

0

2

4

6

8

10

0 5 10 15 20 25 30 35 40 45 50-8

-6

-4

-2

0

2

4

6

8

10

Page 24: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 24 04/21/23

MATLAB’S DEFAULT COLORMAP

MATLAB’s default colormap is HSV and can be set via

colormap(‘default’) or colormap(‘hsv’)

Unless specified otherwise, colormaps are 64x3, i.e. 64 colors

Other sizes can be specified, e.g.

colormap(hot(128))

Page 25: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 25 04/21/23

Try it!

Plot the peaks function in the range(-4:0.2:4) and v=[90,0]. Go through hsv, hot, flag, copper etc. colormaps. Check map’s sizes

?

Page 26: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 26 04/21/23

DEFINING YOUR OWN COLORMAP

Want to display the peaks profile in only two colors. Negative values in red and positive values blue. Define a colormap for this purpose.

0

1020

30

4050

0

10

20

30

40

50-8

-6

-4

-2

0

2

4

6

8

10

Page 27: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 27 04/21/23

Pseudorandom color :pcolor

Command pcolor(z) draws a rectangular grid of cells with colors determined by z; function height above each cell. Mapping from z to color is determined by colormap

5 10 15 20 25 30 35 40 45

5

10

15

20

25

30

35

40

45

5 10 15 20 25 30 35 40 45

5

10

15

20

25

30

35

40

45

Page 28: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 28 04/21/23

OVERLAYING CONTOURS

Do the following:– get rid of the grid lines(try shading

command)– Superimpose black colored

gridlines(lookup help on contour)

5 10 15 20 25 30 35 40 45

5

10

15

20

25

30

35

40

45

Page 29: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 29 04/21/23

pcolor and colormap

The following will display 32 bins of current colormap– z=[1:32;1:32]’– pcolor(z)

1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2

5

10

15

20

25

30

Page 30: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 30 04/21/23

WORKING WITH IMAGES

A two dimensional image can be modeled by z=f(x,y) where (x,y) is the pixel position and z the corresponding height

The height is then mapped to either a grayscale or color range

Page 31: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 31 04/21/23

LOADING IMAGES

Images are loaded like sound file. For example– load clown

Now check your workspace to see which variable is “clown” loaded in.

Check the size of the image array and its min and max

Page 32: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 32 04/21/23

DISPLAYING IMAGES

There are a number of ways to display an image. For the start use– image(z)– z is the array your image is loaded in.

Check

Each element of z specifies the color of a rectangular patch in the image

Elements of z are used as indices into the current colormap to determine the color

Page 33: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 33 04/21/23

INDEXED IMAGES

MATLAB supports a number of image formats one of which is “indexed”

“intensities” in indexed images are not really intensities; they are simply entries into the colormap

If image value at (x,y)=32, this simply means that pixel color at (x,y) comes from the row 32 of the colormap

Page 34: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 34 04/21/23

Example of indexed image: clown

When you loaded clown you got two arrays– X: image data– map:colormap

Now check image value at position (100,100). I get 4. What color is this pixel?. MATLAB goes to the 4th row of map

This row has 3 columns, red=0.29, green=0 and blue=0

Page 35: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 35 04/21/23

How to display the color of a single pixel?

In the previous slide we found out that X(100,100) has no green or blue component and is 29%red. But what color is it?

Here is a way to do get it. Type– image(X(100,100))– Is it what you expect?. Is it reddish?

Page 36: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 36 04/21/23

PLAYING WITH COLORMAPS

Unless a digital image is accompanied by a colormap, it is impossible to determine pixel colors

Check the colormap that accompanies the loaded image; its size and its contents.

Change the colormaps to hot, hsv, cool, gray and see the results

Page 37: 1999 BG Mobasseri1 1/5/2016 June 14, ‘99 COLORS IN MATLAB

1999 BG Mobasseri 37 04/21/23

HOMEWORK

Plot z=sin(sqrt(x^2+y^2)) in the range(-pi to pi)in increments of 0.2– Make the function appear red for

z<-0.5, white for -0.5<z<0.5 and blue for z>0.5

– Do the same except for making the colors between red and white follow 32 gradations of colormap hot