CvCapture
lastmod 2019-10-16created 2015-07-1510 edits
Notes on the CvCapture object of OpenCV.
Attributes
| Property | Meaning | Notes |
| cv2.CAP_PROP_FRAME_WIDTH | Frame width | - |
| cv2.CAP_PROP_FRAME_HEIGHT | Frame height | - |
| cv2.CAP_PROP_FRAME_COUNT | Total number of frames | - |
| cv2.CAP_PROP_FPS | Frame rate | - |
| cv2.CAP_PROP_FOURCC | Codec code | - |
| cv2.CAP_PROP_BRIGHTNESS | Image brightness | Camera only |
| cv2.CAP_PROP_CONTRAST | Image contrast | Camera only |
| cv2.CAP_PROP_SATURATION | Image saturation | Camera only |
| cv2.CAP_PROP_HUE | Image hue | Camera only |
| cv2.CAP_PROP_GAIN | Image gain | Camera only |
| cv2.CAP_PROP_EXPOSURE | Image exposure | Camera only |
| cv2.CAP_PROP_POS_MSEC | Frame playback time | Returns ms |
| cv2.CAP_PROP_POS_FRAMES | Current frame | Less than the total number of frames |
| cv2.CAP_PROP_POS_AVI_RATIO | Relative position in the video file | 0 = start, 1 = end |
Camera example
#include <opencv/highgui.h>
int main()
{
char const * TITLE = "CAMERA";
cvNamedWindow(TITLE, 0);
cvResizeWindow(TITLE, 320, 240);
CvCapture * capture = cvCreateCameraCapture(0);
IplImage * frame = nullptr;
char keycode = 0;
while (true) {
// Get the camera frame.
frame = cvQueryFrame(capture);
if (!frame) {
break;
}
cvShowImage(TITLE, frame);
keycode = cvWaitKey(33);
// Check the ESC keycode.
if (keycode == 27) {
break;
}
}
cvReleaseCapture(&capture);
cvReleaseImage(&frame);
cvDestroyWindow(TITLE);
return 0;
}
Caveats!
The IplImage returned by the cvQueryFrame function above must never be released. The comment in the corresponding source code is as follows.
/* Just a combination of cvGrabFrame and cvRetrieveFrame
!!!DO NOT RELEASE or MODIFY the retrieved frame!!! */
CVAPI(IplImage*) cvQueryFrame( CvCapture* capture );
Also, after cvShowImage you must WAIT using cvWaitKey(); for the image to be displayed.
End of file
capVideo.open("CarsDrivingUnderBridge.mp4");
if (!capVideo.isOpened()) {
std::cout << "error reading video file\n";
return 0;
}
if (capVideo.get(CV_CAP_PROP_FRAME_COUNT) < 2) {
std::cout << "error: video file must have at least two frames\n";
return 0;
}
if ((capVideo.get(CV_CAP_PROP_POS_FRAMES) + 1) < capVideo.get(CV_CAP_PROP_FRAME_COUNT)) {
capVideo.read(imgFrame2);
} else {
std::cout << "end of video\n";
}
See also
Favorite site
- Reading and Writing Images and Video
- Saving camera input video as AVI
- Playing AVI video
- Second OpenCV program -Video-
- Get video FPS with VideoCapture
IP CAMERA
- Ip / network camera access
- Reading a network camera (IP Camera) in OpenCV
- ip camera h264 error while decoding
- how to stream h264 video with rtsp in opencv- partially answered