# 用 Python 和 OpenCV 来测量相机到目标的距离

OpenCV 和 Python 版本： 这个例子可以在 Python 2.7/Python 3.4+ 和 OpenCV 2.4.X上运行。

### 用相似三角形计算物体或者目标到相机的距离

F = (P x D) / W

F = (248px x 24in) / 11in = 543.45

D’ = (W x F) / P

D’ = (11in x 543.45) / 170 = 35 英寸

### 用Python和OpenCV来测量相机到目标的距离

# 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

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

#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

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 = 24.0

# initialize the known object width, which in this case, the piece of

# paper is 11 inches wide

KNOWN_WIDTH = 11.0

# initialize the list of images that we'll be using

IMAGE_PATHS = ["images/2ft.png", "images/3ft.png", "images/4ft.png"]

# 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

marker = find_marker(image)

focalLength = (marker[1][0] * KNOWN_DISTANCE) / KNOWN_WIDTH

• 相机到物体的距离

• 这个物体的宽度（单位英尺或米）。注意：也可以用高度，这个例子中我们使用宽度。

41 # loop over the images

42 for imagePath in IMAGE_PATHS:

# load the image, find the marker in the image, then compute the

# distance to the marker from the camera

46marker = find_marker(image)

47inches = distance_to_camera(KNOWN_WIDTH, focalLength, marker[1][0])

# draw a bounding box around the image and display it

box = np.int0(cv2.cv.BoxPoints(marker))

cv2.drawContours(image, [box], -1, (0, 255, 0), 2)

cv2.putText(image, "%.2fft" % (inches / 12),

(image.shape[1] - 200, image.shape[0] - 20), cv2.FONT_HERSHEY_SIMPLEX,

2.0, (0, 255, 0), 3)

cv2.imshow("image", image)

cv2.waitKey(0)

### 结果

\$ python distance_to_camera.py

### 总结

1、 目标的实际宽度（或高度），单位可以是英寸或者米。

2、 标定过程 1 中相机到目标的距离。

• ## http://python.jobbole.com/84378/

• 程序员大咖整理发布，转载请联系作者获得授权

07-19
07-15 2万+

05-22 4万+
08-23 1万+
01-23 1776
04-12 7486
05-22