激光测距仪网络摄像头 DIY Python
Laser Rangefinder Webcam DIY Python
在网上搜索 DIY 激光测距仪网络摄像头之后;我找到了一个很酷的项目,它以厘米为单位找到激光的距离。我想知道是否有人可以帮助我更改一些代码行,以便它可以用小数来测量以码为单位的距离(就像分数一样)。
这是我找到的网站 link webcam laser rangefinder
我对三角学一无所知,除了我记得的高中代数和几何外,我一无所知。换句话说,我不明白这个 link 所讲的数学;这就是为什么,我要求某人向我展示一个代码示例,如果它以码而不是厘米为单位进行测量,代码会是什么样子。提前谢谢你。
这是代码
## program written by Shane Ormonde 7th sept 2013
## updated on 25th January 2014
## calculates the distance of a red dot in the field of view of the webcam.
import cv2
from numpy import *
import math
#variables
loop = 1
dot_dist = 0
cv2.namedWindow("preview")
vc = cv2.VideoCapture(1)
if vc.isOpened(): # try to get the first frame
rval, frame = vc.read()
else:
rval = False
#print "failed to open webcam"
if rval == 1 :
while loop == 1:
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
loop = 0
num = (frame[...,...,2] > 236)
xy_val = num.nonzero()
y_val = median(xy_val[0])
x_val = median(xy_val[1])
dist = ((x_val - 320)**2 + (y_val - 240)**2 )**0.5 # distance of dot from center pixel
dist = abs(x_val - 320) # distance of dot from center x_axis only
print " dist from center pixel is " + str(dist)
# work out distance using D = h/tan(theta)
theta =0.0011450*dist + 0.0154
tan_theta = math.tan(theta)
if tan_theta > 0: # bit of error checking
obj_dist = int(5.33 / tan_theta)
print "3[12;0H" + "the dot is " + str(obj_dist) + "cm away"
elif rval == 0:
print " webcam error "
假设码=厘米*0.010936
更改此块:
if tan_theta > 0: # bit of error checking
obj_dist = int(5.33 / tan_theta)
为此:
if tan_theta > 0: # bit of error checking
obj_dist = int(5.33 / tan_theta)
#convert from centimeters to yards
obj_dist = obj_dist * 0.010936
如果我们查看它告诉我们点有多远的部分,我们会看到 print "3[12;0H" + "the dot is " + str(obj_dist) + "cm away"
行。这告诉我们距离存储在变量 obj_dist
中。所以我通过代码回头看看 obj_dist 是如何得到它的值的。唯一一次引用 obj_dist
是在线:obj_dist = int(5.33 / tan_theta)
。查看该行的另一种方法是 obj_dist = centimters_away
。所以要得到码,我们所要做的就是添加一条线,将 obj_dist
从厘米转换为码,这就是我所做的。
在网上搜索 DIY 激光测距仪网络摄像头之后;我找到了一个很酷的项目,它以厘米为单位找到激光的距离。我想知道是否有人可以帮助我更改一些代码行,以便它可以用小数来测量以码为单位的距离(就像分数一样)。 这是我找到的网站 link webcam laser rangefinder
我对三角学一无所知,除了我记得的高中代数和几何外,我一无所知。换句话说,我不明白这个 link 所讲的数学;这就是为什么,我要求某人向我展示一个代码示例,如果它以码而不是厘米为单位进行测量,代码会是什么样子。提前谢谢你。
这是代码
## program written by Shane Ormonde 7th sept 2013
## updated on 25th January 2014
## calculates the distance of a red dot in the field of view of the webcam.
import cv2
from numpy import *
import math
#variables
loop = 1
dot_dist = 0
cv2.namedWindow("preview")
vc = cv2.VideoCapture(1)
if vc.isOpened(): # try to get the first frame
rval, frame = vc.read()
else:
rval = False
#print "failed to open webcam"
if rval == 1 :
while loop == 1:
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
loop = 0
num = (frame[...,...,2] > 236)
xy_val = num.nonzero()
y_val = median(xy_val[0])
x_val = median(xy_val[1])
dist = ((x_val - 320)**2 + (y_val - 240)**2 )**0.5 # distance of dot from center pixel
dist = abs(x_val - 320) # distance of dot from center x_axis only
print " dist from center pixel is " + str(dist)
# work out distance using D = h/tan(theta)
theta =0.0011450*dist + 0.0154
tan_theta = math.tan(theta)
if tan_theta > 0: # bit of error checking
obj_dist = int(5.33 / tan_theta)
print "3[12;0H" + "the dot is " + str(obj_dist) + "cm away"
elif rval == 0:
print " webcam error "
假设码=厘米*0.010936
更改此块:
if tan_theta > 0: # bit of error checking
obj_dist = int(5.33 / tan_theta)
为此:
if tan_theta > 0: # bit of error checking
obj_dist = int(5.33 / tan_theta)
#convert from centimeters to yards
obj_dist = obj_dist * 0.010936
如果我们查看它告诉我们点有多远的部分,我们会看到 print "3[12;0H" + "the dot is " + str(obj_dist) + "cm away"
行。这告诉我们距离存储在变量 obj_dist
中。所以我通过代码回头看看 obj_dist 是如何得到它的值的。唯一一次引用 obj_dist
是在线:obj_dist = int(5.33 / tan_theta)
。查看该行的另一种方法是 obj_dist = centimters_away
。所以要得到码,我们所要做的就是添加一条线,将 obj_dist
从厘米转换为码,这就是我所做的。