单目测距原理参考博客
在以上博客中,测距原理博主介绍的很好,主要贴下自己写的python测试代码,做个简单的记录。
import os
import cv2
import time
import numpy as np
import math
def dist_cab():
//图像分辨率
frame_width = 1280
frame_height = 720
//相机内参
camera_cx = 628.060
camera_cy = 345.411
camera_fx = 716.168
camera_fy = 715.988
//标定点像素、相机高度、标定点实际距离
cab_pixel_y = 650
camera_h = 0.735*1000
cab_dist = 1.61*1000
//求主点对应的实际距离,与相机的角度
a1 = math.atan(camera_h / cab_dist)
a2 = math.atan((cab_pixel_y - camera_cy) / camera_fy)
a3 = a1 - a2
op_dist = camera_h * math.atan(a3)
//求任意像素点的y方向实际距离
if_pixel_y = 600
b1 = math.atan((if_pixel_y - camera_cy) / camera_fy)
b2 = a3 + b1
if_dist = camera_h / math.tan(b2)
print(if_dist)
//求该像素点的x方向实际距离
if_pixel_x = 900
pixel_y_dist = math.sqrt(pow((if_pixel_y - camera_cy), 2) + pow(camera_fy, 2))
dist_y_slope = camera_h / math.sin(b2)
dist_x = dist_y_slope * (if_pixel_x - camera_cx) / pixel_y_dist
print(dist_x)
if __name__ == '__main__':
dist_cab()
print('exit...')