Python 中的线性过滤器未按预期工作
Linear FIlter in Python not working as expected
我正在尝试实现一个线性滤波器,它可以区分当前像素上方 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 )
我认为你的内核定义有问题。使用当前的内核定义,您只需将像素替换为以像素为中心的 3x3 window 中的平均强度值。我相信这就是你想要的内核:
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)
我正在尝试实现一个线性滤波器,它可以区分当前像素上方 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 )
我认为你的内核定义有问题。使用当前的内核定义,您只需将像素替换为以像素为中心的 3x3 window 中的平均强度值。我相信这就是你想要的内核:
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)