【发布时间】:2019-10-06 13:50:28
【问题描述】:
我想实现一个径向中值滤波器。我有以下图片(大小=(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()
我想找到一个方便的方法来解决这个问题
【问题讨论】:
-
请分享预期的输出图像。
-
@MarkSetchell,我已经在一些生成数据上测试了脚本(我更改了我的帖子)
-
@MarkSetchell,我想改变径向平均值 (scipy-lectures.org/advanced/image_processing/auto_examples/…) 以计算径向中位数
-
有点不清楚问题是什么,您想实现您的代码以生成与
ndimage.median_filter(a, 3)相同的数字吗?或者您是否正在尝试实现更快的代码版本? -
@RK1,
ndimage.median_filter(a, 3)用大小 = 3 的窗口中的中值替换。我想做径向中值滤波器
标签: python image numpy image-processing