0 0 1 2 3 4 5 6 7 8 9 0 1 1 1 2 4 1 1 2 3 3 1 1 2 4 2 1 1 3 5 2 1 1 4 6 1 1 4 7 Rows Are Numbered 0 - 7 and Columns 0 - 9

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

BORDER (CONTOUR) FOLLOWING PROCEDURE

0 0 1 2 3 4 5 6 7 8 9
The pixels highlighted in yellow
0 0 0 0 0 0 0 0 0 0 0
are the boundary pixels. The
1 0 0 0 0 4 1 1 0 0 0 pixels highlighted in green are
the neighbors surrounding the
2 0 0 0 4 1 0 1 2 0 0
highlighted border pixel. For
3 0 0 3 1 0 0 0 1 2 0 tracing the outer boundary, you
4 0 0 2 1 0 0 0 1 3 0 travel the neighbors in a
clockwise direction until you
5 0 0 0 2 1 0 1 4 0 0
find a ‘1’ in the path. That ‘1’
6 0 0 0 0 1 1 4 0 0 0 will be the new boundary pixel.
7 0 0 0 0 0 0 0 0 0 0 You repeat the process until you
come to the starting 01
Rows are numbered 0 - 7 and columns 0 - 9
transition. The starting 0 -1
1) Scan the image left to right starting at (0, 0) pixels have light blue highlights.
2) If no 0 1 transition, move to next row Border pixels are then labelled 1
3) Continue this scanning until you get a 0 1 (Row 1, Col
through 4. 3->4)
Let (id, jd) be the location of ‘0’ and (ic, jc) the location of ‘1’. (id, jd) = (1, 3) and
(ic, jc) = (1, 4) are the row and column numbers of ‘0’ and ‘1’.
Let k1 = ic-id and k2 = jc-jd. Then starting at (id, jd) we derive the
coordinates of the eight neighbors in a clockwise direction. In this specific
case, k1 = 0 and k2 = 1. If k2 = 1, then we go up and move in a clockwise
direction. Then the eight neighbors are
(1, 3), (0, 3), (0, 4), (0, 5), (1, 5), (2, 5), (2, 4), (2, 3)
If k2 = -1 then you go down. Also only k1 or k2 can be 1 or -1.
4) Find the coordinates of the eight neighbors surrounding the ‘1’
5) Starting at the ‘0’, go around the neighbors in a clockwise direction until you
get another 0 1 transition.
Repeat 4) and 5) until you come to the starting point.
Note: If ‘0’ is on the left same row go up. If ‘0’ is on the right in the same row go
down. If ‘0’ is above ‘1’ on the same column, go right. If ‘0’ is below ‘1’ on the
same column, go left. It is that simple.
If the new border pixel is to the E of the previous one, label it as ‘1’. If it is SE or
NW, label it as ‘2’. If it is S or N, label it as ‘2’. If is SW or NE, label it as’4’.
E – East, S = South, N – North, SE – Southeast, SW – Southwest, NE –
Northeast, NW – Northwest.
1-E
4-SW 3-S 2-SE
k1= ic-id, k2 = jc-jd (Four Possibilities)
x1 x2 x3 x5 x6 x7 x7 0 x1 x3 x4 x5
0 1 x4 x4 1 0 x6 1 x2 x2 1 x6
x7 x6 x5 x3 x2 x1 x5 x4 x3 x1 0 x7
0 on the left 0 on the right 0 above 0 below
k1 = 0, k2 = 1 k1 = 0, k2 = -1 k1 = 1, k2 = 0 k1 = -1, k2 = 0
------------------------------------------------------------------------------------------------------------
Calculation of neighbor coordinates

k1 = 0, k2 = ±1 Horizontal k1 = ±1, k2=0 Vertical

x[0] y[0] x[0] y[0]

x[1] = x[0]-k2 y[1] = y[0] x[1] = x[0 y[1] = y[0]+k1

x[2] = x[1] y[2] = y[1]+k2 x[2] = x[1]+k1 y[2] = y[1

x[3] = x[2] y[3] = y[2]+k2 x[3] = x[2]+k1 y[3] = y[2]

x[4] = x[3]+k2 y[4] = y[3] x[4] = x[3]+k2 y[4] = y[3]-k1

x[5] = x[4]+k2 y[5] = y[4] x[5] = x[4]+k2 y[5] = y[4]-k1

x[6] = x[5] y[6] = y[5]-k2 x[6] = x[5]-k1 y[6] = y[5]

x[7] = x[6] y[7] = y[6]-k2 x[7] = x[6]-k1 y[7] = y[6]

Combine the two cases to simplify programming


k1= ic-id, k2 = jc-jd Calculation of neighbor coordiantes
x1 x2 x3 x5 x6 x7
k1 = 0, k2 = ±1 or k1 = ±1, k2=0
0 1 x4 x4 1 0
x7 x6 x5 x3 x2 x1 x[0] y[0]
0 on the left 0 on the right
x[1] = x[0]-k2 y[1] = y[0]+k1
k1 = 0, k2 = 1 k1 = 0, k2 = -1
----------------------------------------------------- x[2] = x[1]+k1 y[2] = y[1]+k2
x7 0 x1 x3 x4 x5
x[3] = x[2]+k1 y[3] = y[2]+k2
x6 1 x2 x2 1 x6
x5 x4 x3 x1 0 x7 x[4] = x[3]+k2 y[4] = y[3]-k1

0 above 0 below x[5] = x[4]+k2 y[5] = y[4]-k1


k1 = 1, k2 = 0 k1 = -1, k2 = 0
x[6] = x[5]-k1 y[6] = y[5]-k2
===================================================================
0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1 1 1 0 0
0 0 1 1 1 1 1 1 1 1 0 0
0 0 1 1 1 1 1 1 1 1 0 0
0 0 1 1 0 0 0 0 0 0 0 0
0 0 1 1 0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1 1 1 0 0
0 0 1 1 1 1 1 1 1 1 0 0
0 0 1 1 0 0 0 0 0 0 0 0
0 0 1 1 0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1 1 1 0 0
0 0 1 1 1 1 1 1 1 1 0 0
0 0 1 1 1 1 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0
Using neighborhood 0 – 1 transitions, find the outer contour of the image.
Border following procedure
1. Read image using my program as a guide
2. All pixels that are 255 are reset to 1.
3. Set up a nested loops to scan image row by row
4. Start scan and check for 0 to 1 transition.
5. Store coordinates (row and column numbers) as (ic, jc)
6. Enter (ic, jc) as ibr[0] and ibr[1] – note ibr and ibc are arrays that will store
all the boundary pixels. I suggest a size of 500 for the two arrays ibr and
ibc
7. Label the 0 before 1 as (id, jd)
8. Call function nabor(ic, jc, id, jd, nx, ny)
9. Note that nx and ny are arrays of size 8
10. Note that nx[0] = id and ny[0] = jd.
11. Use the formulas above to calculate the remaining seven neighbors nx[1],
nx[2], …,nx[7] and ny[1], ny[2], …, ny[7]
12. Set up a loop to check which of the seven neighbors is 1
13. Store the coordinates of the 1 in an array and label this 1 as (ic, jc) and the
previous 0 as (id, jd)
14. Go back to step 7 and repeat this process
15. Stop when ic = ibr[0] and jc = ibc[0]
To create a list file that contains information on the location of images in a folder
do the following:
1) Start Command Prompt
2) Go to the directory containing the images
3) If you want a list of image locations for the digit ‘4’ type the following
command:
dir /B /O /S *4.pgm >RCR_PGM_4.lst
4) The file RCR_PGM_4.lst will be a text file that contains the location of all
images that belong to the digit ‘4’

You might also like