Results 1 to 14 of 14

Thread: Getting recursion error in loop!

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Getting recursion error in loop!

    I have a recursion loop that calls itself as a form of flood-fill. It calls itself until no more pixels need changing. It works great, however I get this error in the middle of my run. How do I fix this?


    Error:
    An unhandled exception of type 'System.StackOverflowException' occurred in System.Drawing.dll



    Heres the code
    Code:
        Private Sub recurrence(ByVal x, ByVal y)
            myBitmap.SetPixel(x, y, Color.FromArgb(150, 150, 150, 150))
            PictureBox1.Refresh()
            If x < blobs(1, ii) Then blobs(1, ii) = x
            If x > blobs(2, ii) Then blobs(2, ii) = x
            If y < blobs(3, ii) Then blobs(3, ii) = y
            If y > blobs(4, ii) Then blobs(4, ii) = y
    
            Application.DoEvents()
            If myBitmap.GetPixel(x + 1, y) = Color.FromArgb(255, 255, 255) Then recurrence(x + 1, y)
            If myBitmap.GetPixel(x - 1, y) = Color.FromArgb(255, 255, 255) Then recurrence(x - 1, y)
            If myBitmap.GetPixel(x, y + 1) = Color.FromArgb(255, 255, 255) Then recurrence(x, y + 1)
            If myBitmap.GetPixel(x, y - 1) = Color.FromArgb(255, 255, 255) Then recurrence(x, y - 1)
        End Sub
    The DoEvents() and the Refresh() are to slow it down and view whats going on.

    I found it only does this when a large area needs filling. ALOT of smaller areas do fine, so it's not total loops, just total loops in that single fill run.
    Last edited by MotoMan_Yz400; Aug 10th, 2007 at 04:00 PM.

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Getting recursion error in loop!

    Change your code such that it is NOT using recursion.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: Getting recursion error in loop!

    I feel as if i need it though. How else could i go about this?

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Getting recursion error in loop!

    What is the blobs array? and what is the variable ii? what is x and y? And what's the result/effect that you're looking for? Keep in mind that we know nothing about your project other than what you tell or show us.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: Getting recursion error in loop!

    Sorry, the main loop runs through an image reading each pixel along the way (in a 1bpp image). from 0 to width and from 0 to height (so it scans the whole image). when it reaches a white pixel, it sends the x and y of the white pixel to the recurrence sub. II is simply the the blob count (the recurrence sub runs fully once for each blob, but loops within itself if there are still unread white pixels in the blob). I use the array blob, to keep the min and max of x and y for white dot on the screen (each white dot is 30-100 pixels in dia and are irregularly shaped, so i call them blobs.) So the recurrence() sub just runs through every pixel in the white blobs in an 1bpp image, and records its limits of each individually. and once recurrence is finished with every white pixel in the blob, resumes the scanning of the image for more white blobs, until it has read every pixel in the image then it quits. However when the recurrence() gets into a large blob, where there are alot of looping, it overflows, and errors.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: Getting recursion error in loop!

    Anyone know a solution for this? If I cant get this to work, my program is basically trashed!

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Getting recursion error in loop!

    There's no need to utilize recursion at all... You just loop thru the bitmap and read each pixel's value; if it is a white pixel, set it to a new color.
    Code:
    'bm is the bitmap you're working on
     For i As Integer = 0 To bm.Width - 1
                For j As Integer = 0 To bm.Height - 1
                    If bm.GetPixel(i, j) = Color.FromArgb(255, 255, 255) Then
                        bm.SetPixel(i, j, Color.FromArgb(155, 155, 155))
                    End If
                Next
            Next

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: Getting recursion error in loop!

    Sorry, i'm horrible at explaining problems.

    Here's a sample image. (just imagine this as a B/W 24bpp image, not a blurred grayscale like this one)


    Now it will cycle threw, and when it gets on white "blob", it stops, sends the X and Y cords from the first white pixel it gets to the recursion loop. The loop stays in that white blob, recording the physical extends of the white blob (changes the color of each pixel read). Once done with that blob, recursion loop has finished with every pixel in that blob, and the cycling of the image for any white pixels is continues. here's the complete code (guess i should have posted it in the beginning) This is VERY sloppy. I wasnt going for styling points, or cleanliness on this one. Just wanted to hurry and get the concept working.

    Code:
    Public Class Form1
        Dim myBitmap As New Bitmap("c:\lighttest5.bmp")
        Dim pixelcolor As Color
        Dim x1 As Integer = 1000
        Dim x2 As Integer = 0
        Dim y1 As Integer = 1000
        Dim y2 As Integer = 0
        Dim blobs(4, 10) As Integer
        Dim ii As Integer = 0
        'Public Function GetPixel(ByVal x As Integer, ByVal y As Integer) As Color
        'End Function
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim xx As Integer = 0
            Dim yy As Integer = 0
            Dim i As Integer = 0
            For i = 1 To 10
                blobs(1, i) = 1000
                blobs(2, i) = 0
                blobs(3, i) = 1000
                blobs(4, i) = 0
    
            Next
            ' Create a Bitmap object from an image file.
    
            PictureBox1.Image = myBitmap
            ' Get the color of a pixel within myBitmap.
            Application.DoEvents()
            'For i = 1 To 100
            For yy = 0 To myBitmap.Height - 1 Step 5
                For xx = 0 To myBitmap.Width - 1 Step 5
    
                    pixelcolor = myBitmap.GetPixel(xx, yy)
                    If pixelcolor = Color.FromArgb(255, 255, 255) Then
                        ii = ii + 1
    
                        recurrence(xx, yy)
                        Application.DoEvents()
                        For i = 1 To 5
                            For xxx As Integer = blobs(1, ii) To blobs(2, ii)
                                myBitmap.SetPixel(xxx, blobs(3, ii), Color.FromArgb(255, 0, 0))
                                myBitmap.SetPixel(xxx, blobs(4, ii), Color.FromArgb(255, 0, 0))
                            Next
                            For yyy As Integer = blobs(3, ii) To blobs(4, ii)
                                myBitmap.SetPixel(blobs(1, ii), yyy, Color.FromArgb(255, 0, 0))
                                myBitmap.SetPixel(blobs(2, ii), yyy, Color.FromArgb(255, 0, 0))
                            Next
                            PictureBox1.Refresh()
                        Next
                    End If
                Next
            Next
            MsgBox("Done!")
        End Sub
    
    
        Private Sub recurrence(ByVal x, ByVal y)
            myBitmap.SetPixel(x, y, Color.FromArgb(150, 150, 150, 150))
            PictureBox1.Refresh()
            If x < blobs(1, ii) Then blobs(1, ii) = x
            If x > blobs(2, ii) Then blobs(2, ii) = x
            If y < blobs(3, ii) Then blobs(3, ii) = y
            If y > blobs(4, ii) Then blobs(4, ii) = y
    
            Application.DoEvents()
            If myBitmap.GetPixel(x + 1, y) = Color.FromArgb(255, 255, 255) Then recurrence(x + 1, y)
            If myBitmap.GetPixel(x - 1, y) = Color.FromArgb(255, 255, 255) Then recurrence(x - 1, y)
            If myBitmap.GetPixel(x, y + 1) = Color.FromArgb(255, 255, 255) Then recurrence(x, y + 1)
            If myBitmap.GetPixel(x, y - 1) = Color.FromArgb(255, 255, 255) Then recurrence(x, y - 1)
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            For b As Integer = 1 To 5
                For a As Integer = -3 To 3
                    myBitmap.SetPixel((((blobs(2, b) - blobs(1, b)) / 2) + blobs(1, b) + a), Math.Round(((blobs(4, b) - blobs(3, b)) / 2) + blobs(3, b)), Color.FromArgb(255, 255, 255, 255))
                    myBitmap.SetPixel(((blobs(2, b) - blobs(1, b)) / 2) + blobs(1, b), (((blobs(4, b) - blobs(3, b)) / 2) + blobs(3, b) + a), Color.FromArgb(255, 255, 255, 255))
                Next
                PictureBox1.Refresh()
            Next
        End Sub
    End Class
    PS- dont worry about commandbutton2, it's just there to show the center points of each blob.

  9. #9
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Getting recursion error in loop!

    Try this approach... I used a colormap object to change the color of the image... It is much much faster than looping thru the image pixel by pixel
    Code:
    Private oldColor As Color = Nothing
        Private newColor As Color = Nothing
        Private bm As New Bitmap("c:\test.bmp")
    
        Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
            If (Not IsNothing(Me.oldColor)) AndAlso (Not IsNothing(Me.newColor)) Then
                ChangeColor(e.Graphics, bm, oldColor, newColor)
            Else
                'Draw the original image on the picturebox
                e.Graphics.DrawImage(bm, New PointF(0, 0))
            End If
        End Sub
    
        Private Sub ChangeColor(ByVal g As Graphics, ByVal bm As Bitmap, ByVal oldColor As Color, ByVal newColor As Color)
            Dim cm(0) As System.Drawing.Imaging.ColorMap
            cm(0) = New System.Drawing.Imaging.ColorMap()
            cm(0).OldColor = oldColor
            cm(0).NewColor = newColor
            Dim attr As New System.Drawing.Imaging.ImageAttributes
            attr.SetRemapTable(cm)
            Dim rect As New Rectangle(0, 0, bm.Width, bm.Height)
            g.DrawImage(bm, rect, 0, 0, bm.Width, bm.Height, GraphicsUnit.Pixel, attr)
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Me.oldColor = Color.Black
            Me.newColor = Color.Blue
            Me.PictureBox1.Refresh()
        End Sub

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: Getting recursion error in loop!

    The goal of the program is not to change a color in a picture. It's to locate each white dot, and find it's location on the screen. See I'm using this in a multi-touch input project. The computer will see a white dot for each finger (input) in the screen. So it needs to know where these white dots are on an X and Y plain. I'm using the fill method bc once it reaches a white pixel it loops threw the white dot and it remains in each white dot as it changes the colors of the pixels (symbolizing it has "processes" that pixel), recording the the physical constraints of that white dot. That way i may take those constraints and figure where on X and Y this dot lyes. It needs to do this for each white dot in the image, and record the constraints individually. So if i have 5 white dots, it will return 5 - X and Y cords designating the center of each white dot. Another program i saw in java uses the "walk-around" method that walks a pointer around the dot until it returns to it's starting location. I would use this method, however I'm having trouble grasping how it is done since i dont know java. So this fill method, I do know how to do easily.


    Stanav, I really do appreciate the help your giving me, i know your trying... I'm just having a hard time explaining exactly my issue, and what i'm looking for i guess.

    This is the output image of my program now. This is just a test image, the real images will be read from a webcam in real time. So the location, size, and quantity of white blobs will change. See when the white blobs are small, it doesnt go into a recursion error. However when the blobs are larger they spring up that error.

    Last edited by MotoMan_Yz400; Aug 13th, 2007 at 04:15 PM.

  11. #11
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: Getting recursion error in loop!

    do you still get this error when doevents is removed?
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: Getting recursion error in loop!

    Quote Originally Posted by Lord Orwell
    do you still get this error when doevents is removed?
    Yeah i know whats causing the issue, just not sure how to solve it. See when the recursion loop calls itself, it in a form duplicates it's self. Each duplicate of the sub is stored in memory. If the sub calls itself too many times, it fills up the mem, and boom. I'm just not sure how to solve it.

  13. #13
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: Getting recursion error in loop!

    i have used the exact same function myself with no problems. So i was suggesting that maybe you were calling it from a timer or something and the function was being launched numerous times.
    You could probably do a "walk around" like you suggested but see if this small change makes any difference:
    Code:
    Private Sub recurrence(ByVal x, ByVal y)
       If myBitmap.GetPixel(x + 1, y) = Color.FromArgb(150, 150, 150) Then  exit sub       
    myBitmap.SetPixel(x, y, Color.FromArgb(150, 150, 150, 150))
            PictureBox1.Refresh()
            If x < blobs(1, ii) Then blobs(1, ii) = x
            If x > blobs(2, ii) Then blobs(2, ii) = x
            If y < blobs(3, ii) Then blobs(3, ii) = y
            If y > blobs(4, ii) Then blobs(4, ii) = y
    
            Application.DoEvents()
            If myBitmap.GetPixel(x + 1, y) = Color.FromArgb(255, 255, 255) Then recurrence(x + 1, y)
            If myBitmap.GetPixel(x - 1, y) = Color.FromArgb(255, 255, 255) Then recurrence(x - 1, y)
            If myBitmap.GetPixel(x, y + 1) = Color.FromArgb(255, 255, 255) Then recurrence(x, y + 1)
            If myBitmap.GetPixel(x, y - 1) = Color.FromArgb(255, 255, 255) Then recurrence(x, y - 1)
        End Sub
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: Getting recursion error in loop!

    Quote Originally Posted by Lord Orwell
    i have used the exact same function myself with no problems. So i was suggesting that maybe you were calling it from a timer or something and the function was being launched numerous times.
    You could probably do a "walk around" like you suggested but see if this small change makes any difference:

    No it didnt work. It overloaded my blob array. It needs to finish the the sub routine so any "If this then exit sub" wont work.... I'm just stuck... i guess I'm scrapping the idea for now until i have another 1:00AM epiphany and the answer comes to me in a dream... wow that sounded highly religious

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width