我有以下代码使用EmgucV在imagebox中显示图像:
Capture capture;
Image<Bgr, Byte> image;
public Form1()
{
InitializeComponent();
Application.Idle += new EventHandler(Start);
}
void Start(object sender, EventArgs e)
{
capture = new Capture();
image = capture.QueryFrame();
imageBox1.Image = image;
}
我得到了例外Attempted to read or write protected memory
。我需要做些什么才能纠正这个问题?
这表明可能存在本机内存泄漏
我认为您的代码中存在错误。在应用程序生命周期中,您的Start
方法将被多次调用(非常频繁)。
看起来您应该在应用程序中只使用一个Capture对象。
只需将Capture实例移动到Form构造函数:
Capture capture;
public Form1()
{
InitializeComponent();
Application.Idle += new EventHandler(Capture);
capture = new Capture();
}
void Capture(object sender, EventArgs e)
{
imageBox1.Image = capture.QueryFrame();
}
使用Windows Form c#的当前修复Emgu CV版本3.4.1;添加一个按钮调用它btnCapture,添加一个PictureBox控件将名称保留为其默认值,以达到此答案的目的。希望此代码示例有所帮助
public VideoCapture capture;
private void btnCatpure_Click(object sender, EventArgs e)
{
// each click a single frame will be capture and then display in the control.
Mat iframe = new Mat();
capture.Retrieve(iframe, 0);
Mat grayFrame = new Mat();
CvInvoke.CvtColor(iframe, grayFrame, ColorConversion.Bgr2Gray);
pictureBox1.Image = iframe.Bitmap;
pictureBox1.Image = grayFrame.Bitmap;
}