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

(点击上方蓝字，快速关注我们)

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

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

46 marker = find_marker(image)

47 inches = 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 中相机到目标的距离。

• 本文已收录于以下专栏：

## 简介Opencv在Python中的使用

OpenCV是一个C++库，用于实时处理计算机视觉方面的问题，涵盖了很多计算机视觉领域的模块。...
• nature_XD
• 2017年04月25日 23:02
• 10990

## OpenCV Python教程（1、图像的载入、显示和保存）

• sunny2038
• 2013年06月12日 17:52
• 242489

## 在python3.5中使用OpenCV

• qxconverse
• 2017年03月02日 21:38
• 12442

## python opencv 绘制简单图形

• huanglu_thu13
• 2016年08月27日 23:02
• 3154

## 用Python+OpenCV让电脑帮你玩微信跳一跳

• moneydboat
• 2017年12月31日 13:36
• 4377

## Python-OpenCV(5)

• Gavin__Zhou
• 2016年04月06日 09:43
• 1296

## 一个无聊的python + opencv 示例

• haluoluo211
• 2016年10月13日 15:13
• 1922

## Python和OpenCV环境配置

• GarfieldEr007
• 2016年03月05日 20:34
• 5698

## windows下的python+ opencv安装攻略

• qq_14845119
• 2016年08月29日 16:37
• 48688

## OpenCV-Python教程（8、Canny边缘检测）

• sunny2038
• 2013年06月30日 17:48
• 35406

举报原因： 您举报文章：用 Python 和 OpenCV 来测量相机到目标的距离 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)