python计算机视觉常用的包_Python计算机视觉轮廓:要解包的值太多?

在尝试使用pyimagesearch.com的代码创建移动文档扫描仪时,遇到`cv2.findContours`函数引发的`ValueError: too many values to unpack`错误。问题出现在代码尝试解包边缘检测后的轮廓数。尽管该代码在作者的环境中正常工作,但在当前环境中,找到的轮廓数量超过预期。解决方案可能涉及更新轮廓处理部分,以适应当前图像的边缘检测结果。
摘要由CSDN通过智能技术生成

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

有什么问题,我该怎么解决?

Ubuntu中的终端命令python scan.py --image images/page.jpg

结果:STEP 1: Edge Detection

Traceback (most recent call last):

File "scan.py", line 40, in

(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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值