shapedescr error using python3 - opencv3

asked 2017-01-05 01:34:27 -0500

Hello all :)

I've been trying to modernize a python2 script to python3 and after struggling with the errors I've narrowed it down to the following error.

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

The function is supposed to slice an image into different chunks

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
edit retag flag offensive close merge delete

Comments

can you try to check len(cnt) before calling arclength() ?

berak gravatar imageberak ( 2017-01-05 01:40:36 -0500 )edit
1

Sure, here's the code I tried

squares = []
for cnt in contours:
    print(len(cnt))
    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)

And here's the output

1748
OpenCV Error: Assertion failed (count >= 0 && (depth == CV_32F || depth == CV_32S)) in arcLength, file /Users/jenkins/miniconda/1/x64/conda-bld/work/opencv-3.1.0/modules/imgproc/src/shapedescr.cpp, line 281
----------------------------------------------------------------
error                          Traceback (most recent call last)
<ipython-input-11-fab4ecfaecdc> in <module>()
      2 for cnt in contours:
      3         print(len(cnt))
----> 4         cnt_len = cv2.arcLength(cnt, Tru
abhi18av gravatar imageabhi18av ( 2017-01-05 02:11:54 -0500 )edit

I was also in a same problem. By the way, Did you set Path properly to the module? If ok,Try it about findContours seeing that document. http://stackoverflow.com/questions/25...

luck gravatar imageluck ( 2017-01-05 22:28:28 -0500 )edit