My first instrument reported that a 2× upscale removed 99.76 % of the salt-and-pepper noise I had injected into seven photographs. That number is real. It is also wrong, and the reason it is wrong is the most useful thing I learned in this whole experiment: the upscaler did not remove the noise, it diluted it below the threshold my instrument was using to call something noise.
Here is the short version of what actually happens to a single corrupted pixel when you resize by a factor f with bilinear interpolation. Its total energy is conserved exactly: the sum of the deltas it leaves behind is 127 · f² code levels, no more and no less. What changes is the peak. At 2× the peak drops to 55.9 % of its original height. At 3× it does not drop at all — it is 100 %. At 4×, 76.4 %. At 8×, 88.2 %. The sequence is not monotonic and it is not an artefact of any algorithm: it falls out of where the output pixel centres land relative to the source sample, and it can be predicted to within 0.006 of the measured value with one multiplication.
Everything below comes out of five files in this campaign's tracking/ directory: nhip112_impulse.json, nhip121_med_cost.json, nhip121_hotpixel_census.json, nhip122_crossover.json, nhip123_geom_exact.json and nhip123_energy.json. Seven photographs, 512-pixel centre crops, grayscale, bilinear resize, and injection masks tracked by coordinate rather than by brightness threshold — that last decision is the whole reason the second instrument worked and the first one did not.
The first instrument found 977 defective pixels that were never defective
I wanted to know how far a point defect spreads after an upscale. The obvious way to measure "did the defect survive" is to count pixels pinned at 0 or 255, because that is what salt-and-pepper noise is: dead black and blown white. So version one counted exactly that. It reported a large, satisfying removal rate.
Then a control caught it. I ran the same counter on the clean, uncorrupted crops — no noise injected at all — and it found survivors anyway. That is impossible unless the counter is reading something other than my noise.
So I counted the real thing. In the same seven 512-pixel crops, before any processing:
| crop | pixels at 0 | pixels at 255 |
|---|---|---|
| med_048d3778 | 40 | 0 |
| med_1344197c | 0 | 0 |
| med_6766a320 | 0 | 216 |
| med_792e8b64 | 653 | 1 |
| med_ac7b6755 | 21 | 6 |
| med_b4fd2338 | 18 | 0 |
| med_c079d084 | 22 | 0 |
| total | 754 | 223 |
Nine hundred and seventy-seven genuinely clipped pixels in 1,835,008 samples — about 0.053 %. They are not defects. They are specular highlights, deep shadows, the black inside a lens barrel, and every one of them is a correctly exposed part of a real photograph. A threshold-based defect detector cannot tell them apart from injected noise, because there is nothing to tell apart: both are the value 255.
Version two replaced the threshold with a mask. When I inject a corrupted pixel, I record its coordinate. When I ask "did it survive", I ask whether the output pixels whose nearest source sample is that coordinate are still pinned. Now "survived" is exact and needs no threshold, and the clean-frame control returns zero for every arm. That control is C1_clean_survivors in nhip112_impulse.json and it is the only reason the numbers in this article mean anything.
What one bright pixel actually becomes
Take a 64×64 field that is uniformly 128 and set one pixel to 255. Resize it. This is not a photograph and not a JPEG, so nothing you see is a compression artefact — it is the resampler and nothing else.
| factor | pixels touched | bbox (px) | peak (levels) | peak ÷ 127 |
|---|---|---|---|---|
| 2× | 16 | 4 | 71.0 | 0.5591 |
| 3× | 25 | 5 | 127.0 | 1.0000 |
| 4× | 64 | 8 | 97.0 | 0.7638 |
| 6× | 144 | 12 | 106.0 | 0.8346 |
| 8× | 256 | 16 | 112.0 | 0.8819 |
Two things in that table are worth stopping on.
The peak is not monotonic. If upscaling progressively blurred a defect, the peak would fall as the factor rose. It does not: 2× costs you 44 % of the peak and 8× costs you 12 %. 3× costs you nothing at all. Any mental model in which "higher factor = more smoothing of small features" is refuted by the 3× row.
The footprint is 4 pixels at 2×, not 2. The naive expectation — one source pixel becomes one 2×2 block — describes which output pixels have that pixel as their nearest source, not the extent of the damage. The damage bleeds into neighbouring pixels at a peak of 71 levels. That distinction is what broke the first instrument, and it is also what decides which repair filter works later.
The peak loss is bilinear weight arithmetic, not an algorithm
Here is the part I expected to be messy and turned out to be exact. Bilinear interpolation at output pixel i samples the source at coordinate (i + 0.5)/f − 0.5. If that coordinate lands at fractional distance d from the nearest source sample, the weight on that sample — in one dimension — is 1 − d, and in two dimensions (1 − dx)(1 − dy).
For an even factor, the pixel centre lands halfway between samples. For an odd factor, it lands exactly on one.
| factor | source coord of the peak | fractional offset | predicted weight | measured | error |
|---|---|---|---|---|---|
| 2× | 31.750 | 0.25 | 0.5625 | 0.5591 | −0.0034 |
| 3× | 32.000 | 0.00 | 1.0000 | 1.0000 | 0.0000 |
| 4× | 31.875 | 0.125 | 0.7656 | 0.7638 | −0.0018 |
| 6× | 31.9167 | 0.0833 | 0.8403 | 0.8346 | −0.0057 |
| 8× | 31.9375 | 0.0625 | 0.8789 | 0.8819 | +0.0030 |
Largest error across all five factors: 0.0057, which is less than one code level out of 127. There is no fitting and no free parameter here — (1 − 1/(2f))² is the entire model, and it explains a curve that looks arbitrary.
The 3× case is exact because (3 + 0.5)/3 − 0.5 = 0.6667, and the nearest integer is 1, at distance 0.3333... no. Let me state it the way it actually computes: the output pixel with the maximum response sits at source coordinate 32.0 exactly, which is the injected pixel itself, at distance zero. Weight 1.0. The impulse passes through a 3× bilinear resize untouched in peak, spread over a 5×5 area.
The tiny negative bias at 2×, 4× and 6× is quantisation: the operation runs in 8-bit, so a delta of 71 is really 70.9 rounded. That is the whole residual.
Energy is conserved exactly, and that is why "removed" was the wrong word
Sum every delta the defect leaves in the output and divide by 127 — the total budget of one full-scale pixel:
| factor | Δ sum ÷ 127 | ideal f² | footprint | Δ sum ÷ peak |
|---|---|---|---|---|
| 2× | 4.000 | 4 | 4 | 7.15 |
| 3× | 9.000 | 9 | 5 | 9.00 |
| 4× | 16.000 | 16 | 8 | 20.95 |
| 5× | 25.000 | 25 | 9 | 25.00 |
| 6× | 36.031 | 36 | 12 | 43.17 |
| 8× | 64.504 | 64 | 16 | 73.14 |
The total is f² to within 0.8 % at every factor I measured, up to 16×. Bilinear resize distributes a point source over f² units of area — as you would expect from conservation — and does not attenuate it.
Which means my headline number was describing my instrument, not the image. "99.76 % of injected impulses removed" was computed by counting output pixels still pinned at 0 or 255. At 2× the peak is 71 levels above background, so a salt pixel at 255 becomes 199 and a pepper pixel at 0 becomes 57. Neither is pinned, so neither counts as a survivor, so the instrument reports removal. The defect is still there at a contrast of 71 levels, which on a mid-grey background is obvious to the eye and would be obvious to any sharpening pass downstream. What the upscale did was move the energy below a threshold I chose, and I then reported the threshold as if it were a property of the image.
The correct statement is: the upscale reduces the peak contrast of a point defect by 12 % to 44 %, and reduces its detectability by a threshold you picked, while conserving its total energy exactly. Between those two sentences is the difference between a measurement and a measurement plus an unstated definition.
The practical corollary is uncomfortable. At 8× a hot pixel keeps 88 % of its peak, so an upscale makes it more conspicuous per-pixel than a 2× does. If your pipeline upscales and then looks for dead pixels by brightness, an 8× upscale will hand you 16× as many candidate pixels at nearly full contrast, and you will write a bug report about a sensor that was fine.
Before or after? Both, and the density decides
Now the repair. The obvious ordering is: upscale first (that is the deliverable), then clean up at the end. The less obvious ordering is: clean at native resolution first, then upscale once. Which one wins depends on something nobody puts in a filter preset — how much noise there is.
Same seven crops, same bilinear 2×, truth defined as the 2× upscale of the clean crop so every filter is charged for what it changes. Arms: D upscale only; A3 median3 at native then 2×; B3/B5/B7 2× then median of that size.
| density p | D | A3 (pre) | B3 (post) | B5 (post) | A3 − B3 | images where A3 wins |
|---|---|---|---|---|---|---|
| 0 % | — | 42.858 | 50.448 | 43.444 | −7.590 | 0/7 |
| 0.05 % | 42.046 | 42.767 | 45.742 | 43.211 | −2.975 | 1/7 |
| 0.10 % | 39.107 | 42.802 | 43.589 | 42.969 | −0.787 | 2/7 |
| 0.20 % | 36.018 | 42.739 | 40.995 | 42.593 | +1.744 | 4/7 |
| 0.30 % | 34.100 | 42.597 | 39.192 | 42.039 | +3.405 | 6/7 |
| 0.50 % | 31.972 | 42.570 | 37.179 | 41.319 | +5.391 | 7/7 |
| 1.00 % | 28.879 | 42.325 | 34.126 | 39.648 | +8.199 | 7/7 |
The crossover is between 0.1 % and 0.2 %. Below it the post-filter wins, at 0 % by 7.6 dB, because a 3×3 median at native resolution is destructive to real detail: measured against the untouched native crop, it costs 40.2 dB on clean frames. Above 0.2 % the pre-filter wins, at 1 % by 8.2 dB, because it sees each defect at full amplitude and replaces it with a genuine neighbourhood median before any of that energy has been spread.
And notice the shape of the pre-filter's curve. From 0 % to 1 % density, A3 moves from 42.858 to 42.325 — 0.53 dB total. It is almost flat. The post-filter falls 16.3 dB over the same range and the unfiltered upscale falls 20.1 dB. That flatness is not a small effect: it means the pre-filter's worst case is essentially its best case, while every post-filter's quality is a function of a parameter the operator does not control.
I have to report the weak part of this honestly. At exactly 0.2 %, where the average says the pre-filter wins by 1.744 dB, only 4 of the 7 images agree. The mean has crossed but the population has not. Below 0.3 % I would not call the ranking settled on this sample; from 0.3 % upward it is 6/7 and then 7/7, and that is where I would put the threshold in practice.
There is a second, independent result in that table that supports the pre-filter from a different direction: B5 beats B3 at every density, and at 0.005 % density it even beats A3 on two of the seven images. A 5×5 median is a better post-filter than a 3×3. Why, exactly?
Why the post-filter needs a bigger window: 4 pixels versus 3
Go back to the footprint table. At 2×, a single hot pixel occupies a 4×4 area of the output at meaningful contrast. Now apply a 3×3 median at the output. At the centre of the defect, half the window — 5 of 9 samples — is defect; the median of a window that is mostly defect returns something close to defect. The filter is structurally too small to see the edge of the thing it is supposed to remove.
A 5×5 window gives 9 clean samples against 16 defect samples at the worst position and does only marginally better at the centre, but at the border of the footprint it works, and that is where most of the perceptible contrast sits. That is the entire explanation for B3 at 26.60 dB and B5 at 32.28 dB at 5 % density — a 5.7 dB gap that has nothing to do with tuning and everything to do with window size versus footprint size.
And the rule for the footprint has no exceptions. Measured bounding box equals 2f at every even factor and 2f − 1 at every odd factor, from 2× to 16×:
| f | measured bbox | 2f | 2f − 1 | rule holds |
|---|---|---|---|---|
| 2 | 4 | 4 | — | ✓ |
| 3 | 5 | — | 5 | ✓ |
| 4 | 8 | 8 | — | ✓ |
| 5 | 9 | — | 9 | ✓ |
| 6 | 12 | 12 | — | ✓ |
| 8 | 16 | 16 | — | ✓ |
| 10 | 20 | 20 | — | ✓ |
| 16 | 32 | 32 | — | ✓ |
Eight factors, zero exceptions. The odd-factor case is one pixel narrower because the output centre lands on the source sample and the two nearest neighbours are symmetric, so the bilinear support spans f − 1 on each side plus the centre, rather than f on each side. This is documented sediment of coordinate convention; the bilinear interpolation article has the general form, and the sampling-offset detail is the part everyone gets wrong when they assume "one pixel becomes f × f".
So: if you insist on repairing after the resize, your median window must be at least 2f + 1 per side to be able to see past the footprint — which for 8× means a 17×17 median, which will demolish every real edge in the frame. That is not a filter choice, that is a reason to move the repair upstream.
When you should not do any of this
Three cases where the right answer is to leave the pixels alone.
The "defects" are clipped highlights. My census found 754 genuinely black and 223 genuinely white pixels in 7 clean crops — specular detail and dark interiors, all of them real. Any median pass, before or after, will round them off. On a product shot with a chrome edge or a light source in frame, this is visible damage for no benefit.
The source is already clean and you are going to sharpen afterwards. On a clean frame the pre-filter costs 7.6 dB relative to doing nothing but the resize (
A342.86 vsB350.45, andDis by definition the truth). You would be paying for a repair with no defect to repair, and then adding edge enhancement on top of a slightly flattened image, which is how you get halos.You are upscaling to a fixed target and cannot change the order. If the deliverable is already produced — the resize happened upstream, the file is what it is — a 3×3 median at the output is worse than nothing at high factors, because it costs real detail and cannot reach the footprint. Either use
2f + 1and accept the cost, or escalate to the source.
The general principle that survives all of this: a filter should be applied where the defect is still small enough to be filtered. A point defect is one pixel at native resolution and f² worth of area after a resize. Every extra operation between the defect and the filter makes the filter's job worse by a factor that compounds.
If you need the resize itself done
The reason I ran this measurement at all is that the ordering of a repair relative to a resize is a pipeline decision, not a filter setting, and pipeline decisions are the ones that stay wrong for years. In production, resizing should happen once, at an integer factor where possible, from the best available source, and it should consume the cleaned input rather than output something a downstream stage has to fix.
If the resize half of that is what you need handled, the upscaler at upgolab.com runs the resize and picks a preset for the content type; the numbers above are the argument for doing your sensor cleanup before you hand it a file rather than after.
That 28-second clip is a pass through five presets on real product frames — direct link if the embed does not load. Watch what changes between presets and compare it to the peak table above: the presets differ mostly in how much local contrast survives, which is exactly the axis along which a hot pixel's peak moves from 55.9 % to 88.2 % depending on factor alone.
What the numbers said, including the part that insulted me
I set out to measure the repair radius a point defect needs after an upscale, expecting it to scale with the factor in some smooth way. Three things came back instead.
My first instrument was measuring clipped highlights, not noise. 977 genuinely clipped pixels in seven clean crops. It took a control I nearly skipped — count survivors on a frame where I injected nothing — to notice that the number I was reporting as a removal rate was substantially a measurement of the photographs.
The peak loss is arithmetic and the total energy loss is zero. (1 − 1/(2f))² predicts the measured peak fraction to within 0.0057 across 2×, 3×, 4×, 6× and 8×, and the sum of the deltas is f² to within 0.8 %. The "99.76 % removed" figure that would have made a much better headline is a statement about my threshold. Energy is conserved; only the peak moves; and at odd factors it does not move at all — 3× and 5× pass a hot pixel through at full 127-level contrast.
The pre-filter versus post-filter question has no universal answer and that is the answer. 7.6 dB in favour of post-filtering on a clean frame, 8.2 dB in favour of pre-filtering at 1 % density, crossover between 0.1 % and 0.2 %, and at 0.2 % the mean has crossed while only 4 of 7 images agree. The pre-filter's curve is flat within 0.53 dB across the entire density range; the post-filter's spans 16.3 dB. If you have to pick one without knowing the density, pick the one whose error does not depend on a parameter you cannot see.
Two cross-file checks, because they cost nothing and catch everything: the 0 %-density row in the crossover file reproduces the clean-cost file exactly (42.858 and 50.448 in both), and the peak fractions in nhip123_geom_exact.json reproduce the ones recorded independently by nhip112_impulse.json to 0.003. The campaign's four arm-level controls all pass: zero survivors on clean frames, bit-identical results on repeated runs, injection counts within 1.5σ of binomial expectation, and a known single impulse removed to 0.0 maximum absolute error by median3 at native.
Checklist
- [ ] Count clipped pixels in your own clean frames before trusting any defect detector. If 0.05 % of your samples are already at 0 or 255, a threshold-based detector is measuring your photographs
- [ ] Track defects by coordinate, not by brightness. Record the mask at injection time if you are testing, or use a coordinate-verified map if you are not
- [ ] Expect a footprint of
2f(even factor) or2f − 1(odd factor) per defect, notf - [ ] Expect peak contrast of
(1 − 1/(2f))²and total energy of exactlyf²— the defect is redistributed, never attenuated - [ ] Do not claim a defect is "removed" unless you state the threshold that defines removal
- [ ] Repair at native resolution when density is above ~0.3 %; the pre-filter's error stays within 0.6 dB across two decades of density
- [ ] If repairing after the resize, size the window at
2f + 1or larger, and check what that does to real edges first - [ ] Skip the median entirely on clean, high-contrast sources — 7.6 dB is a lot to spend on nothing
- [ ] Upscale once, at an integer factor, from the cleaned source, and treat the output as the file of record

