A switching median filter for salt and pepper noise
Written from scratch in MATLAB. A standard median filter blurs everything it touches; a switching one only intervenes where a pixel is actually corrupted.
The question
Salt and pepper noise replaces isolated pixels with extreme values. A plain median filter removes them, but it also rewrites every uncorrupted pixel in the image, so you trade noise for a loss of detail everywhere.
Approach
A switching median filter that first detects whether a pixel is an impulse and only replaces it if so, leaving clean pixels untouched. Tested across noise densities from 10 to 40 percent on multiple images, and at 40 percent I applied the filter recursively for ten iterations to see how SNR evolves.
Outcome
The switching filter preserves detail that the standard median destroys, and the gap widens as noise density rises. Recursive application at high density keeps improving SNR for several iterations before it plateaus.
What it taught me
Detect first, then act. A filter that treats every pixel identically is throwing away the information that some of them were fine, and that principle turns up again in every weighted objective I have written since.