gpt4 book ai didi

python - 如何使用 morphologyEx 和 drawContours 检测空的公园空间?

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

我有以下图像,其中包含无人机捕获的汽车和空 parking 位。我想检测空的公园空间并绘制一个看起来像预期图像的框。

这是我的代码:

import cv2
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline

img = cv2.imread('parking.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
blurred = cv2.bilateralFilter(gray,21,41,41)
edged = cv2.Canny(blurred,400,600)
thresh = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 5, 40)

mask = cv2.bitwise_not(edged)
thresh = cv2.bitwise_and(thresh,thresh,mask=mask)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
thresh = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

output = img.copy()
cv2.drawContours(output, contours, -1, (0,255,0), 1)
plt.imshow(output)

问题是它检测到所有现有的矩形。如何减少轮廓以仅检测车辆?

输入图像:

enter image description here

输出图像:

enter image description here

我试图获得什么,预期输出:

enter image description here

最佳答案

我认为这种方法行不通。有两种方法可以解决这个问题:

  • 训练一个物体检测神经网络来寻找空的空间。这将产生最准确的预测,但实现起来有点困难,并且需要带注释的数据集。
  • 使用主要背景颜色(相当均匀)获得“空”空间,然后处理 Blob 以确定汽车之间或在代码中检测到的帧内的空区域

  • 您可以通过添加 corner detection 来提高检测质量。找到 parking 位的角落。有些可能会被汽车遮挡,但您将有足够的数据点来找到正确分割空间的序列。
    不幸的是,所有这些都需要更多的编码才能包含在答案中,但希望它能给你一些指导。

    关于python - 如何使用 morphologyEx 和 drawContours 检测空的公园空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57672783/

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