Can';无法在OpenCV上进行颜色检测(Python)

Can';无法在OpenCV上进行颜色检测(Python),python,opencv,Python,Opencv,我正试图按照一些教程让python自动播放俄罗斯方块,但我就是无法让它工作。我跟着去“https://pysource.com/2019/12/07/detect-tetris-board-and-tetrominoes-python-plays-tetris-p-3/“主要是,但一直在其他地方寻找win32gui替代品,如”https://www.pyimagesearch.com/2014/08/04/opencv-python-color-detection/“ 当我尝试运行代码时: im

我正试图按照一些教程让python自动播放俄罗斯方块,但我就是无法让它工作。我跟着去“https://pysource.com/2019/12/07/detect-tetris-board-and-tetrominoes-python-plays-tetris-p-3/“主要是,但一直在其他地方寻找win32gui替代品,如”https://www.pyimagesearch.com/2014/08/04/opencv-python-color-detection/“

当我尝试运行代码时:

import numpy as np
import argparse
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--tetris.png")
args = vars(ap.parse_args())

image = cv2.imread(args["tetris.png"])

boundaries = [([164, 120, 104])]
boundaries1 = [([166, 122, 106])]

for (boundaries) in boundaries:
    lower = np.array(boundaries, dtype = "uint8")
    upper = np.array(boundaries1, dtype = "uint8")

    mask = cv2.inRange(image, lower, upper)
    output = cv2.bitwise_and(image, image, mask = mask)

    cv2.imshow("images", np.hstack([image, output]))
    cv2.waitKey(0)
我收到此错误消息,但我不知道它来自何处:

Traceback (most recent call last):
  File "C:\Users\-my.user-\Desktop\detection.py", line 18, in <module>
    mask = cv2.inRange(image, lower, upper)
cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-cff9bdsm\opencv\modules\core\src\arithm.cpp:1743: error: (-215:Assertion failed) ! _src.empty() in function 'cv::inRange'
回溯(最近一次呼叫最后一次):
文件“C:\Users \-my.user-\Desktop\detection.py”,第18行,在
遮罩=cv2.inRange(图像,下部,上部)
cv2.error:OpenCV(4.4.0)C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-cff9bdsm\OpenCV\modules\core\src\arithm.cpp:1743:error:(-215:断言失败)_函数“cv::inRange”中的src.empty()

我正在尝试在俄罗斯方块上进行颜色检测,但我找不到任何好的替代方案,有人能告诉我如何做/如何修复错误吗?

您的代码是错误的。试着这样做:

import numpy as np
import argparse
import cv2



image = cv2.imread("Untitled-1.jpg")

boundaries = (164, 120, 104)
boundaries1 = (166, 122, 106)

for (boundaries) in boundaries:
    lower = np.array(boundaries, dtype = "uint8")
    upper = np.array(boundaries1, dtype = "uint8")

    mask = cv2.inRange(image, lower, upper)
    output = cv2.bitwise_and(image, image, mask = mask)

    cv2.imshow("images", np.hstack([image, output]))
    cv2.waitKey(0)

如果正确读取图像,请使用print()语句进行检查。是!!非常感谢。