ImageSpotFinder: Work in progress to combine ImageSpotFinders

This commit is contained in:
2025-10-04 21:15:15 +02:00
parent 57f2d05b32
commit e643513bfe
10 changed files with 177 additions and 193 deletions
+47 -32
View File
@@ -5,8 +5,8 @@
#ifdef JFJOCH_USE_CUDA
#include "../image_analysis/spot_finding//ImageAnalysisGPU.h"
#include "../image_analysis/spot_finding/ImageSpotFinder.h"
#include "../image_analysis/spot_finding/ImageSpotFinderGPU.h"
static void fill_test_image(std::vector<int32_t>& input, size_t width, size_t height) {
input.resize(width * height);
for (size_t i = 0; i < width * height; i++)
@@ -19,43 +19,26 @@ static void fill_test_image(std::vector<int32_t>& input, size_t width, size_t he
// Helper to run GPU and get DiffractionSpot list via StrongPixelSet -> FindSpotsImage
static std::vector<DiffractionSpot> run_gpu_and_collect_spots(const std::vector<int32_t>& input,
size_t width, size_t height,
const SpotFindingSettings& settings)
const SpotFindingSettings& settings,
const std::vector<bool>& res_mask)
{
ImageSpotFinderGPU gpu(static_cast<int32_t>(width), static_cast<int32_t>(height));
REQUIRE(ImageSpotFinderGPU::GPUPresent());
// Set input buffer pointer to our CPU data, register and upload
// Note: RegisterBuffer/UnregisterBuffer are optional for plain memcpy path,
// but we use them to mirror intended flow.
const_cast<ImageSpotFinderGPU&>(gpu).SetInputBuffer((void*)input.data());
gpu.RegisterBuffer();
gpu.LoadDataToGPU();
// Run kernel
gpu.RunSpotFinder(settings);
// Collect strong pixels and convert to spots like CPU does
StrongPixelSet strong;
gpu.GetSpotFinderResults(strong);
std::vector<DiffractionSpot> spots;
strong.FindSpotsImage(settings, spots);
gpu.UnregisterBuffer();
return spots;
memcpy(gpu.GetHostBuffer(), input.data(), width * height * sizeof(int32_t));
return gpu.Run(settings, res_mask);
}
// Mirror of ImageSpotFinder_SignalToNoise
TEST_CASE("GPUImageAnalysis_SignalToNoise") {
TEST_CASE("ImageSpotFinderGPU_SignalToNoise") {
if (!ImageSpotFinderGPU::GPUPresent()) {
WARN("No CUDA GPU present. Skipping GPUImageAnalysis_SignalToNoise");
WARN("No CUDA GPU present. Skipping ImageSpotFinderGPU_SignalToNoise");
return;
}
const size_t width = 100, height = 100;
std::vector<float> resolution(width * height, 2.0f);
std::vector<bool> res_mask(width * height, false);
std::vector<bool> mask(width * height, false);
resolution[width * 50 + 50] = 1.0f;
std::vector<int32_t> input;
fill_test_image(input, width, height);
@@ -72,23 +55,22 @@ TEST_CASE("GPUImageAnalysis_SignalToNoise") {
// GPU produces strong pixels; FindSpotsImage uses mask/resolution implicit in StrongPixelSet.
// StrongPixelSet doesn't carry resolution/mask by itself, but FindSpotsImage(settings, vec)
// matches CPU ImageSpotFinder test behavior for these synthetic inputs.
auto spots = run_gpu_and_collect_spots(input, width, height, settings);
auto spots = run_gpu_and_collect_spots(input, width, height, settings, res_mask);
REQUIRE(spots.size() == 2);
REQUIRE(spots[0].RawCoord().y == 25);
REQUIRE(spots[1].RawCoord().y == 50);
}
TEST_CASE("GPUImageAnalysis_CountThreshold") {
TEST_CASE("ImageSpotFinderGPU_CountThreshold") {
if (!ImageSpotFinderGPU::GPUPresent()) {
WARN("No CUDA GPU present. Skipping GPUImageAnalysis_SignalToNoise");
WARN("No CUDA GPU present. Skipping ImageSpotFinderGPU_SignalToNoise");
return;
}
const size_t width = 100, height = 100;
std::vector<float> resolution(width * height, 2.0f);
std::vector<bool> res_mask(width * height, false);
std::vector<bool> mask(width * height, false);
resolution[width * 50 + 50] = 1.0f;
std::vector<int32_t> input;
fill_test_image(input, width, height);
@@ -105,7 +87,7 @@ TEST_CASE("GPUImageAnalysis_CountThreshold") {
// GPU produces strong pixels; FindSpotsImage uses mask/resolution implicit in StrongPixelSet.
// StrongPixelSet doesn't carry resolution/mask by itself, but FindSpotsImage(settings, vec)
// matches CPU ImageSpotFinder test behavior for these synthetic inputs.
auto spots = run_gpu_and_collect_spots(input, width, height, settings);
auto spots = run_gpu_and_collect_spots(input, width, height, settings, res_mask);
REQUIRE(spots.size() == 3);
REQUIRE(spots[0].RawCoord().y == 25);
@@ -113,4 +95,37 @@ TEST_CASE("GPUImageAnalysis_CountThreshold") {
REQUIRE(spots[2].RawCoord().y == 75);
}
TEST_CASE("ImageSpotFinderGPU_20M") {
if (!ImageSpotFinderGPU::GPUPresent()) {
WARN("No CUDA GPU present. Skipping ImageSpotFinderGPU_SignalToNoise");
return;
}
const size_t width = 4500, height = 4500;
std::vector<bool> res_mask(width * height, false);
std::vector<bool> mask(width * height, false);
std::vector<int32_t> input;
fill_test_image(input, width, height);
SpotFindingSettings settings{
.signal_to_noise_threshold = 3.0,
.photon_count_threshold = 0,
.min_pix_per_spot = 1,
.max_pix_per_spot = 20,
.high_resolution_limit = 0.5,
.low_resolution_limit = 3.0,
};
// GPU produces strong pixels; FindSpotsImage uses mask/resolution implicit in StrongPixelSet.
// StrongPixelSet doesn't carry resolution/mask by itself, but FindSpotsImage(settings, vec)
// matches CPU ImageSpotFinder test behavior for these synthetic inputs.
auto spots = run_gpu_and_collect_spots(input, width, height, settings, res_mask);
REQUIRE(spots.size() == 2);
REQUIRE(spots[0].RawCoord().y == 25);
REQUIRE(spots[1].RawCoord().y == 50);
}
#endif