首页 > 解决方案 > Python中的线性过滤器没有按预期工作

问题描述

我正在尝试实现一个线性滤波器,使当前像素上方的 3 个像素的平均值之间产生差异。我究竟做错了什么?

import numpy as np
from skimage import io,color
import matplotlib.pyplot as plt

# Image loading
img = io.imread('lena_256.jpg')
img = color.rgb2gray(img)*255
plt.figure(),plt.imshow(img,cmap='gray')

img_f1 = img.copy()
size = img.shape


 kernel = np.vstack((np.ones(3),np.zeros(3),-np.ones(3)))
 kernel/=3
for i in range(size[0]-2):
    for j in range(size[1]-2):
        # define the neighborhood - the current pixel will be at line=i+1 and column=j+1
        V = img[i:i+3, j:j+3]
        # multiply each pixel in the neighborhood with the weight in the kernel
        V = V * kernel
        # make the sum of the results and put it in the current pixel
        img_f1[i+1,j+1] = np.sum(V)

# Visualize the result
plt.figure(),plt.imshow(img_f1,cmap='gray', vmin = 0, vmax = 255 )

标签: pythonmatplotlibscikit-image

解决方案


我认为您只是对内核的定义有疑问。使用当前的内核定义,您只需将像素替换为以像素为中心的 3x3 窗口中的平均强度值。我相信这是您想要的内核:

kernel = np.vstack((np.ones(3),np.zeros(3),-np.ones(3)))
kernel/=3
print(kernel)

[[ 0.33333333  0.33333333  0.33333333]
 [ 0.          0.          0.        ]
 [-0.33333333 -0.33333333 -0.33333333]]

请注意,此内核将始终将上面的平均值减去下面的平均值,这可能会导致负像素强度。因此,当您绘制图像时,设置vmin = 0将使具有负强度的像素显示为黑色。在这一点上,这取决于你到底想要什么,你可以比较这三个图来决定:

# crop negative img intensities to 0
plt.imshow(img_f1, cmap='gray', vmin = 0, vmax = 255) 

# absolute value of image intensities
plt.imshow(abs(img_f1), cmap='gray', vmin = 0, vmax = 255) 

# let imshow normalize the data on its own
plt.imshow(img_f1, cmap='gray')

# set minimum and maximum intensity values to the extreme values that could be
# generated by the filtering operation
plt.imshow(img_f1, cmap='gray', vmin = -255, vmax = 255)

推荐阅读