2

I have the following image bearing handwritten text and lines of the page. I want to extract to remove the lines from the image, so that only the handwritten text is left. I tried implementing it, using opencv in Python. But, I don't get the best results. Please help!

import numpy as np
import cv2
image = cv2.imread('image.jpg')
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
(gray,50,150,apertureSize = 3)
cv2.imwrite('edges-50-150.jpg',edges)
minLineLength=100
lines = cv2.HoughLinesP(image=edges,rho=1,theta=np.pi/180, threshold=100,lines=np.array([]), minLineLength=minLineLength,maxLineGap=80)
a,b,c = lines.shape
for i in range(a):
    x = lines[i][0][0] - lines [i][0][2]
    y = lines[i][0][1] - lines [i][0][3]
    if x!= 0:
        if abs(y/x) <1:
            cv2.line(gray, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (255, 255, 255), 1, cv2.LINE_AA)
se = cv2.getStructuringElement(cv2.MORPH_ELLIPSE , (3,3))
gray = cv2.morphologyEx(gray, cv2.MORPH_CLOSE, se)
cv2.imwrite('houghlines.jpg', gray)
cv2.imwrite('another.png', gray)

Image being tested on

Results I get with the code

Lee
  • 850
  • 11
  • 23
thehandle
  • 21
  • 1
  • 6
  • 1
    I remove the lines by morph-op, while you do it by Hough. https://stackoverflow.com/questions/47667238/find-single-color-horizontal-spaces-in-image/47675213#47675213 But it doesn't matter. As you have found contours, filter by area, width/height. This is my result. https://i.stack.imgur.com/JyRBN.png – Kinght 金 Dec 26 '17 at 10:24
  • Could you tell how you removed the horizontal lines? The `morphed` variable entails the lines, I believe. Also, the result is expected in a non-binary format (the one as shown in my question). – thehandle Dec 26 '17 at 12:50
  • I found also this [other question](https://stackoverflow.com/questions/41149763/python-how-to-ocr-characters-crossed-by-a-horizontal-line), which I am working on. The method seems to work but gives error to me when I try to execute the code. [Error described here](https://stackoverflow.com/questions/47973238/getting-division-by-zero-error-with-python-and-opencv). – lucians Dec 26 '17 at 14:07
  • @Silencer It would be great if you could post an answer to explain how the morph-op is used here to detect the lines. – Ganesh Tata Dec 27 '17 at 07:35
  • Yes @Silencer, it would be really helpful then. – thehandle Dec 29 '17 at 05:44

1 Answers1

0

I am a little bit new with OpenCV, but after you tried to remove the lines, you tried to get the letters ?
A solution could be to create an empty image and put the letters at the same coords.
And maybe filter the positive letters with a minimum size...

TheDelta
  • 136
  • 11