Zi 字媒體
2017-07-25T20:27:27+00:00
使用OpenCV實現攝像頭測距 [Find distance from camera to object/marker using Python and OpenCV] (GOOGLE:OPENCV 測距)
資料來源:
https://zhuanlan.zhihu.com/p/63149294
https://www.pyimagesearch.com/2015/01/19/find-distance-camera-objectmarker-using-python-opencv/
https://github.com/zxdefying/OpenCV_project/tree/master/distance_to_camera
GITHUB:https://github.com/jash-git/Find-distance-from-camera-to-object-using-Python-and-OpenCV
前置動作+原理說明:(使用相似三角形計算物體到相機的距離)
假設物體的寬度為W,將其放到離相機距離為D 的位置,然後對物體進行拍照。在照片上量出物體的像素寬度P,於是可以得出計算相機焦距F 的公式:
F =(P x D)/ W
比如我在相機前24 英寸距離(D=24 inches)的位置橫著放了一張8.5 x 11 英寸(W=11 inches)的紙,拍照後通過圖像處理得出照片上紙的像素寬度P=248 pixels。
所以焦距F 等於:
F =(248px x 24in)/ 11in = 543.45
此時移動相機離物體更近或者更遠,我們可以應用相似三角形得到計算物體到相機的距離的公式:
D’=(寬x F)/ P
PDF
Code:
from imutils import paths
import numpy as np
import imutils
import cv2
# 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 12 inches wide
KNOWN_WIDTH = 11.0
def get_focalLength():
# 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("./2ft.jpg")
marker = find_marker(image)
focalLength = (marker[1][0] * KNOWN_DISTANCE) / KNOWN_WIDTH
return focalLength
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)
# the contour of paper is not closed, so apply close operation(dilate and erode)
kernel = np.ones((3, 3), np.uint8)
close = cv2.morphologyEx(edged, cv2.MORPH_CLOSE, kernel)
# 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(close.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
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
if __name__ == "__main__":
focalLength = get_focalLength()
# loop over the images
for imagePath in sorted(paths.list_images("images")):
# load the image, find the marker in the image, then compute the
# distance to the marker from the camera
image = cv2.imread(imagePath)
marker = find_marker(image)
inches = distance_to_camera(KNOWN_WIDTH, focalLength, marker[1][0])
# draw a bounding box around the image and display it
box = cv2.cv.BoxPoints(marker) if imutils.is_cv2() else cv2.boxPoints(marker)
box = np.int0(box)
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(imagePath.split('/')[-1], image)
if cv2.waitKey(0) & 0xFF == ord('q'):
cv2.destroyAllWindows()
寫了
5860316篇文章,獲得
23313次喜歡