首页 > 解决方案 > 车窗分割

问题描述

我正在做一个项目,我们需要从车内对窗户车进行分割。我正在使用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 ))

问题

有什么工作吗?

我想要什么?

我正在添加绘图,以便您可以看到结果(原始图像,蒙版和输出): 所有代码的输出

[编辑]:

我还发布了一个带有遮挡的图像(这似乎并没有那么失败,但它是检测窗口而不是使用固定掩码的原因)

有遮挡的图像

标签: pythonopencvimage-processingimage-segmentation

解决方案


推荐阅读