尝试使用capture.QueryFrame()获取多个线程来获取帧

时间:2016-08-30 14:21:03

标签: c# multithreading emgucv

所以我有一个主要类,它从我的网络摄像头获取帧,以及处理帧的多个线程。我已经锁定了从凸轮抓取框架的主类的一部分,因此只有一个线程可以处理同一帧。现在我得到一个外部异常,我不知道为什么。 我的主要课程:

public Image<Bgr, byte> GetImage()
{
    Image<Bgr, byte> returnable;
    Mat f = null;
    lock (locker)
    {
       do
       {
            f = capture.QueryFrame();
        } while (!capture.Grab());
        returnable = f.ToImage<Bgr, byte>();
    }
    return returnable;
}

我的主题:

Image<Bgr,byte> image;
image = o.GetImage(); //o is the main class
Image imag = image.ToBitmap();
string savePath = path + rofNumber + "/Original.jpg";
imag.Save(savePath); //Exception is on this line
toFind = o.GetNumbersFromDatabase();
labels = FindLabels(image);

例外:

System.Runtime.InteropServices.ExternalException was unhandled
ErrorCode=-2147467259
HResult=-2147467259
Message=Er is een algemene fout opgetreden in GDI+.
Source=System.Drawing
StackTrace:
   bij System.Drawing.Image.Save(String filename, ImageCodecInfo encoder,    EncoderParameters encoderParams)
   bij OCR.Worker.Run() in C:\Users\...\Code\Visual Studio\OCR\OCR\Worker.cs:regel 178
   bij System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   bij System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   bij System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   bij System.Threading.ThreadHelper.ThreadStart()
InnerException: 

1 个答案:

答案 0 :(得分:0)

I've found the problem myself. Since I'm working with multiple threads, I should have been more cautious with saving the images.

The problem was that one of the threads was saving the image and before it finished, another thread started saving to the same file, which caused the error.

The reason why I tried this in the first place is to get a kinda small slideshow of the camera feed.

Conclusion:

I used a different name per thread for saving the image!

In the end I've fixed this problem myself but I've learned that I need to be truely cautious with multiple threads...