4

I'm quite new to OpenCV and Python. I need to apply Gaussian Blur on contours that I get using cv2.findContours. I have successfully applied Gaussian Blur, but only on rectangles. I couldn't find any examples that show how to apply it to randomly shaped contours.

I'm using OpenCV version 3.1.0.

Thanks!

Gorgi Rankovski
  • 2,303
  • 1
  • 23
  • 32

2 Answers2

10

I had the same issue and here's the solution that actually worked for me (and it works on any contour) :

import cv2 as cv
import numpy as np

Here I define the list of vertices of an example polygon ROI:

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

Read the original Image:

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

create a blurred copy of the entire image:

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

create a mask for the ROI and fill in the ROI with (255,255,255) color :

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)

create a mask for everywhere in the original image except the ROI, (hence mask_inverse) :

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

combine all the masks and above images in the following way :

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

here's a example of an original image witth the ROI being the oblique license plate (a parallelogram):

original image

and the resulting image:

target polygon (here just a parallelogram) blurred

Moalana
  • 421
  • 3
  • 11
  • 1
    Hey thanks for answering this! It's been two years since I asked the question and I ended up finding a solution, however I have forgotten to post it here. Will check your solution and accept it as answer if it works. – Gorgi Rankovski Jan 24 '19 at 16:17
0

You might consider blurring the image everywhere and then taking a weighted average between the blurred image, and the original image, where the weights are one inside the contour region, and zero outside:

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

Alternatively, use copyTo:

outImg = img.clone();
blurredImg.copyTo(outImg,contourMask);
willem
  • 311
  • 1
  • 6