Search code examples
c#image-processingstack-overflowemgucv

How to deal with memory stack overload


I am trying to programm an edge detection method. And I have used emgucv Image class. Since I need gray values I have declared it as

Image<Gray,float> MyImage = new Image<Gray,float>;

I select an image and assign its pixel values into MyImage as

public void selectImage()
          { 
               OpenFileDialog opp = new OpenFileDialog();
              if (opp.ShowDialog() == DialogResult.OK)
              {                 
                   MyImage = new Image<Gray,float>(opp.FileName);
                  InputArray = new Image<Gray, float>(opp.FileName);
                  Convert.ToString(MyImage);
                  pictureBox1.Image = MyImage.ToBitmap();                
              }             
          }

When I click on edge detection button it calls the main recursive function

private void detect_edges_Click(object sender, EventArgs e)
        {          
           hueckel_operator(1, 1);
        }

This operator repeats itself with 5pixel intervals. In other words I apply it on x axis by incrementing x parameter by 5 and at the end of the row I increment y axis by 5 and so on.

In hueckel_operator, the function "a()" which calculates again a very heavy formula is being called 8 times. Here is the a() function

 public double a(int j,  int counter6,  int counter7)
            {

                for (int II = 0; II <= j ; II++)
                {
                    for (KK = 1; KK < 70; KK++)
                    {
                         x_value = input_i_x(KK); //this function brings the x coordinate
                         y_value = input_i_y(KK); // this function brings the y coordinate

                result += HueckelDisk(x_value,y_value,j) * MyImage[x_value+counter6, y_value+counter7].Intensity;
                //MyImage.Dispose();
                    }
                }                           
                return result;
            }

But the problem is approximately at the coordinate (75,5) it throws a stack overflow exception. I debugged it with performance analyse and MyImage seems to eat all memory. You probably wants to see recursive function but since it is too big I can not put it here and I am sure that recursive function (hueckel_operator()) can not reach terminating condition since I found out how many times it has been called. What I want is to find out if there is another way to calculate "result" in a more efficient way.

My other question is that, object MyImage is used in function a() 69*j times and does it mean that it allocates memory space 69*j times whenever a() is called?

During my desperate tries, I have declared and defined almost every variable as global in order to reduce the memory usage because I have thought otherwise whenever hueckel_operator() and a() are called, local variables would allocate extra memory in stack over and over, is it a good or necessary approach?

I use 4 very nested and heavy functions and I don't use any class. Would it be the main problem? To be honest I don't see anything here to convert into class.

I know, I have asked too many questions but I am really desperate right now. I am reading articles since some weeks and I guess I need a kick start. Any help would be appreciated.

enter image description here


Solution

  • A stack overflow exception doesn't really have much to do with memory usage - it's from stack usage. In your case, a recursive call.

    Recursion can only go so deep until the stack is exhausted. Once that happens, you get your stack overflow.

    If you are not in an unbounded recursion but you need to go deeper, you can specify the stack size when you create a thread, and run your recursive function on that:

    var stackSize = 10000000;
    var thread = new Thread(new ThreadStart(StartDetection), stackSize);
    

    However, the default size is 1MB - which is quite a bit for most tasks. You may want to verify your recursion is in fact not unbound, or that you can't reduce or remove it.