赞
踩
系统:win 10
YOLO版本:yolov5 6.1
拍摄视频设备:安卓手机
电脑显卡:NVIDIA 2080Ti(CPU也可以跑,GPU只是起到加速推理效果)
单目测距原理相较于双目十分简单,无需进行立体匹配,仅需利用下边公式线性转换即可:
D = (F\*W)/P
其中D是目标到摄像机的距离, F是摄像机焦距(焦距需要自己进行标定获取), W是目标的宽度或者高度(行人检测一般以人的身高为基准), P是指目标在图像中所占据的像素
了解基本原理后,下边就进行实操阶段
可以参考张友正标定法获取相机的焦距
直接使用代码获得焦距,需要提前拍摄一个矩形物体,拍摄时候相机固定,距离被拍摄物体自行设定,并一直保持此距离,背景为纯色,不要出现杂物;最后将拍摄的视频用以下代码检测:
import cv2 win_width = 1920 win_height = 1080 mid_width = int(win_width / 2) mid_height = int(win_height / 2) foc = 1990.0 # 根据教程调试相机焦距 real_wid = 9.05 # A4纸横着的时候的宽度,视频拍摄A4纸要横拍,镜头横,A4纸也横 font = cv2.FONT_HERSHEY_SIMPLEX w_ok = 1 capture = cv2.VideoCapture('5.mp4') capture.set(3, win_width) capture.set(4, win_height) while (True): ret, frame = capture.read() # frame = cv2.flip(frame, 1) if ret == False: break gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (5, 5), 0) ret, binary = cv2.threshold(gray, 140, 200, 60) # 扫描不到纸张轮廓时,要更改阈值,直到方框紧密框住纸张 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) binary = cv2.dilate(binary, kernel, iterations=2) contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # cv2.drawContours(frame, contours, -1, (0, 255, 0), 2) # 查看所检测到的轮框 for c in contours: if cv2.contourArea(c) < 1000: # 对于矩形区域,只显示大于给定阈值的轮廓,所以一些微小的变化不会显示。对于光照不变和噪声低的摄像头可不设定轮廓最小尺寸的阈值 continue x, y, w, h = cv2.boundingRect(c) # 该函数计算矩形的边界框 if x > mid_width or y > mid_height: continue if (x + w) < mid_width or (y + h) < mid_height: continue if h > w: continue if x == 0 or y == 0: continue if x == win_width or y == win_height: continue w_ok = w cv2.rectangle(frame, (x + 1, y + 1), (x + w_ok - 1, y + h - 1), (0, 255, 0), 2) dis_inch = (real_wid \* foc) / (w_ok - 2) dis_cm = dis_inch \* 2.54 # os.system("cls") # print("Distance : ", dis\_cm, "cm") frame = cv2.putText(frame, "%.2fcm" % (dis_cm), (5, 25), font, 0.8, (0, 255, 0), 2) frame = cv2.putText(frame, "+", (mid_width, mid_height), font, 1.0, (0, 255, 0), 2) cv2.namedWindow('res', 0) cv2.namedWindow('gray', 0) cv2.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。