簡體   English   中英

即使圖像在Python中的OpenCV中包含許多行,Hough Line Transform也只識別一行

[英]Hough Line Transform identifies only one line even though image contains many lines in OpenCV in Python

我在OpenCV中使用拉普拉斯變換進行邊緣檢測,然后使用霍夫線變換檢測其中的線。 這些識別的線最終需要從圖像中移除。

import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('Feb_16-0.jpg',0)
kernel = np.ones((1,1),np.uint8)
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
blur = cv2.GaussianBlur(opening,(1,1),0)
ret3,th4 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) 
laplacian = cv2.Laplacian(th4,cv2.CV_8UC1)
cst = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP(laplacian,1,np.pi/180,100,minLineLength,maxLineGap)
for x1,y1,x2,y2 in lines[0]:
    cv2.line(cst,(x1,y1),(x2,y2),(0,255,0),2)

cv2.imwrite('houghlines5.jpg',cst)

我期望找出條例草案中的所有內容: 互聯網法案

拉普拉斯邊緣檢測的結果如下:

邊緣檢測結果

而Hough Line Transform返回的結果只標識了下圖中綠線所標記的一條線: 轉型互聯網法案

任何人都可以幫我弄清楚代碼中需要進行哪些修改,以便識別出互聯網法案的所有大膽的橫向/縱向線?

在我看來,你只是閱讀“行”中的第一個元素:

for x1,y1,x2,y2 in lines[0]:
    cv2.line(cst,(x1,y1),(x2,y2),(0,255,0),2)

因此,您只繪制了已發現的第一條(最重要的)線(最長,最厚)。 我建議你試試:

for line in lines:    
    for x1,y1,x2,y2 in line:
         cv2.line(cst,(x1,y1),(x2,y2),(0,255,0),2)

此外,如果仍然沒有得到正確的結果,我建議將minLineLength設置得更低。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM