单目摄像头测量距离:相似三角形法
已知一点经纬度和距离,计算另一点的经纬度,详见: https://blog.csdn.net/qq_41694461/article/details/123130094.
用相似三角形法目标到相机的距离
计算相机焦距
目前摄像机大多数都是自动对焦的,所以我们第一步就要求出相机焦距,公式如下:
f = (w*L)/W
f = (h*L)/H
f:镜头焦距
w:图象的宽度(被摄物体在ccd靶面上成象宽度即目标的像素宽度)
W:被摄物体宽度
L:被摄物体至镜头的距离
h:图象高度(被摄物体在ccd靶面上成像高度)视场(摄取场景)高度
H:被摄物体的高度
ccd靶面规格尺寸 | 单位mm |
---|---|
规格 | W H |
1/3" | 4.8 3.6 |
1/2" | 6.4 4.8 |
2/3" | 8.8 6.6 |
1" | 12.7 9.6 |
摄像机画面宽度和高度与监视器接收机画面宽度和高度一样,其比例均为4:3,当L不变,H或W增大时,f变小,当H或W不变,L增大时,f增大。
eg:假设我在离相机距离 D = 24 英寸的地方放一张标准的 8.5 x 11 英寸的 A4 纸(横着放;W = 11)并且拍下一张照片。我测量出照片中 A4 纸的像素宽度为 P = 249 像素。
因此我的焦距 F 是:
F = (248px x 24in) / 11in = 543.45
假设我将相机移到距离目标 3 英尺(或者说 36 英寸)的地方并且拍下上述的 A4 纸。通过自动的图形处理我可以获得图片中 A4 纸的像素距离为 170 像素。将这个代入公式得:
像素距离:即目标在图像中的宽度/高度
D’ = (11in x 543.45) / 170 = 35 英寸
我们可以看到,要想得到距离,我们就要知道摄像头的焦距和目标物体的尺寸大小,这两个已知条件根据公式:
D’ = (W x F) / P
代码实现
PYthon代码实现:
# import the necessary packages
import numpy as np
import cv2
# 找到目标函数
def find_marker(image):
# convert the image to grayscale, blur it, and detect edges
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(gray, 35, 125)
# find the contours in the edged image and keep the largest one;
# we'll assume that this is our piece of paper in the image
(cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
# 求最大面积
c = max(cnts, key=cv2.contourArea)
# compute the bounding box of the of the paper region and return it
# cv2.minAreaRect() c代表点集,返回rect[0]是最小外接矩形中心点坐标,
# rect[1][0]是width,rect[1][1]是height,rect[2]是角度
return cv2.minAreaRect(c)
# 距离计算函数
def distance_to_camera(knownWidth, focalLength, perWidth):
# compute and return the distance from the maker to the camera
return (knownWidth * focalLength) / perWidth
# initialize the known distance from the camera to the object, which
# in this case is 24 inches
KNOWN_DISTANCE = 18.11
# initialize the known object width, which in this case, the piece of
# paper is 11 inches wide
# A4纸的长和宽(单位:inches)
KNOWN_WIDTH = 11.69
KNOWN_HEIGHT = 8.27
# initialize the list of images that we'll be using
IMAGE_PATHS = ["img.png", "img_1.png", "Picture3.jpg"]
# load the furst image that contains an object that is KNOWN TO BE 2 feet
# from our camera, then find the paper marker in the image, and initialize
# the focal length
# 读入第一张图,通过已知距离计算相机焦距
image = cv2.imread(IMAGE_PATHS[0])
print(image.shape)
marker = find_marker(image)
print(marker[1][0])
focalLength = (marker[1][0] * KNOWN_DISTANCE) / KNOWN_WIDTH
# 通过摄像头标定获取的像素焦距
# focalLength = 811.82
print('focalLength = ', focalLength)
# 打开摄像头
camera = cv2.VideoCapture(0)
while camera.isOpened():
# get a frame
(grabbed, frame) = camera.read()
marker = find_marker(frame)
if marker == 0:
print(marker)
continue
# focalLength = (marker[1][0] * KNOWN_DISTANCE) / KNOWN_WIDTH
inches = distance_to_camera(KNOWN_WIDTH, focalLength, marker[1][0])
print("a4值得宽",KNOWN_WIDTH, focalLength, marker[1][0], inches)
# draw a bounding box around the image and display it
# box = np.int0(cv2.cv.BoxPoints(marker))
box = cv2.boxPoints(marker)
box = np.int0(box)
cv2.drawContours(frame, [box], -1, (0, 255, 0), 2)
# inches 转换为 cm
cv2.putText(frame, "%.2fcm" % (inches * 30.48 / 12),
(frame.shape[1] - 200, frame.shape[0] - 20), cv2.FONT_HERSHEY_SIMPLEX,
2.0, (0, 255, 0), 3)
# show a frame
cv2.imshow("capture", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
camera.release()
cv2.destroyAllWindows()
效果图:
![在这里插入图片描述](https://img-blog.csdnimg.cn/a1f98441737942a985ed3f6d16b5c29e.png
总结
1 我们首先要计算出焦距大小
2 要知道我们求得目标的宽度(对于目标在像素中重叠,或者遮挡,这个方法应该不可以用)