Jim421616
Jim421616

Reputation: 1536

circular median filter in python

I want to create a circular median filter with a given radius, rather than a square filter from an array. Here's my attempt so far:

#   Apply median filter to each image
import matplotlib.pyplot as plt
radius = 25
disk_filter = plt.fspecial('disk', radius)
w1_median_disk = plt.imfilter(w1data, disk_filter, 'replicate')

w2_median_disk = plt.imfilter(w2data, disk_filter, 'replicate')

w1data and w2data are the 2-d numpy arrays I'm trying to apply the filter to. The fspecial module is from Matlab, but I want to use it (or something equivalent) in my Python code. Any ideas?

I get the error message "

disk_filter = plt.fspecial('disk', radius)
AttributeError: 'module' object has no attribute 'fspecial'"

I'm wondering if there's either a module I can import that contains fspecial, or an equivalent command in Python.

Upvotes: 1

Views: 4737

Answers (3)

David
David

Reputation: 1979

If you are willing to install/use an additional package I strongly recommend the amazing skimage for any kind of image processing in Python! Filtering with a disk-like filter is just two lines of code:

import skimage
import skimage.data
import skimage.morphology
import skimage.filters

# load example image
original = skimage.data.camera()

# create disk-like filter footprint with given radius
radius = 10
circle = skimage.morphology.disk(radius)

# apply median filter with given footprint = structuring element = selem
filtered = skimage.filters.median(original, selem = circle)

Upvotes: 2

Jim421616
Jim421616

Reputation: 1536

Here's something I found that seems to do the job:

 from scipy.ndimage.filters import generic_filter as gf

 #   Define physical shape of filter mask
 def circular_filter(image_data, radius):
     kernel = np.zeros((2*radius+1, 2*radius+1))
     y, x = np.ogrid[-radius:radius+1, -radius:radius+1]
     mask = x**2 + y**2 <= radius**2
     kernel[mask] = 1                
     filtered_image = gf(image_data, np.median, footprint = kernel)
     return filtered_image

But I'm not sure I understand perfectly what's going on. In particular, what exactly do the lines

     y, x = np.ogrid[-radius:radius+1, -radius:radius+1]
     mask = x**2 + y**2 <= radius**2
     kernel[mask] = 1

do?

Upvotes: 1

f5r5e5d
f5r5e5d

Reputation: 3706

scrape 'cameraman' image from:
https://www.mathworks.com/help/images/ref/fspecial.html

enter image description here

import numpy as np
import matplotlib.pyplot as plt

import os
from scipy import misc
path = 'D:/My Pictures/cameraman.bmp'
cameraman = misc.imread(path, flatten=0)

cameraman = np.average(cameraman, axis=2)

r = 10
y,x = np.ogrid[-r: r+1, -r: r+1]
disk = x**2+y**2 <= r**2
disk = disk.astype(float)

from scipy import signal
blur = signal.convolve2d(cameraman, disk, mode='full', boundary='fill', fillvalue=0)

import matplotlib 
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.imshow(cameraman, cmap = matplotlib.cm.Greys_r)
ax1.set_title('cameraman')
ax2.imshow(blur, cmap = matplotlib.cm.Greys_r)
ax2.set_title('signal.convolve2d(cameraman, disk..')

or you may want to use scipy.ndimage.filters.convolve for its 'reflect' edge treatment

from scipy.ndimage.filters import convolve
blur = convolve(cameraman, disk)

Upvotes: 1

Related Questions