是否有任何方法可以从实况视频中提取静止帧

时间:2019-11-07 07:46:50

标签: python python-3.x opencv4android

我正在研究手语项目,我只是想知道如何从实况视频中提取静止帧。是否有任何内置方法,或者我必须为其编写任何逻辑。

1 个答案:

答案 0 :(得分:0)

OpenCV允许您从视频源收到每个帧的回叫,并且可以根据需要分析,修改和/或保存该帧。

使用Python的一个简单示例可能是(基于此处的教程:https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    # Here we just save the frame to a file
    cv2.imwrite('frame_'+str(i)+'.jpg',frame)
    i+=1

    # Display the resulting frame
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

对于一个更复杂的示例,这次是在Android中,以下代码将在检测到特定颜色时将其保存到图像文件中(基于OpenCV Color Blob检测示例:

    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
        mRgba = inputFrame.rgba();

        if (mIsColorSelected) {
            mDetector.process(mRgba);
            List<MatOfPoint> contours = mDetector.getContours();
            Log.e(TAG, "Contours count: " + contours.size());
            Imgproc.drawContours(mRgba, contours, -1, CONTOUR_COLOR);

            Mat colorLabel = mRgba.submat(4, 68, 4, 68);
            colorLabel.setTo(mBlobColorRgba);

            Mat spectrumLabel = mRgba.submat(4, 4 + mSpectrum.rows(), 70, 70 + mSpectrum.cols());
            mSpectrum.copyTo(spectrumLabel);

            File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            String testFilename = "TestOCVwrite.png";
            File testFile = new File(path, testFilename);
            String testFullfileName = testFile.toString();
            Log.i(TAG, "Writing to filename: " + testFilename);
            Boolean writeResult = Imgcodecs.imwrite(testFullfileName, mRgba);
            if (!writeResult) {
                Log.i(TAG, "Failed to write to filename: " + testFilename);
            }
        }

        return mRgba;
    }