示例#1
0
def textDetect(img, original):
    """ Text detection using contours """
    # Resize image
    small = resize(img, 2000)
    image = resize(original, 2000)

    # Finding contours
    mask = np.zeros(small.shape, np.uint8)
    im2, cnt, hierarchy = cv2.findContours(np.copy(small), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)

    #implt(img, 'gray')

    # Variables for contour index and words' bounding boxes
    index = 0
    boundingBoxes = np.array([0,0,0,0])
    bBoxes = []

    # CCOMP hierarchy: [Next, Previous, First Child, Parent]
    # cv2.RETR_CCOMP - contours into 2 levels
    # Go through all contours in first level
    while (index >= 0):
        x,y,w,h = cv2.boundingRect(cnt[index])
        # Get only the contour
        cv2.drawContours(mask, cnt, index, (255, 255, 255), cv2.FILLED)
        maskROI = mask[y:y+h, x:x+w]
        # Ratio of white pixels to area of bounding rectangle
        r = float(cv2.countNonZero(maskROI)) / (w * h)

        # Limits for text (white pixel ratio, width, height)
        # TODO Test h/w and w/h ratios
        if r > 0.1 and 1600 > w > 10 and 1600 > h > 10 and h/w < 3 and w/h < 10 and (60 // h) * w < 1000:
            bBoxes += [[x, y, w, h]]

        # Index of next contour
        index = hierarchy[0][index][0]

    # Group intersecting rectangles
    bBoxes = groupRectangles(bBoxes)
    i = 0
    f = open("output/words/normal/bounding_boxes_normal.txt","w")
    for (x, y, w, h) in bBoxes:
        boundingBoxes = np.vstack((boundingBoxes, np.array([x, y, x+w, y+h])))
        cv2.imwrite("output/words/normal/"+str(i)+".jpg",image[y:y+h, x:x+w])
        f.write(str(i) + "\t => \t" + "("+str(x)+","+str(y)+")"+","+"("+str(x+w)+","+str(y+h)+")"+"\n")
        # cv2.rectangle(image, (x, y),(x+w,y+h), (0, 255, 0), 2)
        i = i+1
    #implt(image, t='Bounding rectangles')

    # Recalculate coordinates to original scale
    bBoxes = boundingBoxes.dot(ratio(image, small.shape[0])).astype(np.int64)
    return bBoxes[1:]
示例#2
0
def text_detect(img, original):
    """ Text detection using contours """
    # Resize image
    small = resize(img, 2000)
    image = resize(original, 2000)

    # Finding contours
    mask = np.zeros(small.shape, np.uint8)
    cnt, hierarchy = cv2.findContours(np.copy(small), cv2.RETR_CCOMP,
                                      cv2.CHAIN_APPROX_SIMPLE)

    implt(img, 'gray')

    # Variables for contour index and words' bounding boxes
    index = 0
    boxes = []
    # CCOMP hierarchy: [Next, Previous, First Child, Parent]
    # cv2.RETR_CCOMP - contours into 2 levels
    # Go through all contours in first level
    while (index >= 0):
        x, y, w, h = cv2.boundingRect(cnt[index])
        # Get only the contour
        cv2.drawContours(mask, cnt, index, (255, 255, 255), cv2.FILLED)
        maskROI = mask[y:y + h, x:x + w]
        # Ratio of white pixels to area of bounding rectangle
        r = cv2.countNonZero(maskROI) / (w * h)

        # Limits for text (white pixel ratio, width, height)
        # TODO Test h/w and w/h ratios
        if r > 0.1 and 2000 > w > 10 and 1600 > h > 10 and h / w < 3 and w / h < 10:
            boxes += [[x, y, w, h]]

        # Index of next contour
        index = hierarchy[0][index][0]
    # Group intersecting rectangles
    boxes = group_rectangles(boxes)
    bounding_boxes = np.array([0, 0, 0, 0])
    for (x, y, w, h) in boxes:
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 8)
        print((x, y, w, h))
        bounding_boxes = np.vstack(
            (bounding_boxes, np.array([x, y, x + w, y + h])))

    implt(image, t='Bounding rectangles')

    # Recalculate coordinates to original scale
    boxes = bounding_boxes.dot(ratio(image, small.shape[0])).astype(np.int64)
    return boxes[1:]
def edges_det(img, min_val, max_val):
    """ Preprocessing (gray, thresh, filter, border) + Canny edge detection """
    kernel = np.ones((2, 2), np.uint8)

    img = cv2.cvtColor(resize(img), cv2.COLOR_BGR2GRAY)

    #    img = cv2.bilateralFilter(img, 9, 75, 75)
    img = cv2.GaussianBlur(img, (5, 5), 0)
    #    img = cv2.GaussianBlur(img, (5, 5), 0)
    implt(img, 'gray', 'blur')
    img = cv2.adaptiveThreshold(img, 175, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                cv2.THRESH_BINARY, 115, 1)
    implt(img, 'gray', 'Adaptive Threshold')
    img = cv2.erode(img, kernel, iterations=1)
    implt(img, 'gray', 'erode')
    img = cv2.dilate(img, kernel, iterations=1)
    implt(img, 'gray', 'dilate')
    img = cv2.medianBlur(img, 1)
    #    img = cv2.blur(img,(10,10))
    img = cv2.copyMakeBorder(img,
                             5,
                             5,
                             5,
                             5,
                             cv2.BORDER_CONSTANT,
                             value=[0, 0, 0])
    implt(img, 'gray', 'Median Blur + Border')
    return cv2.Canny(img, min_val, max_val)
示例#4
0
def edges_det(img, min_val, max_val):
    """ Preprocessing (gray, thresh, filter, border) + Canny edge detection """
    img = cv2.cvtColor(resize(img), cv2.COLOR_BGR2GRAY)

    # Applying blur and threshold
    img = cv2.bilateralFilter(img, 9, 75, 75)
    img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                cv2.THRESH_BINARY, 115, 4)
    implt(img, 'gray', 'Adaptive Threshold')

    # Median blur replace center pixel by median of pixels under kelner
    # => removes thin details
    img = cv2.medianBlur(img, 11)

    # Add black border - detection of border touching pages
    # Contour can't touch side of image
    img = cv2.copyMakeBorder(img,
                             5,
                             5,
                             5,
                             5,
                             cv2.BORDER_CONSTANT,
                             value=[0, 0, 0])
    implt(img, 'gray', 'Median Blur + Border')

    return cv2.Canny(img, min_val, max_val)
示例#5
0
def edges_det(img, min_val, max_val):
    img = cv2.cvtColor(resize(img), cv2.COLOR_BGR2GRAY)
    img = cv2.bilateralFilter(img, 9, 75, 75)
    img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 115, 4)
    implt(img, 'gray', 'Adaptive Threshold')
    
    img = cv2.medianBlur(img, 11)
    
    img = cv2.copyMakeBorder(img, 5, 5, 5, 5, cv2.BORDER_CONSTANT, value =[0, 0, 0])
    implt(img, 'gray', 'Median Blur + Border')
    
    return cv2.Canny(img, min_val, max_val)
示例#6
0
def textDetectWatershed(thresh, original):
    """ Text detection using watershed algorithm """
    # According to: http://docs.opencv.org/trunk/d3/db4/tutorial_py_watershed.html
    img = resize(original, 3000)
    thresh = resize(thresh, 3000)
    # noise removal
    kernel = np.ones((3,3),np.uint8)
    opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 3)

    # sure background area
    sure_bg = cv2.dilate(opening,kernel,iterations=3)

    # Finding sure foreground area
    dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)
    ret, sure_fg = cv2.threshold(dist_transform,0.01*dist_transform.max(),255,0)
    # Finding unknown region
    sure_fg = np.uint8(sure_fg)
    # cv2.imshow("image",sure_fg)
    # cv2.waitKey(10)

    unknown = cv2.subtract(sure_bg,sure_fg)

    # Marker labelling
    ret, markers = cv2.connectedComponents(sure_fg)



    # Add one to all labels so that sure background is not 0, but 1
    markers += 1

    # Now, mark the region of unknown with zero
    markers[unknown == 255] = 0



    markers = cv2.watershed(img, markers)

    #implt(markers, t='Markers')
    image = img.copy()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Creating result array
    boundingBoxes = np.array([0,0,0,0])
    bBoxes = []

    for mark in np.unique(markers):
        # mark == 0 --> background
        if mark == 0:
            continue

        # Draw it on mask and detect biggest contour
        mask = np.zeros(gray.shape, dtype="uint8")
        mask[markers == mark] = 255

        cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
        c = max(cnts, key=cv2.contourArea)

        # Draw a bounding rectangle if it contains text
        x,y,w,h = cv2.boundingRect(c)
        cv2.drawContours(mask, c, 0, (255, 255, 255), cv2.FILLED)
        maskROI = mask[y:y+h, x:x+w]
        # Ratio of white pixels to area of bounding rectangle
        r = float(cv2.countNonZero(maskROI)) / (w * h)
        # Limits for text


        # WORK ON THIS
        if r > 0.1 and 2000 > w > 15 and 1500 > h > 15:
            bBoxes += [[x, y, w, h]]

    # Group intersecting rectangles
    #bBoxes = groupRectangles(bBoxes)
    i = 0
    f = open("./output/words/watershed/bounding_boxes_watershed.txt","w")
    for (x, y, w, h) in bBoxes:
        boundingBoxes = np.vstack((boundingBoxes, np.array([x, y, x+w, y+h])))
        cv2.imwrite("./output/words/watershed/"+str(i)+".jpg",image[y:y+h, x:x+w])
        f.write(str(i) + "\t => \t" + "("+str(x)+","+str(y)+")"+","+"("+str(x+w)+","+str(y+h)+")"+"\n")
        # cv2.rectangle(image, (x, y),(x+w,y+h), (0, 255, 0), 2)
        i = i+1

    #implt(image)

    # Recalculate coordinates to original size
    bBoxes = boundingBoxes.dot(ratio(original, 3000)).astype(np.int64)
    return bBoxes[1:]
    max_area = MIN_COUNTOUR_AREA
    page_contour = np.array([[0, 0], [0, height - 5], [width - 5, height - 5],
                             [width - 5, 0]])

    for cnt in contours:
        perimeter = cv2.arcLength(cnt, True)
        approx = cv2.approxPolyDP(cnt, 0.03 * perimeter, True)

        # Page has 4 corners and it is convex
        if (len(approx) == 4 and cv2.isContourConvex(approx)
                and max_area < cv2.contourArea(approx) < MAX_COUNTOUR_AREA):

            max_area = cv2.contourArea(approx)
            page_contour = approx[:, 0]

    # Sort corners and offset them
    page_contour = four_corners_sort(page_contour)
    return contour_offset(page_contour, (-5, -5))


page_contour = find_page_contours(edges_image, resize(image))
print("PAGE CONTOUR:")
print(page_contour)
implt(cv2.drawContours(resize(image), [page_contour], -1, (0, 255, 0), 3))

#Write To File
text = pytesseract.image_to_string(image)
print(text)
write_file(text, image)
示例#8
0
    for cnt in contours:
        perimeter = cv2.arcLength(cnt, True)
        approx = cv2.approxPolyDP(cnt, 0.03 * perimeter, True)

        if (len(approx) == 4 and cv2.isContourConvex(approx)
                and maxArea < cv2.contourArea(approx) < MAX_COUNTOUR_AREA):

            maxArea = cv2.contourArea(approx)
            pageContour = approx[:, 0]

    pageContour = fourCornersSort(pageContour)
    return contourOffset(pageContour, (-5, -5))


pageContour = findPageContours(closedEdges, resize(image))
print("PAGE CONTOUR:")
print(pageContour)
implt(cv2.drawContours(resize(image), [pageContour], -1, (0, 255, 0), 3))

pageContour = pageContour.dot(ratio(image))


def edgesDet(img, minVal, maxVal):

    img = cv2.cvtColor(resize(img), cv2.COLOR_BGR2GRAY)
    img = cv2.bilateralFilter(img, 9, 75, 75)
    img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                cv2.THRESH_BINARY, 115, 4)
    implt(img, 'gray', 'Adaptive Threshold')
示例#9
0
    # Create target points
    tPoints = np.array([[0, 0], [0, height], [width, height], [width, 0]],
                       np.float32)

    # getPerspectiveTransform() needs float32
    if sPoints.dtype != np.float32:
        sPoints = sPoints.astype(np.float32)

    M = cv2.getPerspectiveTransform(sPoints, tPoints)
    return cv2.warpPerspective(img, M, (int(width), int(height)))


#--

image = cv2.cvtColor(cv2.imread("test1.jpeg"), cv2.COLOR_BGR2RGB)

# Edge detection ()
imageEdges = edgesDet(image, 200, 250)

# Close gaps between edges (double page clouse => rectangle kernel)
closedEdges = cv2.morphologyEx(imageEdges, cv2.MORPH_CLOSE, np.ones((5, 11)))

pageContour = findPageContours(closedEdges, resize(image))

# Recalculate to original scale
pageContour = pageContour.dot(ratio(image))

newImage = perspImageTransform(image, pageContour)
cv2.imwrite("2_1.jpg", cv2.cvtColor(newImage, cv2.COLOR_BGR2RGB))
def text_detect(img, original):
    """ Text detection using contours """
    # Resize image
    small = resize(img, 2000)
    image = resize(original, 2000)
    
    # Finding contours
    mask = np.zeros(small.shape, np.uint8)
    im2, cnt, hierarchy = cv2.findContours(np.copy(small), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
    
#     implt(img, 'gray')
    
    # Variables for contour index and words' bounding boxes
    index = 0    
    boxes = []
    test_digits = np.array(np.zeros((1,784)))
    i = 0
    
    # CCOMP hierarchy: [Next, Previous, First Child, Parent]
    # cv2.RETR_CCOMP - contours into 2 levels
    # Go through all contours in first level
    while (index >= 0):
        x,y,w,h = cv2.boundingRect(cnt[index])
        # Get only the contour
        cv2.drawContours(mask, cnt, index, (255, 255, 255), cv2.FILLED)
        maskROI = mask[y:y+h, x:x+w]
        # Ratio of white pixels to area of bounding rectangle
        r = cv2.countNonZero(maskROI) / (w * h)
        
        # Limits for text (white pixel ratio, width, height)
        # TODO Test h/w and w/h ratios
#         if r > 0.1 and 2000 > w > 10 and 1600 > h > 10 and h/w < 3 and w/h < 10:
        if r > 0.1 and 40 > w > 1 and 1600 > h > 10 and (h/w < 3 or w/h < 10):
            boxes += [[x, y, w, h]]
            roi = image[y:y+h, x:x+w]
            
            digit = cv2.cvtColor(roi, cv2.COLOR_RGB2GRAY)
            
            digit, j = digit_preprocessing(digit)
            
            cv2.imwrite("../data/form/output{0}.jpg".format(i), digit)
            digit_row = np.resize(digit,(1,784))
            test_digits = np.append(test_digits,digit_row,axis=0)
            i += 1
        
        # Index of next contour
        index = hierarchy[0][index][0]
        
        
    # Group intersecting rectangles
    boxes = group_rectangles(boxes)
    bounding_boxes = np.array([0,0,0,0])
    for (x, y, w, h) in boxes:
        cv2.rectangle(image, (x, y),(x+w,y+h), (0, 255, 0), 8)
        bounding_boxes = np.vstack((bounding_boxes, np.array([x, y, x+w, y+h])))

    implt(image, t='Bounding rectangles')

    # Recalculate coordinates to original scale
    boxes = bounding_boxes.dot(ratio(image, small.shape[0])).astype(np.int64)
    
    return test_digits, boxes[1:]