模糊图像处理 去除模糊_图像模糊如何工作
模糊圖像處理 去除模糊
定義 (Definition)
Roughly speaking, blurring an image is make the image less sharp. This can be done by smoothing the color transition between the pixels.
粗略地說,模糊圖像會使圖像清晰度降低。 這可以通過平滑像素之間的顏色過渡來完成。
To accomplish this target, we need to apply a convolution operation of a specialized matrix, called kernel, to the image’s matrix.
為了實(shí)現(xiàn)這個(gè)目標(biāo),我們需要對圖像的矩陣應(yīng)用稱為內(nèi)核的專用矩陣的卷積運(yùn)算。
What is convolution?
什么是卷積?
Mathematically speaking, a convolution of two matrix, A with size m x n, and B with size p x q, is a (m + p -1) x (n+q-1) matrix C with entries:
從數(shù)學(xué)上講,兩個(gè)矩陣(卷積為mxn的A和卷積為pxq的B)的卷積是一個(gè)(m + p -1)x(n + q-1)矩陣C,其條目為:
Convolution in matrices矩陣卷積Casually speaking, convolution is just forming a new matrix in which the entries are the sums of product of the entries of one matrix with the corresponding entry of another matrix. All of these products could be calculated along the rows and columns.
隨意地說,卷積只是形成一個(gè)新矩陣,其中項(xiàng)是一個(gè)矩陣的項(xiàng)與另一矩陣的相應(yīng)項(xiàng)的乘積之和。 所有這些乘積都可以沿著行和列進(jìn)行計(jì)算。
What is kernel?
什么是內(nèi)核?
Kernel is a matrix that has purpose to transform an image. It is not exclusive to image blurring. It can also be used to detect edges, sharpening edges, and others kind of image transformation. Kernel that used in blurring image is a low-pass filter kernel. It allows low frequency to enter and stop the higher frequency.
內(nèi)核是旨在轉(zhuǎn)換圖像的矩陣。 它并非僅用于圖像模糊。 它還可以用于檢測邊緣,銳化邊緣和其他類型的圖像轉(zhuǎn)換。 用于模糊圖像的內(nèi)核是低通濾波器內(nèi)核。 它允許低頻進(jìn)入并停止更高的頻率。
應(yīng)用 (Application)
Why do we even need to blur an image in the first place? After all, doesn’t it make the image become less visible?
為什么我們甚至首先需要模糊圖像? 畢竟,這是否會使圖像的可見度降低?
It turns out that blurring an image has several purpose.
事實(shí)證明,使圖像模糊有幾個(gè)目的。
Firstly, it reduce the noises in the image. A random brightness spot or incorrect color spot, depends on the type of noise, could be reduced by blurring the image with suitable type of blur.
首先,它減少了圖像中的噪點(diǎn)。 根據(jù)噪聲的類型,可以通過使用適當(dāng)?shù)哪:愋蛯D像進(jìn)行模糊處理來減少隨機(jī)的亮點(diǎn)或不正確的色點(diǎn)。
Blurring an image also reduces the size of image. With appropriate blurring function, we can deblurring the blurred image into the original image. This can be very helpful in transferring a vast size of images.
模糊圖像也會縮小圖像的尺寸。 使用適當(dāng)?shù)哪:δ?#xff0c;我們可以將模糊圖像去模糊為原始圖像。 這對于傳輸大量圖像非常有幫助。
Blurring also used in media. For example, when the news’ picture is not appropriate or explicit. Another use is to hide the face, name, and all private data of people that happens to be included in the image accidentally.
模糊還用于媒體中。 例如,當(dāng)新聞的圖片不合適或不明確時(shí)。 另一個(gè)用途是隱藏恰好包含在圖像中的人的面部,名字和所有私人數(shù)據(jù)。
Lastly, for entertainment purpose. For instance, in movies and digital artworks. The blurring effect may enhance the feels of lovely citylights scene. Or it may helps movie audience knows this particular scene occurs in past.
最后,出于娛樂目的。 例如,在電影和數(shù)字藝術(shù)品中。 模糊效果可以增強(qiáng)可愛的城市燈光場景的感覺。 或者它可以幫助電影觀眾知道這個(gè)特定場景過去發(fā)生的情況。
模糊類型 (Types of Blurring)
There are a number of blurring filter type that can be used. All has its own characteristics. In this section, I explain two of them: Mean/Box/Average filter and Gaussian filter. In Python, these blur filter is contained in OpenCV package. In all of these section, the module I used is
可以使用多種模糊濾波器類型。 都有自己的特點(diǎn)。 在本節(jié)中,我將解釋其中的兩個(gè):均值/框/平均濾波器和高斯濾波器。 在Python中,這些模糊過濾器包含在OpenCV軟件包中。 在所有這些部分中,我使用的模塊是
# For blur and convolutionimport cv2# For creating matrix
import numpy as np# From showing images
from matplotlib import pyplot as plt
1.均值過濾器(平均過濾器/箱式過濾器) (1. Mean Filter (Average Filter/Box Filter))
This filter takes the average of pixels in kernel and replace the central pixel with this average. This kernel has all of its elements same and sums up to 1. The kernel must be odd-sized. Hence, if the size of the kernel is a x b, the mean filter kernel is
該濾鏡獲取內(nèi)核中像素的平均值,然后用該平均值替換中心像素。 該內(nèi)核的所有元素相同,總和為1。內(nèi)核必須為奇數(shù)大小。 因此,如果內(nèi)核的大小為axb,則平均濾波器內(nèi)核為
The general form of mean filter kernel均值濾波核的一般形式For example, when a = 3 and b = 3, the kernel is
例如,當(dāng)a = 3且b = 3時(shí),內(nèi)核為
The greater value of kernel size, the greater blurring effect because the number of pixels involved is greater and the transition of colors become smoother.
內(nèi)核大小的值越大,模糊效果越大,這是因?yàn)樗婕暗南袼財(cái)?shù)更大并且顏色的過渡變得更平滑。
# Import the imageimg = cv2.imread('103057.jpg')# Show the original image
plt.figure()
plt.imshow(img)
plt.show()# Creating a mean filter kernel
def meankernel(size):
mk = np.ones((size, size), np.float32)/(size ** 2)
return mk# Convolute the kernel with image
for size in range(3, 14, 2):
blurImg = cv2.filter2D(img, -1, meankernel(size))
plt.figure()
plt.imshow(blurImg)
plt.show()
The OpenCV module has given a function for mean filter: cv2.blur(). Here is the sample code for mean filter with different size of kernel:
OpenCV模塊提供了均值過濾器功能:cv2.blur()。 以下是具有不同內(nèi)核大小的均值過濾器的示例代碼:
# Import the imageimg = cv2.imread('103057.jpg')# Show the original image
plt.figure()
plt.imshow(img)
plt.show()# Show the blurred image with different size of kernel
for size in range(3, 14, 2):
blurImg = cv2.blur(img,(size,size))
plt.figure()
plt.imshow(blurImg)
plt.show()
The original image is:
原始圖像是:
Original image.原始圖像。And the blurred images are
而且模糊的圖像是
3 x 3 mean filter kernel — 5 x 5 mean filter kernel3 x 3平均濾波器內(nèi)核— 5 x 5平均濾波器內(nèi)核 7 x 7 mean filter kernel — 9 x 9 mean filter kernel7 x 7平均過濾器內(nèi)核— 9 x 9平均過濾器內(nèi)核 11 x 11 mean filter kernel — 13 x 13 mean filter kernel11 x 11平均濾波器內(nèi)核— 13 x 13平均濾波器內(nèi)核2.高斯濾波器 (2. Gaussian Filter)
This filter gives different weight to each entries in matrix as entries in kernel. The closer pixel to the selected pixel has greater weight while the further pixel has lower weight. In theory, all pixel in matrix contributes to the value of the entry in final matrix. In fact, the complete (or theoretical) formula for this filter is
該過濾器為矩陣中的每個(gè)條目賦予不同的權(quán)重,作為內(nèi)核中的條目。 距所選像素最近的像素具有較大的權(quán)重,而另一個(gè)像素具有較低的權(quán)重。 從理論上講,矩陣中的所有像素都有助于最終矩陣中條目的值。 實(shí)際上,此過濾器的完整(或理論上)公式為
Gaussian function高斯函數(shù)where x and y is the horizontal and vertical distance. σ stands for the standard deviation. The higher value of σ, the greater blurring effect.
其中x和y是水平和垂直距離。 σ表示標(biāo)準(zhǔn)偏差。 σ值越高,模糊效果越大。
In practice, we estimate the Gaussian function by an odd-sized kernel whose entries are the estimation of the Gaussian function at that pixel. Moreover, this kernel cannot be sufficiently large because the further pixel give smaller contribution to the value of kernel. Often, we ignore the σ and just give it to the program to determine the suitable value for the given size kernel. For example, the approximation of 3 x 3 Gaussian kernel is
在實(shí)踐中,我們通過一個(gè)奇數(shù)大小的內(nèi)核來估計(jì)高斯函數(shù),其條目是該像素處的高斯函數(shù)的估計(jì)。 而且,該內(nèi)核不能足夠大,因?yàn)榱硗獾南袼貙?nèi)核的值貢獻(xiàn)較小。 通常,我們會忽略σ,而只是將其提供給程序以確定給定大小內(nèi)核的合適值。 例如,3 x 3高斯核的近似值為
Approximation for 3 x 3 Gaussian kernel. Source: Wikipedia3 x 3高斯核的近似值。 資料來源:維基百科To calculate the Gaussian kernel, we can use the OpenCV function of cv2.GaussianBlur(). Here is the sample code for Gaussian filter with different size of kernel:
要計(jì)算高斯內(nèi)核,我們可以使用cv2.GaussianBlur()的OpenCV函數(shù)。 以下是具有不同內(nèi)核大小的高斯濾波器的示例代碼:
# Import the imageimg = cv2.imread('103057.jpg')# Show the original image
plt.figure()
plt.imshow(img)
plt.show()# Show the blurred image with different size of kernel
for size in range(3, 14, 2):
blurImg = cv2.GaussianBlur(img,(size,size), 0)
plt.figure()
plt.imshow(blurImg)
plt.show()
The original image is:
原始圖像是:
Original image.原始圖像。And the blurred images are
而且模糊的圖像是
3 x 3 Gaussian kernel — 5 x 5 Gaussian kernel3 x 3高斯核— 5 x 5高斯核 7 x 7 Gaussian kernel — 9 x 9 Gaussian kernel7 x 7高斯內(nèi)核— 9 x 9高斯內(nèi)核 11 x 11 Gaussian kernel — 13 x 13 Gaussian kernel11 x 11高斯核— 13 x 13高斯核比較方式 (Comparison)
The Gaussian kernel gives better result in separating frequencies. But, it is slow because of all the calculation involved. On the other hand, mean kernel works in reducing random noise in image space and it is fast. But, it gives worse performance in separating frequency.
高斯核在分離頻率方面給出了更好的結(jié)果。 但是,由于涉及所有計(jì)算,所以速度很慢。 另一方面,均值內(nèi)核可以減少圖像空間中的隨機(jī)噪聲,而且速度很快。 但是,它在分離頻率方面的性能較差。
We can compromise both kernel by apply mean kernel 4 times on the image. The blurred image would look like the Gaussian kernel.
我們可以通過在映像上應(yīng)用平均4次內(nèi)核來破壞兩個(gè)內(nèi)核。 模糊的圖像看起來像是高斯核。
Here is some blurred images with different kernel size
這是一些具有不同內(nèi)核大小的模糊圖像
Original — 3 x 3 mean kernel — 3 x 3 Gaussian kernel原始— 3 x 3平均內(nèi)核— 3 x 3高斯內(nèi)核 Original — 11 x 11 mean kernel — 11 x 11 Gaussian kernel原始— 11 x 11平均內(nèi)核— 11 x 11高斯內(nèi)核 Original — 21 x 21 mean kernel — 21 x 21 Gaussian kernel原始— 21 x 21平均內(nèi)核— 21 x 21高斯內(nèi)核 Original — 51 x 51 mean kernel — 51 x 51 Gaussian kernel原始— 51 x 51平均內(nèi)核— 51 x 51高斯內(nèi)核 11 x 11 Gaussian kernel for σ = 0 (default), 10, and 2511 x 11高斯核,σ= 0(默認(rèn)),10和25翻譯自: https://medium.com/@akeylanaufal/how-image-blurring-works-652051aee2d1
模糊圖像處理 去除模糊
總結(jié)
以上是生活随笔為你收集整理的模糊图像处理 去除模糊_图像模糊如何工作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: lol无法连接至验证服务,请前往官网查看
- 下一篇: position: sticky的用法详