Code:
void main()
{
int i = GetCurrentEdgePoint().x; // sets i to the x-coordinate of the current edge pixel
int j = GetCurrentEdgePoint().y; // sets j to the y-coordinate of the current edge pixel
int i_prev = GetPreviousEdgePoint().x; // sets i_prev to the x-coordinate of the last edge pixel
int j_prev = GetPreviousEdgePoint().y; // sets j_prev to the y-coordinate of the last edge pixel
int i_diff = i - i_prev; // i_diff is the difference between the x-coordinates of the current, and the previous, edge pixels
int j_diff = j - j_prev; // j_diff is the difference between the y-coordinates of the current, and the previous, edge pixels
for (int trial_number = 0; trial_number < 8; trial_number ++)
{
// CvPoint is a structure containing the x and y coordinates of a point.
// Here we set next_diff to the value of the next pixel to try, relative to the current pixel
CvPoint next_diff = GetNextTrial(i_diff, j_diff, trial_number);
// We then add next_diff to the x and y coordinates of the current pixel
int x = i + next_diff.x;
int y = j + next_diff.y;
// Then we test to see whether, at the coordinates (x,y), there lies an edge in the image
if (is_edge(x, y) == true) // is_edge returns true if the pixel at (x, y) is an edge
{
AddCoordinate(cvPoint(x, y));
return 1;
}
}
}
// This function takes the difference in coordinates of the last two edge point, and returns the relative location of the next pixel to be tested.
CvPoint GetNextTrial(int i_diff, int j_diff, int trial_number)
{
switch (i_diff)
{
case -1:
switch (j_diff)
{
case -1:
switch (trial_number)
{
case 0:
return cvPoint(-1, -1);
case 1:
return cvPoint(0, -1);
case 2:
return cvPoint(-1, 0);
case 3:
return cvPoint(1, -1);
case 4:
return cvPoint(-1, 1);
case 5:
return cvPoint(1, 0);
case 6:
return cvPoint(0, 1);
}
case 0:
switch (trial_number)
{
case 0:
return cvPoint(-1, 0);
case 1:
return cvPoint(-1, -1);
case 2:
return cvPoint(-1, 1);
case 3:
return cvPoint(0, -1);
case 4:
return cvPoint(0, 1);
case 5:
return cvPoint(1, -1);
case 6:
return cvPoint(1, 1);
}
case 1:
switch (trial_number)
{
case 0:
return cvPoint(-1, 1);
case 1:
return cvPoint(-1, 0);
case 2:
return cvPoint(0, 1);
case 3:
return cvPoint(-1, -1);
case 4:
return cvPoint(1, 1);
case 5:
return cvPoint(0, -1);
case 6:
return cvPoint(1, 0);
}
}
case 0:
switch (j_diff)
{
case -1:
switch (trial_number)
{
case 0:
return cvPoint(0, -1);
case 1:
return cvPoint(-1, -1);
case 2:
return cvPoint(1, -1);
case 3:
return cvPoint(-1, 0);
case 4:
return cvPoint(1, 0);
case 5:
return cvPoint(-1, 1);
case 6:
return cvPoint(1, 1);
}
case 0:
switch (trial_number)
{
case 0:
return cvPoint(1, 0);
case 1:
return cvPoint(1, 1);
case 2:
return cvPoint(1, -1);
case 3:
return cvPoint(0, 1);
case 4:
return cvPoint(0, -1);
case 5:
return cvPoint(-1, 1);
case 6:
return cvPoint(-1, -1);
case 7:
return cvPoint(-1, 0);
}
case 1:
switch (trial_number)
{
case 0:
return cvPoint(0, 1);
case 1:
return cvPoint(-1, 1);
case 2:
return cvPoint(1, 1);
case 3:
return cvPoint(-1, 0);
case 4:
return cvPoint(1, 0);
case 5:
return cvPoint(-1, -1);
case 6:
return cvPoint(1, -1);
}
}
case 1:
switch (j_diff)
{
case -1:
switch (trial_number)
{
case 0:
return cvPoint(1, -1);
case 1:
return cvPoint(0, -1);
case 2:
return cvPoint(1, 0);
case 3:
return cvPoint(-1, -1);
case 4:
return cvPoint(1, 1);
case 5:
return cvPoint(-1, 0);
case 6:
return cvPoint(0, 1);
}
case 0:
switch (trial_number)
{
case 0:
return cvPoint(1, 0);
case 1:
return cvPoint(1, -1);
case 2:
return cvPoint(1, 1);
case 3:
return cvPoint(0, -1);
case 4:
return cvPoint(0, 1);
case 5:
return cvPoint(-1, -1);
case 6:
return cvPoint(-1, 1);
}
case 1:
switch (trial_number)
{
case 0:
return cvPoint(1, 1);
case 1:
return cvPoint(1, 0);
case 2:
return cvPoint(0, 1);
case 3:
return cvPoint(1, -1);
case 4:
return cvPoint(-1, 1);
case 5:
return cvPoint(0, -1);
case 6:
return cvPoint(-1, 0);
}
}
default:
return cvPoint(0, 0);
}
}
Now, this works absolutely fine. However, it seems a very code-exhaustive way of going about it. Particularly seeing as I now want to test not just the 8 pixels surrounding the current pixel, but the 24 pixels surronding it. Thus, my GetNextTrial() function will be 4 times longer again.