- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我和我的 friend 们正在组装这个有人发布在这个网站上的自制激光测距仪:http://shaneormonde.wordpress.com/2014/01/25/webcam-laser-rangefinder/ .一切顺利,直到我们进入 python 代码部分,因为我们不熟悉任何编程语言。正如网站上的项目所描述的那样,它应该检测网络摄像头视野中的红点。不幸的是,我们已经购买了绿色激光器。您如何更改代码以使其检测到绿点?这是代码:
## 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 "\033[12;0H" + "the dot is " + str(obj_dist) + "cm away"
elif rval == 0:
print " webcam error "
最佳答案
rval, frame = vc.read()
读取 BGR 格式的图像。您直接链接的教程页面说明了这一点。
So all of the pixel data is stored in a numpy array called frame. By default opencv grabs a 640 x 480 image ( thats 640 on the x-axis and 480 on the y ). The numpy array has three dimensions, (x)(y)(BGR), so the first two dimensions have just one number in each specifying the x or y co-ordinate of the pixel. The third dimension contains three numbers, the RGB content of that pixel. Although for some reason the values are arranged BGR.
因此,换行
num = (frame[...,...,2] > 236)
到
num = (frame[...,...,1] > 236)
应该能帮到你。
为了非常简要地解释这条线的作用,它检查颜色平面中的高强度值(超过 236 - 其中 min=0 和 max=255)并记录所有这些像素。这个想法是,如果你发射红色激光,红色平面将在点区域周围集中高强度值。
关于python - 我如何更改此激光测距仪 python 代码以检测绿点而不是红点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27886053/
我正在使用 codeigniter,我的程序在本地主机中运行良好,但在网上出现错误。很难描述,但一切就像图片一样: 更糟糕的是,如果我在 js 中编写这样的代码: var a='{"status":0
我想知道是否有人知道您是否可以使用 JQuery Mobile 在页脚选项卡上添加红点/圆圈? 请参阅所附的示例图片。 最佳答案 你可以试试: http://thrivingkings.com/bad
我是一名优秀的程序员,十分优秀!