Open video
VideoCapture class
The new VideoCapture class in opencv2.x and above provides an interface to capture video from a camera or video file
Four constructor methods of VideoCapture class
public VideoCapture(); public VideoCapture(CaptureType capture Type); public VideoCapture(int CamIndex); public VideoCapture(String fileName);
- Function Description:
1. Null ViDeoCapture object, no initialization
2. Use the specific methods in CaptureType type to make your camera or video
3. Use Index to open the camera
4. Use file name method to open video file
-
Frame taking method
cap.Read(frame); frame=cap.QueryFrame();
Functions commonly used by VideoCapture class
public void Start(System ServiceModel.Dispatcher.ExceptionHandler en = null); public void Stop(); public bool IsOpened public virtual bool Grab(); public void Pause(); public double GetCaptureProperty(CaProp index); public bool SetCaptureProperty(CapProp property,double value);
-
Function description
1. star() - start the snapshot thread
2. stop() - stop
3. IsOpened() - judge whether the camera / video is on
4. Grab() - grab the video file or device of the next frame
5. Pause() - pause the snapshot thread
6. GetCaptureProperty() - get related parameter information of video class
7. SetCaptureProperty() - set a property of video class information
Read video and display
- Note: the essence of reading video is to read image, which is composed of image frame by frame
1. There are two ways to read:
VideoCapture cap; cap = new VideoCapture("1.avi");
VideoCapture cap = new VideoCapture("1.avi");
2. Cycle through each frame
Mat frame = new Mat();//Variables defining the framing method while(ture){ cap.Read(frame);//Frame taking method 1 //frame = cap.QueryFrame(); / / frame taking method 2 CvInvoke.Imshow("img",frame);//display picture if(CvInvoke.WaitKey(30)==27)//Display one frame every 30ms press esc to jump out of the cycle break; }
- explain
1. Open video to load locally
VideoCapture cap = new VideoCapture("1.avi");
It can also be network loading
new VideoCapture("website");
2. Add exception handling when reading video
A. Failed to open video (various reasons)
if(!cap.IsOpened)//Check whether the opening is successful return;
B. Frame fetching failure exception (program end judgment)
cap.Read(frame); if(!frame.IsEmpty)//Judge whether the current frame is empty { inshow("video",frame); waitKey(30); }else break;
case
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Emgu.CV; using Emgu.CV.Structure; using Emgu.CV.CvEnum; using Emgu.Util; namespace video { class Program { static void Main(string[] args) { VideoCapture cap = new VideoCapture("1.avi"); if (!cap.IsOpened) { Console.WriteLine("Failed to open video"); return; } Mat frame = new Mat(); while(true) { cap.Read(frame); if (frame.IsEmpty) { Console.WriteLine("frame is empty..."); break; } CvInvoke.Imshow("video", frame); CvInvoke.WaitKey(30); } } } }
Turn on the camera
break;
}
CvInvoke.Imshow("video", frame);
CvInvoke.WaitKey(30);
}
}
}
}