• OpenCV3 Python应用-2


    1.视频文件读写

     1 # import cv2
     2 # videoCapture = cv2.VideoCapture('F:\xzb\Working\5555.avi')
     3 # fps = videoCapture.get(cv2.CAP_PROP_FPS)
     4 # size = (int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),
     5 #         int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
     6 # videoWriter = cv2.VideoWriter(
     7 #     'F:\xzb\Working\6666.avi',cv2.VideoWriter_fourcc("I","4","2","0"),
     8 #     fps,size)
     9 #
    10 # success,frame=videoCapture.read()
    11 # while success:
    12 #     videoWriter.write(frame)
    13 #     success,frame = videoCapture.read()

    2.捕获摄像头的帧

     1 import cv2
     2 cameraCapture = cv2.VideoCapture(0)
     3 fps = 30
     4 #帧速率假设值
     5 size = (int(cameraCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),
     6         int(cameraCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
     7 videoWriter = cv2.VideoWriter(
     8     'F:\xzb\Working\Made_video1.avi',cv2.VideoWriter_fourcc("I","4","2","0"),
     9     fps,size)
    10 
    11 success,frame=cameraCapture.read()
    12 numFramesRemaining=10*fps-1
    13 #设置时间
    14 while success and numFramesRemaining >0:
    15     videoWriter.write(frame)
    16     success,frame = cameraCapture.read()
    17     numFramesRemaining -=1
    18 cameraCapture.release()    
     

    cameraCapture类的get()方法不能返回摄像头帧速率的准确值,它总是返回0

    常见于驱动程序仅支持基本功能的系统

    要想针对摄像头构建合适的VideoWriter类,要么对帧速率作出假设,要么使用计时器来测量(推荐后者)

    摄像头的数量和顺序由系统决定,但OpenCV不提供查询摄像头的数量和顺序的方法

    如果使用无效索引构建了VideoCapture类,就不会得到帧,VideoCapture的read()返回(false,None)

    为了避免这种情况,在执行该函数之后使用VideoCapture.isOpened方法做一个判断,返回一个Boolean值

    当需要同步一组或者多头摄像头时,read()方法不再合适,可使用grab()和retrive()方法

      如:

    1 success0=cameraCapture0.grab()
    2     success1=cameraCapture1.grab()
    3     if success0 and success1:
    4         frame0=cameraCapture0.retrive()
    
    

    3.在窗口显示图像

    1 import cv2
    2 import numpy as np
    3 
    4 img = cv2.imread('test.jpg')
    5 cv2.imshow('new_test.png',img)      #imshow()两个参数:显示图像的帧名称,要显示的图像本身
    6 cv2.waitKey()
    7 cv2.destroyAllWindows()              #释放由OpenCV创建的所有窗口
    
    

    4.在窗口显示摄像头帧

    namedWindow(),imshow(),DestoryWindow()函数允许指定窗口名来创建,显示,销毁窗口
    任意窗口下都可以通过waitKey()函数获取键盘输入,通过setMouseCallback()函数获取鼠标输入
    waitKey()的参数为等待键盘触发时间,单位为毫秒,其返回值是-1或者ASCII码(http://asciitable.com/)
    "注:有些系统,waitKey()返回值比ASCII偏大(LINUX中使用GTK为GUI库时有bug),可通过读取返回值最后一个字节提取ASCII码"
    如:
    keycode = cv2.waitKey(1)
    if keycode != -1:
    keycode &=0xFF
    OpenCV的窗口函数和waitKey()函数相互依赖
    窗口函数只有调用waitKey()才更新,waitKey()只有在OpenCV成为活动窗口才捕获输入信息
    鼠标回调函数setMouseCallback()有五个参数,param为第三个是可选参数,默认是0
    回调事件参数取不同的值对应不同的鼠标事件(具体见OpenCV3计算机视觉P.26)
    OpenCV不提供任何处理窗口事件方法(如:点击窗口关闭按钮,无效),所以一般开发时将其嵌入应用程序框架中
    
    
     1 import cv2
     2 clicked = False
     3 def onMouse(event, x, y, flags, param):
     4     global clicked
     5     if event == cv2.EVENT_LBUTTONUP:
     6         clicked = True
     7 
     8 cameraCapture = cv2.VideoCapture(0)
     9 cv2.namedWindow('MyWindow')
    10 cv2.setMouseCallback('MyWindow',onMouse)
    11 
    12 print('cbsujkbc jskb ')
    13 success, frame = cameraCapture.read()
    14 while success and cv2.waitKey(1) == -1 and not clicked:
    15     cv2.imshow('MyWindow',frame)
    16     success, frame = cameraCapture.read()
    17 
    18 cv2.destroyWindows('MyWindow')
    19 cameraCapture.release()


    格式啥的太乱了,还得熟悉在这写博客才行.

  • 相关阅读:
    python的argparse模块
    Robotframework之SSHLibrary库
    Python 中的 getopt 模块
    Python list 列表
    Samba windows 10 share: mount error(112): Host is down
    安装两个版本的python安装包,后安装的python程序打开时闪退
    NetScaler VPX configration
    drupal smtp could not connect to smtp
    drupal7 判断用户是否具有某个权限
    微信支付报错:time_expire时间过短,刷卡至少1分钟,其他5分钟]
  • 原文地址:https://www.cnblogs.com/xzb0-0/p/10211278.html
Copyright © 2020-2023  润新知