Opencv 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 implement several simple filter effects using OpenCV, including grayscale, vintage, and emboss effects.
Here are the main filter effects:
| **Filter Effect** | **Implementation Method** |
| --- | --- |
| **Grayscale Filter** | `cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)` |
| **Vintage Filter** | Adjust the weights of color channels to simulate old photo effects. |
| **Emboss Filter** | Use convolution kernel `[[-2, -1, 0], [-1, 1, 1], [0, 1, 2]]` for convolution operation. |
| **Blur Filter** | `cv2.GaussianBlur(image, (15, 15), 0)` |
| **Sharpen Filter** | Use convolution kernel `[[0, -1, 0], [-1, 5, -1], [0, -1, 0]]` for convolution operation. |
| **Edge Detection Filter** | `cv2.Canny(gray_image, 100, 200)` |
## 1γGrayscale Filter
The grayscale filter is one of the simplest filters, which converts a color image to a grayscale image.
A grayscale image has only one channel, and the value of each pixel represents the brightness.
### Implementation Steps
1. Read the image.
2. Use the `cv2.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 imageImage
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')`: Reads the image file.
* `cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)`: Converts the image from BGR color space to grayscale color space.
* `cv2.imwrite('gray_output.jpg', gray_image)`: Saves the grayscale image.
* `cv2.imshow('Gray Image', gray_image)`: Displays the grayscale image.
* * *
## 2γVintage Filter
The vintage filter adjusts the color channels of the image to give it a retro effect.
Typically, the vintage 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 the vintage effect image.
## 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 Sepia Image
cv2.imwrite('vintage_output.jpg', vintage_image)
# Display Vintage Image
cv2.imshow('Vintage Image', vintage_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
**Code Analysis:**
* `cv2.split(image)`: Splits the BGR channels of the image.
* `np.clip()``: Ensures pixel values are between 0 and 255.
* `cv2.merge((b, g, r))`: Merges the adjusted channels to generate the vintage effect image.
* * *
## 3γEmboss Filter
The emboss filter calculates the difference between adjacent pixels in the image to generate an effect similar to embossing. This filter is usually used to enhance edges and textures in images.
### Implementation Steps
1. Read the image.
2. Convert the image to a grayscale image.
3. Use the convolution kernel to calculate the emboss effect.
## Example
import cv2
import numpy as np
# Read image
image = cv2.imread('input.jpg')
# Convert to grayscale imageImage
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 Embossed Image
cv2.imwrite('emboss_output.jpg', emboss_image)
# Display Embossed Image
cv2.imshow('Emboss Image', emboss_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
**Code Analysis:**
* `cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)`: Converts the image to a grayscale image.
* `cv2.filter2D(gray_image, -1, kernel)`: Applies the convolution kernel to generate the emboss effect.
* * *
## 4γBlur Filter
The blur filter smooths the image to reduce noise and details in the image.
## Example
import cv2
# Read image
image = cv2.imread("path/to/image.jpg")
# Blur Filter
blurred_image = cv2.GaussianBlur(image,(15,15),0)
# Display results
cv2.imshow("Blur Filter", blurred_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
* * *
## 5γSharpen Filter
The sharpen filter enhances the edges of the image to make the image clearer.
## 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 results
cv2.imshow("Sharpen Filter", sharpened_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
* * *
## 6γEdge Detection Filter
The edge detection filter detects edges in the image and highlights the contours of objects.
## 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 results
cv2.imshow("Edges Filter", edges_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
YouTip