Click here to Skip to main content
11,637,176 members (69,920 online)
Click here to Skip to main content

How to Process Image & Write Video Using EmguCV

, 23 Feb 2014 CPOL 10.7K 6 11
Rate this:
Please Sign up or sign in to vote.
Video write and process image frames. This project has been made in EmguCV 2.4.9 version and Microsoft Visual Studio 2010 platform.

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

Sorry

Sorry for my English. If you notice errors or can suggest a more correct version, please let me know.

History

  • 23rd February, 2014 - First release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

SOHAM_GANDHI
Software Developer Opulent Infotech
India India
I am Soham Gandhi from India. I studied BE in Information Technology at University of Pune. (www.unipune.ac.in). I have been learning OpenCV, Enjoying it.
My research interest in Image Processing, Artificial Intelligence, Security Systems, Security Metrics. I work on OpenCV, EmguCV, Visual Studio 2008/10, Java platform.

Home Page (Blog)


You Tube Channel

You may also be interested in...

Comments and Discussions

 
QuestionWhere is the library.ConvertImageToByte Pin
Nutnaree Kleawsirikul15-Mar-15 22:22
memberNutnaree Kleawsirikul15-Mar-15 22:22 
AnswerRe: Where is the library.ConvertImageToByte Pin
SOHAM_GANDHI15-Mar-15 22:50
professionalSOHAM_GANDHI15-Mar-15 22:50 
QuestionAbout compression Pin
Jean-Philippe Encausse7-Jun-14 1:05
memberJean-Philippe Encausse7-Jun-14 1:05 
AnswerRe: About compression Pin
SOHAM_GANDHI7-Jun-14 2:30
memberSOHAM_GANDHI7-Jun-14 2:30 
GeneralHelpfull Pin
CHARLIE9126-May-14 19:37
memberCHARLIE9126-May-14 19:37 
GeneralRe: Helpfull Pin
jose ali6-Jun-14 9:30
memberjose ali6-Jun-14 9:30 
AnswerRe: Helpfull Pin
SOHAM_GANDHI6-Jun-14 18:44
memberSOHAM_GANDHI6-Jun-14 18:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150728.1 | Last Updated 23 Feb 2014
Article Copyright 2014 by SOHAM_GANDHI
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid