OPENCV – PYTHON | Vignette Effect Explained with Mathematics | Gaussian Kernel & Deviation
The vignette effect, often employed in photography and digital image editing, adds a subtle, artistic touch to images by darkening or softening the edges, drawing focus towards the center. This effect mimics the natural optical phenomenon where the light intensity decreases towards the periphery of an image. In this article, we delve into the mathematics behind the vignette effect and demonstrate its implementation using Gaussian kernel and deviation in OpenCV with Python.
Flow of the Article:
- Understanding the Gaussian Kernel
- Mathematics behind the gaussian kernel
- Understanding the vignette effect
- Coding the Vignette Effect
- Project using the Vignette Effect with Code
Video Explanation:
Understanding the Vignette Effect:
The vignette effect is characterized by a gradual decrease in brightness or saturation towards the corners or edges of an image, creating a visually appealing framing effect. This effect is commonly used to draw attention to the central subject or evoke a nostalgic atmosphere in photographs.
Mathematical Explanation:
The vignette effect is often achieved using a Gaussian filter, which applies a weighted average to each pixel based on its distance from the center. Mathematically, the Gaussian function is defined as:
Implementing the Vignette Effect in OpenCV:
import cv2
import numpy as np
def apply_vignette(img, sigma):
rows, cols = img.shape[:2]
X, Y = np.meshgrid(np.arange(cols), np.arange(rows))
centerX, centerY = cols // 2, rows // 2
distance_sq = (X – centerX)**2 + (Y – centerY)**2
gaussian_kernel = np.exp(-distance_sq / (2 * sigma**2))
normalized_kernel = gaussian_kernel / np.max(gaussian_kernel)
vignette_img = cv2.multiply(img, normalized_kernel)
return vignette_img
# Load the input image
img = cv2.imread(‘image.jpg’)
# Apply vignette effect with specified sigma (standard deviation)
sigma = 100
vignette_result = apply_vignette(img, sigma)
# Display the results
cv2.imshow(‘Original Image’, img)
cv2.imshow(‘Vignette Effect’, vignette_result)
cv2.waitKey(0)
cv2.destroyAllWindows()
In the code above, we define a function apply_vignette()
that takes an input image and the sigma value for the Gaussian kernel. We create a mesh grid representing the image coordinates and calculate the distance of each pixel from the center. We then compute the Gaussian kernel using the distance values and apply it to the input image using element-wise multiplication. Finally, we display the original and vignette-enhanced images using OpenCV.
Project using OpenCV: Code Here
Conclusion:
Understanding the vignette effect and its implementation using Gaussian kernel and deviation in OpenCV empowers photographers and digital artists to add a captivating visual element to their images. By applying this effect judiciously, one can enhance the composition, draw focus to the central subject, and evoke a mood or emotion in photographs, elevating the overall aesthetic appeal of the visual content.
infoaryan.com, Aryan Verma, OpenCV Python projects, python opencv articles, computer vision, image processing, vignette effect project, creating the vignette effect, aryan verma projects, opencv, machine learning