任何人都可以帮助我使用EmguCv C#从webcamera实时更新直方图框中的直方图。
HistogramBox hb = new HistogramBox();
Form frm = new Form();
hb.GenerateHistograms(img, 256);// show 256 shades
hb.Enabled = true;
hb.Size = new System.Drawing.Size(255, 255);// change to your preferred size
frm.Controls.Add(hb);
hb.Refresh();
frm.ShowDialog();
这是我得到的片段,但是当我包含在webcamera捕获方法中时,对话框会刷新,使其变为空白而不是更新。
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.UI;
namespace Redcolor_tracker_GUI
{
public partial class Form1 : Form
{
Capture capWebcam = null;
bool inProcess = false;
Image<Bgr, Byte> img;
Image<Gray, Byte> imgout;
HistogramBox hb = new HistogramBox();
bool toggle = false;
Form frm = new Form();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
capWebcam = new Capture();
}
catch (NullReferenceException except)
{
txtXYRadius.Text = except.Message;
return;
}
Application.Idle += processFrameGUI;
inProcess = true;
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (capWebcam != null)
{
capWebcam.Dispose();
}
}
void processFrameGUI(object sender,EventArgs srg)
{
img=capWebcam.QueryFrame();
if(img==null) return;
imgout=img.InRange(new Bgr(0,0,0),new Bgr(0,0,0));
imgout=imgout.SmoothGaussian(9);
if (toggle)
{
hb.GenerateHistograms(img, 256);
hb.Refresh();// show 256 shades
hb.Enabled = true;
}
ibOriginal.Image=img;
ibProcessed.Image=imgout;
}
private void btnPauseOResume_Click(object sender, EventArgs e)
{
if (inProcess == true)
{
Application.Idle -= processFrameGUI;
inProcess = false;
btnPauseOrResume.Text = "resume";
}
else
{
Application.Idle += processFrameGUI;
inProcess = true;
btnPauseOrResume.Text = "resume";
}
}
private void button1_Click(object sender, EventArgs e)
{
if (toggle == false)
{
toggle = true;
}
else
{
toggle = false;
}
hb.Size = new System.Drawing.Size(400, 400);
frm.Controls.Add(hb);
frm.ShowDialog();
}
}
}
完整的代码******************
谢谢
答案 0 :(得分:1)
您可以通过在定义的时间间隔内调用HistogramBox
方法(例如,使用计时器)来更新单独工作线程中的Refresh
控件。请注意,您应该通过引用其SyncContext将刷新调用封送到UI线程中。
示例:强>
//Runs on a worker Thread
this.Invoke((MethodInvoker)delegate {
hb.Refresh();; // Runs on UI thread
});
答案 1 :(得分:0)
尝试在ClearHistogram()
内调用processFrameGUI(object sender,EventArgs srg)
:
void processFrameGUI(object sender,EventArgs srg)
{
img=capWebcam.QueryFrame();
if(img==null) return;
imgout=img.InRange(new Bgr(0,0,0),new Bgr(0,0,0));
imgout=imgout.SmoothGaussian(9);
if (toggle)
{
// Note the following line:
hb.ClearHistogram();
hb.GenerateHistograms(img, 256);
hb.Refresh();// show 256 shades
hb.Enabled = true;
}
ibOriginal.Image=img;
ibProcessed.Image=imgout;
}