请帮我纠正下面的c#代码 [英] please help me to correct the below c# code
问题描述
这里我的目标是使用emgucv从文件夹中读取图像并检测其前景,但下面的代码不会显示任何事情给出异常
Here my aim to to use emgucv for reading images from a folder and detect its foreground but the below code will not show any thing give an exception
private void timer1_Tick(object sender, EventArgs e)
{
if (File.Exists(Application.StartupPath + "\\frames\\" + i + ".jpg"))
{
using (Image<Bgr, Byte> image = new Image<Bgr, byte>(Application.StartupPath + "\\frames\\" + i + ".jpg"))
using (MemStorage storage = new MemStorage()) //create storage for motion components
{
if (_forgroundDetector == null)
{
//_forgroundDetector = new BGCodeBookModel<Bgr>();
_forgroundDetector = new FGDetector<Bgr>(Emgu.CV.CvEnum.FORGROUND_DETECTOR_TYPE.FGD);
//_forgroundDetector = new BGStatModel<Bgr>(image, Emgu.CV.CvEnum.BG_STAT_TYPE.FGD_STAT_MODEL);
}
_forgroundDetector.Update(image);
imageBox1.Image = image;
//update the motion history
_motionHistory.Update(_forgroundDetector.ForegroundMask);
imageBox2.Image = _forgroundDetector.ForegroundMask;
}
}
i++;
}
查看此消息的结尾有关调用
实时(JIT)调试而非此对话框的详细信息。
******* *******异常文本**************
Emgu.CV.Util.CvException:OpenCV:无法分配589824000字节
$ C $ b at Emgu.CV.CvInvoke.CvErrorHandler(Int32 status,String funcName,String errMsg,String fileName,Int32 line,IntPtr userData)in C:\ Emgu\emgucv-windows-universal-gpu 2.4.9.1847 \\ \\ emgu.CV \PInvoke \CvInvokeCore.cs:第132行
在Emgu.CV.CvInvoke.CvFGDetectorProcess(IntPtr探测器,IntPtr图像)
在Emgu.CV .VideoSurveillance.FGDetector`1.Update(图像`2图像)在C:\ Emgu \emgucv-windows-universal-gpu 2.4.9.1847 \Emgu.CV \ VideoSurveillance\FGDetector.cs:第65行
at testlucasimagebox.Form1.timer1_Tick(Object sender,EventArgs e)
在System.Windows.Forms。在System.Windows.Forms.Timer.TimerNativeWindow.WndProc上的Timer.OnTick(EventArgs e)
(消息& m)
在System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)
但是当我使用网络摄像头的图像时,下面的代码工作正常
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
Emgu.CV.Util.CvException: OpenCV: Failed to allocate 589824000 bytes
at Emgu.CV.CvInvoke.CvErrorHandler(Int32 status, String funcName, String errMsg, String fileName, Int32 line, IntPtr userData) in C:\Emgu\emgucv-windows-universal-gpu 2.4.9.1847\Emgu.CV\PInvoke\CvInvokeCore.cs:line 132
at Emgu.CV.CvInvoke.CvFGDetectorProcess(IntPtr detector, IntPtr image)
at Emgu.CV.VideoSurveillance.FGDetector`1.Update(Image`2 image) in C:\Emgu\emgucv-windows-universal-gpu 2.4.9.1847\Emgu.CV\VideoSurveillance\FGDetector.cs:line 65
at testlucasimagebox.Form1.timer1_Tick(Object sender, EventArgs e)
at System.Windows.Forms.Timer.OnTick(EventArgs e)
at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
but when i use images from a webcam the below code works fine
using (Image<Bgr, Byte> image = _capture.RetrieveBgrFrame())
using (MemStorage storage = new MemStorage()) //create storage for motion components
{
if (_forgroundDetector == null)
{
//_forgroundDetector = new BGCodeBookModel<Bgr>();
_forgroundDetector = new FGDetector<Bgr>(Emgu.CV.CvEnum.FORGROUND_DETECTOR_TYPE.FGD);
//_forgroundDetector = new BGStatModel<Bgr>(image, Emgu.CV.CvEnum.BG_STAT_TYPE.FGD_STAT_MODEL);
}
_forgroundDetector.Update(image);
capturedImageBox.Image = image;
//update the motion history
_motionHistory.Update(_forgroundDetector.ForegroundMask);
forgroundImageBox.Image = _forgroundDetector.ForegroundMask;
这里是我使用网络摄像头时的输出图像/>
https://www.dropbox.com/s/k04b0ng8ez0zmvr/emgucv.jpg [ ^ ]
推荐答案
错误正是它所说的,您正在尝试加载562-ish MB的映像,框架无法加载。帧文件夹中图像的大小是多少?
第二次看,看起来你正在每个刻度线上创建一个新的前景探测器,你不应该这样做。作为类的成员创建一次,并使用每个帧更新它。它可能持有对你创建的每一个的引用,并且在它达到某个点之后,它不能再分配更多的内存。
The error is exactly what it says, you are trying to load an image that is 562-ish MB, which the framework can't load. What is the size of your images in the frames folder?
On second look, looks like you are creating a new Foreground Detector on each tick, which you should not do. Create it once as a member of your class and update it with each frame. Its probably holding references to each one you create, and after it hits a certain point, it can't allocate any more memory.
这篇关于请帮我纠正下面的c#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!