Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

OpenCV Simple Filter Effects

OpenCV Simple Filter Effects

OpenCV provides a rich set of image processing and computer vision algorithms, widely used in fields such as image processing, video analysis, and object detection.

This article introduces how to use OpenCV to implement several simple filter effects, including grayscale, retro, and emboss effects.

The following are the main filter effects:

Filter Effects Implementation Methods
Grayscale Filter cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Retro Filter Simulates an old-photo effect by adjusting the weights of the color channels.
Emboss Filter Using Convolution Kernels [[-2, -1, 0], [-1, 1, 1], [0, 1, 2]] to perform a convolution operation.
Blur filter cv2.GaussianBlur(image, (15, 15), 0)
Sharpen filter Using Convolution Kernels [[0, -1, 0], [-1, 5, -1], [0, -1, 0]] to perform a convolution operation.
Edge detection filter cv2.Canny(gray_image, 100, 200)

1. Grayscale Filter

The grayscale filter is one of the simplest filters; it converts a color image into a grayscale image.

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

Implementation Steps

  1. Read the image.
  2. Use cv2.cvtColor() The function converts the image from the BGR color space to the grayscale color space.

Example

import

cv2

Read an image

image

=

cv2.

imread

(

'input.jpg'

)

Convert to grayscale image

gray_image

=

cv2.

cvtColor

(

image

,

cv2.

COLOR_BGR2GRAY

)

Save the grayscale image

cv2.

imwrite

(

'gray_output.jpg'

,

gray_image

)

Display the grayscale image

cv2.

imshow

(

'Gray Image'

,

gray_image

)

cv2.

waitKey

(

0

)

cv2.

destroyAllWindows

(

)

Code analysis:


2. Vintage Filter

The vintage filter adjusts the color channels of an image to give it a retro look.

Typically, a 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 produce an image with a vintage effect.

Example

import

cv2

import

numpy

as

np

Read an image

image

=

cv2.

imread

(

'input.jpg'

)

Split the BGR channels

b

,

g

,

r

=

cv2.

split

(

image

)

Adjust channel intensity

r

=

np.

clip

(

r *

0.393

0.769

0.189

,

0

,

255

)

.

astype

(

np.

uint8

)

g

=

np.

clip

(

r *

0.349

0.686

0.168

,

0

,

255

)

.

astype

(

np.

uint8

)

b

=

np.

clip

(

r *

0.272

0.534

0.131

,

0

,

255

)

.

astype

(

np.

uint8

)

Merge channels

vintage_image

=

cv2.

merge

(

(

b

,

g

,

r

)

)

Save the vintage image

cv2.

imwrite

(

'vintage_output.jpg'

,

vintage_image

)

Display the vintage image

cv2.

imshow

(

'Vintage Image'

,

vintage_image

)

cv2.

waitKey

(

0

)

cv2.

destroyAllWindows

(

)

Code analysis:


3. Emboss Filter

The emboss filter computes the difference between adjacent pixels to create an effect similar to embossing; this filter is commonly used to enhance edges and texture.

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 an image

image

=

cv2.

imread

(

'input.jpg'

)

Convert to grayscale image

gray_image

=

cv2.

cvtColor

(

image

,

cv2.

COLOR_BGR2GRAY

)

Define the convolution kernel

kernel

=

np.

array

(

[

[

2

,

1

,

0

]

,

                   

[

1

,

 

1

,

1

]

,

                   

[

0

,

 

1

,

2

]

]

)

Apply the convolution kernel

emboss_image

=

cv2.

filter2D

(

gray_image

,

1

,

kernel

)

Save the embossed image

cv2.

imwrite

(

'emboss_output.jpg'

,

emboss_image

)

Display the embossed image

cv2.

imshow

(

'Emboss Image'

,

emboss_image

)

cv2.

waitKey

(

0

)

cv2.

destroyAllWindows

(

)

** Code Analysis:**


4. Blur Filter

The blur filter smooths an image to reduce noise and detail.

Example

import

cv2

Read an 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 an image to make it clearer.

Example

import

cv2

import

numpy

as

np

Read an 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 an image to highlight the outlines of objects.

Example

import

cv2

Read an image

image

=

cv2.

imread

(

"path/to/image.jpg"

)

Convert to grayscale

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

(

)