我一直在尝试将python2脚本现代化为python3,在遇到错误后,我将其缩小到以下错误。
def slicePage(image):
# area of the little rectangles next to the sentences, relative to image size
rect_area = 0.00035 * image.shape[0] * image.shape[1]
# threashold image and detect contours
imgray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
*_,thresh = cv2.threshold(imgray, 250, 255, cv2.THRESH_BINARY)
contours,*_ = cv2.findContours(thresh.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
# find the little rectangles next to the sentences
squares = []
for cnt in contours:
cnt_len = cv2.arcLength(cnt, True)
cnt = cv2.approxPolyDP(cnt, 0.1*cnt_len, True)
if len(cnt) == 4 and cv2.contourArea(cnt) > rect_area:
cnt = cnt.reshape(-1, 2)
squares.append(cnt)
# check if number of squares fits pattern
if (len(squares) == 0) or (len(squares) % len(ITEMS) != 0) or (len(squares) / len(ITEMS) > MAX_SENTENCES_PER_PAGE):
return None
# items start below the highest point of each square
cut_y = sorted([min([p[1] for p in sq]) for sq in squares])
# use right-most point to start looking for the vertical black line
max_x = max([max([p[0] for p in sq]) for sq in squares]) + 1
# from the first rectangle, walk right until hitting the line
line = thresh[cut_y[0], max_x:]
line_start = np.where( line < 255 )[0][0]
cut_x = max_x + line_start + np.where( line[line_start:] == 255 )[0][0]
# adjust y-values if they intersect with any of the text
for i in range(len(cut_y)):
counter = 10
# there should not be any non-white pixels on the vertical line
while len(np.where( thresh[cut_y[i], cut_x:] < 255 )[0]) > 0 and counter > 0:
counter -= 1
cut_y[i] -= 2
slices = []
for i in range(len(cut_y)):
top = cut_y[i]
bottom = cut_y[i+1] if i+1 < len(cut_y) else image.shape[0]
left = cut_x; right = image.shape[1]
area_thresh = thresh[top:bottom, left:right]
# cut off whitspace
yBlackRange = np.where(np.min(area_thresh, axis=1) < 255)[0]
bottom = top + yBlackRange[-1]
top = top + yBlackRange[0]
xBlackRange = np.where(np.min(area_thresh, axis=0) < 255)[0]
right = left + xBlackRange[-1]
left = left + xBlackRange[0]
slices.append(image[top:bottom, left:right])
return slices
我得到的主要错误缩小到这个块
# find the little rectangles next to the sentences
squares = []
for cnt in contours:
cnt_len = cv2.arcLength(cnt, True)
cnt = cv2.approxPolyDP(cnt, 0.1*cnt_len, True)
if len(cnt) == 4 and cv2.contourArea(cnt) > rect_area:
cnt = cnt.reshape(-1, 2)
squares.append(cnt)
我使用 Python3 (anaconda)和 OpenCV3 。我遇到的错误与OpenCV3中的imagedesc功能有关。
Traceback (most recent call last):
File "<ipython-input-51-7586a536b1c0>", line 5, in <module>
cnt_len = cv2.arcLength(cnt, True)
error: /Users/jenkins/miniconda/1/x64/conda-bld/work/opencv-3.1.0/modules/imgproc/src/shapedescr.cpp:281: error: (-215) count >= 0 && (depth == CV_32F || depth == CV_32S) in function arcLength
答案 0 :(得分:1)
您的代码包含以下内容:
contours,*_ = cv2.findContours(thresh.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
但是,您不应该在OpenCV 2.x和OpenCV 3.x之间更改cv2.findContours
的返回值。因此,不是迭代轮廓列表,而是实际上尝试迭代图像。
3.x文档中的contours tutorial显示了如何正确捕获结果。
>>> import cv2
>>> print cv2.__version__
2.4.11
>>> help(cv2.findContours)
Help on built-in function findContours in module cv2:
findContours(...)
findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> contours, hierarchy
>>> import cv2
>>> print cv2.__version__
3.1.0
>>> help(cv2.findContours)
Help on built-in function findContours:
findContours(...)
findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> image, contours, hierarchy
答案 1 :(得分:-1)
contours
中的一个或多个值似乎违反了此检查 - count >= 0 && (depth == CV_32F || depth == CV_32S)
。对于第一次检查,只需确保每个值都是正值。至于第二次检查,你可能比我更了解深度是指什么:)