UWA logo

School of Computer Science
& Software Engineering

 

Computer Vision CITS4240

Laboratory 1


The aim of this lab session is to introduce you to grey-tone image manipulation techniques such as brightness and contrast adjustment, gamma correction and histogram equalisation.

These grey-tone image manipulation techniques are available on most image manipulation packages, but few people really know how to use these operations effectively.

The on-line version of this lab sheet is available from the course web page at http://undergraduate.cs.uwa.edu.au/units/CITS4240/
(Go the the School's web page at http://www.csse.uwa.edu.au/, click on 'Undergraduate', then click on 'CITS4240 Computer Vision'). It is suggested that you use the on-line version of this lab sheet as this will give you access to the active links within the lab sheet. All subsequent lab sheets will only be distributed via the web and will not be provided in printed form.


The laboratory work will be done in the Linux environment. If you are new to using Linux I suggest that you quickly work through an introduction to using Linux before going further.


Our Images Database

Our shared directory for the course is /cslinux/examples/CITS4240. In it you will find an Images directory. You can also access the images from the course web page. Here's a link to the images

You will find a variety of images here, extra images will be added from time to time during the course. Some are from a database of facial images and some have been taken from the WA CrimeStoppers web page.

As you do not have write permissions on this directory I suggest that you create an Images directory in your own area and copy any images that you want to work on from the shared area to your own. Please do this with some discretion as we do not want the disc filled with excessive copies of identical images.

When you download images from the Images page make sure you get the full image and not just the thumbnail that is displayed on the page. Either click on the thumbnail to see it full size version and do a [File | Save As], or do a right-click on the thumbnail and then select 'Save Link Target As...' to obtain the full size image.

Using XV

XV is an image display and manipulation program written by John Bradley at the University of Pennsylvania. Invariably you will find it on all computers running the Unix operating system and it is included in standard Linux distributions.

XV has a large number of 'controls' which will allow us to illustrate most of the common image grey-tone manipulation techniques.

In an xterm window type 'xv' (run it in background by typing xv &). A window will appear with the default xv image.

Click the right mouse button on this window, this will bring up the main xv control panel.

Click on the 'Windows' button in the middle-top of the control panel and select 'Visual Schnauzer'. This will bring up a window that will be reminiscent of a standard Microsoft Windows window. You will need to repeatedly double-click on the 'Parent' folder until you get to the root directory ('/'), then you will need to find your way down through the cslinux, examples, CITS4240 and Images directories. Note that you can also get to these images (in a non-visual way) via the load button on the main control panel.

Choose an image that interests you and load it by simply double-clicking on its icon.

You may want to increase the size of the image. On the main xv control panel click on the 'Image Size' button and select, say, 'Double Size'.

Interrogating the Image

To find out more about the image click on the 'Windows' button and select 'Image Info'. This will bring up a window presenting some data about the image.

You can 'interrogate' individual pixels in the image by positioning the cursor at a point in the image and pressing the middle mouse button (this may be the central roller button, or the mouse may be configured such that you have to simultaneously press the left and right buttons to 'simulate' a middle button press). An information line will be displayed at the top or bottom of the image. As you move the mouse around (with the middle button pressed) you will see these values change. It might look something like this:

                                          saturation
             red     green  blue              |  
             value   value  value       hue   |   value
                 \     |    /             \   |   /
       49, 153 = 115, 115, 115  #737373   (0  0  43  HSV)  [2 1]
       /     \                  /  |  \                      |
  column     row            red, green, blue values      position of mouse
  number     number         expressed in hexadecimal     relative to where
                                                         you first clicked
                                                         in the image

If the image is a greyscale image (no colour) the red, green and blue values will be identical (as in the example above). Each of the red, green and blue values can range from 0 to 255. Black will be represented by the values 0 0 0 and white by the values 255 255 255.

The information line also displays the colour in terms of Hue, Saturation and Value. This is often a more intuitive representation of colour. Hue represents the colour's location on the colour circle (red, orange, yellow, green, indigo, blue violet, and back to red again). Saturation represents how 'solid' the colour is - say, a deep red vs a pale pastel pink. Value represents how dark or light the colour is - say, dark red vs light red. In this case where the image is a greyscale image the Hue and Saturation values are 0 and the Value represents the grey level.

Modifying Grey Values in the Image

Click on the 'Windows' button on the main xv control panel and select 'Color Editor'. This will bring up a (complex) window with many controls.

In the top left of this window there may be the message "No colormap information in 24 bit mode". If this is the case go back to the main xv control panel, click on the '24/8 Bit' button and select '8-bit mode'.

Back in the Color Editor window click on the 'Grey' button (it's on the left hand side). This will convert your image to a greyscale image with no colour. (We do not want to be concerned with the complexities of colour at this stage).

The thing that is of main interest to us is the 'intensity' graph at the middle-bottom of this window.

This shows how intensity values in the image are mapped to the display. You look up an original image value along the bottom axis of the graph, travel up until you hit the line, then travel left until you hit the left axis. The value on the left axis becomes the new value for that intensity. In the default case the graph is a diagonal line, and does nothing - an intensity value of 1 is mapped to 1, 2 to 2, 3 to 3 etc. But you can do many interesting things by modifying this curve. Here is an example of a curve that enhances contrast in the dark portions of an image and suppresses contrast in the light regions.
You can click on the small squares on the intensity curve and drag them around. Notice how the image changes.

Modify the curve to do the following:

Gamma
The last two operations can be more conveniently done via the 'gamma' control. Click on the 'gamma' button on the bottom-right of the intensity graph, enter a value and see what it does. Experiment with values greater than 1, between 0 and 1, and less than 0.

The function used for gamma adjustment is

y = 255 x (i / 255)1/G
where: i is the input grey value (0-255), G is the gamma value, and y is the output grey value (0-255).
The input value is divided by 255 to give a number between 0-1. This is then raised to the power 1/G to give a new number (but which is still between 0-1). Finally this number is multiplied by 255 to restore its range to 0-255.

Brightness and Contrast
On the left hand side of the Colour Editor Window you will see the 'Brite' and 'Dim' buttons. These increase and decrease the brightness of the image by shifting the intensity curve up or down respectively. The 'Sharp' and 'Dull' buttons increase and decrease the contrast of the image by increasing/decreasing the slope of the intensity curve.

Image Normalisation
On occasions you may have an image containing pixel values that only cover a limited range, say, 50-100 rather than the full range of 0-255. When you view such an image it will appear murky with little contrast. To overcome this one can rescale all the pixel values so that they cover the full range 0-255, this is known as normalisation. The 'Norm' button on the Colour Editor Window will do this for you. Try it, the intensity graph will display the mapping that xv used to normalise the image. (It is possible that your image contains values covering the full range (0-255), in this case the 'Norm' operation will do nothing).

Algorithms
On the xv main control panel is an Algorithms menu button. Here you will find a number of image operations that you can perform. Experiment with the edge detection. Ideally the final output of edge detection is a binary image indicating edge points and non-edge points. To do this you will need to threshold the edge detected image by manipulating the intensity profile. You will notice that it can be hard to find an appropriate threshold level.

Histogram Equalisation
In many cases much of the information in an image can be concentrated within a small range of grey values within the image. This can be identified by looking at the grey-level histogram of the image. The histogram simply plots (as a bar graph) how many times each grey value occurs in the image. Here is an image and its histogram.

       

In this image you can see that most of the grey values are in the range 50-120.

It can be useful to construct an intensity mapping curve that seeks to expand the contrast of the range of grey values that occur most frequently, and conversely compress the contrast of the range of grey values that occur infrequently. Ideally, this process produces an image with a uniform histogram where every grey value occurs with equal frequency in the image. In practice, with a discrete number of grey values, this can only be approximately achieved.

The mathematics behind constructing an intensity mapping curve that produces a uniform histogram will be covered later in the course. In the meantime all you have to do is click on the 'HistEq' button on the left of the Colour Editor Window. You will notice the intensity mapping curve change to the curve that theoretically equalises the histogram.

Here is the image above after histogram equalisation, and its corresponding histogram. Note that in a real image, which has a discrete number of possible grey levels, one can only achieve an approximation to a uniform histogram.

       

Histogram equalisation can be useful, but you will find many occasions where it does not help, especially on images that have few distinct grey values.

Getting started with MATLAB

MATLAB is a computing environment for high-performance numerical computation and visualization. MATLAB stands for matrix laboratory and its basic data structure is a matrix. Many numerical problems, especially those typical to linear algebra, can be solved in a fraction of the time it would take under more traditional environments such as C. Work through the MATLAB tutorial by Griffiths or Fosdick et al to build up some familiarity with MATLAB. In particular, you should get yourself familiar with its basic syntax, matrix and vector operations, MATLAB scripts, and input and output.

Copy a few images including the image 'shapes.png' into your own Images directory.
Invoke MATLAB (simply by typing matlab &).
It may take a little while for MATLAB to start up. So be patient. See how to speed up the invocation of MATLAB.

The Image Processing Toolbox

The Image Processing Toolbox is a collection of image processing functions, ranging from image display through to filtering, analysis, and image transforms. Most of these functions are written as M-files, that is they consist of a series of MATLAB statements that implement the function. You can view the MATLAB code by using the statement

  type function_name
Try this for the MATLAB function hist, which plots the histogram of a vector of data. If you simply type
   help function_name
MATLAB responds by typing out the first contiguous block of comments in the script. Thus, document your own scripts sensibly with a block of comments at the top in a way that conforms with MATLAB style and makes it easy for others to use your code.

To find out more about any command, say, imread type "help imread".
"help images" will give you a list of all the image related commands within MATLAB. You can also browse the MATLAB Help Desk, which provides tutorials, supports navigation and search of MATLAB functions.

MATLAB also has a built-in editor, which is useful for editing your own MATLAB functions for future Laboratory exercises. Together with the built-in debugger, the task of debugging your MATLAB functions inside the MATLAB editor becomes a less painful one. To invoke the MATLAB built-in editor, simple select [File | Open...] to open an existing MATLAB function file or select [File | New] to start a new MATLAB function file.

You can import images into MATLAB using a number of standard tools. The one you will be most interested in is imread. The function imread attempts to infer the format of the file from its contents, and reads a number of types of images, including png format. Thus, the command

   im = imread('shapes.png');
will return the image im.

To determine the number of rows and columns in this image use the MATLAB function size, which returns the dimensions of the matrix im. The command size(im) without the terminating semi-colon returns the values of the function to the screen. If the image you have loaded is colour, then size(im) will return the size of a 3-dimensional array, with the last dimension having the value 3 for the red, green, and blue bands of the image.

You can now view your image with

  imshow(im);   % Note: You may need to use colormap(gray).

To simplify the display of images a small function called show.m has been provided by Dr Peter Kovesi. (You may have to scroll down within the web page to find show.m -- there seems to be a bug in Mozilla and Konquerer in the way anchor points are handled). The show.m function 'wraps up' all the commands to display an image, set its colormap and set the axes into the one function. Do a [File | Save As...] to copy show.m to your own directory.

Calculate and display the histogram of this image using

   imhist(im);

Note: If the image you have loaded is colour, then imhist will complain, as it only works with greyscale images. You could generate a greyscale version of the image using

   g = rgb2gray(im);

Decide on a suitable threshold to produce a binary image containing simple connected objects. You can threshold your image at this value to obtain a black and white image, BW, using MATLAB's 'greater than' logical operator with the following command:

   BW = im > 150;           % threshold the image at a grey value of 150
This creates an image of 0s or 1s depending on whether each pixel in the image is greater than 150 or not. Display the image and adjust the threshold if needed.

The function bwarea will calculate the area of this object. Likewise, you can use sum(sum(BW)).

Portfolio Requirements

Nothing from this lab sheet is required for submission. Your portfolio should include lab exercises from Laboratory 2 onward.


Return to Computer Vision 412