有什么方法可以从实时视频中提取静止帧
Is there any method to extract still frames from a live video
我正在做一个手语项目,我只是想知道如何从实时视频中提取静止帧。是否有任何内置方法或我必须为其编写任何逻辑。
OpenCV 允许您接收来自视频源的每一帧的回调,您可以分析、修改 and/or 根据需要保存该帧。
一个使用 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 检测示例:[=13],以下代码会将帧保存到图像文件=]
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;
}
我正在做一个手语项目,我只是想知道如何从实时视频中提取静止帧。是否有任何内置方法或我必须为其编写任何逻辑。
OpenCV 允许您接收来自视频源的每一帧的回调,您可以分析、修改 and/or 根据需要保存该帧。
一个使用 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 检测示例:[=13],以下代码会将帧保存到图像文件=]
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;
}