[英]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 窗口中用平均强度值替换像素。 我相信这是你想要的内核:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.