next up previous contents
Next: Colour images Up: Lecture 4: Images in Previous: Lecture 4: Images in   Contents

Greyscale images

IDL has routines to read in most of the standard image formats:

File type Extension IDL routine
Microsoft Bitmap .bmp read_bmp
Portable Network Graphics .png read_png
JPEG .jpg or .jpeg read_jpeg
Macintosh PICT .??? read_pict
Portable Bit/grey/pixelmap .pbm .pgm .ppm read_ppm
Sun Raster format .??? read_srf
Tagged Image File Format .tiff or .tif read_tiff
X11 Bitmap files .xbm read_x11_bitmap
X Windows Dump (from xwd) ??? read_xwd

1For greyscale images, most of these require a file name and return a 2-d array of type Byte, but the syntax is not the same for all of these routines - see the manual for details. As an example, try this:

IDL>
read_jpeg,'~jmarsham/public_html/Teaching/IDL_course/200407101200_MSG1_EVEB70.jpeg',meteosat

This has read in a greyscale image from the file met.png and put it in a variable called Meteosat. If we do

 
IDL> help,meteosat
we find that Meteosat is a 900 X 900 array of type byte. The basic command in IDL to display an image is tv. If we do this:
tv,meteosat
then we can see the image, but there are a couple of problems. The image is not the same size as the window and it looks a bit washed out. We can fix the first problem by shrinking the image and opening a window of the right size - you can open a 450 X 450 pixel window like this:
 
IDL> meteosat=rebin(meteosat,450,450)
IDL> window,0,xsize=450,ysize=450,retain=2

This provides us a nice example for writing our own procedure to open a window of the correct size for an image and then to display the image in it.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pro showimage,image,window=window
; A short procedure to display an image in a window the same size as
; the image. The program refuses to display an image which is
; uselessly small. This is important as opening a window 0 pixels wide
; causes IDL to hang up.

; the if not keyword_set() is really useful for setting defaults for keywords
if not keyword_set(window) ne 1 then window=0

siz=size(image)
if(siz(1) gt 4  and siz(2) gt 4 ) then begin
    
    window,window,xsize=siz(1),ysize=siz(2)
    tv,image

endif else print,'Image too small'

end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Now we can display our image with:

IDL> showimage,meteosat,win=2

The image is still dark and lacks contrast in some areas though. If it was on the television, you would turn up the contrast. We can see why if we plot a histogram of the image:

 
IDL> plot,histogram(meteosat)

We're only using 20-170ish out of 256 colours (i.e. greys). We can use the bytscl function to use the full range.

IDL> tv,bytscl(meteosat)
and this shows its histogram:
plot,histogram(bytscl(meteosat))
we are now using the full 0-255 range. bytscl will rescale any array to 0-255. Contrast in some areas can be improved using,
tv,hist_equal(meteosat)
plot,histogram(hist_equal(meteosat))
show's its histogram - note it has been ``flattened''.

IDL has a few built-in functions for image processing. There is a boxcar smoother:

IDL> tv,bytscl(smooth(meteosat,9))
This can be used to reduce noise in an image. The second argument is the size of the box used. If you set this to 9, then each pixel in the new image is the average of the pixels in a 9 X 9 square in the original image. Be aware that there are better ways to smooth an image (e.g. median - look it up). There are two edge-detecting functions, called Roberts and Sobel, presumably after the inventors of the algorithms used:
IDL> tv,roberts(meteosat)
IDL> tv,sobel(meteosat)

One other handy is

IDL> rdpix,meteosat
left click on the image to read values and right click to finish (note you can view one image and rdpix another).

That just about covers what you need to know to get started with greyscale images. The next part looks at displaying colour images.


next up previous contents
Next: Colour images Up: Lecture 4: Images in Previous: Lecture 4: Images in   Contents
John Marsham 2005-04-22