86 lines
2.9 KiB
C++
86 lines
2.9 KiB
C++
// Copyright (2019-2024) Paul Scherrer Institute
|
|
|
|
#include "CPUSpotFinder.h"
|
|
|
|
template <int N>
|
|
void FindSpots(DeviceOutput &output,
|
|
int big_column, int big_row,
|
|
const SpotFindingSettings& settings,
|
|
const float *d_array) {
|
|
auto image = (int16_t *) output.pixels;
|
|
|
|
int64_t sum = 0;
|
|
int64_t sum2 = 0;
|
|
int64_t valid_count = 0;
|
|
|
|
for (int y = 0; y < N; y++) {
|
|
for (int x = 0; x < N; x++) {
|
|
size_t coord = (big_row * N + y) * RAW_MODULE_COLS + big_column * N + x;
|
|
if ((image[coord] != INT16_MIN) || (image[coord] != INT16_MAX)) {
|
|
sum += image[coord];
|
|
sum2 += image[coord] * image[coord];
|
|
valid_count++;
|
|
}
|
|
}
|
|
}
|
|
|
|
int64_t variance = valid_count * sum2 - sum * sum;
|
|
float threshold = variance * settings.signal_to_noise_threshold * settings.signal_to_noise_threshold;
|
|
|
|
for (int y = 0; y < N; y++) {
|
|
for (int x = 0; x < N; x++) {
|
|
size_t line = big_row * N + y;
|
|
size_t col = big_column * N + x;
|
|
size_t coord = line * RAW_MODULE_COLS + col;
|
|
|
|
bool strong_pixel = true;
|
|
|
|
if ((line == 255) || (line == 256)
|
|
|| (col == 255) || (col == 256)
|
|
|| (col == 511) || (col == 512)
|
|
|| (col == 767) || (col == 768))
|
|
strong_pixel = false;
|
|
|
|
if (d_array[coord] != 0) {
|
|
if ((d_array[coord] < settings.high_resolution_limit)
|
|
|| (d_array[coord] > settings.low_resolution_limit))
|
|
strong_pixel = false;
|
|
}
|
|
|
|
if ((settings.photon_count_threshold < 0)
|
|
&& (settings.signal_to_noise_threshold <= 0))
|
|
strong_pixel = false;
|
|
|
|
if ((settings.photon_count_threshold >= 0)
|
|
&& (image[coord] <= settings.photon_count_threshold)) {
|
|
strong_pixel = false;
|
|
}
|
|
|
|
if (image[coord] > 32760)
|
|
strong_pixel = false;
|
|
|
|
if (settings.signal_to_noise_threshold > 0) {
|
|
int64_t in_minus_mean = image[coord] * valid_count - sum;
|
|
if ((in_minus_mean * in_minus_mean <= threshold)
|
|
|| (in_minus_mean <= 0)
|
|
|| (valid_count < N * N / 2))
|
|
strong_pixel = false;
|
|
}
|
|
|
|
if (strong_pixel) {
|
|
output.spot_finding_result.strong_pixel[coord / 8] |= (1 << (coord % 8));
|
|
output.spot_finding_result.strong_pixel_count++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void FindSpots(DeviceOutput &output, const SpotFindingSettings& settings, const float *d_array) {
|
|
for (auto &i: output.spot_finding_result.strong_pixel)
|
|
i = 0;
|
|
|
|
for (int i = 0; i < RAW_MODULE_LINES / 32; i++) {
|
|
for (int j = 0; j < RAW_MODULE_COLS / 32; j++)
|
|
FindSpots<32>(output, j, i, settings, d_array);
|
|
}
|
|
} |