【问题标题】:too many values to unpack, python, findcontours [closed]太多的值无法解包,python,findcontours [关闭]
【发布时间】:2017-07-17 03:34:54
【问题描述】:

我正在做一个 OCR 项目,在这里我正在做手写数字识别,我遇到的问题是函数 findcontours()

# Import the modules
import cv2
from sklearn.externals import joblib
from skimage.feature import hog
import numpy as np

# Load the classifier
clf = joblib.load("digits_cls.pkl")

# Read the input image 
im = cv2.imread("photo_2.jpg")

# Convert to grayscale and apply Gaussian filtering
im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
im_gray = cv2.GaussianBlur(im_gray, (5, 5), 0)

# Threshold the image
ret, im_th = cv2.threshold(im_gray, 90, 255, cv2.THRESH_BINARY_INV)

# Find contours in the image
ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Get rectangles contains each contour
rects = [cv2.boundingRect(ctr) for ctr in ctrs]

【问题讨论】:

  • 错误:要解压的值太多是由 findContours 行引起的!

标签: python opencv numpy scikit-learn ocr


【解决方案1】:

documentation example on findContours 表明该函数返回三个值(或更严格地说,它返回一个包含三个值的元组)。

但是你只有两个:

ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

你可以改成:

_, ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

假设您不需要第一个,只需要轮廓和层次结构

【讨论】:

    猜你喜欢
    • 2017-10-13
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多