【发布时间】:2019-12-31 12:33:21
【问题描述】:
我有以下图像,其中有无人机捕获的汽车和空停车位。我想检测空的公园空间并绘制一个看起来像预期图像的框。
这是我的代码:
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)
问题是它检测到所有现有的矩形。如何减少轮廓以仅检测车辆?
输入图像:
输出图像:
我试图获得的,预期的输出:
【问题讨论】:
-
我认为依靠简单的形态学运算和轮廓检测来解决这个问题没有一种稳健的方法。更好的方法是使用深度学习,例如训练 HOG 对象检测器或 CNN。看看this 使用Mask R-CNN 解决这个问题的文章
标签: python python-3.x opencv image-processing contour