Question

I understand that stackoverflow exceptions are usually infinite loops, but I've checked my logic and stepped through the code and cannot figure this out. There is no infinite loop because I mark every pixel with locationID as I process it!

    private void setAttachedPixels(ref Image<Gray, byte> source, int X, int Y, byte locationID)  //recursively set a pixel, and all adjacent unmarked pixels, to a certain number
    {
        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
                setAttachedPixels(ref source, X+1, Y-1, locationID); //down left pixel
                setAttachedPixels(ref source, X+1, Y, locationID);  //down
                setAttachedPixels(ref source, X+1, Y+1, locationID);  //down right
                setAttachedPixels(ref source, X, Y - 1, locationID);  //left
                setAttachedPixels(ref source, X, Y + 1, locationID);  //right
                setAttachedPixels(ref source, X - 1, Y - 1, locationID);//up left
                setAttachedPixels(ref source, X - 1, Y, locationID);  //up
                setAttachedPixels(ref source, X - 1, Y + 1, locationID);  //up right
            }
        }
        return;
    }

Code explanation: I'm trying to loop through an image (basically a 2-dimentional array) with some blobs in it and 'count' how many pixels are part of each blob. Hopefully you can see what I'm doing.

Here is a screenshot of the error. It and the stack trace is of no help, but I know someone will ask.

enter image description here

Was it helpful?

Solution

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       
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top