【问题标题】:Getting integer from array as a separate variable in python从数组中获取整数作为python中的单独变量
【发布时间】:2018-04-24 18:50:06
【问题描述】:

我正在尝试使用 opencv 查找图像 x 和 y 坐标,这似乎工作正常。但我只想返回一个 x 和 y 而不是 opencv 函数创建的整个数组。这就是我正在尝试的方式:

def getImageXY():
    im = pyautogui.screenshot()
    im.save(filePathSrc)

    img_rgb = cv2.imread(filePathSrc)
    template = cv2.imread(filePathToFind)

    res = cv2.matchTemplate(img_rgb, template, cv2.TM_CCOEFF_NORMED)
    threshold = .8

    loc = np.where(res >= threshold)

    x = loc[1]
    y = loc[0]

    return x, y

这就是我试图分配值的地方

def main():
    x, y = ImageFinder.getImageXY()

    print x, y

我的预期输出是“322, 766”(只是两个单独的单个 x 和 y 值)

但这是我的实际输出: [313 502 314 503 296 485 297 470 486 298 471 487 267 299 472 488 300 473 489 474 490 475]

即整个数组。

我做错了什么?在 java 中做同样的事情会像我期望的那样工作,但在 python 中却不行......

有什么猜测吗?

谢谢

【问题讨论】:

  • 修复缩进
  • 缩进正确,只是帖子中没有显示
  • 您的loc 是否有任何二维变化?
  • 你可以手动运行你的行,从cv2.imread-s 开始,然后看看loc最后包含什么。很明显,可以有多个像素满足条件。阅读documentation 也可能是个好主意。
  • @Colin 你是对的,它是 2d。奇怪的是,当我打印它时它连接了两个数组,所以在我上面的输出中,313 是正确的 x 而 502 是正确的 y 值。所以我只是假设它存储了坐标 [x,y,x,y] 等等......非常感谢您的帮助!

标签: python arrays variables assign


【解决方案1】:

您的loc 是二维的,这会导致您看到的效果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    相关资源
    最近更新 更多