Tuesday, March 11, 2014

How to Process Image & Write Video Using EmguCV

How to Process Image & Write Video Using EmguCV

Processing Images

The following function processes the frame. Process frame extracts some details like frame number, time index, total frames. This function shows the images sequences in picture box.
frame = _capture.QueryFrame(); 

Frame is converted into bitmap and assigned to picture box to display.
pictureBox1.Image = frame.ToBitmap(); 

Function sleeps for a specified time with division of frame rate.
Thread.Sleep((int)(1000.0 / FrameRate)); 

Frame 1 gets converted into byte array. That byte array gets converted into hex values of each frame. Then that hex values are stored into the array list for further process.
string s1 = BitConverter.ToString(library.ConvertImageToByte(frame)); //.Replace("-", "");
hex.Add(s1); 
private void ProcessFrame(object sender, EventArgs arg)
{
  try
  {
    Framesno = _capture.GetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_POS_FRAMES);
    frame = _capture.QueryFrame();
    if (frame != null)
    {
      pictureBox1.Image = frame.ToBitmap();
      if (cam == 0)
      {
        Video_CNTRL.Value = (int)(Framesno);
        double time_index = _capture.GetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_POS_MSEC);
        Time_Label.Text = "Time: " + TimeSpan.FromMilliseconds(time_index).ToString().Substring(0, 8);
        double framenumber = _capture.GetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_POS_FRAMES);
        Frame_lbl.Text = "Frame: " + framenumber.ToString();
        Thread.Sleep((int)(1000.0 / FrameRate));
        TotalFrames = _capture.GetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_COUNT);
     }
     string s1 = BitConverter.ToString(library.ConvertImageToByte(frame));
     hex.Add(s1);
   }
 }
 catch (Exception ex)
 {
     MessageBox.Show(ex.Message.ToString());
 }
}  

Writing Video

For writing video, VideoWriter class is used. Videowriter class has a constructor with the following parameters:
  • File Name: Name of the output video file.
  • FPS: FrameRate of the created video stream.
  • Frame Width: Width Size of the video frames.
  • Frame Height: Height Size of the video frames.
  • IsColor – If it is false, video is created with grayscale frames, if it is true, video is created with color frames. (The flag is currently supported on Windows only).
For some functions, refer to my previous articles and tip/tricks:
private void button12_Click(object sender, EventArgs e)
{   
    saveFileDialog1.Filter="AVI|*.avi";
    if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        textBox7.Text = saveFileDialog1.FileName;
        VW = new VideoWriter(textBox7.Text, 30,320, 240, true);
        try
        {
          foreach (String h in hex)
          {
              frame=library.ConvertByteToImage(library.DecodeHex(h));
              VW.WriteFrame(frame);
          }
          MessageBox.Show("Video Generated Successfully", "Success");
        }
        catch (Exception ex){ }
    }
} 

Points of Interest

  • Learn how to write the video using image sequence

References

  1. http://www.emgu.com/wiki/index.php/Main_Page
  2. http://www.emgu.com/wiki/index.php/Tutorial
  3. http://www.emgu.com/wiki/index.php/Documentation
  4. http://www.emgu.com/wiki/index.php?title=Video_Files
Post a Comment