I’ll put the 0 out front

Image special effect processing is based on the image pixel data characteristics, the original image for certain steps of calculation, such as pixel difference, gray transformation, color channel fusion, so as to achieve the desired effect. Image effects processing is very widely used in the daily life of a computer vision application, appear in all kinds of beautiful software, the mathematical principles behind such fine filters are interlinked, this paper mainly introduces the eight basic algorithms of image effects, on the basis of these algorithms can be secondary development, generate more advanced filters.

This paper adopts object-oriented design, defines an image processing class ImgProcess, make the application of image special effects algorithm more concise, such as

import cv2
import numpy as np

process = ImgProcess('1.jpg')
glassImg = process.glass()
cv2.imshow("glass", glassImg)
cv2.waitKey(delay = 0)
Copy the code

You can create a frosted glass image. The constructor of this class is

class ImgProcess:
    def __init__(self, img) - >None:
        self.src = cv2.imread(img)
        self.gray = cv2.cvtColor(self.src, cv2.COLOR_BGR2GRAY)
        self.h, self.w = self.src.shape[:2]
Copy the code

Reading is the basic information of the image. This article still uses Bingbing as a model ~

So below, the formal introduction of various algorithms ~

1 Frosted glass effect

Frosted glass special effect is to replace the current pixel with the color of a random pixel in the image neighborhood, so as to achieve the fuzzy fuzzy effect of frosted glass.

# Frosted glass effect
def glass(self) :
    glassImg = np.zeros((self.h, self.w, 3), np.uint8)
    for i in range(self.h - 6) :for j in range(self.w - 6):
            index = int(np.random.random() * 6)
            glassImg[i, j] = self.src[i + index, j + index]
    return glassImg
Copy the code

2 relief Effect

Relief effect, is to make the image to appear “protruding on the surface of the stone”, according to the degree of concave and convex to form a three-dimensional three-dimensional effect. The mathematical principle is to first depict the outline of the image, and then reduce the pixel value around the edge, so as to produce a three-dimensional relief effect.

# Relief effect
def relief(self) :
    reliefImg = np.zeros((self.h, self.w, 1), np.uint8)
    for i in range(self.h):
        for j in range(self.w - 1):
            edge = int(self.gray[i, j]) - int(self.gray[i, j + 1])    # get edge
            val = edge + 120                                # Create a three-dimensional feeling
            if val > 255:
                val = 255
            if val < 0:
                val = 0
            reliefImg[i, j] = val
    return reliefImg
Copy the code

3 Oil painting Special Effects

Oil painting special effects, is to make the image looks like paint painted, producing a classical, pleated effect. Almost all retouching software support oil painting special effects, its mathematical principle is

  • Define a convolution kernel
  • The convolution check graph is scanned and the gray level of the pixels in the scanning frame is quantified
  • Count the number of pixels at different levels
  • Find the pixels with the most gray level in the scanning frame, and average the gray value of these pixels
  • Replace the original pixel value with the mean value
  • Repeat the above operation until the whole image is scanned by the convolution kernel
def oil(self) :
    oilImg = np.zeros((self.h, self.w, 3), np.uint8)
    for i in range(2, self.h - 2) :for j in range(2, self.w - 2) :# quantization vector
            quant = np.zeros(8, np.uint8)
            # 4x4 convolution kernel
            for k in range(-2.2) :for t in range(-2.2):
                    level = int(self.gray[i + k, j + t] / 32)
                    # quantization counting
                    quant[level] = quant[level] + 1

            # Evaluate the maximum quantization value and its index
            valMax = max(quant)
            valIndex = list(quant).index(valMax)

            # Pixel average
            for k in range(-2.2) :for t in range(-2.2) :if self.gray[i + k, j + t] >= (valIndex * 32) \
                        and self.gray[i + k, j + t] <= ((valIndex + 1) * 32):
                        (b, g, r) = self.src[i + k, j + t]
            oilImg[i, j] = (b, g, r)
    return oilImg
Copy the code

Mosaic effects

Mosaic effect is a widely used image or video processing method at present. It degrades the color level details of a specific area in the image or video and causes the effect of color block disruption. The main purpose is usually to make a specific area unrecognizable. The math is simple: make the pixels in a set the same.

# Mosaic effects
def mask(self) :
    maskImg = np.zeros((self.h, self.w, 3), np.uint8)
    for i in range(self.h - 5) :for j in range(self.w - 5) :if i%5= =0 and j%5= =0 :
                for k in range(5) :for t in range(5):
                        (b, g, r) = self.src[i, j]
                        maskImg[i + k, j + t] = (b, g, r)
    return maskImg
Copy the code

5 Sketch Effects

Sketch special effects, is the use of a single color to show the changes in lightness of the painting. The mathematical principle is to produce the spatial modeling of sketch by gaussian blur and gray inversion.

# Sketch effects
def sketch(self) :
    temp = 255 - self.gray 
    gauss = cv2.GaussianBlur(temp, (21.21), 0)
    inverGauss = 255 - gauss      
    return cv2.divide(self.gray, inverGauss, scale = 127.0)
Copy the code

6 Nostalgic special effects

Nostalgia effect, is based on the psychological formula of the original image of the three color channel transformation and low-pass filtering, to produce nostalgia light and shadow effect. Psychological formula (the human eye is more sensitive to green) :

  • B= 0.272 * r + 0.534 * g + 0.131 * B
  • G = 0.349 * r + 0.686 * g + 0.168 * b
  • R = 0.393 * r + 0.769 * g + 0.189 * b
# Nostalgic effects
def old(self) :
    oldImg = np.zeros((self.h, self.w, 3), np.uint8)
    for i in range(self.h):
        for j in range(self.w):
            b = 0.272 * self.src[i, j][2] + 0.534 * self.src[i, j][1] + 0.131 * self.src[i, j][0]
            g = 0.349 * self.src[i, j][2] + 0.686 * self.src[i, j][1] + 0.168 * self.src[i, j][0]
            r = 0.393 * self.src[i, j][2] + 0.769 * self.src[i, j][1] + 0.189 * self.src[i, j][0]
            if b > 255:
                b = 255
            if g > 255:
                g = 255
            if r > 255:
                r = 255
            oldImg[i, j] = np.uint8((b, g, r))
    return oldImg
Copy the code

7 fleeting special effects

Fleeting special effects, is commonly used meitu software feature processing means. Its mathematical principle is based on the original image blue channel transformation, transformation using empirical formula 14B14 \ SQRT B14b

# Fleeting special effects
def fleet(self) :
    fleetImg = np.zeros((self.h, self.w, 3), np.uint8)
    for i in range(self.h):
        for j in range(0, self.w):
            b = math.sqrt(self.src[i, j][0]) * 14
            g = self.src[i, j][1]
            r = self.src[i, j][2]
            if b > 255:
                b = 255
            fleetImg[i, j] = np.uint8((b, g, r))
    return fleetImg
Copy the code

8 Cartoon Special Effects

Cartoon effects, as the name suggests, are cartoon effects.

# Cartoon special effects
def cartoon(self) :
    num = 7   # Number of bilateral filters
    for i in range(num):
        cv2.bilateralFilter(self.src, d = 9, sigmaColor = 5, sigmaSpace = 3)
    median = cv2.medianBlur(self.gray, 7)
    edge = cv2.adaptiveThreshold(median, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, blockSize = 5, C = 2)
    edge = cv2.cvtColor(edge, cv2.COLOR_GRAY2RGB)
    return cv2.bitwise_and(self.src, edge)
Copy the code



Limited by space, this article complete engineering code private letter I get ~


Computer vision basics tutorial outline

The chapter content

Color space and digital imaging

Fundamentals of computer geometry

2. Image enhancement, filtering, pyramid

3. Image feature extraction

4. Image feature description

5. Image feature matching

6 Stereovision

7 Actual Project

Welcome to my AI channel “AI Technology Club”.