修正了“ValueError:没有足够的值来解包(预期为3,得到2)”但仍然得到

2024-04-25 16:56:24 发布

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

我正在学习opencv,在运行3块代码时遇到了一个问题。我试着修好它,但一点用都没有。其他的解决方案说明了courses变量的错误,我用修正的方法修改了它,但是仍然无法运行这段代码。有人能帮我吗?你知道吗

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)

Tags: thetoimagenumbercv2finalcopyimshow
1条回答
网友
1楼 · 发布于 2024-04-25 16:56:24

不同版本的openCV处理countours的方式不同。 为了安全起见,请使用以下代码,这些代码应跨版本工作:

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)

相关问题 更多 >

    热门问题