78 lines
2.6 KiB
C++
78 lines
2.6 KiB
C++
// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#include <bitset>
|
|
|
|
#include "ImageSpotFinder.h"
|
|
#include "StrongPixelSet.h"
|
|
|
|
ImageSpotFinder::ImageSpotFinder(int32_t width, int32_t height)
|
|
: width(width), height(height), input_buffer(width * height), output_buffer(width * height / 32 + 1) {}
|
|
|
|
size_t ImageSpotFinder::OutputSize() const {
|
|
return (width * height) / 32 + ((width * height % 32 != 0) ? 1 : 0);
|
|
}
|
|
|
|
size_t ImageSpotFinder::OutputByteSize() const {
|
|
return OutputSize() * sizeof(uint32_t);
|
|
}
|
|
|
|
std::vector<int32_t> &ImageSpotFinder::GetInputBuffer() {
|
|
return input_buffer;
|
|
}
|
|
|
|
std::vector<DiffractionSpot> ImageSpotFinder::ExtractSpots(const SpotFindingSettings &settings, const std::vector<bool> &res_mask) {
|
|
StrongPixelSet pixel_set;
|
|
|
|
for (int i = 0; i < OutputSize(); i++) {
|
|
if (output_buffer[i]) {
|
|
std::bitset<32> bset = output_buffer[i];
|
|
for (int bit = 0; bit < 32; bit++) {
|
|
if (bset.test(bit)) {
|
|
size_t npixel = i * 32 + bit;
|
|
size_t col = npixel % width;
|
|
size_t line = npixel / width;
|
|
if (line < height && res_mask[npixel] == 0)
|
|
pixel_set.AddStrongPixel(col, line, input_buffer[npixel]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
std::vector<DiffractionSpot> vec;
|
|
pixel_set.FindSpotsImage(settings, vec);
|
|
return vec;
|
|
}
|
|
|
|
void ImageSpotFinder::AzimIntegration(const AzimuthalIntegration &integration, AzimuthalIntegrationProfile &profile) {
|
|
const auto azint_bins = integration.GetBinNumber();
|
|
std::vector<float> azint_sum(azint_bins);
|
|
std::vector<float> azint_sum2(azint_bins);
|
|
std::vector<uint32_t> azint_count(azint_bins);
|
|
|
|
for (int i = 0; i < azint_count.size(); i++) {
|
|
azint_sum[i] = 0.0f;
|
|
azint_sum2[i] = 0.0f;
|
|
azint_count[i] = 0;
|
|
}
|
|
|
|
auto &pixel_to_bin = integration.GetPixelToBin();
|
|
auto &corrections = integration.Corrections();
|
|
|
|
if (pixel_to_bin.size() != width * height || corrections.size() != width * height)
|
|
throw std::runtime_error("ImageSpotFinder::AzimIntegration: Mismatch in size");
|
|
|
|
for (int i = 0; i < width * height; i++) {
|
|
const uint16_t bin = pixel_to_bin[i];
|
|
if (bin < azint_bins) {
|
|
float val = static_cast<float>(input_buffer[i]) * corrections[i];
|
|
azint_sum[bin] += val;
|
|
++azint_count[bin];
|
|
}
|
|
}
|
|
profile.Clear(integration);
|
|
profile.Add(azint_sum, azint_count);
|
|
}
|
|
|
|
|