opencv捕捉摄像头及用 Python 和 OpenCV 来测量相机到目标的距离

1、捕获摄像头和实时显示

import cv2
import numpy as np
import pickle
import matplotlib.pyplot as plt

cap = cv2.VideoCapture(0) #抓拍视频,0为id号

while True:
    ret,frame = cap.read() #ret是返回下一帧,frame是读取该文件的内容
    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # Display the resulting frame
    cv2.imshow('frame',gray)
    #等待1秒按下q键退出
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

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

2、从摄像头内抓拍图片

import cv2
import numpy as np
import pickle
import matplotlib.pyplot as plt

cap = cv2.VideoCapture(0)
index = 0
while True:
    ret,frame = cap.read()
    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # Display the resulting frame
    cv2.imshow('frame',gray)
    #等待1秒,按下则将图片写入kk.jpg,且index文件数+1;按下q则退出
    if cv2.waitKey(1) & 0xFF == ord('p'):
        cv2.imwrite("kk.jpg",frame)
        index = index + 1
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

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

用python来测相机距离http://python.jobbole.com/84378/

展开阅读全文

没有更多推荐了,返回首页