Every "denoise before you upscale" tutorial says the same thing, and so did I: strip the noise first, because enlarging a spike into four fat spikes only makes it worse. Then I measured the case where nobody has added noise — a clean photograph — and the ranking flipped. Median-3 after the 2× upscale beat median-3 before it by 7.59 dB.
Both statements cannot be the rule. One of them has to be a special case, and finding out which one tells you whether the advice applies to your file at all.
The two numbers that contradicted each other
Seven photographs, 512-pixel centre crops, one bilinear 2× upscale. The same seven crops in every arm. The "truth" image is the bilinear 2× of the clean crop, so an arm that does nothing to the image scores the resize error alone, and every filter is charged for whatever it changes.
Clean frames, nothing injected. Median-3 applied at native resolution before the resize (call it A3), median-3 applied to the 2× output (B3), median-5 and median-7 likewise:
| arm | where the filter runs | mean PSNR | min | max |
|---|---|---|---|---|
| A3 | median-3 at 1×, then 2× | 42.858 dB | 39.080 | 49.394 |
| B3 | 2×, then median-3 | 50.448 dB | 48.404 | 52.953 |
| B5 | 2×, then median-5 | 43.444 dB | 40.662 | 46.753 |
| B7 | 2×, then median-7 | 39.740 dB | 36.068 | 44.370 |
A3 loses to B3 on all seven crops, between 3.56 and 9.36 dB. It is not a tie broken by one bad image. And the bill is easy to read off a fifth column I measured for exactly this purpose: median-3 at native resolution differs from the untouched crop by 40.200 dB. That is a real change to the picture, paid before the upscale even happens, and then enlarged.
So on a clean file the order is unambiguous: do not median-filter before the resize. The pre-filter costs 7.6 dB and buys nothing, because there is no spike to remove.
Where the pre-filter starts winning
Now inject salt-and-pepper noise at known densities, using the same 0.5%/1%/2%/5% logic anyone would use to demo a median filter, and sweep the low end between 0 and 1%:
| injected | pixels in a 512² crop | D: no filter | A3: median-3 pre | B3: median-3 post | B5: median-5 post | crops where A3 > B3 |
|---|---|---|---|---|---|---|
| 0 | 0 | 99.000 | 42.858 | 50.448 | 43.444 | 0/7 |
| 0.05% | 131 | 42.046 | 42.767 | 45.742 | 43.211 | 1/7 |
| 0.10% | 262 | 39.107 | 42.802 | 43.589 | 42.969 | 2/7 |
| 0.20% | 524 | 36.018 | 42.739 | 40.995 | 42.593 | 4/7 |
| 0.30% | 786 | 34.100 | 42.597 | 39.192 | 42.039 | 6/7 |
| 0.50% | 1 311 | 31.972 | 42.570 | 37.179 | 41.319 | 7/7 |
| 1.00% | 2 621 | 28.879 | 42.325 | 34.126 | 39.648 | 7/7 |
The mean crosses at 0.2%, but the per-image count is the more honest number: at 0.2% only 4 of 7 crops prefer the pre-filter, and it takes 0.5% before all seven agree. Anywhere below 0.1%, the post-filter is still ahead on the average, and at 0.05% it is ahead by 2.98 dB.
Two things in that table deserve a sentence each, because they are the reason the whole comparison is trustworthy.
Arm A3 barely moves. From 0% to 1% corruption it loses 0.533 dB, and its floor stays at 42.3 dB. A median filter at native resolution is nearly immune to density: it either removes a spike or it does not, and the spikes it misses are the ones with a neighbour, which are rare at these densities.
Arm B3 collapses. From 0% to 1% it loses 16.322 dB — from 50.448 to 34.126. It wins the clean frame by 7.59 dB and loses the dirty frame by 8.20 dB. Everything about that arm depends on how many of its input pixels are not spikes, and after the upscale every spike has been smeared into a 2×2 block of them.
Arm D, for scale: upscale-only falls from 42.046 dB at the lowest tested density to 28.879 dB at 1%.
Why the crossover exists at all
The reason is geometric, and it is measurable without touching a single image pixel. Take the injected 0/1 mask alone and bilinear-upscale it with exactly the same kernel:
| injected | duplicated output px ≥ 0.5 per impulse | output px ≥ 0.1 per impulse | corrupt area in source | same damage in 2× output | ratio |
|---|---|---|---|---|---|
| 0.05% | 4.000 | 11.976 | 0.0508% | 0.1521% | 2.994× |
| 0.10% | 4.000 | 11.975 | 0.0982% | 0.2940% | 2.994× |
| 0.20% | 4.000 | 11.962 | 0.1965% | 0.5877% | 2.991× |
| 0.50% | 4.000 | 11.920 | 0.5011% | 1.4933% | 2.980× |
| 1.00% | 4.000 | 11.871 | 1.0044% | 2.9806% | 2.968× |
Every impulse becomes exactly 4 full-height copies and touches about 11.9 output pixels once you count anything carrying at least ten percent of the mask weight. Because the impulses are spread randomly, the covered area grows by 2.98×, not 4×: some of the duplicated pixels land on a neighbour that was already corrupted. The ratio drifts slowly downward with density for the same reason — at 1% the newly-created blocks start overlapping each other.
Now put the two facts together. A post-filter is a 3×3 median in the output grid, so an impulse that has been duplicated into a 2×2 block is no longer an isolated outlier — it is a small cluster with three identical neighbours, and a 3×3 median cannot remove it. That is the 16.3 dB collapse in one sentence. A larger post-filter gets closer: at 1% B5 reaches 39.648 dB, and median-7 measured on the same crops in the earlier sweep in nhip112_impulse.json reaches 38.631 dB — but neither approaches A3's 42.325 dB, and both of them pay a heavy price on clean frames (43.444 and 39.740 dB against B3's 50.448).
Then I asked how much real clipping exists, and the answer reframed the question
The crossover is defined on injected salt-and-pepper, which is the easy case: an impulse at a random location surrounded by intact pixels. Real photographs clip differently. A blown highlight is a region, not a spike, and a 3×3 median cannot move a pixel whose eight neighbours are all 255 either.
So I counted. In the same seven 512-pixel crops, every pixel at exactly 0 or exactly 255, then asked how many of its 8 neighbours are also clipped:
| crop | clipped px | % of crop | isolated | clustered (≥1 clipped neighbour) | at 255 | 255 and isolated |
|---|---|---|---|---|---|---|
| #0 | 40 | 0.0153% | 16 | 24 | 0 | 0 |
| #1 | 0 | 0.0000% | 0 | 0 | 0 | 0 |
| #2 | 216 | 0.0824% | 5 | 211 | 216 | 5 |
| #3 | 654 | 0.2495% | 17 | 637 | 1 | 1 |
| #4 | 27 | 0.0103% | 12 | 15 | 6 | 3 |
| #5 | 18 | 0.0069% | 11 | 7 | 0 | 0 |
| #6 | 22 | 0.0084% | 2 | 20 | 0 | 0 |
| total | 977 | 0.0532% | 63 | 914 | 223 | 9 |
Across 1 835 008 pixels there are 977 clipped. Of those, 63 are isolated — 6.45%. At the white end it is worse: of 223 pixels at 255, only 9 have no clipped neighbour. The other 214 are part of a blown highlight, which is exactly the shape a median filter cannot repair. The most clipping-heavy crop (#3, 0.2495%) is 97% clustered.
The counts reconcile with the earlier census of the same crops to four decimal places on every image, which is the control that says I counted the same thing twice rather than two different things.
This is the part that reframed the article for me. The crossover table says a median pre-filter starts paying at 0.2% corruption. The census says real photographs in this set carry 0.0532% clipping on average, and only 0.0034% of pixels are isolated spikes (63 of 1 835 008, or 0.0034%). The injected densities where the pre-filter wins are not just above the real clipping rate — they are above it by an order of magnitude, and dominated by a corruption shape real files do not have.
So the honest answer is: for these files, neither. Not "denoise before upscaling" and not "denoise after". A median filter is not the tool the data picks, because the thing it removes is not what is actually there.
What the order actually costs, in milliseconds
If you do decide to run the post-filter, it runs on 4× as many pixels. Wall-clock, minimum of 7 repetitions on one machine, same 512-pixel crop and its bilinear 2× output:
| filter | at 512×512 (1×) | at 1024×1024 (2×) | ratio |
|---|---|---|---|
| median-3 | 14.0 ms | 40.1 ms | 2.87× |
| median-5 | 51.4 ms | 161.3 ms | 3.14× |
| median-7 | 97.5 ms | 323.3 ms | 3.32× |
The ratio is not 4 because a 1024² image has a smaller border fraction and the per-pixel cost scales with kernel area, not with pixel count alone. Scaled by pixel count alone the median-3 post-filter would land near 1 s on a 24-megapixel photograph — an extrapolation, not a measurement, which is not a disaster but is not free either — and it is being spent to fix something that, per the census above, is mostly not there.
Two smaller things the numbers forced me to say
A3 never recovers to clean. Its ceiling in the noisy sweep is 42.802 dB at 0.10%, against 42.858 dB when nothing was injected at all — dropping as corruption rises, not rising. The pre-filter is not "removing noise and preserving the rest"; it is paying 40.2 dB of native-resolution change up front and then holding roughly still. Because that change is applied before a 2× upscale, it is applied to the resolution the image actually has, which is the only reason it holds up better than a filter applied to invented pixels.
B5 looks like a good compromise in the noisy arms and is not one. At 1% corruption it scores 39.648 dB, better than B3's 34.126 dB. But on clean frames B5 costs 7.004 dB relative to B3 (43.444 against 50.448), so it wins the case you were testing and loses the case you actually have. If you must pick one arm for unknown input, that trade is the one to look at, not the noisy-frame ranking.
A reproduction note about honesty
The crossover result reproduces to 0.000 dB across two files. The clean arm of the noisy sweep (nhip122_crossover.json, p = 0) and the clean-frame measurement (nhip121_med_cost.json) were computed by different scripts at different times from the source images, and they agree on all four arms to three decimals — A3 42.858, B3 50.448, B5 43.444. That is a cross-file control, not a check inside one script, and it is the reason I am willing to quote the 7.59 dB figure at all.
Both scripts re-ran today in 4.7 s and 13.1 s respectively and printed the stored numbers unchanged. The isolation census and the mask bloom were written for this article and are new files.
When you should still run the median filter
None of this says a median filter is a bad tool. It says this measurement does not support using it as a fixed pre- or post-step in an upscaling pipeline, and here are the cases where the reasoning does not cover it:
- Sensor dead pixels. A stuck pixel is by definition isolated and at 0 or 255. That is the impulse case, and A3's near-invariance to density is exactly the property you want — one pass before the resize, at native resolution.
- Film scratches and dust on a scan. Isolated, high-contrast, three-dimensional; generally not clipped at 255, so a median-3 at native resolution before the upscale will touch it and the upscale will not have smeared it yet.
- Video from a failing sensor or a very noisy compact camera. Here the corruption really is impulse-shaped and dense, and the 0.5%–1% rows of the table are the régime you are in. Measure a representative frame first; if the impulse density is above 0.2%, pre-filter.
- You are printing. A 40 dB change at native resolution (the A3 bill) is visible on a 300 dpi print even when it is invisible at fit-to-window. Do not spend it unless there is a spike to remove.
And the cases where this data says don't:
- A clean transfer from a modern camera. There is nothing to filter; B3's 50.448 dB is available and A3 throws away 7.59 dB.
- A photograph whose only defect is a blown highlight. The census says that is 97% of the clipping in one of these seven crops. A median filter will leave it alone and charge you 7.6 dB anyway.
- Anything where you were about to apply median-7 "to be safe". B7 costs 10.71 dB against B3 on clean frames and does not recover it on dirty ones.
A method note
The truth image in the noisy sweep is the bilinear 2× of the clean crop, not the clean crop upscaled by a better kernel. That choice penalises every filter for what it changes and gives the resize arm a 99.000 dB floor, which is what makes the crossover a comparison of filters rather than a comparison of kernels. The cost is that all absolute numbers in these tables are relative to bilinear at 2× and should not be quoted against Lanczos or a neural model. I have not measured those, and I am not going to guess.
The 7 crops are 512-pixel centre crops from real photographs, not synthetic gradients. Crop #1 carries no clipped pixel at all, which is why one row of the census table is all zeros and one bar in the chart is empty — that is the file, not a bug in the counting.
Checklist before you reach for the median filter
- Count the clipped pixels in your own file. If the number is near 0.05% of the frame and mostly clustered, a median filter is the wrong tool — the crossover table does not apply to your input.
- If the corruption is genuinely isolated (dead pixels, dust, impulse noise), filter at native resolution before the resize. A3 is the only arm that barely moves with density.
- If you are filtering after the resize anyway, understand that a 3×3 median can no longer see an impulse as isolated: it is a 2×2 block, and B3's 16.3 dB collapse from 0% to 1% is what that costs.
- Measure your own frame's impulse density before choosing. Below 0.1% the post-filter still wins the average; above 0.5% all seven crops agree on the pre-filter; between the two it depends on the image.
- Do not use median-5 or median-7 as a hedge. They cost more on clean frames than they recover on dirty ones.
If you want to run the same two upscales side by side on your own file before deciding, UpgoLAB's browser upscaler does a 2× without an install, which is enough to compare a pre-filtered and a post-filtered output at 100% on the patch that actually matters.
That 28-second clip is a pass through five presets on real product frames — direct link if the embed does not load. The preset comparison is not what this article measures, but it is the same question in a different tool: where in the pipeline a filter runs, and what it is being asked to remove.
Every figure above comes from four files in this campaign's tracking/ directory: nhip121_med_cost.json (clean-frame arms), nhip122_crossover.json (the density sweep), nhip124_medisolation.json (the 8-neighbour clipping census) and nhip125_impulse_bloom.json (the mask-only bloom). The median-7 figure at 1% is quoted from nhip112_impulse.json, where only the p = 0.5/1/2/5% points were swept. Seven photographs, 512-pixel centre crops, bilinear 2×, Pillow and numpy. Median filtering as a definition is standard and documented, e.g. the median filter article; salt-and-pepper noise is likewise standard reading, and the reason it is the wrong model for this problem is its definition — it is a per-pixel random process, and real clipping is not.


