Ask Your Question

Remove the black line surrounding text in opencv

asked 2019-06-06 09:08:05 -0600

I am trying to remove the black lines surrounding the text if present any. My purpose is to just have enough portion of the image to extract each character in it. The additional black lines are noise when i am trying to extract characters.

I have tried using floodfill in opencv but the image contains some white pixels before the black line starts in the upper left corner. So it hasn't been fruitful. I tried cropping by means of finding contours but even that does not work. This is the original image

Original Image

import cv2
import numpy as np

img = cv2.imread('./Cropped/22.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
_,thresh = cv2.threshold(gray,1,255,cv2.THRESH_BINARY)
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
x,y,w,h = cv2.boundingRect(cnt)
crop = img[y:y+h,x:x+w]

cv2.imshow('Cropped Image',crop)

and using floodfill

img = cv2.imread('./Cropped/22.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # threshold the gray image to binarize, and negate it
    gray = cv2.bitwise_not(gray)
    bw = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, \
                                cv2.THRESH_BINARY, 15, -2)

    # find external contours of all shapes
    contours,h = cv2.findContours(bw, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

    # create a mask for floodfill function, see documentation
    h,w,_ = img.shape
    mask = np.zeros((h+2,w+2), np.uint8)

    # determine which contour belongs to a square or rectangle
    for cnt in contours:
        poly = cv2.approxPolyDP(cnt, 0.02*cv2.arcLength(cnt,True),True)
        if len(poly) == 4:
            # if the contour has 4 vertices then floodfill that contour with black color
            cnt = np.vstack(cnt).squeeze()
            _,binary,_,_ = cv2.floodFill(bw, mask, tuple(cnt[0]), 0)
    # convert image back to original color
    binary = cv2.bitwise_not(binary)        

    cv2.imshow('Image', binary)

results in the two cases are as follows

Using Find Contours method

But there appears to be no change. And this using floodfill

Using floodfill. which does not remove any borders.

If any can please help out,it will be really great.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2019-06-07 15:07:57 -0600

OpenCV's contours will work if you filter the contours by aspect ratio. In the image below the 2nd from the left is thresholded at about 150 using binaryInverse type thresholding. There is a function called boundingRect that will give the upright rect of a contour. Define the contour aspect ratio as rect.width / rect.height and only keep contours with AR < 1 since numbers are tall. This gives the 3rd image from the left. The last is just inverted.

image description

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2019-06-06 09:08:05 -0600

Seen: 1,810 times

Last updated: Jun 07 '19