The mapping took its width and height from the CONVERTED geometry unconditionally, while pixel_to_bin is sized per mode: converted when the geometry is transformed, raw module layout when it is not. In raw mode the two disagreed - 2068x2162 reported against a 1024x4096 map on a JF4M. Only the adaptive spot finders read those dimensions, and they read them for exactly the thing that breaks: the CPU finder derives npix = w*h and then indexes the image, pixel_to_bin and the resolution mask with it, so it walked ~277k pixels past the end of all three; the GPU finder stays in bounds but decodes the strong-pixel bit index with the wrong row stride and reports spots at wrong coordinates. Nothing combines raw geometry with adaptive detection today, so this was latent rather than live. Take them from GetXPixelsNum()/GetYPixelsNum(), which already follow the geometry mode. The converted path is unchanged - it is the same number there - and every internal use is inside SetupConvGeom, which only runs when the geometry is transformed. Covered by two tests: the mapping's dimensions must match pixel_to_bin in both modes, and the CPU adaptive finder must return a spot planted on a raw-geometry image at that raw pixel. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
76 lines
2.8 KiB
C++
76 lines
2.8 KiB
C++
// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#include <catch2/catch_all.hpp>
|
|
|
|
#include "../common/AzimuthalIntegrationMapping.h"
|
|
#include "../image_analysis/spot_finding/AdaptiveSpotFinderCPU.h"
|
|
|
|
namespace {
|
|
|
|
SpotFindingSettings AdaptiveSettings() {
|
|
SpotFindingSettings s{};
|
|
s.adaptive_threshold = true;
|
|
s.false_pixels_per_frame = 100.0f;
|
|
s.min_pix_per_spot = 1;
|
|
s.max_pix_per_spot = 50;
|
|
s.high_resolution_limit = 0.0f;
|
|
s.low_resolution_limit = 1.0e6f;
|
|
s.high_res_gap_Q_recipA = std::nullopt;
|
|
return s;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
// Raw (untransformed) geometry: the mapping is built over the raw module layout, which is smaller
|
|
// than the converted image. The finder has to walk the raw image, so a spot planted at a raw pixel
|
|
// comes back at that pixel.
|
|
TEST_CASE("AdaptiveSpotFinderCPU_RawGeometry", "[AdaptiveSpotFinder]") {
|
|
DiffractionExperiment x(DetJF4M());
|
|
x.DetectorDistance_mm(80).BeamX_pxl(1030).BeamY_pxl(1080);
|
|
x.QSpacingForAzimInt_recipA(0.05).QRangeForAzimInt_recipA(0.05, 5.0);
|
|
x.GeometryTransformation(false);
|
|
|
|
PixelMask pixel_mask(x);
|
|
AzimuthalIntegrationMapping mapping(x, pixel_mask);
|
|
|
|
const size_t w = x.GetXPixelsNum();
|
|
const size_t h = x.GetYPixelsNum();
|
|
REQUIRE(w * h == mapping.GetPixelToBin().size());
|
|
REQUIRE(w * h < static_cast<size_t>(x.GetPixelsNumConv()));
|
|
|
|
ImagePreprocessorBuffer buffer(x.GetPixelsNum());
|
|
for (size_t i = 0; i < w * h; i++)
|
|
buffer[i] = 8 + static_cast<int32_t>(i % 5); // background 8..12
|
|
|
|
// A 3x3 blob on a pixel that has a ring - with all of its neighbours on one too.
|
|
const auto &pixel_to_bin = mapping.GetPixelToBin();
|
|
size_t spot_row = 0, spot_col = 0;
|
|
for (size_t row = 100; row < h - 100 && spot_row == 0; row++) {
|
|
for (size_t col = 100; col < w - 100; col++) {
|
|
bool all_binned = true;
|
|
for (int dr = -1; dr <= 1; dr++)
|
|
for (int dc = -1; dc <= 1; dc++)
|
|
all_binned &= pixel_to_bin[(row + dr) * w + col + dc] != UINT16_MAX;
|
|
if (all_binned) {
|
|
spot_row = row;
|
|
spot_col = col;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
REQUIRE(spot_row > 0);
|
|
|
|
for (int dr = -1; dr <= 1; dr++)
|
|
for (int dc = -1; dc <= 1; dc++)
|
|
buffer[(spot_row + dr) * w + spot_col + dc] = 200;
|
|
|
|
std::vector<bool> res_mask(x.GetPixelsNum(), false);
|
|
AdaptiveSpotFinderCPU finder(mapping);
|
|
const auto spots = finder.Run(buffer, AdaptiveSettings(), res_mask);
|
|
|
|
REQUIRE(spots.size() == 1);
|
|
CHECK(std::lround(spots[0].RawCoord().x) == static_cast<long>(spot_col));
|
|
CHECK(std::lround(spots[0].RawCoord().y) == static_cast<long>(spot_row));
|
|
}
|