1
votes

I want to implement a ring median filter - what it does is basically replace the center pixel by the median values of the pixels in a circular ring around it. I can make a circular mask of some radius like this

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

Then I can use scipy's generic filter to take median of the values

import scipy.ndimage.filters as scp_filt
circular_median = scp_filt.generic_filter(my_image, np.median, footprint=kernel)

The above mask is including pixels inside the circle. What pixels should lie on the circumference seems slightly ambiguous to me (think of circle inscribed in 3*3, 5*5, 7*7 kernels). Is there a "ring" function I can use OTS or something?

Why all this - trying to use a part of this paper http://pages.swcp.com/~spsvs/resume/PODS_DSS2009_2009-01-15.pdf Ring median filter, after subtraction from the original image leaves objects with a scale size of less than the radius.

Also, not sure if I should do this in RGB or not. I have only seen median filters on grayscale images

1
You can simply compute the relative coordinates of the ring, and then apply it on you image. It is comparable to apply a custom structuring element in mathematical morphology. - FiReTiTi
Yes, that's exactly my question - what are the exact relative coordinates. Say if 0,0 is the center of a 11*11 kernel, what are the coordinates of the circle. It's slightly ambiguous, and for that the Bresenham algo (see my answer below) seems good. There's an old question here stackoverflow.com/questions/17454931/… which uses a gaussian2D, but for that the condition ring = ring>eps & ring<1e-9 is manual, and needs to be changed for each kernel size. - madratman
The easiest way would be before applying the median filter, to compute the ring coordinates. In your case, you can start with the point (0,5), then trace the circle/ring shape with a small step (let's say a degree at a time), but you have to check that the new point is not already into the list. - FiReTiTi

1 Answers

-1
votes

Use the Bresenham algorithm for circles https://www.daniweb.com/programming/software-development/threads/321181/python-bresenham-circle-arc-algorithm

Scipy generic filter along with this ring median kernel is quite slow in practice, at least in python