0

I have an input signature for which I want to remove the gridlines of same color. enter image description here

So far I am using this python code to do that

import cv2
import numpy as np

## Load image and make B&W version
image = cv2.imread('Downloads/image.png')
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Remove horizontal
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (50,1))
detected_lines = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(detected_lines, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(thresh, [c], -1, (0,0,0), 2)

cv2.imwrite('Downloads/out.png', thresh)

Which takes me to the following image.

enter image description here

Now I am trying to infill the gaps introduced, I tried using a vertical kernel and morph close but that infills the other spaces in images a lot.

Any ideas on how I can achieve the said infilling using a more sophisticated operation and get the infill done without tampering with the existing signature much.

anonR
  • 849
  • 7
  • 26
  • A simple trick is to replace the line by the content of a thin rectangle just above or below. –  Feb 07 '23 at 10:23
  • That would be helpful if all the lines were vertical, But as you can see in the signature above, some lines are at an angle to the gridline being removed so replacing the line with the content just above or below wouldn't make the remaining lines fully connected. I think I need some solution which can detect the lines and infill where the continuity is broken by a little. – anonR Feb 07 '23 at 15:48
  • I said a simple trick. :) –  Feb 07 '23 at 15:50
  • Am I right to think that this is just a sketch that you made for the sake of illustration ? If true, post real scans. –  Feb 07 '23 at 15:54
  • Its a demo image made by taking an signature image from google, But the image is a true representative of the original data. The original images are indeed of same color and cropped in similar manner. – anonR Feb 07 '23 at 16:11
  • try morphology close with a kernel large enough to fill the gaps – fmw42 Feb 07 '23 at 17:20
  • Tried with a vertical kernel large enough, But in most of the signatures it tends to fill the loops within characters and make the final image noisy for further analysis. – anonR Feb 07 '23 at 17:24

0 Answers0