【发布时间】:2015-07-30 16:34:04
【问题描述】:
嗨,我想知道这是否可能,因为我试过了,但我的变量总是空的。在我的项目中,我通过树莓派上的 PiCamera 跟踪静态对象和激光指示器,并将它们的轮廓的质心分别计算为 (smallx,smally) 和 (small2x,small2y)。
我使用它们坐标之间的差异来查看指针是否应该向上、向下、向左或向右移动,以便与第一个静态对象相遇。之后,它会选择 1 到 4 之间的方向移动,因为我的方向控件在 x-y 轴上并不完美并且是倾斜的。
我留下了从这里查找的控件和轮廓,并缩短了我的总代码,这样你就不会遇到一大堆垃圾来整理。
编辑:根据我的理解,我不认为我可以在不发布几百行代码和我的小设备的情况下提供可运行的东西,但我会把它归结为相关的代码的确切部分。运行 Python 2.7.3,使用 opencv2.4.10
代码:
#import libraries like picamera and opencv
#set empty variables like:
up = down = left = right = set()
smallx = smally = small2x = small2y = 0
#etc etc
with picamera.PiCamera() as camera:
with picamera.array.PiRGBArray(camera) as rawCapture:
#Calibrate my controls with the camera. updates the up, down, left, and right sets.
with picamera.PiCamera() as camera:
with picamera.array.PiRGBArray(camera) as rawCapture:
# Take pictures, threshold them, find contours, append their arrays to list
if len(cnts)>0: #If any objects were identified
contm = sorted(smalList, key=lambda tup: tup[1])
smallest = cnts[smalList[0][0]] #**Take smallest object(my static object)**
smallM = cv2.moments(smallest)
smallx = int(smallM['m10']/smallM['m00']) #**Calculate xcoord**
smally = int(smallM['m01']/smallM['m00']) #**Calculate ycoord**
cv2.line(frame, (smallx,smally), (smallx,smally), 1, 8,0) #Draws centroid
# print(len(cnts))
if len(cnts)==2: #If only 2 objects were identified
smallester = cnts[smalList[1][0]] #** Take pointer object **
small2 = cv2.moments(smallester)
small2x = int(small2['m10']/small2['m00']) #**Calculate xcoord**
small2y = int(small2['m01']/small2['m00']) #**Calculate ycoord**
x = small2x - smallx
y = small2y - smally
print x #These prints return a value
print y
if x < 0: #Difference = Pointer - Object
s1 = right
if x >0:
s1 = left
if y < 0:
s2 = down
if y >0:
s2 = up
print s1, s2 #set([]),set([])
print up,down,left,right #set([1,2]),set([3,4]),set([1,4]),set([2,3])
selecty = s1&s2 #set([])
#Tell the pointer where to go
我什至应该使用集合吗?
【问题讨论】:
-
如果
s1和s2始终仍然是0,那么您的任何if条件都不会评估 true-y。根据您发布的内容,我们无法告诉您原因。但是当然可以分配一个集合,达到那些行。 -
small2x,smallx = 5,3 -
set(1, 2)是TypeError。你是说set([1, 2]),还是{1, 2}? -
我检查了我的 if 条件,我收到了 0 和 -83 之类的值,所以他们应该评估,是的,我做了,让我编辑它
-
我添加了更多,但我理解的不够多,无法提供可以运行的东西。我希望你能明白我在做什么,大部分其他代码都没有参与这部分,但也许它与范围或我的 python 版本有关?我很困惑,因为它看起来很适合我未经训练的眼睛
标签: python variables opencv set variable-assignment