最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - How to Detect Thick Lines as Single Lines Using Hough Transform in OpenCV - Stack Overflow

programmeradmin3浏览0评论

I'm using OpenCV's HoughLinesP function to detect straight lines in an image. When the image contains thin lines, the detection works perfectly. However, when the image contains thick lines, the algorithm detects them as two parallel lines instead of a single line.

Here's my current code:

import cv2
import numpy as np

image_path = "thickLines.png" 
image = cv2.imread(image_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Thresholding to create a binary image
_, binary = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY_INV)

# Edge Detection
edges = cv2.Canny(binary, 50, 150, apertureSize=3)

# Hough Line Transform to Detect Walls
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 100, minLineLength=50, maxLineGap=5)

# Draw Detected Walls
if lines is not None:
    for line in lines:
        x1, y1, x2, y2 = line[0]
        cv2.line(image, (x1, y1), (x2, y2), (0, 255, 0), 2)  # Draw thick lines in green

# Show Final Processed Image
cv2.imshow("Detected Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

I have tried adjusting the Canny edge thresholds and modifying minLineLength and maxLineGap, but the issue persists.

My goal is to detect thick lines as a single line instead of two parallel lines.

Questions:

  1. How can I modify my approach to merge or simplify detected thick lines into a single line?
  2. Are there any parameters in HoughLinesP that can be tuned to achieve this?

Screenshots
Here are screenshots of the issue:
The green lines represents the detected lines from the image

Here are sample images of thick & thin lines

I'm using OpenCV's HoughLinesP function to detect straight lines in an image. When the image contains thin lines, the detection works perfectly. However, when the image contains thick lines, the algorithm detects them as two parallel lines instead of a single line.

Here's my current code:

import cv2
import numpy as np

image_path = "thickLines.png" 
image = cv2.imread(image_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Thresholding to create a binary image
_, binary = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY_INV)

# Edge Detection
edges = cv2.Canny(binary, 50, 150, apertureSize=3)

# Hough Line Transform to Detect Walls
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 100, minLineLength=50, maxLineGap=5)

# Draw Detected Walls
if lines is not None:
    for line in lines:
        x1, y1, x2, y2 = line[0]
        cv2.line(image, (x1, y1), (x2, y2), (0, 255, 0), 2)  # Draw thick lines in green

# Show Final Processed Image
cv2.imshow("Detected Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

I have tried adjusting the Canny edge thresholds and modifying minLineLength and maxLineGap, but the issue persists.

My goal is to detect thick lines as a single line instead of two parallel lines.

Questions:

  1. How can I modify my approach to merge or simplify detected thick lines into a single line?
  2. Are there any parameters in HoughLinesP that can be tuned to achieve this?

Screenshots
Here are screenshots of the issue:
The green lines represents the detected lines from the image

Here are sample images of thick & thin lines

Share Improve this question asked Mar 15 at 4:54 Kuldeep JKuldeep J 5667 silver badges19 bronze badges 2
  • 1 I had a similar problem and solved it by manually detecting close parallel lines and replacing them with mid-lines: stackoverflow/a/67798560/45269 If your lines thickness is constant and known in advance, you could maybe try eroding (or dilating, depending on the foreground/background colors) them before edge detection. – Headcrab Commented Mar 15 at 5:36
  • @Headcrab Thanks for the suggestion, although this was my last resort, the solution provided by @ Spectric is working for me, but thanks for acknowledging the question. :) – Kuldeep J Commented Mar 22 at 5:10
Add a comment  | 

1 Answer 1

Reset to default 3

You're looking to skeletonize the image - reduce the thickness of all features to 1 pixel.

scikit-image.morphology has a neat method, skeletonize, just for this.

import cv2
import numpy as np
from skimage.morphology import skeletonize

image_path = "thickLines.png"

image = cv2.imread(image_path)

# binarize image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY_INV)

# convert to skeleton, then to mat
skeleton = skeletonize(binary)
skeletonImg = (skeleton * 255).astype(np.uint8)

# Hough Line Transform to Detect Walls
lines = cv2.HoughLinesP(skeletonImg, 1, np.pi / 180, 80, minLineLength=50, maxLineGap=5)

# Draw Detected Walls
if lines is not None:
    for line in lines:
        x1, y1, x2, y2 = line[0]
        cv2.line(image, (x1, y1), (x2, y2), (0, 255, 0), 2)  # Draw thick lines in green

# Show Final Processed Image
cv2.imshow("Detected Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Result:

thin thick
发布评论

评论列表(0)

  1. 暂无评论