python - 如何使用 3x3 内核平滑图像

标签 python opencv image-processing computer-vision

我试图通过遍历其像素来平滑图像,计算 3x3 block 的平均值,然后将平均值应用于该 block 中的所有 9 个像素。

代码:

    import matplotlib.pyplot as plt
    import numpy as np
    import cv2 as cv
    from PIL import Image


    # 1. Load Image
    name = 'ebay.png'
    img = cv.imread(name) #import image

    h, w = img.shape[:2]


    # 2. Smooth with kernel size 3

    for y in range(0, w, 3):
        for x in range(0, h, 3):     
            px1 = img[x][y] #0/0
            px2 = img[x][y+1] #0/1
            px3 = img[x][y+2] #0/2
            px4 = img[x+1][y] #1/0
            px5 = img[x+1][y+1] #1/1
            px6 = img[x+1][y+2] #1/2
            px7 = img[x+2][y] #2/0
            px8 = img[x+2][y+1] #2/1
            px9 = img[x+2][y+2] #2/2

            average = np.average(px1 + px2 + px3 + px4 + px5 + px6 + px7 + px8 + px9)    

            img[x][y] =  average  #0/0
            img[x][y+1] = average   #0/1
            img[x][y+2] = average   #0/2
            img[x+1][y] = average   #1/0
            img[x+1][y+1] = average   #1/1
            img[x+1][y+2] = average  #1/2
            img[x+2][y] = average  #2/0
            img[x+2][y+1] = average   #2/1
            img[x+2][y+2] = average  #2/2

# 3. Transform the resulting image into pgm format and save result        

new_image = Image.fromarray(img)
new_image.save('new.png')

# 4. Show image

new_image.show()

However this just makes my new image just very pixely and not smooth at all. 

我假设我在这里做错了什么:

average = np.average(px1 + px2 + px3 + px4 + px5 + px6 + px7 + px8 + px9)

因为,当我只使用 px5 作为平均值时,新图像看起来好多了(但仍然不是很平滑)。请看下图:

原始图片: Original Image

我的代码现在正在做什么: What my code is doing right now...

平均使用 px5 时的结果: Result when I am using px5 as average

将所有像素相加并除以 9 的结果: enter image description here

最佳答案

所以我在这里遇到了两个问题,多亏了@Ernie Yang 和@Cris Luengo,我才得以解决。非常感谢您的帮助!

1) 我的平均计算的问题是它溢出了我正在总结像素值。这就是为什么结果看起来很奇怪,因为它是环绕的。所以我不得不改变:

average = np.average(px1 + px2 + px3 + px4 + px5 + px6 + px7 + px8 + px9)

到:

average = px1/9. + px2/9. + px3/9. + px4/9. + px5/9. + px6/9. + px7/9. + px8/9. + px9/9.

2) 然而,这并没有使我的图像变得平滑,因为我只是将平均值分配给补丁中的所有 9 个像素。所以这就造成了图片像素化,没有平滑处理。因此,我不得不只将平均结果写入中间像素,而不是邻域中的所有 3x3 像素。我还必须将它写入单独的输出图像。您不能就地执行此操作,因为它会影响后面像素的结果。

正确的代码示例:

    import matplotlib.pyplot as plt
    import numpy as np
    import cv2 as cv
    from PIL import Image
    import scipy.ndimage as ndimage
    from scipy.ndimage.filters import gaussian_filter


    # 1. Load Image
    name = 'ebay.png'
    img = cv.imread(name) #import image


    h, w = img.shape[:2]
    smoothedImage = cv.imread(name) #initialize second image


    # 2. Smooth with with kernel size 3

    for y in range(0, w-2):
        for x in range(0, h-2):     
            px1 = img[x][y] #0/0
            px2 = img[x][y+1] #0/1
            px3 = img[x][y+2] #0/2
            px4 = img[x+1][y] #1/0
            px5 = img[x+1][y+1] #1/1
            px6 = img[x+1][y+2] #1/2
            px7 = img[x+2][y] #2/0
            px8 = img[x+2][y+1] #2/1
            px9 = img[x+2][y+2] #2/2

            average = px1/9. + px2/9. + px3/9. + px4/9. + px5/9. + px6/9. + px7/9. + px8/9. + px9/9.

            smoothedImage[x+1][y+1] = average   #1/1

    # 3. Transform the resulting image into pgm format and save result        

    new_image = Image.fromarray(smoothedImage)
    new_image.save('new.png')

    # 4. Show image

    new_image.show()

原始图片: enter image description here

平滑图像: enter image description here

编辑:

Hey folks, I am back from my afternoon nap. I had quite a few interesting thoughts, here is my improved code now:

import matplotlib.pyplot as plt
import numpy as np
import cv2 as cv
from PIL import Image
import scipy.ndimage as ndimage
from scipy.ndimage.filters import gaussian_filter


# 1. Load Image
name = 'ebay.png'
img = cv.imread(name) #import image
h, w = img.shape[:2]
kernel = 5
radius = (kernel-1)/2

img2 = np.zeros((h, w, 3), dtype = 'uint8') #new image to paint on

    def pxIsInImgRange(x, y):
        if (0<=x) and (x < w): 
                if (0<=y) and (y < h):
                    return True
        return False

    # 2. Smoothing the shit out

    for x in range (-radius, w+radius):
        for y in range (-radius, h+radius):

            if pxIsInImgRange(x,y): 

                    px = 0

                    for vx2 in range (-radius, radius+1):
                        for vy2 in range (-radius, radius+1):
                            x2 = x + vx2
                            y2 = y + vy2
                            if pxIsInImgRange(x2,y2):

                                px = px + (img[y2][x2]/float((kernel*kernel)))
                            else:
                                px = px + 0 


                    img2[y][x] = px

    # 3. Save image                

    new_image = Image.fromarray(img2)
    new_image.save('new.png')

    # 4. Show image

    new_image.show()

内核为 5 的新结果:

enter image description here

关于python - 如何使用 3x3 内核平滑图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50747064/

相关文章:

python - 当 admin.py 中的字段为只读时,模型字段空白 = false 不起作用

python - openpyxl - 使用列表填充列

visual-c++ - OpenCV 特定物体检测

python - OpenCV( python ): Construct Rectangle from thresholded image

c# - 从图像中读取字符

matlab - 使用 K 均值聚类准确检测图像中的颜色区域

python - 比较两个名字和姓氏列表以找到匹配项

python - 当其他对象处于事件状态时附加类内的列表

python - 在 Python 中写入文件并在 Windows 中回车

c++ - 在 OpenCV C++ 中查找 Mat() 的所有峰值