【问题标题】:Fixed "ValueError: not enough values to unpack (expected 3, got 2)" but still getting error修复了“ValueError: no enough values to unpack (expected 3, got 2)”但仍然出现错误
【发布时间】:2020-02-17 07:00:32
【问题描述】:

我正在学习 opencv,但在运行 3 个代码块时遇到了问题。我试图修复它,但根本不起作用。其他解决方案说明了轮廓变量的错误,我用更正的方法更改了它,但仍然无法运行这段代码。谁能帮帮我?

import numpy as np
import cv2

image = cv2.imread('images/numbers.jpg')
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
#cv2.imshow("image", image)
#cv2.imshow("gray", gray)
#cv2.waitKey(0)

# Blur image then find edges using Canny 
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
#cv2.imshow("blurred", blurred)
#cv2.waitKey(0)

edged = cv2.Canny(blurred, 30, 150)
#cv2.imshow("edged", edged)
#cv2.waitKey(0)

# Fint Contours
# _, contours,_ = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
_, contours,_ = cv2.findContours(edged.copy(), cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

#Sort out contours left to right by using their x cordinates
contours = sorted(contours, key = x_cord_contour, reverse = False)

# Create empty array to store entire number
full_number = []

# loop over the contours
for c in contours:
    # compute the bounding box for the rectangle
    (x, y, w, h) = cv2.boundingRect(c)    

    #cv2.drawContours(image, contours, -1, (0,255,0), 3)
    #cv2.imshow("Contours", image)

    if w >= 5 and h >= 25:
        roi = blurred[y:y + h, x:x + w]
        ret, roi = cv2.threshold(roi, 127, 255,cv2.THRESH_BINARY_INV)
        squared = makeSquare(roi)
        final = resize_to_pixel(20, squared)
        cv2.imshow("final", final)
        final_array = final.reshape((1,400))
        final_array = final_array.astype(np.float32)
        ret, result, neighbours, dist = knn.findNearest(test, k=5)
        number = str(int(float(result[0])))
        full_number.append(number)
        # draw a rectangle around the digit, the show what the
        # digit was classified as
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
        cv2.putText(image, number, (x , y + 155),
        cv2.FONT_HERSHEY_COMPLEX, 2, (255, 0, 0), 2)
        cv2.imshow("image", image)
        cv2.waitKey(0) 

cv2.destroyAllWindows()
print ("The number is: " + ''.join(full_number))

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-26-97be138d1b62> in <module>
     19 # Fint Contours
     20 # _, contours,_ = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
---> 21 _, contours,_ = cv2.findContours(edged.copy(), cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
     22 
     23 #Sort out contours left to right by using their x cordinates

ValueError: not enough values to unpack (expected 3, got 2)

【问题讨论】:

  • 文档说它的contours, hierarchy = cv.findContours(...)
  • 据我所知,错误信息非常准确:findContours 只返回两个变量,但您要求三个。当我查看OpenCV doc 时,它说明了contours, hierarchy = cv.findContours(...) 之类的用法
  • @magnus 我已经尝试过了,但仍然无法解决问题

标签: python python-3.x opencv ocr opencv3.0


【解决方案1】:

不同版本的 openCV 以不同的方式处理计数。 为了安全起见,请改用以下代码,它应该可以跨版本工作:

try:
    _, contours,_ = cv2.findContours(edged.copy(), cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
except ValueError:    
    contours ,_ = cv2.findContours(edged.copy(), cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

或者,您也可以使用辅助库 imutils:

contours = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(contours)

【讨论】:

  • 我添加了try catch异常并得到了这个错误-------------------------------- ------------------------------------------------------- TypeError Traceback(最近一次调用最后一次) in 32 33 #使用x坐标从左到右排序轮廓---> 34 contours = sorted(contours, key = x_cord_contour, reverse = False) 35 36 #创建空数组以存储整数类型错误:“NoneType”和“NoneType”实例之间不支持“
  • 我尝试了第二个@peter 它抛出了这个错误“TypeError: ' 35 contours = sorted(contours , key = x_cord_contour, reverse = False)"
  • x_cord_contour 函数长什么样子?
  • 参见这个 def x_cord_contour(contour): # 这个函数从 findContours 中获取轮廓 # 然后输出 x 质心坐标 if cv2.contourArea(contour) > 10: M = cv2.moments(contour)返回 (int(M['m10']/M['m00']))
  • 我不知道你的代码会是什么样子。我不知道它应该做什么。同样,如果您有新问题,请打开一个新问题。但请先尝试自己修复。我已经提示你出了什么问题。
猜你喜欢
  • 2022-11-12
  • 2022-07-22
  • 1970-01-01
  • 2020-10-10
  • 1970-01-01
  • 2020-10-26
  • 2017-12-23
  • 2020-11-19
  • 1970-01-01
相关资源
最近更新 更多