【问题标题】:Finding contours using opencv (image processing)使用opencv(图像处理)寻找轮廓
【发布时间】:2019-07-18 13:37:33
【问题描述】:

我一直在使用opencv 实施一个项目“车牌检测器”。更新我的Python Version 和opencv 后,我在查找轮廓时遇到错误,如下所示:

imgContours, contours, npaHierarchy = cv2.findContours(imgThreshCopy, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)   # find all contours
ValueError: not enough values to unpack (expected 3, got 2)

这是我使用的代码:

def findPossibleCharsInScene(imgThresh):
listOfPossibleChars = []                # this will be the return value

intCountOfPossibleChars = 0

imgThreshCopy = imgThresh.copy()

imgContours, contours, npaHierarchy = cv2.findContours(imgThreshCopy, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)   # find all contours

height, width = imgThresh.shape
imgContours = np.zeros((height, width, 3), np.uint8)

for i in range(0, len(contours)):                       # for each contour

    if Main.showSteps == True: # show steps ###################################################
        cv2.drawContours(imgContours, contours, i, Main.SCALAR_WHITE)
    # end if # show steps #####################################################################

    possibleChar = PossibleChar.PossibleChar(contours[i])

    if DetectChars.checkIfPossibleChar(possibleChar):                   # if contour is a possible char, note this does not compare to other chars (yet) . . .
        intCountOfPossibleChars = intCountOfPossibleChars + 1           # increment count of possible chars
        listOfPossibleChars.append(possibleChar)                        # and add to list of possible chars
    # end if
# end for

if Main.showSteps == True: # show steps #######################################################
    print("\nstep 2 - len(contours) = " + str(len(contours)))  # 2362 with MCLRNF1 image
    print("step 2 - intCountOfPossibleChars = " + str(intCountOfPossibleChars))  # 131 with MCLRNF1 image
    cv2.imshow("2a", imgContours)
# end if # show steps #########################################################################

return listOfPossibleChars
# end function

我应该做哪些改变来纠正它?

【问题讨论】:

标签: python-3.x opencv image-processing opencv3.0


【解决方案1】:

我认为 cv2.findContours() 函数只返回 2 个值。您应该将代码更改为以下

contours, npaHierarchy = cv2.findContours(imgThreshCopy, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

【讨论】:

  • 只是为了添加到您的答案中,它从 2 个返回值(至少 2.4.x 版本)更改为 3(我可以确认的版本 3.4 和 3.5),现在又是 2(版本 4 。X)。这就是它如此令人困惑的原因,而且很多教程都有错误,具体取决于所使用的 OpenCV 版本
  • 这正是我所面临的。
猜你喜欢
  • 2011-06-13
  • 2012-01-16
  • 1970-01-01
  • 2021-08-16
  • 1970-01-01
  • 1970-01-01
  • 2011-12-30
  • 2023-03-09
  • 1970-01-01
相关资源
最近更新 更多