graphics and visualisation with matlab part 2dsavas.staff.shef.ac.uk › teaching › matlab ›...

25
GRAPHICS AND VISUALISATION WITH MATLAB Part 2 UNIVERSITY OF SHEFFIELD CiCS DEPARTMENT Deniz Savas & Mike Griffiths March 2012

Upload: others

Post on 31-May-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: GRAPHICS AND VISUALISATION WITH MATLAB Part 2dsavas.staff.shef.ac.uk › teaching › matlab › matlab_4.pdf · •Matlab allows us to re-define the viewing direction of the eye

GRAPHICS AND VISUALISATION WITH MATLAB

Part 2

UNIVERSITY OF SHEFFIELD

CiCS DEPARTMENT

Deniz Savas & Mike Griffiths

March 2012

Page 2: GRAPHICS AND VISUALISATION WITH MATLAB Part 2dsavas.staff.shef.ac.uk › teaching › matlab › matlab_4.pdf · •Matlab allows us to re-define the viewing direction of the eye

Topics

• Handle Graphics

• Animations

• Images in Matlab

Page 3: GRAPHICS AND VISUALISATION WITH MATLAB Part 2dsavas.staff.shef.ac.uk › teaching › matlab › matlab_4.pdf · •Matlab allows us to re-define the viewing direction of the eye

Handle Graphics

• All Matlab graphics output is created in units of graphics objects with associated properties and a tree structured hierarchical dependency. User can manipulate these objects by changing their properties. The following diagram shows all the possible Matlab Graphics Objects and their hierarchy.

Page 4: GRAPHICS AND VISUALISATION WITH MATLAB Part 2dsavas.staff.shef.ac.uk › teaching › matlab › matlab_4.pdf · •Matlab allows us to re-define the viewing direction of the eye

Graphics Objects

Root

Figure

AXESUser-Interface

Control

IMAGE

SURFACERECTANGLE

PATCHLINE

LIGHTS

TEXT

UICONTROL

UICONTEXTMENU

UIMENU

Page 5: GRAPHICS AND VISUALISATION WITH MATLAB Part 2dsavas.staff.shef.ac.uk › teaching › matlab › matlab_4.pdf · •Matlab allows us to re-define the viewing direction of the eye

Graphics Objects

• The Root: Contains everything• Figure: Individual Graphics Window• Uicontrols: User interface icons usually created by GUIDE

utility.• Axes : defines mapping from real coordinates to screen

coordinates• Image: A Matlab image consists of a data-matrix and possibly a

colormap vector.• Light: Defines the position and color of light sources. These

effect patch and surface objects• Line, patch, rectangle : self explanatory• Surface: A surface object is the 3D representation of matrix

data where the element values are assumed to contain the z-coordinates.

• Text: Character strings drawn on the figure. ( labels etc. )

Page 6: GRAPHICS AND VISUALISATION WITH MATLAB Part 2dsavas.staff.shef.ac.uk › teaching › matlab › matlab_4.pdf · •Matlab allows us to re-define the viewing direction of the eye

Querying and Setting Object Properties

• value= get( object_handle, ‘Property Name’ )�• set(object_handle,’Property Name’ ,newvalue)�

• Special symbol gca refers to the current axes object. get(gca) returns all its properties. Having found out what the property names are a subsequent set(gca , ‘Property Name’, newval ) can be used to change a given property.

• Best way of familiarising is by using the property editor after a graph is drawn.

• Similarly gcf for Current Figure Object can be used• Most other object’s handles have to be obtained during the creation by

using the function in the form; handle = function( )�• For example: hplot1 = plot( X, Y );

Page 7: GRAPHICS AND VISUALISATION WITH MATLAB Part 2dsavas.staff.shef.ac.uk › teaching › matlab › matlab_4.pdf · •Matlab allows us to re-define the viewing direction of the eye

Practice Session

• Investigate the tools menu of the figure window. Line, Text and Axis Properties.

• Add a graph label and axis labels.

• Add an arrow pointing to the root and title it.

• Use File-Property Editor to change y axis

• to logarithmic Scaling.

Page 8: GRAPHICS AND VISUALISATION WITH MATLAB Part 2dsavas.staff.shef.ac.uk › teaching › matlab › matlab_4.pdf · •Matlab allows us to re-define the viewing direction of the eye

Practice Session

• Type get(gca) to find out the properties of the

• current-graphics axes.

• set(gca ,’string’ , ‘string’ ) can be used to • set properties via keyboard.

• Investigate saving and reading figures.

Page 9: GRAPHICS AND VISUALISATION WITH MATLAB Part 2dsavas.staff.shef.ac.uk › teaching › matlab › matlab_4.pdf · •Matlab allows us to re-define the viewing direction of the eye

Viewpoint• Matlab allows us to re-define the viewing direction of the eye for

3D plots via the view command.

• Format: view ( azimuth , elevation )

• This view direction is defined by two terms namely;

• Azimuth = Angle the eye makes with the y axis ( in degrees)

• and

• Elevation = angle the eye makes with the x/y plane ( in degrees).

Page 10: GRAPHICS AND VISUALISATION WITH MATLAB Part 2dsavas.staff.shef.ac.uk › teaching › matlab › matlab_4.pdf · •Matlab allows us to re-define the viewing direction of the eye

view position and camera

• [ az el]=view; ---> return the current eye location in two scalars az and el.• view( az , el ) ---> set eye location.• view(3) ---> set to default 3D view• View( az , 90) : directly above ( -90 is directly below).

• Camera Toolbar• In the Figure Window, select the View->Camera Toolbar

option. This will display a set of icons which gives full control to the camera position that controls the view.

Page 11: GRAPHICS AND VISUALISATION WITH MATLAB Part 2dsavas.staff.shef.ac.uk › teaching › matlab › matlab_4.pdf · •Matlab allows us to re-define the viewing direction of the eye

Animations with Matlab• There are three distinct methods for creating

animations;– By using the erase or xor mode of graphics and drawing

and redrawing on the same graph.– By varying the position properties of graphics objects.– By creating movies ( useful for complex pictures)�

Page 12: GRAPHICS AND VISUALISATION WITH MATLAB Part 2dsavas.staff.shef.ac.uk › teaching › matlab › matlab_4.pdf · •Matlab allows us to re-define the viewing direction of the eye

Animation by using the erase modes

Techniques used by Matlab for drawing or redrawing a graphics object is determined by that objects EraseMode property.

By default all objects’ EraseMode property are ‘Normal’. In ‘Normal’ mode when an object’s property that effects its visibility

changes, the object is erased and redrawn automatically, while taking care of all the 3D hidden surface/line aspects. This is what happens when you use the property editor to edit an object.

However this behaviour can be altered by setting the ‘EraseMode’ property of an object to one of;– None: Do not erase the object when it moves or alters.– Background: Erase the object by redrawing it in the background colour– Xor : Erase only the object by Exclusive OR ing the graphics area with the

objects pixels.

Page 13: GRAPHICS AND VISUALISATION WITH MATLAB Part 2dsavas.staff.shef.ac.uk › teaching › matlab › matlab_4.pdf · •Matlab allows us to re-define the viewing direction of the eye

Matlab Movies

• Create the movie as a set of captured frames that can be displayed one after the other thus creating the effect of a movie.

• Commands are : getframe and movieExample:The following code captures the fast forrier transforsm of a series of step functions plots them and stores the plotted output in a structure named M which contains the bit-map data ( as type uint8 matrix and the colour-mapfor k= 1:16plot( fft(eye(k+16) ) ) axis equalM(k) = getframe;

endand the captured frames can now be played back by typing

movie(M)

Page 14: GRAPHICS AND VISUALISATION WITH MATLAB Part 2dsavas.staff.shef.ac.uk › teaching › matlab › matlab_4.pdf · •Matlab allows us to re-define the viewing direction of the eye

Practice Session 8 Animation

• In the examples/animation directory, there is a script called makemov.m

– View this script and run it to save a movie matrix and replay the saved movie by using the moviecommand.• In the same directory study and run the script named

animateroot.m• Study the scripts animpeaks1 and animpeaks2 ,

noting the use of the set function to update the graphics. Use the profiler to compare the two scripts

Page 15: GRAPHICS AND VISUALISATION WITH MATLAB Part 2dsavas.staff.shef.ac.uk › teaching › matlab › matlab_4.pdf · •Matlab allows us to re-define the viewing direction of the eye

Matlab & Images

• Graphics features of Matlab we have seen so far were related to creating of images by mostly vector graphics techniques. We are now going to study the manipulating and displaying of previously captured images.

• Whatever the format, fundamentally images are represented as two or three dimensional arrays with each element representing the colour or intensity of a particular pixel of the picture.

• It would be perfectly valid to represent such image data as ordinary ( double precision) Matlab Matrices but the memory required to store a say 1000 by 1000 image will 8 Mbytes which is too extravagant.

• As most image pixel data will be accurately representable by 8bits, a Matlab data type named uint8 ( unsigned integer) is normally used for image data handling. This reduces the memory requirement to 1/8th !

Page 16: GRAPHICS AND VISUALISATION WITH MATLAB Part 2dsavas.staff.shef.ac.uk › teaching › matlab › matlab_4.pdf · •Matlab allows us to re-define the viewing direction of the eye

Matlab image Types

• A Matlab image consists of a data-matrix and possibly a colourmap matrix. The following three different image types are used;– Indexed Images

– Intensity Images

– RGB (TrueColour) Images

Page 17: GRAPHICS AND VISUALISATION WITH MATLAB Part 2dsavas.staff.shef.ac.uk › teaching › matlab › matlab_4.pdf · •Matlab allows us to re-define the viewing direction of the eye

Indexed Images• These are stored as a two dimensional array of image data + a

colormap matrix.

• The colormap matrix is a m-rows by 3 columns matrix with each of the rows representing a colour defined by the three R,G,B components (ranging 0.0 to 1.0) of that colour. The image data contains indexes to this colour map matrix.

• The colormap is usually stored with the image and saved/loaded by the imwrite/imread functions.

Page 18: GRAPHICS AND VISUALISATION WITH MATLAB Part 2dsavas.staff.shef.ac.uk › teaching › matlab › matlab_4.pdf · •Matlab allows us to re-define the viewing direction of the eye

Practice Session-9An example indexed image

• Type the following commands and inspect the contents of X and map.

• load mandrill• image(X)• colormap(map)• axis off % Remove axis ticks and numbers• axis image % Set aspect ratio to obtain square pixels• Study the contents of X and map

• Investigating an image via its color content

• load flujet ; image(X); colormapeditor• See effects of varying the colormap on the image.

Page 19: GRAPHICS AND VISUALISATION WITH MATLAB Part 2dsavas.staff.shef.ac.uk › teaching › matlab › matlab_4.pdf · •Matlab allows us to re-define the viewing direction of the eye

Intensity Images• Image is a 2 dimensional Matrix whose each element is scaled

by Matlab to produce a colormap index. The colormap index is an m by 3 array as in the indexed images, although this colourmap index is used during display it is not stored as part of the image as it can always be deduced from the data. These image-forms are normally used for gray-scale type images where only the intensity is important, such as x-ray images.

Page 20: GRAPHICS AND VISUALISATION WITH MATLAB Part 2dsavas.staff.shef.ac.uk › teaching › matlab › matlab_4.pdf · •Matlab allows us to re-define the viewing direction of the eye

Intensity Images

• Intensity image is a 2D data matrix, whose elements’ values represent intensity within some range.

• Using the minimum and maximum value stored in that matrix, Matlab applies scaling to determine what intensity a given value implies. It then calculates the colormap index corresponding to this value and uses the colour defined by that index to display that element(pixel).

• The colourmap array is an m by 3 array, similar to the one used in the indexed images. Although this colourmap index is used during display it is not stored as part of the image as it can always be deduced from the data.

• Intensity images are normally used for gray-scale type images where only the intensity is important.

Page 21: GRAPHICS AND VISUALISATION WITH MATLAB Part 2dsavas.staff.shef.ac.uk › teaching › matlab › matlab_4.pdf · •Matlab allows us to re-define the viewing direction of the eye

RGB Images

• Sometimes known as Truecolor images the data is stored as an n-m-3 image. I.e. m by n with 3 planes. These 3 planes represent the R,G and B components of the pixel respectively. There are normally 8 bits allocated to the representation of each colour. Therefore 24 bits represent all 3 colours. Thus making it 2^24=16Million possible colours.

• There is no colormap array for RGB images as all the colourinformation is contained in the data matrix.

Page 22: GRAPHICS AND VISUALISATION WITH MATLAB Part 2dsavas.staff.shef.ac.uk › teaching › matlab › matlab_4.pdf · •Matlab allows us to re-define the viewing direction of the eye

Handling Graphics Images

• Reading/Writing Graphics Images▬ imread , imwrite▬ load , save ( if only the image was saved as a MAT file)�

• Displaying Graphics Images▬ image▬ imagesc▬ Note: For square pixels, use: axis image

• Utilities▬ imfinfo▬ ind2rgb▬

Page 23: GRAPHICS AND VISUALISATION WITH MATLAB Part 2dsavas.staff.shef.ac.uk › teaching › matlab › matlab_4.pdf · •Matlab allows us to re-define the viewing direction of the eye

Practice session-10• Type in these commands:

load earth;image(X);colormap(map) ;

• Find out information about the image file named sunset.jpg in the top level of the exercise directory by using the imfinfo function.

• Read this image into Matlab (Note: use imread )• Display this image on the screen ( use image)• Edit the image figure and investigate its properties.

Page 24: GRAPHICS AND VISUALISATION WITH MATLAB Part 2dsavas.staff.shef.ac.uk › teaching › matlab › matlab_4.pdf · •Matlab allows us to re-define the viewing direction of the eye

Image Processing Exersice

• Exercise: Using Image Processing Tool ( imtool ) to study a photograph of the moon ( Courtesy of Mike Griffiths ) .

We have one of Mike’s amateur astronomy images taken by his telescope during February 2012 in a file named moonshot.jpg .▬ Read this picture into matlab using imread▬ Display it using imshow or image▬ Use imtool to study this image.We can not adjust the contrast as only grayscale images can be altered this

way.▬ Convert this image to grayscale using rgb2gray also make sure to

generate the new colormap by following;• Find the max value of the grayscale image matrix • Using this max define gmap=gray(max) followed by colormap(gmap) when the

image is displayed. ▬ Now use imtool or imcontrast on the new image and adjust the contrasts.▬ We feel that this image can be enhanced by sharpening

use the fspecial(‘unsharp’ ) and imfilter commands to sharpen this image.

Page 25: GRAPHICS AND VISUALISATION WITH MATLAB Part 2dsavas.staff.shef.ac.uk › teaching › matlab › matlab_4.pdf · •Matlab allows us to re-define the viewing direction of the eye

THE END