Tutorial > Working with Images > Lesson 1: Displaying and Processing Images
  

Lesson 1: Displaying and Processing Images
Images, which are 2D arrays, are easily visualized in PV-WAVE and can be processed just like any other array. PV-WAVE also contains many procedures and functions specifically designed for image display and processing. In this lesson, you’ll do the following:
* Reading an Image
* Displaying an Image
* Changing the Size of an Image
* Contrast Enhancement
* Smoothing and Sharpening
* Other Image Manipulations
* Extracting Profiles
Reading an Image
First, import an image to be processed. Reading data files into PV-WAVE is easy if you know the format in which the data is stored. Often, images are stored as arrays of bytes.
To read an image, do the following:
1. Open the file for reading by entering:
OPENR, 1, FILEPATH('head.img', SubDir='data') 
 
note
OPENR opens the file named in quotes and assigns it to the specified logical unit number. Here you assign the file head.img to unit number 1. Unit numbers can range from 1 to 128. The FILEPATH function, used as an argument to OPENR, returns the full path for the file head.img located in the PV-WAVE data directory.
2. The image you read is a 512-by-512-element array of bytes, so you will create a 512-element square array variable called head_LS by entering:
head_LS = BYTARR(512, 512)
3. Read the image into the variable head_LS and close the file head.img by entering:
READU, 1, head_LS
CLOSE, 1
Displaying an Image
The default window size is 640-by-512 pixels.
To display an image, do the following:
1. Since the image is 512-by-512, create a custom-sized window for the display:
WINDOW, XSize=512, YSize=512
The window appears in the upper right corner.
2. Now return to the default colortable, B-W Linear, and display the image by entering:
LOADCT, 0
TV, head_LS
The image appears in the window.
 
note
The TV command writes an array to the display as an image without scaling. TVSCL scales the values of the image array into the range of available colors.
3. To display the image with its values scaled to use the entire colortable, enter:
TVSCL, head_LS
The image uses the entire set of colortable values.
Changing the Size of an Image
Two commands commonly used to expand or shrink image sizes are CONGRID and REBIN.
CONGRID
*Uses nearest neighbor interpolation
*Faster than REBIN
*Can produce final dimensions of arbitrary size
*Uses bilinear interpolation when the Interp keyword is specified
REBIN
*Uses bilinear interpolation
*Takes longer than CONGRID
*Produces higher quality results
*Produces final dimensions that are integral multiples or factors of the original
To change the size of an image, do the following:
1. Use the CONGRID function to create an enlarged image:
head_LSbig = CONGRID(head_LS, 768, 768)
2. Now you need a larger window:
WINDOW, XSize=768, YSize=768
TVSCL, head_LSbig
Work with smaller images now so that you can display six windows simultaneously.
3. Make the image smaller and display it in a smaller window. Enter:
b = CONGRID(head_LS, 384, 384)
WINDOW, 0, XSize=384, YSize=384
TVSCL, b
4. Load a colortable and display the image:
LOADCT, 15 & TV, b
The TV and TVSCL commands can also accept array expressions as arguments.
5. For example, enter the command:
TV, b*1.5 
Each element of b is multiplied by 1.5 and the result is sent to the display. The data in variable b remains unchanged.
Contrast Enhancement
Thresholding is one of the simplest contrast enhancements that can be performed on an image. Thresholding produces a two-level mapping from all of the possible intensities into black and white.
 
note
The PV-WAVE relational operators, EQ, NE, GE, GT, LE, and LT, return a value of 1 if the relation is true and 0 if the relation is false. When applied to images, the relation is evaluated for each pixel and an image of ones and zeros is created. These operators are:
*EQ—Equal
*GE—Greater than or equal to
*GT—Greater than
*LE—Less than or equal to
*LT—Less than
*NE—Not equal
To enhance the contrast of an image, do the following:
1. Add a new window:
WINDOW, 1, XSize=384, YSize=384
2. To display the pixels in the image b that have values greater than 110 as white and all others as black, enter:
TVSCL, b GT 110
3. Similarly, you can display the pixels that have values less than 110 as white by entering the command:
TVSCL, b LT 110
Another way to enhance the contrast of an image is to scale a subrange of pixel values to fill the entire range of displayed brightness. The PV-WAVE maximum operator > (greater than), returns a result equal to the larger of its two parameters.
4. Use the maximum operator to scale pixels with a value of 110 or greater into the full range of displayed brightness. Type:
TVSCL, b > 110
This highlights certain aspects of the neural tissue.
 
note
The LT and GT operators display the pixels in either black or white, whereas the < and > operators display the pixels in the full range of brightness.
5. To scale pixels with a value less than 110 into the full range of brightness, use the PV-WAVE minimum operator < (less than). Enter:
TVSCL, b < 110
This highlights the air spaces, soft tissue, and vascular areas.
Similarly, you can set the minimum brightness to 40, set the maximum brightness to 160, scale the image and display it by entering:
TVSCL, b > 40 < 160
The neural portion is very bright.
6. Although this command illustrates the use of the PV-WAVE minimum and maximum operators, you can execute the same function more efficiently with the command:
c = BYTSCL(b, Min=40, Max=160, Top=!D.N_Colors)
TVSCL, c
Notice that this image looks exactly like the previous one.
7. Add another window:
WINDOW, 2, XSize=384, YSize=384
In many images, the pixels have values that are only a small subrange of the possible values. By spreading the distribution so that each range of pixel values contains an approximately equal number of members, the information content of the display is maximized. The HIST_EQUAL function performs this redistribution on an array.
8. Create and retain a histogram-equalized image in a new variable called h:
h = HIST_EQUAL(c)
9. Display h by entering:
TV, h
The displayed image appears much brighter, but with less detail than c.
Smoothing and Sharpening
The SMOOTH function is typically used to:
*Remove ripples, spikes or high frequency noise
*Blur an image or set of data so that only the general trends can be seen
*Isolate the lower spatial frequency components
*Soften sharp transitions from one color to another in a colortable
Images can be rapidly smoothed to soften edges or compensate for random noise in an image using PV-WAVE’s SMOOTH function. This function performs an equally-weighted smoothing using a square neighborhood of a specified odd width, which is called boxcar averaging; the MEDIAN function finds the median value of an array or applies a median filter of a specified width.
MEDIAN smoothing replaces each point with the median of the 1D or 2D neighborhood of the given width, effectively eliminating high and low values without blurring any edges.
To smooth and sharpen an image, do the following:
1. Display a median smoothed image of c with a 1D neighborhood of 3:
TVSCL, MEDIAN(c, 3)
SMOOTH uses a boxcar average. If the first boxcar width you try does not give you the results you expected, try a different width. Boxcar widths should always be odd numbers.
2. Display the image smoothed using a 3-by-3 neighborhood by entering:
TVSCL, SMOOTH(c, 3)
3. Try a wider neighborhood:
TVSCL, SMOOTH(c, 7)
This image looks a bit blurred and contains only the low frequency components of the original image.
4. Now display the image smoothed using a 5-by-5 neighborhood by entering:
TVSCL, SMOOTH(c, 5)
5. Add a new window:
WINDOW, 3, XSize=384, YSize=384
 
note
Often, an image needs to be sharpened so that edges or high spatial frequency components of the image are enhanced. One way to sharpen an image is to subtract a smoothed image containing only low-frequency components from the original image. This technique is called unsharp masking.
6. To unsharp mask and save as a new variable, enter:
smoothed = FIX(c - SMOOTH(c, 5))
Recall that c was created by displaying only values in the range 40–160. This command subtracts a smoothed version of the image c from the original c and saves it as a variable called smoothed. The FIX command converts byte data to integer data.
7. Display the image, enter:
TVSCL, smoothed
8. It is also possible to subtract a smoothed version of the original image (b) from c. Enter:
smoothed = FIX(c - SMOOTH(b, 5))
9. Display the image, enter:
TVSCL, smoothed
10. Add a new window:
WINDOW, 4, XSize=384, YSize=384, XPos=385, YPos=350
11. Re-display the original image and compare it to the other images:
TVSCL, b
 
note
PV-WAVE has other built-in sharpening functions that use differentiation to sharpen images. The ROBERTS and SOBEL functions are edge enhancement generators. The ROBERTS function returns the Roberts gradient of an image.
12. Try the ROBERTS function by entering:
r = ROBERTS(c)
TV, r
13. The prism colortable, colortable number 6, shows the results better. Load it by entering:
LOADCT, 6
TVSCL, r
The convoluted tissue is displayed primarily in red.
 
note
Another commonly-used gradient operator is the SOBEL operator. PV-WAVE’s SOBEL function operates over a 3-by-3 region, making it less sensitive to noise than some other methods.
14. Display a SOBEL-sharpened version of the image:
so = SOBEL(c)
TVSCL, so
Other Image Manipulations
Sections of images are easily displayed by using subarrays.
1. Create a new window:
WINDOW, 5, XSize=400, YSize=400, XPos=385, YPos=0
2. Create a new array that contains just the 100-by-100 pixels of image b with the cerebellum in it and display it by entering:
d = b(210:309, 130:229)
TV, d
The image appears in the lower-left corner of the new window.
3. Load the standard gamma-II colortable:
LOADCT, 5
The displayed image is small, so you can “magnify” the image to an arbitrary size using bilinear interpolation with the REBIN command.
 
note
REBIN allows you to scale each dimension by an integer factor.
4. Make each dimension of d four times its current size and display the result by entering:
e = REBIN(d, 400, 400)
TV, e
The image has, in effect, been magnified four times.
5. To make this image the same size as the others while still using bilinear interpolation, use the CONGRID function with the Interp keyword:
f = CONGRID(d, 384, 384, /Interp)
6. Change the size of the window:
WINDOW, 5, XSiz=384, YSiz=384, XPos=385, YPos=0
7. Display f:
TVSCL, f
 
note
Simple rotation in multiples of 90 degrees can be accomplished with the ROTATE function.
8. Rotate the magnified image by 90 degrees by entering:
g = ROTATE(f, 1)
The second parameter of ROTATE is an integer from 1 to 8 that specifies which one of the eight possible combinations of rotation and axis reversal to use.
9. Display g:
TV, g
The image is rotated 90 degrees counterclockwise.
10. Improve the image by using TVSCL to display it.
TVSCL, g
Using PV-WAVE Colortables
The predefined colortables offer a variety of ways to view the data. Try the following colortables and compare the results.
 
note
Use the up arrow () key to recall commands, then edit the colortable number to rapidly cycle through the colortables.
*LOADCT, 0
*LOADCT, 1
*LOADCT, 2
*LOADCT, 3
*LOADCT, 4
*LOADCT, 5
*LOADCT, 6
*LOADCT, 7
*LOADCT, 8
*LOADCT, 10
*LOADCT, 11
*LOADCT, 12
*LOADCT, 14
*LOADCT, 15
*LOADCT, 16
*LOADCT, 17
*LOADCT, 18
The WDELETE function enables you clear the screen by removing several of the windows you created in this lesson:
WDELETE, 0 & WDELETE, 1 & WDELETE, 2 & WDELETE, 3 & WDELETE, 4 
All the windows except the window 5 close.
Extracting Profiles
Another useful image processing tool is the Standard Library routine PROFILES. This routine draws row or column profiles of an image, and it allows you to simultaneously view an image and an x-y plot of the pixel brightness in any row or column of the image.
1. Use the PROFILES routine with the rotated image that you just displayed by entering:
PROFILES, g
A new window for displaying the profiles appears.
2. Move the pointer into window 5 (the one containing the image g) to display the profiles of different rows and columns.
3. While the pointer is in window 5, press the left mouse button to switch between displaying row and column profiles.
4. Exit the PROFILES routine by clicking the right mouse button while the pointer is in the image window (5). The PROFILES window closes.
5. Now delete window 5:
WDELETE, 5
More Information on Image Processing
For more information on image display and image processing, see the PV‑WAVE User’s Guide. To see an alphabetical listing of image display and image processing procedures and functions see the PV‑WAVE Reference.

Version 2017.1
Copyright © 2019, Rogue Wave Software, Inc. All Rights Reserved.