SoFunction
Updated on 2024-11-19

Python+OpenCV to realize six common image effects

image fusion

Blend the two images together in a certain ratio

The addWeighted() method:

  • Parameter 1 First image matrix
  • Parameter 2 Weights of the first image matrix
  • Parameter 3 second image matrix
  • Parameter 4 Weights of the second image matrix
  • Offset after fusion

The width and height of the two images to be overlaid should be the same.

If you fill in the pixel offset value after superimposition, don't fill it too big, more than 255 will cause the image to be white.

import cv2
import cv2 as cv

img = ("img/")
tony = ("img/", )

# Modify the width and height of the lena image Keep the width and height of the two images the same before blending them.
height, width = [0:2]
new_height = int(height * 1.5)
new_width = int(width * 2)
new_img = (img, (new_width, new_height))

# Interpolation when performing superimposition
dst = (new_img, 0.5, tony, 0.5, 0)
("dst", dst)

(0)
()

grayscale processing

A color image is usually made by overlaying three channels of BGR

In order to facilitate the identification of image features, we will usually be a color image into gray-scale images to be analyzed, when we turn into a gray image, the picture edges, contours of the features are still able to clearly see, and in this case we only need to analyze a single channel, which will simplify a lot of operations.

1. The previous statement can be read in grayscale when reading the picture

import cv2
img = ("img/", cv.IMREAD_GRAYSCALE)

conversion to grayscale

import cv2

img = ("img/", cv.IMREAD_COLOR)
# Convert all colors of the original image to gray
dstImg = (img, cv.COLOR_BGR2GRAY)
("dstImg", dstImg)
(0)

Color reversal

invert grayscale

Each pixel in the grayscale map is composed of 0 to 255, if a pixel is 100, after inversion is 255 - 100 = 155

import cv2 as cv

img = ("img/", cv.IMREAD_COLOR)
# Convert all colors of the original image to gray
dstImg = (img, cv.COLOR_BGR2GRAY)
# Get height and width
height, width = [0:2]
# Iterate over every pixel point
for row in range(height):
    for col in range(width):
        # 255 - every pixel point = reversed color
        dstImg[row, col] = 255 - dstImg[row, col]

("dstImg", dstImg)
(0)

Color reversal

By the same token, color images have three color channels of BGR, each of which is taken inversely

255 - B = B1 255 - G = G1 255 - R = R1

import cv2 as cv

img = ("img/", cv.IMREAD_COLOR)
# Convert all colors of the original image to gray
dstImg = (img, cv.COLOR_BGR2GRAY)
# Get height and width
height, width = [0:2]
# Iterate over every pixel point
for row in range(height):
    for col in range(width):
        # 255 - every pixel point = reversed color
        dstImg[row, col] = 255 - dstImg[row, col]

("dstImg", dstImg)
(0)

Mosaic effect

Mosaic refers to the current widespread use of an image (video) processing tools, this means that the image of a particular area of the color gradient detail degradation and color blocking effect, because this blurring appears to have a small grid composed of an image of this picture is called a mosaic. The goal is usually to make it unrecognizable.

import cv2

# Read the image cv2 reads out the image are a two-dimensional matrix
img = ('./img/', cv2.IMREAD_COLOR)
# Slice The coordinates of two points can intercept the image #
# x1:x2,y1:y2 Intercepting the eye section
img1 = img[180:250, 180:310]
# Get the height and width
height, width = [0:2]
# Iterate over every pixel point
for row in range(height):
    for col in range(width):
        # If there are rows that are exactly a multiple of 10 and columns that are a multiple of 10
        if row % 10 == 0 and col % 10 == 0:
            # Get the bgr three primary colors for this pixel point
            b, g, r = img1[row, col]
            # Iterate over the 100 pixels next to this pixel, all equal to the pixel in the center #
            for i in range(10):
                for j in range(10):
                    img1[row + i, col + j] = b, g, r

('img', img)

('msk_lena.jpg', img)
()

glazing effect

The hairy glass effect is similar to the mosaic effect, the mosaic is: for example, all pixels within a 4*4 pixel point are the same color as the first pixel point, the hairy glass effect is to traverse each pixel point and replace it with a randomly selected color value near that pixel point.

The larger the offset the more blurred

import random

import cv2
import numpy as np

img = ('./')
height, width = [0:2]
new_img = np.zeros_like(img, np.uint8)
# Define the offset
offset = 6
# Iterate over every pixel point
for row in range(height):
    for col in range(width):
        # Define random values not exceeding 1 to multiply with offset
        index = int(() * offset)
        # Get the randomized row and column numbers If not over the total height use the random rows If over use the height -1
        random_row = row + index if row + index < height else height - 1
        random_col = col + index if col + index < width else width - 1
        # Assigning colors
        b, g, r = img[random_row, random_col]
        new_img[row, col] = b, g, r

('img', img)
('new_img', new_img)

()

relief effect

Embossing effect formula: new_gray = gray0-gray1+120

The 120 is added to increase the gray value

import cv2
import numpy as np

img = ('./')
# Get height width
height, width = [0:2]
# Converted to grayscale
gray_img = (img, cv2.COLOR_BGR2GRAY)

new_img = np.zeros_like(gray_img, np.uint8)
# Iterate over every pixel point
for row in range(height):
    # Because it's important to get neighboring pixels to prevent the subscript from crossing the border and traversing ahead of time with a width of -1.
    for col in range(width - 1):
        # Get the pixel value of the pixel point
        gray0 = gray_img[row, col]
        # Get pixel values of neighboring pixel points
        gray1 = gray_img[row, col + 1]
        # Formulas for using embossing effects
        new_gray = int(gray0) - int(gray1) + 120
        # Determine if the new gray value is out of bounds
        if new_gray > 255:
            new_gray = 255
        elif new_gray < 0:
            new_gray = 0
        # Assignment
        new_img[row, col] = new_gray

('img', img)
('new_img', new_img)

()

To this article on Python OpenCV to achieve six commonly used image effects on this article, more related Python OpenCV image effects content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!