1

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: original image

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

HoughLines result

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()
Bilal
  • 3,191
  • 4
  • 21
  • 49
  • Wow, that's pretty tricky. I wonder if it might be faster to trace this in Inkscape rather than use computer vision to do this. Do you just need to do the one floorplan, or are there many like this? – Nick ODell Nov 30 '22 at 19:41
  • sadly there are hundreds, I made a tool called PIC2CAD which would streamline the process on most PDFs but a permit office I am working with only has terrible scans. – Luke-McDevitt Nov 30 '22 at 19:59

1 Answers1

0

There are a few different things you could try, but to start I would recommend the following:

  • First, threshold the image to identify only the parts that constitute the floor plan
  • Next, dilate the image to connect any broken segments
  • Finally, erode the image to prevent your lines from being too thick

You'll have to mess around with the parameters to get it right, but I think this is your best bet to solve this problem without it getting too complicated.

If you want, you can also try the Sobel operators before thresholding to better identify horizontal and vertical lines.

Kevin
  • 1
  • I created a GUI that allows for me to change all of these parameters and make stages of each. Even then using all options of Houghlines, Thresholding, Dilating, Eroding, and Blurring openCV has to offer I still cannot generate a clean enough output image – Luke-McDevitt Dec 21 '22 at 22:24