gpt4 book ai didi

python-3.x - 切片图像时 Python3 和 OpenCV3 出错

转载 作者:太空宇宙 更新时间:2023-11-03 23:15:07 25 4
gpt4 key购买 nike

我一直在尝试将 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

我得到的主要错误被缩小到这个 block

   # 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

最佳答案

您的代码包含以下内容:

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

但是,您不应该认为 cv2.findContours 的返回值在 OpenCV 2.x 和 OpenCV 3.x 之间发生了变化。因此,您实际上是在尝试遍历图像,而不是遍历轮廓列表。

contours tutorial来自 3.x 文档显示了如何正确捕获结果。

OpenCV 2.x

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

OpenCV 3.x

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

关于python-3.x - 切片图像时 Python3 和 OpenCV3 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41479600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com