# 用 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 英寸

# import the necessary packages

importnumpyasnp

importcv2

deffind_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),)

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

returncv2.minAreaRect(c)

defdistance_to_camera(knownWidth,focalLength,perWidth):

# compute and return the distance from the maker to the camera

return(knownWidth*focalLength)/perWidth

#import the necessary packages

importnumpyasnp

importcv2

deffind_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),)

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

returncv2.minAreaRect(c)

defdistance_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][]*KNOWN_DISTANCE)/KNOWN_WIDTH

37 行从硬盘读取第一张图，——我们将用这张图来作为标定图片。

41# loop over the images

42forimagePathinIMAGE_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][])

# draw a bounding box around the image and display it

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

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

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

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

2.0,(,255,),3)

cv2.imshow("image",image)

cv2.waitKey()

\$pythondistance_to_camera.py

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

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

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

Python开发整理发布，转载请联系作者获得授权

• 发表于:
• 原文链接http://kuaibao.qq.com/s/20180118B0RFQG00?refer=cp_1026
• 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。
• 如有侵权，请联系 cloudcommunity@tencent.com 删除。

2022-08-13

2022-08-13

2022-08-13

2022-08-13

2022-08-13

2022-08-13

2022-08-13