gpt4 book ai didi

image - 如何在 OpenCV Python 中对附近的轮廓进行分组? - 斑马线检测

转载 作者:行者123 更新时间:2023-12-02 16:07:16 25 4
gpt4 key购买 nike

我想检测斑马线。我试图使用 contour 找出图像中斑马线的坐标,但它给出了不同白框的输出(斑马线中只有白线)。但是我需要整个斑马线的坐标。

请告诉我轮廓分组的方法或建议我另一种检测斑马线的方法。

Input image

Output image obtained

Expected output

import cv2
import numpy as np
image = cv2.imread('d.jpg',-1)
paper = cv2.resize(image,(500,500))
ret, thresh_gray = cv2.threshold(cv2.cvtColor(paper, cv2.COLOR_BGR2GRAY),
200, 255, cv2.THRESH_BINARY)
image, contours, hier = cv2.findContours(thresh_gray, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
for c in contours:
rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rect)
# convert all coordinates floating point values to int
box = np.int0(box)
cv2.drawContours(paper, [box], 0, (0, 255, 0),1)
cv2.imshow('paper', paper)
cv2.imwrite('paper.jpg',paper)
cv2.waitKey(0)

最佳答案

您可以 closing用于缩小间隙的形态学操作。

我猫建议以下阶段:

  • thresh_gray 中查找轮廓。
  • 用非常小的区域(噪声)删除轮廓。
  • 删除低纵横比的轮廓(假设斑马线必须又长又窄。
  • 使用morphologyEx执行关闭形态学操作 - 关闭合并关闭组件。
  • 在删除和关闭后的图像中再次找到轮廓。
    在最后阶段,忽略小轮廓。

这是一个工作代码示例:

import cv2
import numpy as np

image = cv2.imread('d.jpg', -1)
paper = cv2.resize(image, (500,500))
ret, thresh_gray = cv2.threshold(cv2.cvtColor(paper, cv2.COLOR_BGR2GRAY), 200, 255, cv2.THRESH_BINARY)
image, contours, hier = cv2.findContours(thresh_gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

# Erase small contours, and contours which small aspect ratio (close to a square)
for c in contours:
area = cv2.contourArea(c)

# Fill very small contours with zero (erase small contours).
if area < 10:
cv2.fillPoly(thresh_gray, pts=[c], color=0)
continue

# https://stackoverflow.com/questions/52247821/find-width-and-height-of-rotatedrect
rect = cv2.minAreaRect(c)
(x, y), (w, h), angle = rect
aspect_ratio = max(w, h) / min(w, h)

# Assume zebra line must be long and narrow (long part must be at lease 1.5 times the narrow part).
if (aspect_ratio < 1.5):
cv2.fillPoly(thresh_gray, pts=[c], color=0)
continue


# Use "close" morphological operation to close the gaps between contours
# https://stackoverflow.com/questions/18339988/implementing-imcloseim-se-in-opencv
thresh_gray = cv2.morphologyEx(thresh_gray, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (51,51)));

# Find contours in thresh_gray after closing the gaps
image, contours, hier = cv2.findContours(thresh_gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

for c in contours:
area = cv2.contourArea(c)

# Small contours are ignored.
if area < 500:
cv2.fillPoly(thresh_gray, pts=[c], color=0)
continue

rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rect)
# convert all coordinates floating point values to int
box = np.int0(box)
cv2.drawContours(paper, [box], 0, (0, 255, 0),1)

cv2.imshow('paper', paper)
cv2.imwrite('paper.jpg', paper)
cv2.waitKey(0)
cv2.destroyAllWindows()

thresh_gray 在删除小的和方形的轮廓之前:
enter image description here

thresh_gray 删除小和方形轮廓后:
enter image description here

thresh_gray 关闭操作后:
enter image description here

最终结果:
enter image description here


备注:
我对使用形态学操作来缩小差距的好处有些怀疑。
使用基于几何的智能逻辑可能会更好。

关于image - 如何在 OpenCV Python 中对附近的轮廓进行分组? - 斑马线检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60259169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com