Python 计算机视觉轮廓 : Too many values to unpack?

标签 python image image-processing computer-vision contour

我从 pyimagesearch.com 获取源代码来制作移动文档扫描仪并尝试测试代码。边缘检测部分有效,但每当我到达它试图找到图像轮廓的部分时,程序都会输出一个错误,指出有太多的值需要解包,尽管作者方面的编程工作正常。

有什么问题,我该如何解决?

关于源代码的博文: http://www.pyimagesearch.com/2014/09/01/build-kick-ass-mobile-document-scanner-just-5-minutes/?__vid=c35c22a06af30132982122000b2a88d7

有关该程序的 Youtube 视频: https://www.youtube.com/watch?v=yRer1GC2298

Ubuntu 中的终端命令

python scan.py --image images/page.jpg 

结果:

STEP 1: Edge Detection
Traceback (most recent call last):
  File "scan.py", line 40, in <module>
    (cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
ValueError: too many values to unpack

代码:

# USAGE
# python scan.py --image images/page.jpg 

# import the necessary packages
from pyimagesearch.transform import four_point_transform
from pyimagesearch import imutils
from skimage.filter import threshold_adaptive
import numpy as np
import argparse
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,
        help = "Path to the image to be scanned")
args = vars(ap.parse_args())

# load the image and compute the ratio of the old height
# to the new height, clone it, and resize it
image = cv2.imread(args["image"])
ratio = image.shape[0] / 500.0
orig = image.copy()
image = imutils.resize(image, height = 500)

# convert the image to grayscale, blur it, and find edges
# in the image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(gray, 75, 200)

# show the original image and the edge detected image
print "STEP 1: Edge Detection"
cv2.imshow("Image", image)
cv2.imshow("Edged", edged)
cv2.waitKey(0)
cv2.destroyAllWindows()

# find the contours in the edged image, kee
(cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, ping only the
# largest ones, and initialize the screen contourcv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]

# loop over the contours
for c in cnts:
        # approximate the contour
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.02 * peri, True)

        # if our approximated contour has four points, then we
        # can assume that we have found our screen
        if len(approx) == 4:
                screenCnt = approx
                break

# show the contour (outline) of the piece of paper
print "STEP 2: Find contours of paper"
cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2)
cv2.imshow("Outline", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

# apply the four point transform to obtain a top-down
# view of the original image
warped = four_point_transform(orig, screenCnt.reshape(4, 2) * ratio)

# convert the warped image to grayscale, then threshold it
# to give it that 'black and white' paper effect
warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
warped = threshold_adaptive(warped, 250, offset = 10)
warped = warped.astype("uint8") * 255

# show the original and scanned images
print "STEP 3: Apply perspective transform"
cv2.imshow("Original", imutils.resize(orig, height = 650))
cv2.imshow("Scanned", imutils.resize(warped, height = 650))
cv2.waitKey(0)

最佳答案

这是至少对我有用的答案。该函数返回 3 个值,因此:

_,contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

关于Python 计算机视觉轮廓 : Too many values to unpack?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27746089/

相关文章:

python - 在 Python 回溯中显示更多级别的异常

python - 函数 def 中的 True & False

python - 如何从多类分类的混淆矩阵中提取 False Positive、False Negative

python - 与 ImageJ 相比,在 Python 上打开原始图像会产生不同的图像

c++ - 从一堆对象中提取一个对象并检测边缘

python - 类型错误 :enqueuqe takes 1 positional argument but 2 were passed

html - 图像下方奇怪的 ~2px 白色区域。可能的 CSS 错误?

c# - byte[] 的图像无限期挂起

php - 使用 PHP 准备 SVG 文件以用于 img 标签数据 uri

opencv - 查找重复图像的软件链