使用EmguCv捕获图像

时间:2014-02-04 11:17:19

标签: c# emgucv image-capture

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

我所做的是将我的代码放在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);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

使用以下基础作为代码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);
            }
        }
    }

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

干杯,

克里斯