TypeError:“非类型”对象不可编辑,边界检测OpenCV

2024-06-12 14:41:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道这里有很多关于这个类型错误的问题:TypeError:“NoneType”对象是不可编辑的,但是我找不到一个对我有帮助的答案。我正在做俄罗斯方块检测,下面是这个网站的视频:https://pysource.com/2019/12/07/detect-tetris-board-and-tetrominoes-python-plays-tetris-p-3/。但是当我试图运行我的代码时,我得到了错误,我不知道如何修复它/甚至不知道哪里出了问题。 这是我的代码:

import numpy as np
import argparse
import cv2
import sys



image = cv2.imread("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")

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

    _, contours = cv2.findContours(board_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)
        
    cnt = contours[0]
    (board_x, board_y, board_w, board_h) = cv2.boundingRect(cnt)
    cv2.drawContours(img, [cnt], -1, (0, 255, 0), 3)
    cv2.drawContours(virtual_board, [cnt], -1, (0, 255, 0), 3)

这就是错误:

Traceback (most recent call last):
  File "C:\Users\-my.user-\Desktop\detection.py", line 21, in <module>
    contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)
TypeError: 'NoneType' object is not iterable

请帮帮我,出了什么问题,我该如何解决


Tags: and代码imageimportboard错误npmask