jjzjj

cv2视频操作,cv.VideoCapture,cap.read(),cap.isOpened(),cap.get(propId) cap.set(propIDd,value),VideoWriter

耶耶LCY 2023-12-18 原文

目录

1.2——视频处理

1.2.1——捕获视频 cv.VideoCapture

1.2.2——cap.read()

1.2.3——cap.isOpened()

1.2.4——cap.get(propId) cap.set(propIDd,value)

1.2.5——播放视频文件

1.2.6——保存视频文件


1.2——视频处理

1.2.1——捕获视频 cv.VideoCapture

语法:cv.VideoCapture(device)

参数:device可以是设备索引(device index) 也可以是视频文件名称/地址 (the name of a video file)

import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)
if not cap.isOpened():
    print("Cannot open camera")
    exit()
while True:
    # Capture frame-by-frame
    ret, frame = cap.read()
    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    # Our operations on the frame come here
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    # Display the resulting frame
    cv.imshow('frame', gray)
    if cv.waitKey(1) == ord('q'):
        break
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()

1.2.2——cap.read()

ret,frame=cap.read()
  • cap.read()返回一个布尔值,这里用ret接收,如果为True,则说明每一帧图像都被正常读取

  • frame用于接收得到的每一帧图片

1.2.3——cap.isOpened()

if not cap.isOpened():
    print("Cannot open camera")
    exit()

用于判断cap是否正常初始化,返回布尔值,正常则返回True

1.2.4——cap.get(propId) cap.set(propIDd,value)

  • 可以使用函数 cap.get(propId) 来获得视频的一些参数信息。这里 propId 可以是 0 到 18 之间的任何整数。每一个数代表视频的一个属性,其中的一些值可以使用 cap.set(propId,value) 来修改,value 就是 你想要设置成的新值

  • 可以使用 cap.get(3) 和 cap.get(4) 来查看每一帧的宽和高。 默认情况下得到的值是 640X480。但是可以使用 cap.set(3,320) 和 cap.set(4,240) 来把宽和高改成 320X240

  • propId有以下值:(以下值可以以数字代替,按顺序从0开始)

CV_CAP_PROP_POS_MSECCurrent position of the video file in milliseconds(视频文件当前文件)
CV_CAP_PROP_POS_FRAMES0-based index of the frame to be decoded/captured next
CV_CAP_PROP_POS_AVI_RATIORelative position of the video file: 0 - start of the film, 1 - end of the film.
CV_CAP_PROP_FRAME_WIDTHWidth of the frames in the video stream
CV_CAP_PROP_FRAME_HEIGHTHeight of the frames in the video stream
CV_CAP_PROP_FPSFrame rate
CV_CAP_PROP_FOURCC4-character code of codec
CV_CAP_PROP_FRAME_COUNTNumber of frames in the video file
CV_CAP_PROP_FORMATFormat of the Mat objects returned by retrieve()
CV_CAP_PROP_MODEBackend-specific value indicating the current capture mode
CV_CAP_PROP_BRIGHTNESSBrightness of the image (only for cameras)
CV_CAP_PROP_CONTRASTContrast of the image (only for cameras)
CV_CAP_PROP_SATURATIONSaturation of the image (only for cameras)
CV_CAP_PROP_HUEHue of the image (only for cameras)
CV_CAP_PROP_GAINGain of the image (only for cameras).
CV_CAP_PROP_EXPOSUREExposure (only for cameras)
CV_CAP_PROP_CONVERT_RGBBoolean flags indicating whether images should be converted to RGB.
CV_CAP_PROP_WHITE_BALANCECurrently unsupported
CV_CAP_PROP_RECTIFICATIONRectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend cur- rently)

1.2.5——播放视频文件

import numpy as np
import cv2 as cv
cap = cv.VideoCapture('vtest.avi')
while cap.isOpened():
    ret, frame = cap.read()
    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    cv.imshow('frame', gray)
    if cv.waitKey(1) == ord('q'):
        break
cap.release()
cv.destroyAllWindows()
  • 将frame接收到的每一帧图片,一帧帧播放出来(imshow)

1.2.6——保存视频文件

语法:VideoWriter(output_filename,FourCC,fps,size)

参数:

  • output_filename:文件路径

  • FourCC:编码

  • fps:帧率 (每秒的帧数)

  • size:视频窗口画幅 (width,height)

编码设置:用cv.VideoWriter_fourcc()设置编码,常用编码格式有以下几种:

FourCC视频文件后缀
cv2.VideoWriter_fourcc('M', 'P', '4', 'V').mp4
cv2.VideoWriter_fourcc('X', 'V', 'I', 'D').avi

有关cv2视频操作,cv.VideoCapture,cap.read(),cap.isOpened(),cap.get(propId) cap.set(propIDd,value),VideoWriter的更多相关文章

  1. ruby CSV : How can I read a tab-delimited file? - 2

    CSV.open(name,"r").eachdo|row|putsrowend我得到以下错误:CSV::MalformedCSVErrorUnquotedfieldsdonotallow\ror\n文件名是一个.txt制表符分隔文件。我是专门做的。我有一个.csv文件,我转到excel,并将文件保存为.txt制表符分隔的文件。所以它是制表符分隔的。CSV.open不应该能够读取制表符分隔的文件吗? 最佳答案 尝试像这样指定字段分隔符:CSV.open("name","r",{:col_sep=>"\t"}).eachdo|row|

  2. ruby-on-rails - Rails 单表继承 : How to override the value written to the type field - 2

    在我的系统中,我已经定义了STI。Dog继承自Animal,在animals表中有一个type列,其值为"Dog"。现在我想让SpecialDog继承自dog,只是为了在某些特殊情况下稍微修改一下行为。数据还是一样。我需要通过SpecialDog运行的所有查询,以返回数据库中类型为Dog的值。我的问题是因为我有一个type列,rails将WHERE"animals"."type"IN('SpecialDog')附加到我的查询中,所以我不能获取原始的Dog条目。所以我想要的是以某种方式覆盖rails在通过SpecialDog访问数据库时使用的值,使其表现得像Dog。有没有办法覆盖用于类型

  3. ruby - File.read ("| echo mystring") 是如何工作的? - 2

    我在我正在处理的一些代码中发现了这一点。它旨在解决从磁盘读取key文件的要求。在生产环境中,key文件的内容位于环境变量中。旧代码:key=File.read('path/to/key.pem')新代码:key=File.read('|echo$KEY_VARIABLE')这是如何工作的? 最佳答案 来自IOdocs:Astringstartingwith“|”indicatesasubprocess.Theremainderofthestringfollowingthe“|”isinvokedasaprocesswithappro

  4. ruby - 如何通过 Rubocop 指示打开 & :read as argument to File. - 2

    我有这个代码File.open(file_name,'r'){|file|file.read}但是Rubocop发出警告:Offenses:Style/SymbolProc:Pass&:readasargumenttoopeninsteadofablock.你是怎么做到的? 最佳答案 我刚刚创建了一个名为“t.txt”的文件,其中包含“Hello,World\n”。我们可以按如下方式阅读。File.open('t.txt','r',&:read)#=>"Hello,World\n"顺便说一下,由于第二个参数的默认值是'r',所以这样

  5. ruby - Chef : Read variable from file and use it in one converge - 2

    我有以下代码,它下载一个文件,然后将文件的内容读入一个变量。使用该变量,它执行一个命令。这个配方不会收敛,因为/root/foo在编译阶段不存在。我可以通过多个聚合和一个来解决这个问题ifFile.exist但我想用一个收敛来完成它。关于如何做到这一点有什么想法吗?execute'download_joiner'docommand"awss3cps3://bucket/foo/root/foo"not_if{::File.exist?('/root/foo')}endpassword=::File.read('/root/foo').chompexecute'join_domain'd

  6. ruby-on-rails - self 在 Rails 模型中的值(value)是什么?为什么没有明显的实例方法可用? - 2

    我的rails3.1.6应用程序中有一个自定义访问器方法,它为一个属性分配一个值,即使该值不存在。my_attr属性是一个序列化的哈希,除非为空白,否则应与给定值合并指定了值,在这种情况下,它将当前值设置为空值。(添加了检查以确保值是它们应该的值,但为简洁起见被删除,因为它们不是我的问题的一部分。)我的setter定义为:defmy_attr=(new_val)cur_val=read_attribute(:my_attr)#storecurrentvalue#makesureweareworkingwithahash,andresetvalueifablankvalueisgiven

  7. Ruby:read_timeout 和 open_timeout 之间的区别 - 2

    标题本身就说明了......read_timeout和open_timeout之间有什么区别? 最佳答案 open_timeout是您愿意等待“打开连接”的时间。在TCP上下文中,在放弃尝试并引发超时错误之前等待握手完成的时间量。read_timeout您可能会猜到,是您愿意等待从连接方接收到某些数据的时间。一个例子可能会清楚地说明这一点:在SOAPoverHTTPoverTCP上下文中(简化):您尝试与服务器建立TCP连接。如果建立连接的时间比open_timeout长,则放弃连接尝试并引发/发出/返回超时错误。如果连接成功,您发

  8. ruby - Rails 路由 : Giving default values for path helpers - 2

    有什么方法可以为url/path助手提供默认值吗?我有一个可选范围环绕我的所有路线:#config/routes.rbFoo::Application.routes.drawdoscope"(:current_brand)",:constraints=>{:current_brand=>/(foo)|(bar)/}do#...allotherroutesgohereendend我希望用户能够使用这些URL访问网站:/foo/some-place/bar/some-place/some-place为了方便起见,我在我的ApplicationController中设置了一个@current

  9. ruby - 用于 Ruby 哈希的 map_values()? - 2

    我想念Ruby中的Hash方法来仅转换/映射散列值。h={1=>[9,2,3,4],2=>[6],3=>[5,7,1]}h.map_values{|v|v.size}#=>{1=>4,2=>1,3=>3}你如何在Ruby中归档它?更新:我正在寻找map_values()的实现。#moreexamplesh.map_values{|v|v.reduce(0,:+)}#=>{1=>18,2=>6,3=>13}h.map_values(&:min)#=>{1=>2,2=>6,3=>1} 最佳答案 Ruby2.4引入了方法Hash#tran

  10. ruby Mechanize : multiline value for textarea gets merged - 2

    编辑:经过进一步测试,问题似乎是站点特定的,理论上应该可以正常工作。本应位于多行的Textarea值正在一行中全部提交。textarea_values="value1\nvalue2"form=page.form_with(:id=>'form_id_here')form['my_textarea']=textarea_valuessubmit=form.button_with(:value=>'Submit')form.click_button(submit)提交的值是value1\nvalue2而不是预期的多行。有没有我可以尝试的另一种添加表单值的方法?

随机推荐