OpenCV image processing - image convolution

image convolution

insert image description here

  • Image filtering is to suppress the noise of the target image while retaining the details of the image as much as possible. It is an indispensable operation in image preprocessing. The quality of its processing effect will directly affect the effectiveness and reliability of subsequent image processing and analysis. .

  • Linear filtering is the most basic method of image processing, which allows us to process images and produce many different effects. First, we need a 2D filter matrix (convolution kernel) and a 2D image to process. Then, for each pixel of the image, calculate the product of its neighboring pixels and the corresponding elements of the filter matrix, and then add them up as the value of the pixel position. This completes the filtering process.

  • The operation of multiplying and summing the image and the filter matrix element by element is equivalent to moving a two-dimensional function to all positions of another two-dimensional function. This operation is called convolution.

Convolution kernel definition rules:

  1. The size of the filter should be an odd number so that it has a center, eg 3x3, 5x5 or 7x7. There is a center and a radius. For example, the radius of a 5x5 kernel is 2
  2. The sum of all elements of the filter matrix should be equal to 1, which is to ensure that the brightness of the image before and after filtering remains unchanged. Of course, this is not a hard requirement.
  3. If the sum of all elements of the filter matrix is ​​greater than 1, the filtered image will be brighter than the original image, otherwise, if it is less than 1, the resulting image will be darker. If the sum is 0, the image will not become black, but will be very dark.
  4. For filtered structures, negative numbers or values ​​greater than 255 may appear. In this case, we can directly truncate them to between 0 and 255. For negative numbers, the absolute value can also be taken.

mean filtering

将卷积核内的所有灰度值加起来,然后计算出平均值,用这个平均值填充卷积核正中间的值,这样做可以降低图像的噪声,同时也会导致图像变得模糊

Mean filter API convolution kernel:
insert image description here

blur()方法参数:

  1. image matrix
  2. convolution kernel
import cv2

img = cv2.imread('img/itheima.jpg')
new_img = cv2.blur(img, (3, 3))
cv2.imshow('img', img)
cv2.imshow('new_img', new_img)

cv2.waitKey()

insert image description here


Gaussian blur

  • Using the mean filter to reduce noise found that the picture is very blurred, and Gaussian blur will retain the true value of the pixel and reduce noise.
  • Gaussian blur is a weighted average calculation. The closer to the center point, the higher the weight, and the farther away, the lower the weight.

Gaussian function API convolution kernel:

insert image description here

将卷积核内所有的灰度值与卷积核对应的值相乘然后加起来除以卷积核内所有的值的和计算出加权平均值替换卷积核中心对应的值。

GaussianBlur()函数参数:

  1. image matrix
  2. Convolution kernel size
  3. The standard deviation of Gaussian filter parameters, the larger the standard deviation, the higher the strength of denoising and the blurrier the picture
import cv2

# 读取图片
img = cv2.imread('img/lena.jpg')
# 创建新窗口
cv2.namedWindow('gaussian', cv2.WINDOW_AUTOSIZE)

def updateSigma(val):
    """
    回调函数 使用高斯滤波给图像去噪
    :param val: 高斯滤波的参数标准差,标准差越大去噪的强度越高、图片越模糊
    """
    # 高斯滤波函数
    gaussian_blur = cv2.GaussianBlur(img, (5, 5), val)
    # 展示
    cv2.imshow('gaussian', gaussian_blur)


# 创建一个可拖动进度条  0~255的值 添加一个回调函数
cv2.createTrackbar('sigma', 'gaussian', 0, 255, updateSigma)

updateSigma(0)

cv2.waitKey()

insert image description here


median filter

  • Sort the gray values ​​of adjacent pixels, and then take the middle value, which can effectively remove the salt and pepper noise

  • The pixel values ​​in the convolution area are sorted from small to large, and the middle value is taken to replace the value in the center area of ​​the convolution kernel

medianBlur()方法参数:

  1. image matrix
  2. The linear size of the aperture must be greater than 1 and must be an odd number 3, 5, 7... The larger the parameter, the higher the denoising intensity, the blurrier the picture
import cv2

img = cv2.imread('./img/itheima_salt.jpg')
# 使用中值滤波函数
new_img = cv2.medianBlur(img, 3)

cv2.imshow('img', img)
cv2.imshow('new_img', new_img)

cv2.waitKey()

insert image description here


Sobel operator

  • Sobel operator is one of the most important operators in pixel image edge detection
  • It is a discrete first-order difference operator used to calculate the approximate value of the first-order gradient of the image brightness function.
  • Using this operator at any point in the image will generate the gradient vector corresponding to that point

Horizontal gradient operator:
insert image description here

Vertical gradient operator:

insert image description here

synthesis:

insert image description here
Sobel()方法参数:

  1. image matrix
  2. If the image depth is -1, it is the same as the original image
  3. order of derivation in horizontal direction
  4. The order of the vertical derivative
import cv2

img = cv2.imread('img/brain.jpg', cv2.IMREAD_GRAYSCALE)
cv2.imshow('img', img)

# sobel算子水平梯度
x_sobel = cv2.Sobel(img, cv2.CV_32F, 1, 0)
# 图像转为8位int
x_sobel = cv2.convertScaleAbs(x_sobel)
cv2.imshow('x_sobel', x_sobel)

# sobel算子垂直梯度
y_sobel = cv2.Sobel(img, cv2.CV_16S, 0, 1)
y_sobel = cv2.convertScaleAbs(y_sobel)
cv2.imshow('y_sobel', y_sobel)

# 合成水平和垂直
x_y_sobel = cv2.addWeighted(x_sobel, 0.5, y_sobel, 0.5, 0)
cv2.imshow('x_y_sobel', x_y_sobel)
cv2.waitKey()

Sobel operator calculations sometimes have deviations, and OpenCV provides an evolutionary version of Sobel operator!

Scharr() function:

  • The usage is the same as the Sobel() function, the Scharr() function

Laplacian

  • After the Laplace transform, the contrast of the gray-scale mutation in the image is enhanced, and the small details in the image are enhanced, making the details of the image clearer than the original image.

Convolution kernel:
insert image description here

Enhanced convolution kernel:

insert image description here

Laplacian()函数参数:

  1. image matrix
  2. picture depth
import cv2

# 以灰度方式读取图像
img = cv2.imread('img/grbq.jpg', cv2.IMREAD_GRAYSCALE)
cv2.imshow('img', img)
# 使用拉普拉斯算子
dst = cv2.Laplacian(img, cv2.CV_32F)
# 转换为8位int
dst = cv2.convertScaleAbs(dst)
cv2.imshow('dst', dst)

cv2.waitKey()

insert image description here
insert image description here


Canny edge detection algorithm

  • The Canny algorithm was developed by John F.Canny in 1986 and is a very commonly used edge detection algorithm
  • It is a multi-stage algorithm with a total of internal 4stages:
    1. 噪声抑制(Noise reduction via Gaussianblur Gaussian blur): Use a 5x5 Gaussian filter to remove noise in the image
    2. 查找边缘的强度及方向(via Sobel filter)
    3. 应用非最大信号抑制(Non-maximum Suppression): A full scan of the image is done to remove any unwanted pixels that may not constitute an edge
    4. 高低阈值分离出二值图像(Hysteresis Thresholding)
      • The ratio of high and low thresholds is T2:T1 = 3:1 / 2:1
      • T2 is the high threshold and T1 is the low threshold

Canny()函数参数:

  1. image matrix
  2. Threshold 1
  3. Threshold 2
import cv2

img = cv2.imread('img/lena.jpg', cv2.IMREAD_GRAYSCALE)
cv2.imshow('img', img)
# 使用Canny边缘检测算法
dst_img = cv2.Canny(img, 50, 180)

cv2.imshow('dst_img', dst_img)
cv2.waitKey()

insert image description here


sharpening filter

  • The image sharpening operation is to find the edge of the object first and then add the edge to the original image, thus strengthening the edge of the object

Convolution kernel:
insert image description here

import cv2
import numpy as np

img = cv2.imread("./img/hehua.jpg", cv2.IMREAD_COLOR)
cv2.imshow("src", img)
# 创建卷积核
kernel = np.array([
    [-1, -1, -1],
    [-1, 9, -1],
    [-1, -1, -1]])
# 任意线性滤波器  把图像和卷积核进行卷积  参数2为卷积深度  -1和原图像一样
dst = cv2.filter2D(img, -1, kernel)

cv2.imshow("sharpness filter", dst)

cv2.waitKey(0)
cv2.destroyAllWindows()

insert image description here

Guess you like

Origin blog.csdn.net/bjsyc123456/article/details/124783962