【发布时间】:2017-10-26 20:35:05
【问题描述】:
我使用的是 Emgu 2(使用 opencv 2.4.10.1 的那个),它运行非常稳定,运行时不会崩溃。我现在已经升级到 Emgu 3.1.0.1,我的应用程序有时会在几小时或一天内崩溃,并出现 AccessViolationException。我现在已经看到它在两个不同的位置崩溃了。请参阅下面源代码中标记的两个位置(CRASH1 和 CRASH2)。所以我认为这里有一些根本性的错误。
我正在运行该软件的发布版本。有人知道这里可能发生了什么吗?
CRASH1 异常是:
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
at Emgu.CV.CvInvoke.cveSubtract(IntPtr, IntPtr, IntPtr, IntPtr, Emgu.CV.CvEnum.DepthType)
at Emgu.CV.CvInvoke.Subtract(Emgu.CV.IInputArray, Emgu.CV.IInputArray, Emgu.CV.IOutputArray, Emgu.CV.IInputArray, Emgu.CV.CvEnum.DepthType)
at Emgu.CV.RotationMatrix2D.CreateRotationMatrix(System.Drawing.PointF, Double, System.Drawing.Size, System.Drawing.Size ByRef)
at Emgu.CV.Image`2[[Emgu.CV.Structure.Bgr, Emgu.CV.World, Version=3.1.0.2282, Culture=neutral, PublicKeyToken=7281126722ab4438],[System.Byte, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Rotate(Double, System.Drawing.PointF, Emgu.CV.CvEnum.Inter, Emgu.CV.Structure.Bgr, Boolean)
at Emgu.CV.Image`2[[Emgu.CV.Structure.Bgr, Emgu.CV.World, Version=3.1.0.2282, Culture=neutral, PublicKeyToken=7281126722ab4438],[System.Byte, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Rotate(Double, Emgu.CV.Structure.Bgr, Boolean)
at XXXX.ProcessFrames()
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()
CRASH2 异常是:
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
at Emgu.CV.Util.VectorOfMat.VectorOfMatPush(IntPtr, IntPtr)
at Emgu.CV.Util.VectorOfMat..ctor(Emgu.CV.Mat[])
at XXXX.ProcessFrames()
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()
源码为:
public void start()
{
started = true;
// start processing camera frames
cameraThread = new Thread(new ThreadStart(ProcessFrames));
cameraThread.IsBackground = true;
cameraThread.Start();
}
private void ProcessFrames()
{
Bgr frameBg = new Bgr(0,0,0);
while (true)
{
// try to open the camera device
if (capture == null)
{
try
{
int index;
bool isNumeric = int.TryParse(ConfigurationManager.AppSettings["CameraSource"], out index);
capture = isNumeric ? new Capture(index) : new Capture(ConfigurationManager.AppSettings["CameraSource"]);
capture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameWidth, 640);
capture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameHeight, 480);
capture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.FourCC, Emgu.CV.VideoWriter.Fourcc('M', 'J', 'P', 'G'));
}
catch
{
frameObservers.ForEach(a => a.frame(cameraNotConnectedBitmap, new List<Person>(), recognizer.isActive()));
}
Task.Delay(1000).Wait();
continue;
}
// read a frame from the camera
Mat matFrame = capture.QueryFrame();
Image<Bgr, byte> frame = null;
if (matFrame != null)
{
frame = matFrame.ToImage<Bgr, byte>();
}
if (frame == null)
{
frameObservers.ForEach(a => a.frame(cameraNotConnectedBitmap, new List<Person>(), recognizer.isActive()));
capture = null; // force to reconnect to camera again
Task.Delay(1000).Wait();
continue;
}
try
{
CRASH1 >>>frame = frame.Rotate(Convert.ToInt32(ConfigurationManager.AppSettings["CameraRotation"]), frameBg, false);
double scale = Convert.ToDouble(ConfigurationManager.AppSettings["CameraScale"]);
if (scale > 0)
{
frame = frame.Resize(scale, Emgu.CV.CvEnum.Inter.Linear);
}
Global.FRAME_HEIGHT = frame.Height;
Global.FRAME_WIDTH = frame.Width;
rawCameraFeedFrameObservers.ForEach(a => a.frame(frame));
if (autoBrightness)
{
Image<Lab, Byte> lab = frame.Convert<Lab, Byte>();
Image<Gray, Byte>[] planes = lab.Split();
CvInvoke.CLAHE(planes[0], claheClipLimit, new Size(8, 8), planes[0]);
CRASH2 >>>VectorOfMat vm = new VectorOfMat(planes[0].Mat, planes[1].Mat, planes[2].Mat);
CvInvoke.Merge(vm, lab);
frame = lab.Convert<Bgr, Byte>();
}
}
catch
{
// not the end of the world
Log.INFO("INFO", "Processing the frame failed, skipping this frame");
continue;
}
}
}
【问题讨论】:
-
这看起来像是一个更大的应用程序的一个线程,您的问题可能在其他地方,并且可能与某种竞争条件或非托管数据的其他错误处理有关,这根本没有体现在以前的库版本中
-
线程基本上从相机中抓取一个帧,按照代码中的方式处理它,然后将其发送到 UI 线程,UI 线程制作帧的位图并将其显示在屏幕上。 UI 线程中的代码无论如何都不会操纵框架。所以我看不出它会如何造成访问冲突。
-
我建议处置我在 EMGU 尝试中遇到访问违规问题的所有一次性用品(即 Mat)(尤其是与 BOW 相关的调用)。我已经开始使用 OpenCVSharp,现在一切都更加稳定了。但是最新版本有一些我使用的serios问题:nuget.org/packages/OpenCvSharp3-AnyCPU/3.2.0.20170324
-
你的意思是最后明确地做 frame.Dispose() ?不会有问题,因为我必须发布到 UI 线程以显示它将由相机线程处理的帧。