Gaussian blurring with OpenCV: only blurring a subregion of an image?

后端 未结 4 694
野的像风
野的像风 2020-12-15 22:49

Is it possible to only blur a subregion of an image, instead of the whole image with OpenCV, to save some computational cost?

EDIT: One important po

4条回答
  •  [愿得一人]
    2020-12-15 23:21

    Here's how to do it in Python. The idea is to select a ROI, blur it, then insert it back into the image

    import cv2
    
    # Read in image
    image = cv2.imread('1.png')
    
    # Create ROI coordinates
    topLeft = (60, 140)
    bottomRight = (340, 250)
    x, y = topLeft[0], topLeft[1]
    w, h = bottomRight[0] - topLeft[0], bottomRight[1] - topLeft[1]
    
    # Grab ROI with Numpy slicing and blur
    ROI = image[y:y+h, x:x+w]
    blur = cv2.GaussianBlur(ROI, (51,51), 0) 
    
    # Insert ROI back into image
    image[y:y+h, x:x+w] = blur
    
    cv2.imshow('blur', blur)
    cv2.imshow('image', image)
    cv2.waitKey()
    

    Before -> After

提交回复
热议问题