This is an automatically translated post by LLM. The original post is in Chinese. If you find any translation errors, please leave a comment to help me improve the translation. Thanks!

Low-pass filters are very useful in our daily lives. Image blurring, image denoising, and image recognition all require the use of low-pass filters. Low-pass filtering means removing the high-frequency components (rapidly changing parts) from an image and leaving the low-frequency components (parts with less noticeable changes). There are two implementations of filters: spatial domain and frequency domain. Spatial domain filters operate directly on the spatial image, convolving the image matrix with the filter kernel to obtain the filtered output. Frequency domain filters transform the image to the frequency domain using the Fourier transform and then multiply it with the filter to obtain the output.

This article mainly introduces spatial domain low-pass filters and their implementations.

Commonly used spatial domain filters include average filtering, median filtering, and Gaussian filtering. Here, we mainly introduce median filtering and Gaussian filtering.

Median Filtering

As the name suggests, median filtering is a filter based on statistical methods. The specific implementation method is as follows: for an n*n median filter, the pixel value of the output image at (x, y) is equal to the median of all pixel values in the n*n image area centered at (x, y) in the input image.

The size of the filter can be selected according to different needs.

The effect of the median filter is as follows:

The original image, 3*3 median filter, 5*5 median filter, and 7*7 median filter, respectively.

Original Image
3*3 Median Filter
5*5 Median Filter
7*7 Median Filter

The original image contains a lot of irregularly distributed noise, some of which are salt noise, but most of them are sudden impulse noise. When using a 3*3 median filter, the salt noise has been removed well, but the impulse noise is still obvious. When using a 5*5 median filter, the situation of impulse noise has been greatly improved. The 7*7 filter almost completely removes the noise, but the image is also severely blurred.

Gaussian Filtering

Gaussian filtering is a commonly used blurring method in some image processing software. It is generated by the two-dimensional normal distribution function: \(p(x,y)=\frac1{2\pi\sigma^2}e^{-\frac{x^2+y^2}{2\sigma^2}}\). The specific steps are as follows:

  • To generate an n*n Gaussian filter, set the center of the n-order simulation to (0,0) and generate the coordinates of other positions in the matrix.
  • Substitute the coordinates of each position in the n*n matrix into the two-dimensional normal distribution function to obtain the value of each position.
  • Scale the values in the matrix based on the value of 1 in the upper left corner of the matrix.
  • Round the matrix to obtain the n*n Gaussian filter.

The function for generating a Gaussian filter is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
import numpy as np
sigma=1.5 # Parameter of the Gaussian filter
def f(x,y): # Define the two-dimensional normal distribution function
return 1/(math.pi*sigma**2)*math.exp(-(x**2+y**2)/(2*sigma**2))

def gauss(n): # Generate an n*n Gaussian filter
mid=n//2
filt=np.zeros((n,n))
for i in range(n):
for j in range(n):
filt[i][j]=f(i-mid,j-mid)/f(-mid,-mid)
return filt.astype(np.uint8)

The Gaussian filters of size 3*3, 5*5, and 7*7 are as follows:

Their images in the two-dimensional coordinate system are as follows:

After obtaining the operator of the Gaussian filter, convolve it with the input image to obtain the result of the Gaussian filter.

The effect is as follows:

The original image, 3*3, 5*5, and 7*7 Gaussian filtering results:

Original Image
3*3 Gaussian
5*5 Gaussian
7*7 Gaussian

Gaussian blur is more effective in removing salt noise in the image, but it is more difficult to remove impulse noise. The 7*7 Gaussian filter still cannot remove the impulse noise in the image. On the other hand, compared with Gaussian blur, Gaussian blur retains more information of the image and preserves more details.

Appendix

References

[1] Digital Image Processing, Third Edition / (Rafael C. Gonzalez), translated by Ruan Qiuqi, et al. - Beijing: Electronic Industry Press, 2017.5

[2] Brook_icv. Basic Image Processing (4): Detailed Explanation of Gaussian Filters [G/OL]. Blog Garden: 2017-02-16 [2020-03-23]. https://www.cnblogs.com/wangguchangqing/p/6407717.html#autoid-4-1-0

[3] Yu Ni Xin An. Methods for Calculating Mean, Median, and Mode in Numpy [G/OL]. Blog Garden: 2018-11-04 [2020-03-23]. https://www.cnblogs.com/lijinze-tsinghua/p/9905882.html

Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import cv2 as cv
import numpy as np
import math
img=cv.imread("test1.pgm")

sigma=1.5 # Parameter of the Gaussian filter
def f(x,y): # Define the two-dimensional normal distribution function
return 1/(math.pi*sigma**2)*math.exp(-(x**2+y**2)/(2*sigma**2))

def gauss(n): # Generate an n*n Gaussian filter
mid=n//2
filt=np.zeros((n,n))
for i in range(n):
for j in range(n):
filt[i][j]=f(i-mid,j-mid)/f(-mid,-mid)
return filt.astype(np.uint8)

def gauss_filter(img,n): # Perform Gaussian filtering on the image img with an n*n convolution block
filt=gauss(n)
con=1/np.sum(filt)
shape=img.shape
mid=n//2
temp=np.zeros((shape[0]+n-1,shape[1]+n-1)) # Pad the edges with zeros
for i in range(shape[0]):
for j in range(shape[1]):
temp[i+mid][j+mid]=img[i][j][0]
result=np.zeros((shape[0],shape[1]))
for i in range(shape[0]):
for j in range(shape[1]):
tmp=0
for k in range(n):
for l in range(n):
tmp+=filt[k][l]*temp[i+k][j+l]
result[i][j]=con*tmp
return result.astype(np.uint8)

def center_filter(img, n): # Perform low-pass filtering on the image using an n*n median filter
mid=n//2
shape=img.shape
temp=np.zeros((shape[0]+n-1,shape[1]+n-1))
for i in range(shape[0]):
for j in range(shape[1]):
temp[i+mid][j+mid]=img[i][j][0]
result=np.zeros((shape[0],shape[1]))
tmp=np.zeros(n*n)
for i in range (shape[0]):
for j in range(shape[1]):
for k in range(n):
for l in range(n):
tmp[k*n+l]=temp[i+k][j+l]
result[i][j]=np.median(tmp)
return result.astype(np.uint8)

filename=["test1.pgm","test2.tif"]
size=[3,5,7]
for i in filename:
img=cv.imread(i)
for j in size:
cv.imwrite(i+'gauss-'+str(j)+'.bmp',gauss_filter(img,j))
cv.imwrite(i+'center-'+str(j)+'.bmp',center_filter(img,j))

总体设计思路

视频的播放实际上就是一系列的图片按照一定的顺序,以一定的时间间隔连续播放所产生的视觉效果。因此,使用单片机驱动LCD去播放视频实际上就是让单片机以一定的时间间隔向LCD的缓存推送图片,让其不断刷新屏幕去切换图片即可。在文章的最后我放入了这个项目的源工程文件供大家参考。

阅读全文 »

This is an automatically translated post by LLM. The original post is in Chinese. If you find any translation errors, please leave a comment to help me improve the translation. Thanks!

Overall Design Concept

The playback of a video essentially involves a sequence of images displayed in a specific order at regular time intervals, creating a visual effect. Therefore, driving an LCD to play a video with a microcontroller involves pushing images to the LCD's buffer at certain intervals, continuously refreshing the screen to switch images. At the end of the article, I've included the source project files for reference.

阅读全文 »

在数字图像处理中,直方图均衡是调整图像亮度,对比度,图像增晰等操作中常用的做法。对于图像中各个不同的颜色进行直方图统计,采取统计数据对于颜色进行重新映射,从而达到调整对比度,图像增晰的目的。

阅读全文 »

This is an automatically translated post by LLM. The original post is in Chinese. If you find any translation errors, please leave a comment to help me improve the translation. Thanks!

In digital image processing, histogram equalization is a common practice used to adjust brightness, contrast, and image sharpening. It involves statistical analysis of the histogram of different colors in an image, followed by remapping the colors based on this analysis to achieve the desired adjustments in contrast and sharpness.

1. Histogram of Digital Images

A histogram refers to the statistical analysis of all the pixel colors in an entire image, presenting the statistical data of different colors in the form of a histogram.

For example, consider the following two images:


Their corresponding histograms are:

From these figures, it can be observed that an image with high contrast has a wider distribution of colors, while an image with low contrast has colors concentrated in a specific region. In the second image, the pixel values are concentrated on a single color, resulting in image blurring.

Using image histograms, the color distribution of an image can be clearly understood, providing guidance on how to enhance images and improve contrast.

2. Histogram Equalization

Histogram equalization, as the name suggests, aims to equalize the color distribution in an image histogram. Specifically, it redistributes colors that were previously too concentrated in the histogram, spreading them out to occupy the appropriate pixel values. The specific method is as follows (using 3-bit 8-color as an example):

Consider a 64x64 pixel image (M=64, N=64, MN=4096) with 3 bits (L=8) representing colors, ranging from [0,L-1], i.e., [0,7]. By performing statistical analysis on the pixel values of the entire image, we obtain the following statistical data:

\(r_k\) \(n_k\) \(p_r(r_k)=n_k/MN\)

This is an automatically translated post by LLM. The original post is in Chinese. If you find any translation errors, please leave a comment to help me improve the translation. Thanks!

Code some frequently used LeTaX mathematical formula syntax

阅读全文 »

图像配准即对于内容大致相同,但在角度,大小或是其他几何位置上有所偏差的两张或几张图像进行坐标变换。使得这些图像矫正到同一规整的大小,角度,坐标上。

图像配准对于图像拼接,制作全景图像,进行环境识别等方面有很大的用处。其原理简单,使用矩阵运算转换速度也很快,在选取映射点合适的情况下,配准效果很好。具体的转化方法如下:

阅读全文 »

This is an automatically translated post by LLM. The original post is in Chinese. If you find any translation errors, please leave a comment to help me improve the translation. Thanks!

Image registration refers to the process of transforming the coordinates of two or more images that have similar content but differ in aspects like angle, size, or geometric position. This transformation aims to align these images to the same standardized size, angle, and coordinates.

Image registration finds extensive applications in image stitching, creating panoramic images, and environmental recognition. Its principle is simple, utilizing matrix operations for fast transformation. With appropriately selected mapping points, registration yields excellent results. The specific transformation method is outlined as follows:

Registration Algorithm

Taking image A as the reference for registering image B.

Selecting n pixels in image A: \[ p_1(x_1,y_1,1),p_2(x_2,y_2,1),...,p_n(x_n,y_n,1) \] Forms a coordinate matrix: \[ P=\begin{bmatrix}x_1 &x_2 &...&x_n\\y_1&y_2&...&y_n\\1&1&...&1\end{bmatrix} \] Selecting n pixels corresponding to these n points in image B: \[ q_1(x_1,y_1),q_2(x_2,y_2),...,q_n(x_n,y_n) \] Forms another coordinate matrix: \[ Q=\begin{bmatrix}x_1 &x_2 &...&x_n\\y_1&y_2&...&y_n\\1&1&...&1\end{bmatrix} \]

If image B can be translated, rotated, and scaled to match image A, then there exists a transformation matrix H such that: \[ Q=HP \] Thus, if the transformation matrix H can be computed, we can use the following formula: \[ H^{-1}Q=P \] Mapping each pixel in image B to the corresponding position in image A, thereby obtaining image B registered with image A as the template.

How to compute the transformation matrix H?

Given the point sets P and Q already selected in images A and B, it's evident that \[ H=QP^{-1} \]

As P is not a square matrix, it cannot be inverted directly. Here, we find the pseudo-inverse.

At this point, we've outlined the basic process of image registration:

  • Select corresponding point sets $ P $ and $ Q $ in the template image and the image to be registered.
  • Compute the transformation matrix $ H $ using the formula $ H=QP^{-1} $.
  • Map the image to be registered onto the template image using the transformation matrix.

Registration Example

For instance: using image A as the template to register image B:

Image A Image B

Selecting seven points within them, distributed as follows:

Note: The coordinate system displayed by the drawing tool is opposite to the (x, y) coordinates. Pay attention when recording coordinates.

Selected Points (Image A) \[ P=\begin{bmatrix}1448&1694&1756&383&2290&2035&2150\\1308&1198&2744&2516&933&2693&1968\\1&1&1&1&1&1&1\end{bmatrix} \]

\[ Q=\begin{bmatrix}1042&1252&1708&323&1761&1966&1890\\1077&907&2387&2519&498&2265&1535\\1&1&1&1&1&1&1\end{bmatrix} \]

The computed transformation matrix: \[ H=QP^{-1}=\begin{bmatrix}0.9668&0.2565&693.0275\\-0.2570&0.9671&184.1373\\0.0000&0.0000&1.0000\end{bmatrix} \] Transforming image B using the transformation matrix yields the following result: Registered Image B

Source Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import cv2 as cv
import numpy as np
import math

imgA=cv.imread("Image A.jpg?x-oss-process=style/webp")
imgB=cv.imread("Image B.jpg?x-oss-process=style/webp")
point_A=np.array([[1448,1694,1756,383,2290,2035,2150],[1308,1198,2744,2516,933,2693,1968],[1,1,1,1,1,1,1]])
point_B=np.array([[1042,1252,1708,323,1761,1966,1890],[1077,907,2387,2519,498,2265,1535],[1,1,1,1,1,1,1]])
H=np.dot(point_B,np.linalg.pinv(point_A))
print(H)
shape_A=imgA.shape
shape_B=imgB.shape
result,mistake=np.zeros(shape_A),np.zeros(shape_A)
result=result.astype(np.uint8)
mistake=mistake.astype(np.uint8)
for i in range(shape_A[0]):
for j in range(shape_A[1]):
address=np.dot(H,np.array([i,j,1]))
address=address.astype(np.int)
if address[0]>0 and address[0]<shape_B[0] and address[1]>0 and address[1]<shape_B[1]:
result[i][j]=imgB[address[0]][address[1]]
mistake[i][j]=imgA[i][j]-result[i][j]
cv.namedWindow("test",cv.WINDOW_NORMAL)
cv.imshow("test",mistake)
cv.waitKey(0)
cv.imwrite("result.jpg?x-oss-process=style/webp",result)
cv.imwrite("mistake.jpg?x-oss-process=style/webp",mistake)

Conclusion

The image registration algorithm is relatively straightforward. With well-selected coordinate points, the computed registration results are commendable. However, manual point selection was employed in this instance. For large-scale or real-time image registration, automated point selection becomes essential. Thus, devising a method for the program to select appropriate and corresponding point sets poses a challenge in image registration.

0%