【问题标题】:Apply Gaussian Blur on a polygon using OpenCV and Python使用 OpenCV 和 Python 在多边形上应用高斯模糊
【发布时间】:2016-12-15 20:36:12
【问题描述】:

我对 OpenCV 和 Python 还是很陌生。我需要在使用 cv2.findContours 获得的轮廓上应用高斯模糊。我已经成功应用了高斯模糊,但仅限于矩形。我找不到任何示例来说明如何将其应用于随机形状的轮廓。

我使用的是 OpenCV 3.1.0 版。

谢谢!

【问题讨论】:

    标签: python opencv image-processing gaussianblur


    【解决方案1】:

    我遇到了同样的问题,这是对我真正有用的解决方案(它适用于任何轮廓):

    import cv2 as cv
    import numpy as np
    

    这里我定义了一个示例多边形 ROI 的顶点列表:

    roi_corners = np.array([[(180,300),(120,540),(110,480),(160,350)]],dtype = np.int32)
    

    阅读原图:

    image = cv.imread('image.jpeg')
    

    创建整个图像的模糊副本:

    blurred_image = cv.GaussianBlur(image,(43, 43), 30)
    

    为 ROI 创建一个蒙版并用 (255,255,255) 颜色填充 ROI:

    mask = np.zeros(image.shape, dtype=np.uint8)
    channel_count = image.shape[2]
    ignore_mask_color = (255,)*channel_count
    cv.fillPoly(mask, roi_corners, ignore_mask_color)
    

    为原始图像中除 ROI 之外的所有位置创建掩码,(因此 mask_inverse):

    mask_inverse = np.ones(mask.shape).astype(np.uint8)*255 - mask
    

    按以下方式组合所有蒙版和上面的图像:

    final_image = cv.bitwise_and(blurred_image, mask) + cv.bitwise_and(image, mask_inverse)
    

    这是一个原始图像的示例,其中 ROI 是倾斜的车牌(平行四边形):

    以及生成的图像:

    【讨论】:

    • 您好,感谢您回答这个问题!我问这个问题已经两年了,我最终找到了解决方案,但是我忘记在这里发布了。如果可行,将检查您的解决方案并接受它作为答案。
    【解决方案2】:

    您可能会考虑在各处模糊图像,然后在模糊图像和原始图像之间取加权平均,其中权重在轮廓区域内为 1,在轮廓区域外为 0:

    outImg = img.mul(contourMask) + blurredImg.mul(1-contourMask);
    

    或者,使用 copyTo:

    outImg = img.clone();
    blurredImg.copyTo(outImg,contourMask);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-05
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多