Search code examples
matlabimage-processingfiltercomputer-visionmedian

circular median filter matlab


How can I define a circular filter that acts like median filter (medfilt2), but instead of square neighborhood of [n n] , performs median on a circular neighborhood with radius r ? I need to perform this operation on 2d-image. (And preferably, should work fast of course). Thanks


Solution

  • use ordfilt2 with a circular domain. For example,

    B = ordfilt2(A, order, domain)
    

    replaces each element in A by the order-th element in the sorted set of neighbors specified by the nonzero elements in domain. In your case create a circular domain with something like

    domain=fspecial('disk',10)>0;
    

    this generates a nice binary disk (21x21 matrix), that is probably too big for your needs, so can re-size to whatever you need using the fspecial or imresize. Then the median is the middle value obtained from the sorted non-zero elements of the domain, so:

    B = ordfilt2(A,round(0.5*numel(find(domain)))),domain);