Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

shapedescr error using python3 - opencv3

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