Lab Practise on Segmentation
This week we are going to get familiar with segmentation. To fresh your memory,
here are some points from the lecture note on the definition of segmentation.
To distinguish objects from background
To divide the image into regions or segments, each of which is in some sense
homogeneous, but the union of adjacent segments is not homogeneous in the
Homogeneity here is characterized by some properties like
o smoothly varying intensity, similar statistics, or colour.
There are a number of segmentation programs in Nick Efford’s package in chapter 10.
You can read through these programs as well as the handout that I have given to you
on segmentation. These are very good references for helping you with the task 4 in the
coursework assignment. You can choose whatever method you are mostly
comfortable with for your coursework or design your own. Here let’s look at one of
the programs that we have already talked about in the lecture, “region growing”
The algorithm of region growing is as below (from Nick’s book, pp263):
Let f be an image for which regions are to be grown
Define a set of regions, R1, R2,…, Rn, each consisting of a single seed pixel
for i =1 to n, do
for each pixel, p, at the border of Ri, do
for all neighbours of p do
Let x, y be the neighbour’s coordinates
Let ui be the mean grey level of pixels in Ri
if the neighbour is unassigned and |f(x,y) – ui| <= delta (threshold) then
Add neighbour to Ri
until no more pixels are being assigned to regions
When you read the program, you may be interested to find where and how each
sentence in this algorithm is implemented.
Step1 In NetBeans, open “Open RegionGrower.java ” which is located in the
For help on how to open file in NetBeans, please see the worksheet given in week 4
(lab-instruction-start.doc or lab-instruction-start.pdf)
Double click on the name RegionGrower in the file system, you will see the original
code on the right in the code editor window.
Step 2 Right-click in the Source Editor (code Editor window) and choose from the
“Margin” submenu, choose “show line numbers”, so that you can see the line
numbers of the code, which are used for reading reference for the rest of this
Please try to read through the whole class as much as you can. The followings are
only to help you gain a bit more understanding on some important parts in the class.
In class RegionGrower,
Line 50-68 are to define the types of neighbourhood surrounding a pixel. A 4-
neighbourhood contains only the pixels above, below, to the left and to the right of the
central pixel, An 8-neighbourhood contains all the pixels of a 4-neighbourhood, plus
four diagonal neighbours. These will be used by member variable “private Point
delta”, which is used to located neighbourhood coordinates when growing region.
Line 74, sourceImage to represent the image to be segmented. (see the comment lines
in the code)
Line 77-105, read through those member variables, in particular “List seedpixels”
and “connectivity” and “squareThreshold”.
Line 88, to define a Boolean variable as a checker for each pixel to mark whether a
pixel has been assigned to a region or not. Once a pixel has been assigned to a region,
then its checker assigned[pixel.y][pixel.x] = true;
Line 89 border[y][x] is to record current pixel at [x,y] is a border of a region.
According to the algorithm, the growing processing only happens at the border pixels
(for pixels that don’t belong to a border and have been assigned to a region, there is
no need to process them any more).
Line 97, 98, regionSum and regionMean is to keep the sum and mean of the current
growing region as a measure of whether a new pixel should belong to this region
based on the distance of the intensity of the new pixel and the mean of the region. (see
line306 and line 390.)
Line 284 is the key function is grow()
Line 301, 302, nx, ny, are coordinates of neighbourhood candidates that possibly
belong to the region
Line 303, is to check whether this neighbour has been assigned to a region yet, if not,
Line 305, 306, check the distance between its pixel value and the mean of the region
intensity is smaller than a threshold.
If yes, then
Line 308-line312 are to add this pixel to the region, and update the border of the
region (because every time the program only checks the neighbourhood of the border
pixels), and update the statistics (such as the mean of the region intensity). The update
function is in line 406-413.
Try to read the class “RegionGrower” as much as possible so as to get a view of how
the algorithm on region grow is implemented. Nevertheless, the key part is in grow().
Step 3 Use a main program to call RegionGrower class
Follow the steps described in the worksheet given in week 4 (lab-instruction-start.doc
or lab-instruction-start.pdf), open “RegionGrow” in Chap10. Read the comment
section in the beginning of the program on the instruction on how to run the code, eg
Command line arguments are:
input filename, output filename, seed pixel x and y coordinates, connectivity,
test.jpg region.jpg 128 128 4 35
This means you need to input 6 arguments to run this program. This program reads
an image from a file (say test.jpg, or mathgrey.jpg) and grows a region around a
specified seed pixel (here, say 128 128 as the seed x,y coordinate), using either 4- or
8-connectivity and a given threshold (eg, 35) on the difference between a pixel's grey
level or colour and the mean grey level or colour of the region (the program actually
uses square distance, see details in line 140, 306 and 390 in RegionGrower()). The
output image (eg region.jpg) has pixels set to 255 if they belong to the region, or 0
Note: the class RegionGrower can actually grow from a list of seeds, whilst this
program only pass one pixel coordinate as one specified seed pixel.
If you develop your own segmentation program for the coursework, you may wish to
pass more than one seed ( a list of seed) to RegionGrower to segment the regions for
Step 5 Check the result in the output file that you give. The output file is under the
working directory that you have set up in the properties. The result should be another
image (e.g, region.jpg), which is a binary image. For the second part in task4 of
coursework, calculating the centroids of segmented objects (the coordinate of centroid
is the average of all x coordinates and y coordinates along the boundary of the object),
you can write a little program, which reads in this binary image, scan the pixels,
which are correspondent to segmented region, and add all their x coordinates and y
coordinates together respectively, and divide the sum by the pixel numbers, and the
mean of x and y will be the centroid. If you have achieved the work in task1, then
calculating centroid is not that challenging.
Step 6 you can also run ImageViewer in chapter 05 to check the pixel locations of the
image that you are running for segmentation (input the same image for ImageViewer
in order to do so) if you are not sure where to set the seed coordinates.
Step 7 You also can run RegionGrowTool in chapter 10 to get another feel about the
When working on your coursework related to segmentation, it is perhaps very useful
to find some image examples with simple objects in it. “Simple” means within object
or components of object, the colour intensity is relatively even, so that the
segmentation algorithm is less affected by noise and can achieve better results.