[英]Denoise noisy straight lines / make noisy lines solid Python
我試圖在 python 中非常嘈雜的平面圖圖像中去噪/制作實線,但沒有成功。 我用過的方法是:
我什至嘗試過前兩者的組合。 這是我試圖制作成實線的示例輸入圖像:
使用HoughLines
方法,這是我能達到的最佳結果(線條實心但在有文本的地方瘋狂重疊(這不能通過更改我的 minline/maxlinegap 變量輕松修復):
我嘗試過:掩蔽、高斯模糊和 Houghlinesp。
Houghlinesp代碼:
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()
您可以嘗試幾種不同的方法,但首先我會推薦以下內容:
threshold
處理以僅識別構成平面圖的部分dilate
圖像以連接任何損壞的部分erode
圖像以防止線條太粗您必須弄亂參數才能使其正確,但我認為這是解決此問題而又不會變得太復雜的最佳選擇。
如果需要,您還可以在設置閾值之前嘗試 Sobel 運算符,以更好地識別水平線和垂直線。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.