Blur a specific part of an image

后端 未结 2 1421
生来不讨喜
生来不讨喜 2020-12-10 06:35

I have an image. Like this:

I detect a subject(which is a person in this case) & it masks the image like this:

I want the background of

相关标签:
2条回答
  • 2020-12-10 07:27

    You may use np.where() method to select the pixels where you want blurred values and then replace them as:

    import cv2
    import numpy as np
    
    img = cv2.imread("/home/user/Downloads/lena.png")
    blurred_img = cv2.GaussianBlur(img, (21, 21), 0)
    
    mask = np.zeros((512, 512, 3), dtype=np.uint8)
    mask = cv2.circle(mask, (258, 258), 100, np.array([255, 255, 255]), -1)
    
    out = np.where(mask==np.array([255, 255, 255]), img, blurred_img)
    
    cv2.imwrite("./out.png", out)
    

    0 讨论(0)
  • 2020-12-10 07:27

    as ZdaR said:

    import cv2
    import numpy as np
    
    img = cv2.imread("/home/user/Downloads/lena.png")
    blurred_img = cv2.GaussianBlur(img, (21, 21), 0)
    
    mask = np.zeros((512, 512, 3), dtype=np.uint8)
    mask = cv2.circle(mask, (258, 258), 100, np.array([255, 255, 255]), -1)
    
    out = np.where(mask==np.array([255, 255, 255]), img, blurred_img)
    
    cv2.imwrite("./out.png", out)
    

    this is good idea but I've got same error as penta:

    @ZdaR I get TypeError: Scalar value for argument 'color' is not numeric

    the simple solution is modifying color value when you create Circle:

    mask = cv2.circle(mask, (258, 258), 100, (255, 255,255), -1)
    

    just CHANGE np,array([255,255,255]) to (255,255,255).

    0 讨论(0)
提交回复
热议问题