Basic image filter of computer vision

1. Image noise


Image noise is a signal that interferes with random signals in the process of image acquisition or transmission, which hinders people's understanding, analysis and processing of images.


The generation of image noise comes from the environmental conditions in image acquisition and the quality of sensing components. The main factor of image noise in the process of image transmission is that the transmission channel is polluted by noise.

Common noise

Gaussian noise

Gaussian noise is a kind of noise whose probability density function obeys Gaussian distribution.
In particular, if the amplitude distribution of a noise obeys the Gaussian distribution, and there is no correlation between any two sampling samples, it is called Gaussian white noise.
Note: we must distinguish between Gaussian noise and white noise. Gaussian noise means that the probability density function of noise obeys Gaussian distribution,
White noise refers to the uncorrelation between any two sampling samples of noise, and their description angles are different. White noise does not have to obey Gaussian distribution, and the noise of Gaussian distribution is not necessarily white noise.
1) The image sensor is not bright enough and the brightness is not uniform;
2) Noise and mutual influence of circuit components;
3) The image sensor works for a long time and the temperature is too high;

Let's look at the Gaussian function intuitively

Processing steps

Note: here, the processing of Gaussian noise can be for one pixel or the pixels of a whole image.
Code part:

#Random numbers conforming to normal (Gaussian) distribution are randomly generated. Means and sigma are two parameters
import numpy as np
import cv2
from numpy import shape
import random
def GaussianNoise(src,means,sigma,percetage):
    for i in range(NoiseNum):
        #Take one random point at a time
        #If the pixels of a picture are represented by rows and columns, randX represents the randomly generated rows and randY represents the randomly generated columns
        #random.randint generates random integers
        #Gaussian noise image edge is not processed, so - 1
        #Here, a random number is added to the original pixel gray value
        #If the gray value is less than 0, it is forced to be 0; if the gray value is greater than 255, it is forced to be 255
        if  NoiseImg[randX, randY]< 0:
            NoiseImg[randX, randY]=0
        elif NoiseImg[randX, randY]>255:
            NoiseImg[randX, randY]=255
    return NoiseImg
img = cv2.imread('lenna.png',0)
img1 = GaussianNoise(img,2,4,0.8)
img = cv2.imread('lenna.png')
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Salt and pepper noise

Salt and pepper noise, also known as impulse noise, is a random white or black spot.
• salt and pepper noise = pepper noise + salt noise. The value of salt and pepper noise is 0 (pepper) or 255 (salt).
• the former is low gray noise and the latter is high gray noise. Generally, two kinds of noise appear at the same time, which is black-and-white clutter on the image.
• for color images, it is also possible to display 255 or 0 randomly in three channels of a single pixel BGR. (Note: the color of this point is also restricted by the values of the other two channels, so it may not be either black or white)
• if there is an error in communication and the values of some pixels are lost during transmission, this noise will occur.
• salt and pepper noise may be caused by sudden strong interference to the image signal. For example, a failed sensor results in a pixel value
Is the minimum value, and the saturated sensor causes the pixel value to be the maximum value.

Processing steps

Code part:

import numpy as np
import cv2
from numpy import shape
import random

def fun1(src, percetage):
    NoiseImg = src
    NoiseNum = int(percetage * src.shape[0] * src.shape[1])
    for i in range(NoiseNum):
        # Take one random point at a time
        # If the pixels of a picture are represented by rows and columns, randX represents the randomly generated rows and randY represents the randomly generated columns
        # random.randint generates random integers
        # Salt and pepper noise image edge is not processed, so - 1
        randX = random.randint(0, src.shape[0] - 1)
        randY = random.randint(0, src.shape[1] - 1)
        # random.random generates random floating-point numbers, and randomly takes a pixel, half of which may be white dot 255 and half of which may be black dot 0
        if random.random() <= 0.5:
            NoiseImg[randX, randY] = 0
            NoiseImg[randX, randY] = 255
    return NoiseImg

img = cv2.imread('lenna.png', 0)
img1 = fun1(img, 0.15)
# Write in the folder named Lenna_ Noisy image of pepperandsalt.png
# cv2.imwrite('lenna_PepperandSalt.png',img1)

img = cv2.imread('lenna.png')
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('source', img2)
cv2.imshow('lenna_PepperandSalt', img1)

Other noise

Common noise

import cv2 as cv
import numpy as np
from PIL import Image
from skimage import util
def random_noise(image, mode='gaussian', seed=None, clip=True, **kwargs):
Function: add various random noise to floating-point pictures
image: Input picture (will be converted to floating point), ndarray type
mode:  Optional, str Type, indicating the type of noise to be added
    gaussian: Gaussian noise 
    localvar: The additive noise of Gaussian distribution has a specified local variance at each point of the "image".
    poisson: Poisson noise
    salt: Salt noise, randomly changing the pixel value to 1
    pepper: Noise, randomly changing the pixel value to 0 or-1,Depends on whether the value of the matrix is signed
    s&p: Salt and pepper noise
    speckle: Uniform noise (mean value) mean variance variance),out=image+n*image
seed:  Optional, int If selected, random seeds will be set before generating noise to avoid pseudo-random
clip:  Optional, bool Type, if yes True,After adding mean, Poisson and Gaussian noise, the data of the picture will be trimmed to the appropriate range. If who False,The value of the output matrix may exceed[-1,1]
mean:  Optional, float Type, Gaussian noise and mean noise mean Parameter, default=0
var:   Optional, float Type, variance in Gaussian noise and mean noise, default=0.01(Note: not standard deviation)
local_vars: Optional, ndarry Type, used to define the local variance of each pixel, in localvar Used in
amount:  Optional, float Type, is the proportion of salt and pepper noise, the default value=0.05
salt_vs_pepper: Optional, float Type, the proportion of salt and pepper in salt and pepper noise. The greater the value, the more salt noise. The default value=0.5,Equal amount of salt and pepper
Return value: ndarry Type, and the value is[0,1]perhaps[-1,1]Between, depending on whether it is a signed number
img = cv.imread("lenna.png")
cv.imshow("source", img)

2. Image filtering


Filtering and convolution are roughly the same, but there is a difference of rotating 180 degrees.


1. Eliminate the mixed noise in the image.
2. Extract image features for image recognition.


1. Do not damage the image contour and edge.
2. The visual effect of the image should be better.

Common filtering

Mean filtering

General process:

Advantages: simple algorithm and fast calculation speed;
Disadvantages: reduce noise while blurring the image, especially the edges and details of the scene.

median filtering

Advantages: the inhibition effect is very good, and the clarity of the picture is basically maintained;
Disadvantages: the suppression effect of Gaussian noise is not very good.

Maximum and minimum filtering

3. Image enhancement


Purposefully emphasize the overall or local characteristics of the image, make the original unclear image clear or emphasize some interesting features, expand the differences between different object features in the image, suppress the uninterested features, improve the image quality, enrich the amount of information, strengthen the image interpretation and recognition effect, and meet the needs of some special analysis. (in short, it means performing various operations on the image)


• point processing technology. Only a single pixel is processed.
• domain processing technology. The pixels and their surrounding points are processed, that is, the convolution kernel is used.

Point processing

linear transformation

Example explanation: when the pixel value of the image is (10, 20)
When a > 1: let a=2,b=0,(10,20) become (20,40) {obviously, the interpolation becomes larger, thus enhancing the contrast}
When a < 1: let a=0.5,b=0,(10,20) become (5,10) {obviously, the interpolation becomes smaller, thus reducing the contrast}
When a = 1, b > 0: let a=1,b=5, (10,20) become (15,25) {obviously, the overall value becomes larger}, similarly, B < 0

Piecewise linear transformation

Explanation: it is similar to the linear transformation above, but it is divided into three regions.

Logarithmic transformation

Power law transform / gamma transform

Explanation: through the picture on the right, we can see the height of y=c*x.

neighborhood processing

Histogram equalization
image filtering


Tags: AI Computer Vision image processing

Posted on Mon, 06 Dec 2021 17:25:42 -0500 by Trizen