我正在使用运行 Raspbian Wheezy 并配备 USB 网络摄像头的 Raspberry Pi B+。我的目标是实时测量物体与相机之间的距离。
正在关注 a guide on how to do so with still images
这是我目前正在运行的代码:
# import the necessary packages
import numpy as np
import cv2
import datetime
import time
def find_marker(frame):
# convert the image to grayscale, blur it, and detect edges
gray = cv2.cvtColor(frame, 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 = 11.811
# initialize the known object width, which in this case, the piece of
# paper is 11 inches wide
KNOWN_WIDTH = 2.3622
# 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
#image = cv2.imread(IMAGE_PATHS[0])
#marker = find_marker(image)
#focalLength = (marker[1][0] * KNOWN_DISTANCE) / KNOWN_WIDTH
cap = cv2.VideoCapture(0)
timestamp = datetime.datetime.now()
while(1):
(grabbed, frame) = cap.read()
marker = find_marker(frame)
# for () LOOP THIS TO GET DISTANCE CALCULATION FULLY WORKING!
focalLength = (marker[1][0] * KNOWN_DISTANCE) / KNOWN_WIDTH
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(frame, [box], -1, (0, 255, 0), 2)
ts = timestamp.strftime("%A %d %B %Y %I:%M:%S%p")
cv2.putText(frame, "%.2fft" % (inches / 12),
(frame.shape[1] - 200, frame.shape[0] - 20), cv2.FONT_HERSHEY_SIMPLEX,
2.0, (0, 255, 0), 3)
cv2.putText(frame, ts, (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX,
0.35, (0, 0, 255), 1)
#Write to textfile here and send
# for () LOOP End
cv2.imshow("Frame",frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
# loop over the images
#for imagePath in IMAGE_PATHS:
# 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 = 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)
这是我的输出:
但是,时间(左下角的红色文本)和检测到的距离不会随着程序运行而改变。有没有办法让这两个值在程序结束之前更新?
最佳答案
这就是两个值都没有更新的原因:
时间戳
timestamp
不在 while
循环中
timestamp = datetime.datetime.now()
while(1):
应该是:
while(1):
timestamp = datetime.datetime.now()
距离
distance_to_camera
,使用这个参数,将产生一个恒定的输出:
# assuming:
# a = KNOWN_WIDTH
# b = focalLength
# c = marker[1][0]
# d = KNOWN_DISTANCE
def distance_to_camera(x,y,z):
return (x*y)/z
b = c*d/a
inches = distance_to_camera(a,b,c) # => a*b/c
# inches = a*b/c, b = c*d/a
# inches = a*c*d/a*c
# inches = d << constant output
等于 KNOWN_DISTANCE
。如果您计算一下:KNOWN_DISTANCE/12 = 0.98425
是您获得的距离
编辑:
我刚刚阅读了教程,看起来您应该只在 while
中执行一次 focalLenght
计算。
关于python - OpenCV python - 如何不断更新时间输出和距离输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34908986/