1999 bg mobasseri1 1/5/2016 june 14, ‘99 colors in matlab
TRANSCRIPT
1999 BG Mobasseri 1 04/21/23
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
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
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
1999 BG Mobasseri 5 04/21/23
RGB CUBE
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
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
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
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
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
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?
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
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
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)
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)
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
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.
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
1999 BG Mobasseri 19 04/21/23
Try it!
Interpret colors you see
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
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
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
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
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))
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
?
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
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
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
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
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
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
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
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
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
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?
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
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