2017-02-26 31 views
1

IM上的OCR項目工作,在這裏我做手寫數字識別我的問題是與功能findcontours()值過多解壓,蟒蛇,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] 
+0

的錯誤: findContours行導致解壓縮的值太多! –

回答

0

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) 

假設你不需要的第一個唯一的輪廓和層次