1

Here is the basic idea of my code:

    private void CaptureCameraFrame()
    {
        Capture = new VideoCapture();
        CameraModel.Instance.CameraViewMat = Capture.QueryFrame();

        // do stuff with queried matrix here

        if(noAbortCondition)
        {
            CaptureCameraFrame();
        }
    }

The method should run in a separate thread updating my GUI with the current image after processing. Only Problem is, that I get two different kinds of error:

  1. Attempt to read/write protected memory: This happens on the second runthrough of the method.
  2. I get an null-reference error using `CameraModel.Instance.CameraViewMat right after querying the frame.

The two issues seem to be connected, seems like QueryFrame() runs asynchronously from the rest of the code and isn't done when the program jumps to the next step. Question is: How can I make sure, that querying the image from the camera is finished, and I can use the information in the matrix as well as start a new query?

In all the examples I have found this is done by using time, but I would like to start with a new frame as soon as processing on the last frame is done.

I haven't really done much in C# when it comes to threading, but what I understand in such cases one would use the asyncand awaitkeywords to make sure a method in an asynchronous method is finished. However I wasn't able to make a working implementation in this case.

Roland Deschain
  • 2,211
  • 19
  • 50
  • On thing to add: If I remove the recursion I manage to capture a static image. So from that side of things, everything should be working fine. – Roland Deschain Sep 25 '18 at 09:22

1 Answers1

2

You are creating VideoCapture class instance repeatedly and even not disposing of it. Create your VideoCapture instance only once and use them for your task. At the end dispose it.

public YourConstructor()
{
    Capture = new VideoCapture();
}
private void CaptureCameraFrame()
{
    CameraModel.Instance.CameraViewMat = Capture.QueryFrame();

    // do stuff with queried matrix here

    if(noAbortCondition)
    {
        CaptureCameraFrame();
    }
}

Hopefully, it will work for you!

habib
  • 2,366
  • 5
  • 25
  • 41
  • Thx, this definitely helped. Now I only have to figure out how to put the method into another task, as I here also get an exception concerning protected memory. Maybe this has to do with access privilege of the thread/task.... – Roland Deschain Sep 25 '18 at 10:31
  • Edit: found the thread issue: Shouldn't try to update variables belonging to the GUI thread from the new task^^ – Roland Deschain Sep 25 '18 at 10:39
  • You cannot access GUI from an external thread directly, You can invoke your GUI related code on GUI thread using this.Invoke() from an external thread. – habib Sep 25 '18 at 10:41
  • https://stackoverflow.com/questions/661561/how-do-i-update-the-gui-from-another-thread See this. – habib Sep 25 '18 at 10:44
  • 1
    Thank you a lot for the provided help! – Roland Deschain Sep 25 '18 at 10:46