Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all lines in .jpg image file?

I need to remove the lines in an image, which is a table eventually. I have found a way to remove the horizontal and vertical lines:

convert 1.jpg -type Grayscale -negate -define morphology:compose=darken -morphology Thinning 'Rectangle:1x80+0+0<' -negate out.jpg

The following image:

enter image description here

Was converted to the following one:

enter image description here

As one can see the diagonal line is still there. I tried to rotate the image for 45 degrees and then try to remove it, but was also unsuccessful. How it can be done? Any suggestions are appreciated. I opted for imagemagick, but any other options are welcome

like image 204
saidoff Avatar asked Jan 26 '23 02:01

saidoff


1 Answers

You can try using cv2.HoughLinesP() to detect the diagonal line then use a mask to fill in the contour

import cv2
import numpy as np

image = cv2.imread('1.jpg')
mask = np.zeros(image.shape, np.uint8)
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
canny = cv2.Canny(gray,100,200)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
close = cv2.morphologyEx(canny, cv2.MORPH_CLOSE, kernel)
minLineLength = 10
maxLineGap = 350
lines = cv2.HoughLinesP(close,1,np.pi/180,100,minLineLength,maxLineGap)
for line in lines:
    for x1,y1,x2,y2 in line:
        cv2.line(mask,(x1,y1),(x2,y2),(255,255,255),3)

mask = cv2.cvtColor(mask,cv2.COLOR_BGR2GRAY)
cnts = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

for c in cnts:
    cv2.drawContours(image, [c], -1, (255,255,255), -1)

cv2.imshow('mask', mask)
cv2.imshow('image', image)
cv2.imwrite('image.png', image)
cv2.waitKey()
like image 158
nathancy Avatar answered Feb 04 '23 07:02

nathancy