Analyzing Image Shapes
After using a morphological operation to expose the basic elements within an image, it is often useful to then extract and analyze specific information about those image elements. The following examples use the LABEL_REGION function and the CONTOUR procedure to identify and extract information about specific image objects.
The LABEL_REGION function labels all of the regions within a binary image, giving each region a unique index number. Use this function in conjunction with the HISTOGRAM function to view the population of each region. See Using LABEL_REGION to Extract Image Object Information in the following section for an example.
The CONTOUR procedure draws a contour plot from image data, and allows the selection of image objects occurring at a specific contour level. Further processing using PATH_* keywords returns the location and coordinates of polygons that define a specific contour level. See Using CONTOUR to Extract Image Object Information for an example.
Using LABEL_REGION to Extract Image Object Information
The following example identifies unique regions within the image of the Rhinosporidium seeberi parasitic protozoans and prints out region populations. Complete the following steps for a detailed description of the process.
Example Code
See labelregionexample.pro in the examples/doc/image subdirectory of the IDL installation directory for code that duplicates this example. Run the example procedure by entering labelregionexample at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT labelregionexample.pro.
- Prepare the display device and load a grayscale color table:
- Select and open the image file:
- Get the image dimensions and add a border (for display purposes only):
- Get the dimensions of the padded image, create a window and display the original image:
- Create a large, circular structuring element to extract the large circular foreground features. Define the radius of the structuring element and create the disk:
- Apply the opening operation to the image to remove background noise and display the image:
- Display an intensity histogram to use as a guide when thresholding:
- Retain only the brighter, foreground pixels by setting the threshold intensity at 170 and display the binary image:
- Identify unique regions using the LABEL_REGION function:
- Use the HISTOGRAM function to calculate the number of elements in each region:
- Create a FOR loop that will return the population and percentage of each foreground region based on the results returned by the HISTOGRAM function:
- Load a color table and display the regions. For this example, use the sixteen level color table to more easily distinguish individual regions:
- Create a new window and display the individual region populations by graphing the values of hist using the SURFACE procedure:
file = FILEPATH('r_seeberi.jpg', $
SUBDIRECTORY = ['examples','data'])
READ_JPEG, file, image, /GRAYSCALE
dims = SIZE(image, /DIMENSIONS) padImg = REPLICATE(0B, dims[0]+20, dims[1]+20) padImg[10,10] = image
dims = SIZE(padImg, /DIMENSIONS) WINDOW, 0, XSIZE = 2*dims[0], YSIZE = 2*dims[1], $ TITLE = 'Opened, Thresholded and Labeled Region Images' TVSCL, padImg, 0
radius = 5 strucElem = SHIFT(DIST(2*radius+1), radius, radius) LE radiusTip
EnterPRINT, strucElemto view the structure created by the previous statement.
openImg = MORPH_OPEN(padImg, strucElem, /GRAY) TVSCL, openImg, 1This original image (left) and opened image (right) appear in the following figure.
WINDOW, 2, XSIZE = 400, YSIZE = 300 PLOT, HISTOGRAM(openImg)Note
Using an intensity histogram as a guide for determining threshold values is described in the section, Determining Intensity Values for Threshold and Stretch.
FOR i=1, N_ELEMENTS (hist) - 1 DO PRINT, 'Region', i, $ ', Pixel Popluation = ', hist(i), $ ' Percent = ', 100.*FLOAT(hist[i])/(dims[0]*dims[1])
LOADCT, 12 TVSCL, regions, 3In the following figure, the image containing the labeled regions (right) shows 19 distinct foreground regions.
Tip
Display the color table by entering XLOADCT at the command line. By viewing the color table, you can see that region index values start in the lower-left corner of the image. Realizing this makes it easier to relate the region populations printed in the Output Log with the regions shown in the image.
Using CONTOUR to Extract Image Object Information
It is possible to extract information about an image feature using the CONTOUR procedure. The following example illustrates how to select an image feature and return the area of that feature, in this case, calculating the size of a gas pocket in a CT scan of the thoracic cavity. Complete the following steps for a detailed description of the process.
Example Code
See extractcontourinfo.pro in the examples/doc/image subdirectory of the IDL installation directory for code that duplicates this example. Run the example procedure by entering extractcontourinfo at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT extractcontourinfo.pro.
Note
For more information on computing statistics for defined image objects see Working with Regions of Interest (ROIs)
- Prepare the display device and load a color table:
- Determine the path to the file:
- Initialize the size parameters:
- Import the image from the file:
- Create a window and display the image:
- Create another window and use CONTOUR to display a filled contour of the image, specifying 255 contour levels which correspond to the number of values occurring in byte data:
- Use the PATH_* keywords to obtain information about the contours occurring at level 40:
- Using the coordinate information obtained in the previous step, use the PLOTS procedure to draw the contours of image objects occurring at level 40, using a different line style for each contour:
- The specified contour is drawn with a dashed line or LINESTYLE number 2 (determined by looking at "Graphics Keywords" in Appendix B of the IDL Reference Guide). Use REFORM to create vectors containing the x and y boundary coordinates of the contour:
- Set the last element of the coordinate vectors equal to the first element to ensure that the contour area is completely enclosed:
- This example obtains information about the left-most gas pocket. For display purposes only, draw an arrow pointing to the region of interest:
- Output the resulting coordinate vectors, using TRANSPOSE to print vertical lists of the coordinates:
- Use the POLY_AREA function to compute the area of the polygon created by the x and y coordinates and print the result:
WINDOW, 2 CONTOUR, image, /XSTYLE, /YSTYLE, NLEVELS = 255, $ /FILLNote
ReplaceNLEVELS = 255withNLEVELS = MAX(image)if your display uses less than 256 colors.
CONTOUR, image, /XSTYLE, /YSTYLE, LEVELS = 40, $ PATH_INFO = info, PATH_XY = xy, /PATH_DATA_COORDSThe PATH_INFO variable, info, contains information about the paths of the contours, which when used in conjunction with PATH_XY, traces closed contour paths. Specify PATH_DATA_COORDS when using PATH_XY if you want the contour positions to be measured in data units instead of the default normalized units.
FOR i = 0, (N_ELEMENTS(info) - 1) DO PLOTS, $ xy[*, info[i].offset:(info[i].offset + info[i].n - 1)], $ LINESTYLE = (i < 5), /DATA
x = REFORM(xy[0, info[2].offset:(info[2].offset + $ info[2].n - 1)]) y = REFORM(xy[1, info[2].offset:(info[2].offset + $ info[2].n - 1)])
ARROW, 10, 10, (MIN(x) + MAX(x))/2, COLOR = 180, $ (MIN(y) + MAX(y))/2, THICK = 2, /DATAThe gas pocket is indicated with an arrow as shown in the following figure.
PRINT, '' PRINT, ' x , y' PRINT, [TRANSPOSE(x), TRANSPOSE(y)], FORMAT = '(2F15.6)'The FORMAT statement tells IDL to format two 15 character floating point values that have 6 characters following the decimal of each value.



