【问题标题】:image capturing using EmguCv使用 EmguCv 进行图像捕获
【发布时间】:2014-02-04 11:17:19
【问题描述】:

我正在开发一个程序,它将捕获四次图像 并将其保存到文件夹中

我所做的是将代码放入 for 循环中

但我的问题是它只保存第一个捕获的图像四次而不是捕获四次

private void Form1_Load(object sender, EventArgs e)
{
    string path = @"C:\Users\\Jake_PC\\Desktop\\fitting\\";
    System.IO.Directory.CreateDirectory(path);
    bool useCam = true;

    if (!useCam)
        measureImage(null);
    else 
    {
        try
        {
            camera = new Capture();
        }
        catch (Exception exc)
        {
            MessageBox.Show(exc.Message);
            return;
        }

        Application.Idle += viewImage;
        captureProcess = true;
    }
}

private void btnCapture_Click(object sender, EventArgs e)
{
    if (btnCapture.Text == "Back")
    {
        Application.Restart();
    }
    else
    {
        if (captureProcess == true)
        {
            for (int cap = 0; cap < 4; cap++)
            {
                string path = @"C:\\Users\\Jake_PC\\Desktop\\fitting\\";
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
                ctr = dir.GetFiles().Length;

                camera = new Capture();
                System.Threading.Thread.Sleep(500);
                Application.Idle -= viewImage;
                SaveFileDialog dlg = new SaveFileDialog();
                img.ToBitmap().Save(@"C:\\Users\\Jake_PC\\Desktop\\fitting\\" + ctr + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                camera.Dispose();

                Form1_Load(sender, e);
            }
        }
    }
}

【问题讨论】:

  • 那段代码是一团乱七八糟的来回调用函数,没有明确定义的关注点和边界。你为什么不做一个功能来捕捉和保存图像而不是别的。完全独立于任何用户界面。在定义明确的环境中发现错误要容易得多。
  • 你相机的 fps 是多少?如果是 30fps 左右,那么连续图像的差异几乎不会被注意到。
  • @Naren 我已经编辑了代码并尝试使用线程来延迟捕获但仍然是相同的结果
  • @newbie07 什么是“img”?是全局变量吗?请贴出整个程序找出错误。

标签: c# emgucv image-capture


【解决方案1】:

使用以下基作为您的代码http://www.emgu.com/wiki/index.php?title=Camera_Capture

然后用这段代码替换下面的方法 ProcessFrame(object sender, EventArgs arg):

    int count = 0;
    static string path = @"C:\\Users\\Jake_PC\\Desktop\\fitting\\";
    System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);

    private void ProcessFrame(object sender, EventArgs arg)
    {
        //***If you want to access the image data the use the following method call***/
        //Image<Bgr, Byte> frame = new Image<Bgr,byte>(_capture.RetrieveBgrFrame().ToBitmap());


        string ctr = dir.GetFiles().Length.ToString();

        if (RetrieveBgrFrame.Checked)
        {
            Image<Bgr, Byte> frame = _capture.RetrieveBgrFrame();
            //because we are using an autosize picturebox we need to do a thread safe update
            DisplayImage(frame.ToBitmap());
            frame.ToBitmap().Save(@"C:\\Users\\Jake_PC\\Desktop\\fitting\\" + ctr + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
            count++;
            if(count < 4)
            {
                //this should really be reset in captureButtonClick() method 
                //to prevent a frameready event fireing before disposeing is finsihed
                count = 0; 
                //Stop aquiring by simulating a form button press
                captureButtonClick(null, null); 
            }
        }
        else if (RetrieveGrayFrame.Checked)
        {
            Image<Gray, Byte> frame = _capture.RetrieveGrayFrame();
            //because we are using an autosize picturebox we need to do a thread safe update
            DisplayImage(frame.ToBitmap());
            frame.ToBitmap().Save(@"C:\\Users\\Jake_PC\\Desktop\\fitting\\" + ctr + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
            count++;
            if (count < 4)
            {
                //this should really be reset in captureButtonClick() method 
                //to prevent a frameready event fireing before disposeing is finsihed
                count = 0;
                //Stop aquiring by simulating a form button press
                captureButtonClick(null, null);
            }
        }
    }

如果您有任何问题,请告诉我,

干杯,

克里斯

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    相关资源
    最近更新 更多