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

时间:2025-05-07 07:12:03

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

import cv2
import numpy as np
import pickle
import  as plt

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

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

# When everything done, release the capture
()
()

2、从摄像头内抓拍图片

import cv2
import numpy as np
import pickle
import  as plt

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

# When everything done, release the capture
()
()

用python来测相机距离/84378/