In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you Python and OpenCV how to achieve Gaussian filtering, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Explanation of basic principles: the algorithm of Gaussian blur
The compilation of Gaussian kernel function: the weight matrix is constructed and processed in the form of Gaussian two-dimensional distribution function. It is important to note that I do not specifically judge the situation when sigma = 0.
That is, to achieve:
1) the construction of weight matrix.
According to the formula:
Calculate the internal structure of the matrix, where the coefficients in front of e will be reduced because of normalization, so it is not reflected in the code.
2) normalization of matrix elements
The sum of the internal elements of the matrix is calculated, and finally the normalized weight matrix is obtained by matrix division.
# Gaussian kernel generating function kernel_size: filtered kernel size sigma: local range of influence of Gaussian kernel function def gauss (kernel_size, sigma): # typed 0 fill kernel = np.zeros ((kernel_size) Kernel_size)) # determine the normal distribution median center = kernel_size / / 2 # s: variance sum: record sum s = sigma * * 2 sum = 0 for i in range (kernel_size): for j in range (kernel_size): # because the final normalization processing The 2 Pi calculation under the root sign here can omit x, y = I-center, j-center kernel [I, j] = np.exp (- (x * * 2 + y * * 2) / (2 * s)) sum + = kernel [I, j] # normalized output kernel = kernel / sum return kernel
The compilation of the filtering function: multiplying each pixel (except the edge) and its surrounding pixels by the weight matrix to achieve Gaussian filtering.
It should be noted that this function can only deal with color images, because only color images have img.shape [2] elements, grayscale images can not be processed in this way without img.shape [2] elements.
# implementation of Gaussian filtering Img: input image kermel: enter Gaussian kernel function def myfilter (img,kernel): # read img rows and number of kernel columns h = img.shape [0] w = img.shape [1] # directly copy parent object img1 = copy.copy (img) # remove edge for i in range (1) for j in range (1) Wmai 1): # three-channel processing for c in range (0Magne2): sum = 0 # weighted for k in range (- 1Power2): for l in range (- 1LECO 2): sum + = img [iLiqkMagi jendlPerry c] * kernel [Kham1Magne1] img1 [I JJM c] = sum return img1
At the same time, when dealing with Gaussian filtering, the function is written for the 3x3 kernel_size. To change the size of the kernel_size, you need to change the range range here.
Here is how to deal with grayscale images:
Def myfilter2 (img,kernel): # read img rows and number of core columns h = img.shape [0] w = img.shape [1] # directly copy parent img1 = copy.copy (img) # remove edge for i in range (1): for j in range (1) WMui 1): sum = 0 for k in range (- 1 for k in range 2): for l in range (- 1 Magazine 2): sum + = img [iAccording to Jaiml] * kernel [kwon 1 mencore 1] img1 [iMagol j] = sum return img1
Finally, the whole running code:
Import copyimport cv2import numpy as np# image reading img_y = cv2.imread ('p2.jpg') # Select the Gaussian generating function 3x3, where sigama = 3kernel = gauss (3Power3) # print this generating function print (kernel) # Gaussian filtering process img1 = myfilter (img_y, kernel) cv2.imshow (' P1roomyuantuojuantuojuantujussianfu) cv2.imshow ('P1roomgaussianial, img1) cv2.waitKey (0)
Input and output image result display:
After the original image and Gaussian filtering
It should be noted that the method taken is very primitive, if the input image is too large, it will take a long time to run.
These are all the contents of the article "how to achieve Gaussian filtering in Python and OpenCV". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.