Opencv implements reading camera and video data
- 2020-06-01 10:44:54
- OfStack
In fact, once the camera's video image is read at one speed, it is ready for all sorts of processing.
Then the main class used for obtaining is VideoCapture, and one demo is as follows:
// If there is an external camera, then ID for 0 Built-in for 1 , or use 0 You can represent the built-in camera
cv::VideoCapture cap(0);
// Determine if the camera is on
if(!cap.isOpened())
{
return -1;
}
cv::Mat myframe;
cv::Mat edges;
bool stop = false;
while(!stop)
{
// Get the current frame
cap>>myframe;
// Convert to grayscale
cv::cvtColor(myframe, edges, CV_BGR2GRAY);
// Gaussian filter
cv::GaussianBlur(edges, edges, cv::Size(7,7), 1.5, 1.5);
//Canny Operator detection edge
cv::Canny(edges, edges, 0, 30, 3);
// According to the edge
cv::imshow("current frame",edges);
if(cv::waitKey(30) >=0)
stop = true;
}
cv::waitKey(0);
Similarly, if you want to read a video file, the video file can be regarded as a series of video frames, and when the display time is set to 1 set delay, it can be displayed at 1 set speed. One demo is as follows:
// Open the video file
cv::VideoCapture capture("../images/bike.avi");
// check if video successfully opened
if (!capture.isOpened())
return 1;
// Get the frame rate
double rate= capture.get(CV_CAP_PROP_FPS);
bool stop(false);
cv::Mat frame; // current video frame
cv::namedWindow("Extracted Frame");
// Delay between each frame
// corresponds to video frame rate
int delay= 1000/rate;
// Used to set the moving position of the frame.
input_video.set(CV_CAP_PROP_POS_FRAMES,100);
// for all frames in video
while (!stop) {
// read next frame if any
if (!capture.read(frame))
break;
cv::imshow("Extracted Frame",frame);
// introduce a delay
// or press key to stop
if (cv::waitKey(delay)>=0)
stop= true;
}
// Close the video file
capture.release();
cv::waitKey();