OpenCV Simple Filter Effects

OpenCV provides rich image processing and computer vision algorithms, widely used in image processing, video analysis, object detection, and other fields.

This article will introduce how to use OpenCV to implement several simple filter effects, including grayscale, nostalgic, and emboss effects.

The following are the main filter effects:

Filter EffectImplementation Method
Grayscale Filtercv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Nostalgic FilterSimulate old photo effects by adjusting the weights of color channels.
Emboss FilterUse convolution kernel[[-2, -1, 0], [-1, 1, 1], [0, 1, 2]]to perform convolution operation.
Blur Filtercv2.GaussianBlur(image, (15, 15), 0)
Sharpen FilterUse convolution kernel[[0, -1, 0], [-1, 5, -1], [0, -1, 0]]to perform convolution operation.
Edge Detection Filtercv2.Canny(gray_image, 100, 200)

1. Grayscale Filter

The grayscale filter is one of the simplest filters; it converts color images to grayscale images.

A grayscale image has only one channel, and each pixel value represents brightness.

Implementation Steps

  1. Read the image.
  2. Use thecv2.cvtColor()function to convert the image from BGR color space to grayscale color space.

Example

import cv2

# Read image
image = cv2.imread('input.jpg')

# Convert to grayscale image
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Save grayscale image
cv2.imwrite('gray_output.jpg', gray_image)

# Display grayscale image
cv2.imshow('Gray Image', gray_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Code analysis:

  • cv2.imread('input.jpg'): Read the image file.
  • cv2.cvtColor(image, cv2.COLOR_BGR2GRAY): Convert the image from BGR color space to grayscale color space.
  • cv2.imwrite('gray_output.jpg', gray_image): Save the grayscale image.
  • cv2.imshow('Gray Image', gray_image): Display the grayscale image.

2. Nostalgic Filter

The nostalgic filter adjusts the color channels of an image to give it a retro effect.

Typically, the nostalgic filter increases the intensity of the red and green channels while reducing the intensity of the blue channel.

Implementation Steps

  1. Read the image.
  2. Split the BGR channels of the image.
  3. Adjust the intensity of each channel.
  4. Merge the channels to generate an image with a nostalgic effect.

Example

import cv2
import numpy as np

# Read image
image = cv2.imread('input.jpg')

# Split BGR channels
b, g, r = cv2.split(image)

# Adjust channel intensity
r = np.clip(r * 0.393 + g * 0.769 + b * 0.189, 0, 255).astype(np.uint8)
g = np.clip(r * 0.349 + g * 0.686 + b * 0.168, 0, 255).astype(np.uint8)
b = np.clip(r * 0.272 + g * 0.534 + b * 0.131, 0, 255).astype(np.uint8)

# Merge channels
vintage_image = cv2.merge((b, g, r))

# Save nostalgic image
cv2.imwrite('vintage_output.jpg', vintage_image)

# Display nostalgic image
cv2.imshow('Vintage Image', vintage_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Code analysis:

  • cv2.split(image): Split the BGR channels of the image.
  • np.clip(): Ensure pixel values are between 0 and 255.
  • cv2.merge((b, g, r)): Merge the adjusted channels to generate an image with a nostalgic effect.

3. Emboss Filter

The emboss filter generates an effect similar to embossing by calculating the difference between adjacent pixels in the image. This filter is often used to enhance edges and textures in an image.

Implementation Steps

  1. Read the image.
  2. Convert the image to grayscale.
  3. Use a convolution kernel to compute the emboss effect.

Example

import cv2
import numpy as np

# Read image
image = cv2.imread('input.jpg')

# Convert to grayscale image
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Define convolution kernel
kernel = np.array([[-2, -1, 0],
                   [-1,  1, 1],
                   [ 0,  1, 2]])

# Apply convolution kernel
emboss_image = cv2.filter2D(gray_image, -1, kernel)

# Save emboss image
cv2.imwrite('emboss_output.jpg', emboss_image)

# Display emboss image
cv2.imshow('Emboss Image', emboss_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Code analysis:

  • cv2.cvtColor(image, cv2.COLOR_BGR2GRAY): Convert the image to grayscale.
  • cv2.filter2D(gray_image, -1, kernel): Apply the convolution kernel to generate the emboss effect.

4. Blur Filter

The blur filter reduces noise and detail in an image by smoothing it.

Example

import cv2

# Read image
image = cv2.imread("path/to/image.jpg")

# Blur filter
blurred_image = cv2.GaussianBlur(image, (15, 15), 0)

# Display result
cv2.imshow("Blur Filter", blurred_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

5. Sharpen Filter

The sharpen filter makes an image clearer by enhancing its edges.

Example

import cv2
import numpy as np

# Read image
image = cv2.imread("path/to/image.jpg")

# Sharpen filter
sharpen_kernel = np.array([[0, -1, 0],
                           [-1, 5, -1],
                           [0, -1, 0]])
sharpened_image = cv2.filter2D(image, -1, sharpen_kernel)

# Display result
cv2.imshow("Sharpen Filter", sharpened_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

6. Edge Detection Filter

The edge detection filter highlights the contours of objects by detecting edges in an image.

Example

import cv2

# Read image
image = cv2.imread("path/to/image.jpg")

# Convert to grayscale image
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Edge detection filter
edges_image = cv2.Canny(gray_image, 100, 200)

# Display result
cv2.imshow("Edges Filter", edges_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Other Extensions