# Remove the black line surrounding text in opencv

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

import cv2
import numpy as np

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('Image',img)
cv2.imshow('Cropped Image',crop)
cv2.waitKey(0)


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

# 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)
cv2.waitKey(0)
cv2.destroyAllWindows()


results in the two cases are as follows

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

. which does not remove any borders.

edit retag close merge delete

Sort by » oldest newest most voted

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.

more

Official site

GitHub

Wiki

Documentation