admin管理员组文章数量:1396130
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:
- How can I modify my approach to merge or simplify detected thick lines into a single line?
- 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:
- How can I modify my approach to merge or simplify detected thick lines into a single line?
- 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
1 Answer
Reset to default 3You'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 |
---|---|
本文标签: pythonHow to Detect Thick Lines as Single Lines Using Hough Transform in OpenCVStack Overflow
版权声明:本文标题:python - How to Detect Thick Lines as Single Lines Using Hough Transform in OpenCV - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744623678a2616183.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论