I am attempting to denoise / make solid lines in a very noisy image of a floor-plan in python to no success. The methods I have used are:
I have even tried a combination of the first two. here is the sample input image I am trying to make into solid straight lines:
With using the HoughLines
method this is the best result I could achieve (lines solid but overlapping like crazy wherever there is text (This cannot easily be fixed by changing my minline/maxlinegap variables):
I have tried: masking, Gaussian blur, and Houghlinesp.
Houghlinesp Code:
import cv2
import numpy as np
from tkinter import Tk # from tkinter import Tk for Python 3.x
from tkinter.filedialog import askopenfilename
import os
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filename)
filename3, file_extension = os.path.splitext(filename)
# Read input
img = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
# Initialize output
out = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
# Median blurring to get rid of the noise; invert image
img = 255 - cv2.medianBlur(img, 3)
# Detect and draw lines
lines = cv2.HoughLinesP(img, 1, np.pi/180, 10, minLineLength=40, maxLineGap=30)
for line in lines:
for x1, y1, x2, y2 in line:
cv2.line(out, (x1, y1), (x2, y2), (0, 0, 255), 2)
cv2.imshow('out', out)
cv2.imwrite(filename3+' '+'69'+'.png', out)
cv2.waitKey(0)
cv2.destroyAllWindows()