python - 车窗分割
问题描述
我正在做一个项目,我们需要从车内对窗户车进行分割。我正在使用OpenCV,但这不是强制性的。(python 或 C++ 也可以)
到目前为止,我有一些(不太好)的结果。我遵循了这个顺序:
1) 应用于cv2.grabCut()
可能有窗口的 ROI。
import cv2
import numpy as np
from matplotlib import pyplot as plt
#Read Image
img = cv2.imread("test1.png",-1)
#Grab Cut iterations
itera = 30
p1 = True
if p1: # This takes quite long so this helps when debugging
gb_mask_w1 = np.zeros(img.shape[:2],np.uint8) #output mask
bgdModel = np.zeros((1,65),np.float64)
fgdModel = np.zeros((1,65),np.float64)
rect = (1,12,390,845) #ROI of window 1
cv2.grabCut(img,gb_mask_w1,rect,bgdModel,fgdModel,itera,cv2.GC_INIT_WITH_RECT)
# 0-pixels and 2-pixels are put to 0 (ie background) and all 1-pixels and
# 3-pixels are put to 1(ie foreground pixels).
mask_w1 = np.where((gb_mask_w1==3),0,1).astype('uint8') #
gb_mask_w2 = np.zeros(img.shape[:2],np.uint8)#output mask
rect_1 = (1500,12,323,820) #ROI of window 2
cv2.grabCut(img,gb_mask_w2,rect_1,bgdModel,fgdModel,itera,cv2.GC_INIT_WITH_RECT)
# 0-pixels and 2-pixels are put to 0 (ie background) and all 1-pixels and
# 3-pixels are put to 1(ie foreground pixels).
mask_w2 = np.where((gb_mask_w2==3),0,1).astype('uint8')
2)侵蚀前景像素以获得干净(er)边缘
# Morphological Operations
kernel = np.ones((10,10),np.uint8)
eroded_w1 = cv2.erode(mask_w1,kernel,iterations = 1)
eroded_w2 = cv2.erode(mask_w2,kernel,iterations = 1)
3)Aplpycv2.fastNlMeansDenoising()
避免伪影
# DeNoise artifacts
mask_w1_porc = cv2.fastNlMeansDenoising(eroded_w1)
mask_w2_porc = cv2.fastNlMeansDenoising(eroded_w2)
4)只需应用生成的掩码和绘图
img_treated = img.copy()
# Green background
bskg = np.zeros(img.shape[:3],np.uint8)
bskg[:] = (0, 177, 64)
#Apply mask
img_treated[mask_w1_porc==0] = bskg[mask_w1_porc==0]
img_treated[mask_w2_porc==0] = bskg[mask_w2_porc==0]
#Plot
f,ax=plt.subplots(2,2)
ax[0,0].imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB ))
ax[0,1].imshow(mask_w1_porc)
ax[1,0].imshow(mask_w2_porc)
ax[1,1].imshow(cv2.cvtColor(img_treated,cv2.COLOR_BGR2RGB ))
问题:
碰巧实际上只检测到窗口的一部分。
可能会发生汽车的某些部分被检测为车窗的情况
非常慢(每个窗口约 20 秒)
有什么工作吗?
至少总能检测到窗口的重要部分
它对遮挡很强大
我想要什么?
我想知道我可以在这个管道中添加什么来获得更好的窗口检测 => 一个过滤器来使
cv2.grabCut()
工作更容易?也许有更快的方法?
我避免使用机器学习或 AI 方法,因为我们使用的计算机不是那么强大。但我对这些想法持开放态度
我正在添加绘图,以便您可以看到结果(原始图像,蒙版和输出):
[编辑]:
我还发布了一个带有遮挡的图像(这似乎并没有那么失败,但它是检测窗口而不是使用固定掩码的原因)
解决方案
推荐阅读
- javascript - 如何在我的 chrome 扩展程序中访问 chrome 通知 API?
- excel - 返回收件箱中项目的 mailitem 属性:错误 438 对象不支持此属性或方法
- microsoft-graph-api - 如何在 Microsoft Graph REST API 中识别已取消的事件?
- excel - Excel 的 LEN() 函数对超过 20 个字符的数字返回 5
- python - 来自展平列类型系列或列表的数据框
- email - Jenkins - 扩展电子邮件通知 - 使用来自 jenkins 的用户列表填充默认配方
- graphql - 如何将父类型值作为参数传递给 GraphQL-Dotnet 中的子类型?
- python - 如何在仍然访问完整目录的同时返回目录的最后一部分?特金特
- oracle - 将数据 clob 到多个列中
- python - Pycharm“没有名为 REPORT.py 的模块”,但是它存在并且可以工作。但是之后