# is there a way to apply a median filter diagonally?

For example, some studies show how to smooth a spectrogram with a 2D median filter vertically and horizontally (along time or frequency). My question is how would I apply 2D median filter along the diagonals of the spectrogram instead? This would be useful for a certain study I am doing, so I need to understand how that would be coded in MATLAB.

• I have provided with gblk, a Matlab toolbox you ought to invest in if you want to parse blocks in images – Laurent Duval Mar 16 '18 at 6:57

## 1 Answer

The median filter can be applied on many pixel-set shapes, and indeed this was used to emulate of fasten full 2D median filters by a combination of leaner medians.

In Matlab, your request just amounts to gather diagonal elements in a vector. The simplest could be:

• extract each sliding square patch of the image
• extract the diagonal as a matrix with diag.m
• compute the median

If you want the antidiagonal, flip the square patch with flipud, fliprl. If you want other diagonals than the $\pm 45°$, you have to use discrete lines, for instance with the Bresenham algorithm.

If you want to remain high level, I'd suggest you to use gblk: a pedestrian data block extractor at MatlabCentral to parse an image with overlapping blocks.

• Hi, thanks for reply. This is very interesting. My only question is, how do you do "extract each sliding square patch of the image" in matlab and also can you show matlab example of the discrete lines for other degrees than 45. thank you! – Dan Mar 14 '18 at 22:40
• I appreciate your feedback. If your question is only about codes in Matlab, it does not belong to SE. DSP. However, I would suggest you to provide self-made by updating your question, with actual data, and we can help your in that direction. In other words, the question is interesting, yet yet we ain't coding at your place – Laurent Duval Mar 15 '18 at 6:10