This is not an infinite loop in this case, but it can be very deep. From my understanding of your code, you're not just setting the adjacent pixels but also their adjacent pixels, and the adjacent of the adjacent pixels and so on.
For instance with an image of 1000*1000, if I call setAttachedPixels(image, 0, 999, XX)
, you will end up with 999 calls for just the down left pixel.
If you just want to add the pixel and its adjacent you should use something like:
private void SetPixels(Image<Gray, byte> source, int X, int Y, byte locationID)
{
if (X >= 0 && Y >= 0 && X < source.Rows && Y < source.Cols)
{
if (source.Data[X, Y, 0] == 1) // 1 means unprocessed - locationID starts at 2 and increments elsewhere
{
source.Data[X, Y, 0] = locationID; //mark origin pixel
}
}
}
private void SetAttachedPixels(Image<Gray, byte> source, int X, int Y, byte locationID)
{
setPixels(source, X, Y, locationID);
setPixels(source, X+1, Y-1, locationID); //down left pixel
setPixels(source, X+1, Y, locationID); //down
setPixels(source, X+1, Y+1, locationID); //down right
setPixels(source, X, Y - 1, locationID); //left
setPixels(source, X, Y + 1, locationID); //right
setPixels(source, X - 1, Y - 1, locationID);//up left
setPixels(source, X - 1, Y, locationID); //up
setPixels(source, X - 1, Y + 1, locationID); //up right
}