Class VideoCapture


  • public class VideoCapture
    extends java.lang.Object
    Class for video capturing from video files, image sequences or cameras. The class provides C++ API for capturing video from cameras or for reading video files and image sequences. Here is how the class can be used: INCLUDE: samples/cpp/videocapture_basic.cpp Note: In REF: videoio_c "C API" the black-box structure CvCapture is used instead of %VideoCapture. Note:
    • (C++) A basic sample on using the %VideoCapture interface can be found at OPENCV_SOURCE_CODE/samples/cpp/videocapture_starter.cpp
    • (Python) A basic sample on using the %VideoCapture interface can be found at OPENCV_SOURCE_CODE/samples/python/video.py
    • (Python) A multi threaded video processing sample can be found at OPENCV_SOURCE_CODE/samples/python/video_threaded.py
    • (Python) %VideoCapture sample showcasing some features of the Video4Linux2 backend OPENCV_SOURCE_CODE/samples/python/video_v4l2.py
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected long nativeObj  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
        VideoCapture()
      Default constructor Note: In REF: videoio_c "C API", when you finished working with video, release CvCapture structure with cvReleaseCapture(), or use Ptr<CvCapture> that calls cvReleaseCapture() automatically in the destructor.
      protected VideoCapture​(long addr)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static VideoCapture __fromPtr__​(long addr)  
      protected void finalize()  
      double get​(int propId)
      Returns the specified VideoCapture property
      java.lang.String getBackendName()
      Returns used backend API name Note: Stream should be opened.
      boolean getExceptionMode()  
      long getNativeObjAddr()  
      boolean grab()
      Grabs the next frame from video file or capturing device.
      boolean isOpened()
      Returns true if video capturing has been initialized already.
      boolean open​(int index)
      Opens a camera for video capturing Parameters are same as the constructor VideoCapture(int index, int apiPreference = CAP_ANY)
      boolean open​(int index, int apiPreference)
      Opens a camera for video capturing Parameters are same as the constructor VideoCapture(int index, int apiPreference = CAP_ANY)
      boolean open​(java.lang.String filename)
      Opens a video file or a capturing device or an IP video stream for video capturing.
      boolean open​(java.lang.String filename, int apiPreference)
      Opens a video file or a capturing device or an IP video stream for video capturing.
      boolean read​(Mat image)
      Grabs, decodes and returns the next video frame.
      void release()
      Closes video file or capturing device.
      boolean retrieve​(Mat image)
      Decodes and returns the grabbed video frame.
      boolean retrieve​(Mat image, int flag)
      Decodes and returns the grabbed video frame.
      boolean set​(int propId, double value)
      Sets a property in the VideoCapture.
      void setExceptionMode​(boolean enable)
      Switches exceptions mode methods raise exceptions if not successful instead of returning an error code
      • Methods inherited from class java.lang.Object

        clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • nativeObj

        protected final long nativeObj
    • Constructor Detail

      • VideoCapture

        protected VideoCapture​(long addr)
      • VideoCapture

        public VideoCapture()
        Default constructor Note: In REF: videoio_c "C API", when you finished working with video, release CvCapture structure with cvReleaseCapture(), or use Ptr<CvCapture> that calls cvReleaseCapture() automatically in the destructor.
    • Method Detail

      • getNativeObjAddr

        public long getNativeObjAddr()
      • __fromPtr__

        public static VideoCapture __fromPtr__​(long addr)
      • getBackendName

        public java.lang.String getBackendName()
        Returns used backend API name Note: Stream should be opened.
        Returns:
        automatically generated
      • getExceptionMode

        public boolean getExceptionMode()
      • grab

        public boolean grab()
        Grabs the next frame from video file or capturing device.
        Returns:
        true (non-zero) in the case of success. The method/function grabs the next frame from video file or camera and returns true (non-zero) in the case of success. The primary use of the function is in multi-camera environments, especially when the cameras do not have hardware synchronization. That is, you call VideoCapture::grab() for each camera and after that call the slower method VideoCapture::retrieve() to decode and get frame from each camera. This way the overhead on demosaicing or motion jpeg decompression etc. is eliminated and the retrieved frames from different cameras will be closer in time. Also, when a connected camera is multi-head (for example, a stereo camera or a Kinect device), the correct way of retrieving data from it is to call VideoCapture::grab() first and then call VideoCapture::retrieve() one or more times with different values of the channel parameter. REF: tutorial_kinect_openni
      • isOpened

        public boolean isOpened()
        Returns true if video capturing has been initialized already. If the previous call to VideoCapture constructor or VideoCapture::open() succeeded, the method returns true.
        Returns:
        automatically generated
      • open

        public boolean open​(java.lang.String filename,
                            int apiPreference)
        Opens a video file or a capturing device or an IP video stream for video capturing. Parameters are same as the constructor VideoCapture(const String& filename, int apiPreference = CAP_ANY)
        Parameters:
        filename - automatically generated
        apiPreference - automatically generated
        Returns:
        true if the file has been successfully opened The method first calls VideoCapture::release to close the already opened file or camera.
      • open

        public boolean open​(java.lang.String filename)
        Opens a video file or a capturing device or an IP video stream for video capturing. Parameters are same as the constructor VideoCapture(const String& filename, int apiPreference = CAP_ANY)
        Parameters:
        filename - automatically generated
        Returns:
        true if the file has been successfully opened The method first calls VideoCapture::release to close the already opened file or camera.
      • open

        public boolean open​(int index,
                            int apiPreference)
        Opens a camera for video capturing Parameters are same as the constructor VideoCapture(int index, int apiPreference = CAP_ANY)
        Parameters:
        index - automatically generated
        apiPreference - automatically generated
        Returns:
        true if the camera has been successfully opened. The method first calls VideoCapture::release to close the already opened file or camera.
      • open

        public boolean open​(int index)
        Opens a camera for video capturing Parameters are same as the constructor VideoCapture(int index, int apiPreference = CAP_ANY)
        Parameters:
        index - automatically generated
        Returns:
        true if the camera has been successfully opened. The method first calls VideoCapture::release to close the already opened file or camera.
      • read

        public boolean read​(Mat image)
        Grabs, decodes and returns the next video frame.
        Parameters:
        image - automatically generated
        Returns:
        false if no frames has been grabbed The method/function combines VideoCapture::grab() and VideoCapture::retrieve() in one call. This is the most convenient method for reading video files or capturing data from decode and returns the just grabbed frame. If no frames has been grabbed (camera has been disconnected, or there are no more frames in video file), the method returns false and the function returns empty image (with %cv::Mat, test it with Mat::empty()). Note: In REF: videoio_c "C API", functions cvRetrieveFrame() and cv.RetrieveFrame() return image stored inside the video capturing structure. It is not allowed to modify or release the image! You can copy the frame using cvCloneImage and then do whatever you want with the copy.
      • retrieve

        public boolean retrieve​(Mat image,
                                int flag)
        Decodes and returns the grabbed video frame.
        Parameters:
        flag - it could be a frame index or a driver specific flag
        image - automatically generated
        Returns:
        false if no frames has been grabbed The method decodes and returns the just grabbed frame. If no frames has been grabbed (camera has been disconnected, or there are no more frames in video file), the method returns false and the function returns an empty image (with %cv::Mat, test it with Mat::empty()). SEE: read() Note: In REF: videoio_c "C API", functions cvRetrieveFrame() and cv.RetrieveFrame() return image stored inside the video capturing structure. It is not allowed to modify or release the image! You can copy the frame using cvCloneImage and then do whatever you want with the copy.
      • retrieve

        public boolean retrieve​(Mat image)
        Decodes and returns the grabbed video frame.
        Parameters:
        image - automatically generated
        Returns:
        false if no frames has been grabbed The method decodes and returns the just grabbed frame. If no frames has been grabbed (camera has been disconnected, or there are no more frames in video file), the method returns false and the function returns an empty image (with %cv::Mat, test it with Mat::empty()). SEE: read() Note: In REF: videoio_c "C API", functions cvRetrieveFrame() and cv.RetrieveFrame() return image stored inside the video capturing structure. It is not allowed to modify or release the image! You can copy the frame using cvCloneImage and then do whatever you want with the copy.
      • set

        public boolean set​(int propId,
                           double value)
        Sets a property in the VideoCapture.
        Parameters:
        propId - Property identifier from cv::VideoCaptureProperties (eg. cv::CAP_PROP_POS_MSEC, cv::CAP_PROP_POS_FRAMES, ...) or one from REF: videoio_flags_others
        value - Value of the property.
        Returns:
        true if the property is supported by backend used by the VideoCapture instance. Note: Even if it returns true this doesn't ensure that the property value has been accepted by the capture device. See note in VideoCapture::get()
      • get

        public double get​(int propId)
        Returns the specified VideoCapture property
        Parameters:
        propId - Property identifier from cv::VideoCaptureProperties (eg. cv::CAP_PROP_POS_MSEC, cv::CAP_PROP_POS_FRAMES, ...) or one from REF: videoio_flags_others
        Returns:
        Value for the specified property. Value 0 is returned when querying a property that is not supported by the backend used by the VideoCapture instance. Note: Reading / writing properties involves many layers. Some unexpected result might happens along this chain. VideoCapture -> API Backend -> Operating System -> Device Driver -> Device Hardware The returned value might be different from what really used by the device or it could be encoded using device dependent rules (eg. steps or percentage). Effective behaviour depends from device driver and API Backend
      • release

        public void release()
        Closes video file or capturing device. The method is automatically called by subsequent VideoCapture::open and by VideoCapture destructor. The C function also deallocates memory and clears \*capture pointer.
      • setExceptionMode

        public void setExceptionMode​(boolean enable)
        Switches exceptions mode methods raise exceptions if not successful instead of returning an error code
        Parameters:
        enable - automatically generated
      • finalize

        protected void finalize()
                         throws java.lang.Throwable
        Overrides:
        finalize in class java.lang.Object
        Throws:
        java.lang.Throwable