C# EmguCV,无法分配内存

C# EmguCV,无法分配内存,c#,.net,opencv,memory,emgucv,C#,.net,Opencv,Memory,Emgucv,我尝试从Emgu examples文件夹中调整示例MotionDetection。唯一重要的是我改变了,我不直接从网络摄像头捕捉图像,而是从位图创建图像。基本上不是 using (Image<Bgr, Byte> image = _capture.RetrieveBgrFrame()) 但是,程序会引发一个异常: Emgu.CV.dll中发生类型为“Emgu.CV.Util.CvException”的未处理异常。其他信息:OpenCV:分配121651200字节失败 在这里: \u

我尝试从Emgu examples文件夹中调整示例MotionDetection。唯一重要的是我改变了,我不直接从网络摄像头捕捉图像,而是从位图创建图像。基本上不是

using (Image<Bgr, Byte> image = _capture.RetrieveBgrFrame())
但是,程序会引发一个异常:

Emgu.CV.dll中发生类型为“Emgu.CV.Util.CvException”的未处理异常。其他信息:OpenCV:分配121651200字节失败

在这里:

\u forgroundDetector=新的BGSTAT模型(image,Emgu.CV.CvEnum.BG_STAT_TYPE.FGD_STAT_MODEL);
Windows7,64x,程序构建为“任意CPU”,“首选32位”。我使用最新的Emgu 2.4.9测试版

当我使用1920x1080位图(3MB)时会发生这种情况。当我在应用程序窗口中使用两个不同的摄像头时(704x576和352x288,我修改了程序以便可以从多个摄像头获得图像),这种情况也会发生(但不是使用单个低分辨率摄像头)。当我不使用此方法检测运动时,程序运行没有问题

using (Image<Bgr, Byte> image = new Image<Bgr, Byte>(bmp))
public Bitmap ProcessFrame(Bitmap bmp)
    {
        using (Image<Bgr, byte> image = new Image<Bgr, byte>(bmp))
        using (MemStorage storage = new MemStorage())
        {
            if (_forgroundDetector == null)
            {
                _forgroundDetector = new BGStatModel<Bgr>(image, Emgu.CV.CvEnum.BG_STAT_TYPE.FGD_STAT_MODEL);
            }

            _forgroundDetector.Update(image);

            _motionHistory.Update(_forgroundDetector.ForegroundMask);

            #region get a copy of the motion mask and enhance its color
            double[] minValues, maxValues;
            Point[] minLoc, maxLoc;
            _motionHistory.Mask.MinMax(out minValues, out maxValues, out minLoc, out maxLoc);
            Image<Gray, Byte> motionMask = _motionHistory.Mask.Mul(255.0 / maxValues[0]);
            #endregion

            image[0] = motionMask;

            double minArea = 100;

            storage.Clear();
            Seq<MCvConnectedComp> motionComponents = _motionHistory.GetMotionComponents(storage);

            foreach (MCvConnectedComp comp in motionComponents)
            {
                if (comp.area < minArea) continue;

                double angle, motionPixelCount;
                _motionHistory.MotionInfo(comp.rect, out angle, out motionPixelCount);

                if (motionPixelCount < comp.area * 0.05) continue;

                DrawMotion(image, comp.rect, angle, new Bgr(Color.Red));
            }

            double overallAngle, overallMotionPixelCount;
            _motionHistory.MotionInfo(motionMask.ROI, out overallAngle, out overallMotionPixelCount);
            DrawMotion(image, motionMask.ROI, overallAngle, new Bgr(Color.Green));

            return image.ToBitmap();
        }
    }

private static void DrawMotion(Image<Bgr, Byte> image, Rectangle motionRegion, double angle, Bgr color)
  {
     float circleRadius = (motionRegion.Width + motionRegion.Height) >> 2;
     Point center = new Point(motionRegion.X + motionRegion.Width >> 1, motionRegion.Y + motionRegion.Height >> 1);

     CircleF circle = new CircleF(
        center,
        circleRadius);

     int xDirection = (int)(Math.Cos(angle * (Math.PI / 180.0)) * circleRadius);
     int yDirection = (int)(Math.Sin(angle * (Math.PI / 180.0)) * circleRadius);
     Point pointOnCircle = new Point(
         center.X + xDirection,
         center.Y - yDirection);
     LineSegment2D line = new LineSegment2D(center, pointOnCircle);

     image.Draw(circle, color, 1);
     image.Draw(line, color, 2);
  }
_forgroundDetector = new BGStatModel<Bgr>(image, Emgu.CV.CvEnum.BG_STAT_TYPE.FGD_STAT_MODEL);