我想实现一个径向中值滤波器。我有以下图片(大小 = (Nx,Ny))
我想导出每个像素的半径。对于每个半径,计算中值并将其放入一个新矩阵中,以代替所有具有相同半径的像素。我找到了 Image Smoothing Using Median Filter , 但速度不够快。我创建了自己的脚本,不幸的是,它也不是很快。我在一些通用数据上测试了它:
import cv2
from PIL import Image
from scipy import stats, ndimage, misc
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.image as mpimg
from scipy import stats
a = np.array([[0.,0.,0.,0.,0.],[0.,5.,1.,9.,0.],[0.,10.,2.,10.,0.],[0.,9.,1.,5.,0.],[0.,0.,0.,0.,0.]])
b = a.copy().flatten()
y,x = np.indices((a.shape))
center = [len(x)//2, len(y)//2]
r = np.hypot(x-center[0],y-center[1])
r = r.astype(np.int) # integer part of radii (bin size = 1)
set_r = set(r.flatten()) # get the list of r without duplication
max_r = max(set_r) # determine the maximum r
median_r = np.array([0.]*len(r.flatten())) # array of median I for each r
for j in set_r:
result = np.where(r.flatten() == j)
median_r[result[0]] = np.median(b[result[0]])
a_med = median_r.reshape(a.shape)
am_med = ndimage.median_filter(a, 3)
plt.figure(figsize=(16, 5))
plt.subplot(141)
plt.imshow(a, interpolation='nearest')
plt.axis('off')
plt.title('Original image', fontsize=20)
plt.subplot(142)
plt.imshow(am_med, interpolation='nearest', vmin=0, vmax=5)
plt.axis('off')
plt.title('Median filter', fontsize=20)
plt.subplot(143)
plt.imshow(a_med, interpolation='nearest')
plt.axis('off')
plt.title('Own median', fontsize=20)
plt.subplots_adjust(wspace=0.02, hspace=0.02, top=0.9, bottom=0, left=0,
right=1)
plt.show()
我想找到一个方便的方法来解决这个问题
最佳答案
这里的大多数答案似乎都集中在朴素中值过滤算法的性能优化上。值得注意的是,您可以在 OpenCV/scikit-image/MATLAB/等成像包中找到中值滤波器。实现更快的算法。
如果您要对 uint8 数据进行中值滤波,则在从一个邻域移动到另一个邻域时,可以使用很多巧妙的技巧来重用直方图。
如果您关心速度,我会在成像包中使用中值滤波器,而不是尝试自己滚动。
关于python - 图像 Python3 的中值滤波器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58257890/