2015-12-03 99 views
1

我通过一个实时两个相机流提取2个blob的x位置。我可以得到第一个x位置没有问题,因为它是作为元组给出的(ex = ... object at(455,69))。问题是我需要第二个blob左下角的x位置,但它返回为一个numpy数组,'blob.x'不起作用。我怎样才能得到numpy数组的x位置?任何帮助/指导非常感谢。从numpy数组中提取X坐标

我收到以下错误: ValueError:具有多个元素的数组的真值是不明确的。使用a.any()或a.all()

from SimpleCV import * 
def getscoreforrgb(rgb): 
    return rgbmap[rgb] 


mog1 = MOGSegmentation(history = 200, nMixtures = 5, backgroundRatio = 0.9, noiseSigma = 16, learningRate = 0.9) 
mog0 = MOGSegmentation(history = 200, nMixtures = 5, backgroundRatio = 0.9, noiseSigma = 16, learningRate = 0.9) 
cam1 = SimpleCV.Camera(1, {'width': 640, 'height': 480 }) 
cam0 = SimpleCV.Camera(0, {'width': 640, 'height': 480 }) 
pixcol = Image('/home/pi/Darts/score/scoreboardpy.png') 



while True: 

    frame1 = cam1.getImage() 
    frame0 = cam0.getImage().flipHorizontal() 
    mog1.addImage(frame1) 
    mog0.addImage(frame0) 
    segmentedImage1 = mog1.getSegmentedImage() 
    segmentedImage0 = mog0.getSegmentedImage() 


#### second blob below, does not print x position  

    blobs0 = segmentedImage0.findBlobs() 
    if blobs0 is not None: 
      blobs0.sortArea() 
      blobs0[-1].draw(Color.BLUE, width=4) 
      first_blob = blobs0[-1] 
      bottomLeftCorner = second_blob.bottomLeftCorners() 
      print bottomLeftCorner 
      if bottomLeftCorner: 
        print bottomLeftCorner.x, 
        y = int(bottomLeftCorner.x) 
        print y * 2, 'Y' 
        y2 = y * 2 

#### First blob below, code prints x position  

    blobs1 = segmentedImage1.findBlobs() 
    if blobs1 is not None: 
      blobs1.sortArea() 
      blobs1[-1].draw(Color.RED, width=4) 
      second_blob = blobs1[-1] 
      if second_blob: 
        print second_blob.x, 
        x = int(second_blob.x) 
        print x * 2, 'X' 
        x2 = x * 2 


      colrgb = pixcol[x2, y2] 
      print colrgb 
+0

哪一行会抛出错误? – Julien

+0

当我尝试将'bottomLeftCorner.x'作为'y2'输入到'colrgb = pixcol [x2,y2]' –

+0

这发生在多行中。哪条确切线?它应该在错误信息中说。 – Julien

回答

1

y2不会,如果blobs0 is None定义,在这种情况下,你可能不希望反正做任何事情。

我建议你把一切都在一个单一的if块:

blobs0 = segmentedImage0.findBlobs() 
blobs1 = segmentedImage1.findBlobs() 
if blobs0 is not None and blobs1 is not None: 
    # all your code here 

还你似乎在第一块使用second_blob代替first_blob。你也许应该明白你的代码在做什么,而不是盲目地使用一些旧代码块,希望它能起作用。

+0

谢谢,我刚刚得到它的工作,并不知道如何重组它。我已经清理了它,原来的问题仍然存在,'print bottomLeftCorner.x'后我得到错误''numpy.ndarray'对象没有属性'x''。检索此x位置是总体目标 –

+0

您需要知道您正在使用哪个对象。阅读你正在使用的函数的文档,它会告诉你他们返回的是什么类型。考虑到你的错误信息,它看起来像坐标不是以成员'x'和'y'的结构或类的形式返回,而是作为'np.array'返回。我的猜测是'x,y = bottomLeftCorner'应该可以工作。 – Julien

相关问题