# Apply gaussian blur on a polygon using opencv and python

When working with images in Python, it is often necessary to apply various filters and effects to enhance or modify the image. One common task is to apply a Gaussian blur to a polygon using OpenCV and Python. In this article, we will explore three different ways to achieve this goal.

## Option 1: Using OpenCV’s cv2.filter2D() function

The first option is to use OpenCV’s cv2.filter2D() function to apply a Gaussian blur to the polygon. This function convolves the image with a kernel, which in this case is a Gaussian kernel. Here is the code to accomplish this:

``````
import cv2
import numpy as np

# Define the polygon points
points = np.array([[100, 100], [200, 100], [200, 200], [100, 200]])

# Create a mask for the polygon

# Apply Gaussian blur to the polygon

# Display the result
cv2.imshow('Blurred Image', blurred_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
``````

This code first loads the image using cv2.imread(). Then, it defines the points of the polygon using numpy arrays. Next, it creates a mask for the polygon using cv2.fillPoly(). Finally, it applies the Gaussian blur to the polygon using cv2.GaussianBlur() and displays the result using cv2.imshow().

## Option 2: Using OpenCV’s cv2.warpPerspective() function

The second option is to use OpenCV’s cv2.warpPerspective() function to apply a Gaussian blur to the polygon. This function applies a perspective transformation to the image, which can be used to warp the image according to the polygon’s shape. Here is the code to accomplish this:

``````
import cv2
import numpy as np

# Define the polygon points
points = np.array([[100, 100], [200, 100], [200, 200], [100, 200]])

# Create a mask for the polygon

# Apply perspective transformation to the polygon
transformed_image = cv2.warpPerspective(image, np.array([points]), (image.shape[1], image.shape[0]))

# Apply Gaussian blur to the transformed image
blurred_image = cv2.GaussianBlur(transformed_image, (15, 15), 0)

# Display the result
cv2.imshow('Blurred Image', blurred_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
``````

This code is similar to the previous option, but instead of directly applying the Gaussian blur to the polygon, it first applies a perspective transformation to the image using cv2.warpPerspective(). Then, it applies the Gaussian blur to the transformed image and displays the result.

## Option 3: Using OpenCV’s cv2.drawContours() function

The third option is to use OpenCV’s cv2.drawContours() function to apply a Gaussian blur to the polygon. This function draws contours on the image, which can be used to create a mask for the polygon. Here is the code to accomplish this:

``````
import cv2
import numpy as np

# Define the polygon points
points = np.array([[100, 100], [200, 100], [200, 200], [100, 200]])

# Create a blank image for the mask

# Draw the polygon on the mask

# Apply Gaussian blur to the polygon

# Display the result
cv2.imshow('Blurred Image', blurred_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
``````

This code first creates a blank image for the mask using np.zeros(). Then, it draws the polygon on the mask using cv2.drawContours(). Finally, it applies the Gaussian blur to the polygon using cv2.GaussianBlur() and displays the result.

After exploring these three options, it is clear that Option 1, which uses OpenCV’s cv2.filter2D() function, is the best choice for applying a Gaussian blur to a polygon using OpenCV and Python. This option is more straightforward and requires fewer steps compared to the other options. Additionally, it provides more control over the blur parameters, such as the kernel size and standard deviation.

Rate this post

### 9 Responses

1. Luke says:

Option 1 seems like the simplest and most straightforward way to apply gaussian blur on a polygon.

2. Ares says:

Option 3 seems unnecessary. Why complicate things when Option 1 does the job just fine?

3. Rosalyn Galindo says:

Option 2 seems like the way to go! Warping perspectives sounds cool and futuristic. #BlurOnPoint

4. Layne says:

Option 1 looks cool, but I wonder if Option 2 or 3 offer better results. Thoughts?

5. Renata says:

Option 2 seems like a cool way to blur polygons, but option 3 sounds more fun! #BlurTheContours

6. Colt Clayton says:

Option 3 seems like a fun way to add some artistic touch to polygons!

7. Marigold Hebert says:

Option 1 seems like the easiest way to go, but Option 3 sounds more fun! #BlurOnPolygons

8. Leyla Trujillo says:

Option 2 seems more fun! Warping perspectives and blurring polygons? Count me in! 🌀🔳🔍

9. Nova says:

Option 1 seems like a quick and easy way to apply gaussian blur, but what about the other options? Any pros and cons?