opencv截取任一时间段的视频帧

videocapture.set()

我们使用opencv进行读取视频帧的时候都是从开始进行读的,可是我们应该如何从任意帧或者时间进行开始读呢?
我们可以通过videocapture.set方法

VideoCapture::set(int id,double value)
0  CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds.
1 CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
2 CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
3 CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
CV_CAP_PROP_FPS Frame rate.
CV_CAP_PROP_FOURCC 4-character code of codec.
CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
CV_CAP_PROP_HUE Hue of the image (only for cameras).
CV_CAP_PROP_GAIN Gain of the image (only for cameras).
CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
CV_CAP_PROP_WHITE_BALANCE Currently unsupported
CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)

CV_CAP_PROP_POS_MSEC设置开始的时间(以秒为单位):
以java程序示例:

package opencvtest;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.highgui.HighGui;
import org.opencv.videoio.VideoCapture;

public class main {
	public static void main(String[] args) {
		System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
		VideoCapture capture = new VideoCapture();
		capture.open("c:/opencv/inputimg/vtest.avi");
		HighGui.namedWindow("show");// 创建一个窗口,用来播放视频,窗口通过名字来区分,所以必须要命名。
		Mat image = new Mat();//定义一个Mat,用来接收一帧的图像
		double frameCount = capture.get(7);
		System.out.println("视频总帧数:"+frameCount);
		//设置从第2秒开始读
		capture.set(0,2*1000);
		//读取视频帧
		capture.read(image);
		//显示帧
        HighGui.imshow("show",image);
        HighGui.waitKey(0);
	}
	
}

发现读到的就是第二秒视频:
在这里插入图片描述
同时也可以通过:
设置第几帧图像开始读;

CV_CAP_PROP_POS_FRAMES

get方法

在这里插入图片描述

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值