June 14, ‘99 COLORS IN MATLAB
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
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
RGB COLOR CUBE Cyan Green White 1 Yellow Color is represented by a point in the RGB cube Blue 1 1 Red Magenta
RGB CUBE
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
HUMAN COLOR MODEL The most accurate model here is hue, saturation and value(brightness) HSV white brightness saturation green red blue hue black
NET REFERENCE ON COLOR There is a comprehensive FAQ on color. You can reach it at http://www.inforamp.net/~poynton/Poynton- colour.html
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. black 1 1 magenta
FEW MATLAB’s COLORMAPS Image intensity maps to color
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?
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
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
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)
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)
COLOR ASSIGNMENT How is intensity connected to color? last row highest Colormap Data Values maps to lowest first row
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.
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
Try it! Interpret colors you see
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
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 b Z a
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
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?
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))
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 ?
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.
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
OVERLAYING CONTOURS Do the following: get rid of the grid lines(try shading command) Superimpose black colored gridlines(lookup help on contour)
pcolor and colormap The following will display 32 bins of current colormap z=[1:32;1:32]’ pcolor(z)
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
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
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
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
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
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?
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
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