Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 车窗分割_Python_Opencv_Image Processing_Image Segmentation - Fatal编程技术网

Python 车窗分割

Python 车窗分割,python,opencv,image-processing,image-segmentation,Python,Opencv,Image Processing,Image Segmentation,我正在做一个项目,我们需要从车内分割车窗车。 我正在使用OpenCV,但这不是强制性的。(也Python或C++是OK)< /P> 直到现在,我有一些(不太好)的结果。我遵循以下顺序: 1) 在可能存在windows的ROI中应用cv2.grabCut() import cv2 import numpy as np from matplotlib import pyplot as plt #Read Image img = cv2.imread("test1.png",-1) #Grab C

我正在做一个项目,我们需要从车内分割车窗车。 我正在使用OpenCV,但这不是强制性的。(也Python或C++是OK)< /P> 直到现在,我有一些(不太好)的结果。我遵循以下顺序:

1) 在可能存在windows的ROI中应用
cv2.grabCut()

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)边缘

3) Aplpy
cv2.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()
    的工作更轻松

  • 也许有更快的方法

  • 我避免使用机器学习或人工智能方法,因为我们使用的是一台功能不太强大的计算机。但我对这些想法持开放态度

我正在添加绘图,以便您可以看到结果(原始图像,包括遮罩和输出):

[编辑]:

我还发布了一张带有遮挡的图像(这看起来并没有那么失败,但它是检测窗口而不是固定遮罩的原因)


如果摄像头固定,窗口的坐标不会改变。因此,无需检测,只需使用坐标进行裁剪。或者检测的主要原因是什么?@YunusTemurlenk谢谢你的回答,如果有人在相机和窗口之间=>只应更改窗口段,但在你的图像中似乎没有问题,你应该共享一张你很难检测窗口的图像。我将上传一个这样的例子我刚刚上传了遮挡图像如果相机是固定的,窗口的坐标不会改变。因此,无需检测,只需使用坐标进行裁剪。或者检测的主要原因是什么?@YunusTemurlenk谢谢你的回答,如果有人在相机和窗口之间=>只应更改窗口段,但在你的图像中似乎没有问题,你应该共享一个你很难检测到窗口的图像。我将上传一个这样的示例。我刚刚上传了遮挡图像
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 ))