简体   繁体   English

从numpy数组中提取X坐标

[英]Extracting X coordinate from numpy array

I am extracting the x positions of 2 blobs via a live two camera stream. 我正在通过直播的两个相机流提取2个斑点的x位置。 I can get the first x position no problem because it is given as a tuple, (ex = ...object at (455, 69)). 我可以毫无问题地得到第一个x位置,因为它是作为一个元组给出的(例如,ex = ... object at(455,69))。 The problem is I need the x position of the bottom left corner of the second blob but it returns as a numpy array and 'blob.x' does not work. 问题是我需要第二个Blob的左下角的x位置,但它会返回为numpy数组,并且'blob.x'不起作用。 How can I get the x position of the numpy array? 如何获得numpy数组的x位置? Any help/guidance much appreciated. 任何帮助/指导非常感谢。

I receive the following error: ValueError: The truth value of an array with more than one element is ambiguous. 我收到以下错误:ValueError:具有多个元素的数组的真值不明确。 Use a.any() or a.all() 使用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

y2 will not be defined if blobs0 is None , in which case you probably don't want to do anything anyway. 如果blobs0 is None ,则不会定义y2 ,在这种情况下,您可能仍然不想做任何事情。

I suggest you put everything in a single if block: 我建议您将所有内容放在一个if块中:

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

also you seem to be using second_blob instead of first_blob in the first block. 您也似乎在第一个块中使用了second_blob而不是first_blob And you should probably understand what your code is doing rather than blindly using some chunks of old code hoping it will work. 而且,您应该了解代码的用途,而不是盲目地使用一些旧代码,希望它能起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM