diff --git a/image_analysis/beam_stop/SHADOW_FINDER.md b/image_analysis/beam_stop/SHADOW_FINDER.md index fdcf4fa7f..69df8e270 100644 --- a/image_analysis/beam_stop/SHADOW_FINDER.md +++ b/image_analysis/beam_stop/SHADOW_FINDER.md @@ -81,12 +81,13 @@ own radius**, not to a global threshold. 7. **Reflection guard.** A pixel whose **max**-projection reached `min_reflection` recorded a real reflection — a beam stop cannot block a reflection that was measured — so it is **never masked** (a small-cluster test drops single-pixel zingers). -8. **Central disk (low-res guard).** From the azimuthal **blocked fraction** about the beam centre, - mask a full disk out to the radius where nearly every azimuth is blocked (a disk blocks all - directions; a thin arm/gap does not). Returns nothing when the centre is not behind a disk, and - is **capped just inside the innermost reflection** so a real edge spot is never swallowed. This - guards the low-resolution zone the stop makes un-measurable, including a disk the raw data has - already voided. +8. **Central disk (low-res guard).** Inside the stop a whole ring is blocked, so its own median is + blocked too and step 4 has nothing to compare against. Walking outward, every ring whose + background is below `blocked_ring_ratio` of **what this detector's background typically is** - + the median over the rings the walk is willing to judge - lies wholly inside the stop and is + shadow in its entirety. The comparison is deliberately **not** against the largest background + further out: a sample whose background peaks in a strong ring away from the beam (a powder + standard, a strong solvent ring) then reads as a stop the size of that ring. 9. **Soft boundary.** Grow the core+disk into partially-shadowed pixels (`ratio < penumbra_ratio`) within `penumbra_max_px`, then a morphological close rounds the contour and hole-filling closes the disk interior. diff --git a/image_analysis/beam_stop/ShadowFinder.cpp b/image_analysis/beam_stop/ShadowFinder.cpp index 34a60d610..28bb9f695 100644 --- a/image_analysis/beam_stop/ShadowFinder.cpp +++ b/image_analysis/beam_stop/ShadowFinder.cpp @@ -646,24 +646,32 @@ std::vector ShadowFinder::GetMask(size_t nthreads) const { if (valid[i]) ring_pixels[radius[i]]++; - // A ring lies inside the stop when its background is a fraction of the background further out. - // Counting statistics cannot decide this: on a bright dataset the shadow is still well counted. - // The comparison is only ever used to answer "is this whole ring inside the stop", never to - // judge an individual pixel, so taking the largest background over an outward window is safe - // here in a way it would not be per pixel. It is taken only over the rings this same walk is - // willing to judge, though: at the corner of the detector a ring holds a handful of pixels and - // its median is one pixel's mean, so one recorded reflection out there would otherwise become - // the background every ring inside it is compared against. - std::vector outward_max(max_radius + 2, 0.0f); - for (int rad = max_radius; rad >= 0; rad--) - outward_max[rad] = std::max(ring_pixels[rad] >= MIN_RING_PIXELS ? baseline[rad] : 0.0f, - outward_max[rad + 1]); + // A ring lies inside the stop when its background is a fraction of what this detector's + // background typically is. Counting statistics cannot decide this: on a bright dataset the + // shadow is still well counted. The comparison used to be against the LARGEST background of any + // ring further out, and that reads a sample whose background peaks in a strong ring away from + // the beam - a powder standard, a strong solvent ring - as a beam stop the size of that ring: + // the ordinary background inside it is legitimately below a third of the peak. On one corpus + // dataset it declared 16 % of the detector to be stop, with diffraction rings visible inside the + // disk it masked. The median over the rings this walk is willing to judge is what "typically" + // means, and it is robust from both sides - to a bright ring, and to a corner ring of a handful + // of pixels whose median is one pixel's mean. + std::vector judgeable; + for (int rad = 0; rad <= max_radius; rad++) + if (ring_pixels[rad] >= MIN_RING_PIXELS) + judgeable.push_back(baseline[rad]); + float typical_background = 0.0f; + if (!judgeable.empty()) { + const auto middle = judgeable.begin() + judgeable.size() / 2; + std::nth_element(judgeable.begin(), middle, judgeable.end()); + typical_background = *middle; + } int blocked_out_to = -1; for (int rad = 0; rad <= max_radius; rad++) { if (ring_pixels[rad] < MIN_RING_PIXELS) continue; - if (baseline[rad] >= BLOCKED_RING_RATIO * outward_max[rad]) + if (baseline[rad] >= BLOCKED_RING_RATIO * typical_background) break; blocked_out_to = rad; } diff --git a/tests/ShadowFinderTest.cpp b/tests/ShadowFinderTest.cpp index cfb489ff1..0efdb4c5f 100644 --- a/tests/ShadowFinderTest.cpp +++ b/tests/ShadowFinderTest.cpp @@ -338,3 +338,40 @@ TEST_CASE("ShadowFinder_APolarizedBackgroundIsNotAShadow", "[ShadowFinder]") { CHECK(mask_for(std::nullopt) > static_cast(W) * H / 20); } +// Inside the stop a whole ring is blocked, so its own median is blocked too and the per-pixel +// comparison has nothing to work with; the walk that finds those rings is what covers the disk. +// It decides by comparing a ring against what this detector's background typically is - and NOT +// against the brightest ring anywhere further out, which a sample whose background peaks well away +// from the beam turns into a beam stop the size of that peak. The scene here is that: an ordinary +// stop, and a strong ring at four times the background far outside it. What comes back has to be +// the stop, not a disk reaching the ring. +TEST_CASE("ShadowFinder_ABrightRingIsNotABeamStop", "[ShadowFinder]") { + constexpr int RING_R = 100, RING_HALF = 2, RING_LEVEL = 8 * BACKGROUND; + + const DiffractionExperiment x = TestExperiment(); + const PixelMask pixel_mask(x); + ShadowFinder finder(x, pixel_mask); + + std::vector> frames; + std::vector buffer; + for (int f = 0; f < NFRAMES; f++) { + frames.push_back(Scene(/*cross=*/false, /*reflection=*/false)); + for (int y = 0; y < H; y++) + for (int xi = 0; xi < W; xi++) { + const double r = std::hypot(xi - C, y - C); + if (std::abs(r - RING_R) <= RING_HALF) + frames.back()[I(xi, y)] = RING_LEVEL; + } + DataMessage msg{}; + msg.image = CompressedImage(frames.back(), W, H); + finder.AddImage(msg, buffer); + } + + const auto mask = finder.GetMask(); + CHECK(mask[I(C, C)] == 1); // the stop is still covered + CHECK(mask[I(C, C - STOP_R - 20)] == 0); // lit detector between the stop and the ring + CHECK(mask[I(C, C - RING_R + 5)] == 0); + CHECK(mask[I(C, C - RING_R)] == 0); // and the ring itself + // Anything much beyond the stop, its arm and their penumbra means the walk ran away. + CHECK(std::count(mask.begin(), mask.end(), 1u) < 6000); +}