mirror of
https://github.com/slsdetectorgroup/aare.git
synced 2025-06-06 21:00:41 +02:00
Merge pull request #22 from slsdetectorgroup/fix/move-view-to-frame
move view to frame
This commit is contained in:
commit
783c355784
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@ -96,7 +96,8 @@
|
|||||||
"source_location": "cpp",
|
"source_location": "cpp",
|
||||||
"future": "cpp",
|
"future": "cpp",
|
||||||
"typeindex": "cpp",
|
"typeindex": "cpp",
|
||||||
"*.in": "c"
|
"*.in": "c",
|
||||||
|
"stdfloat": "cpp"
|
||||||
},
|
},
|
||||||
"C_Cpp.errorSquiggles": "enabled"
|
"C_Cpp.errorSquiggles": "enabled"
|
||||||
}
|
}
|
@ -1,8 +1,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include "aare/NDView.hpp"
|
||||||
#include "aare/defs.hpp"
|
#include "aare/defs.hpp"
|
||||||
#include <memory>
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -16,36 +18,35 @@ class Frame {
|
|||||||
ssize_t m_rows;
|
ssize_t m_rows;
|
||||||
ssize_t m_cols;
|
ssize_t m_cols;
|
||||||
ssize_t m_bitdepth;
|
ssize_t m_bitdepth;
|
||||||
std::byte* m_data;
|
std::byte *m_data;
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Frame(ssize_t rows, ssize_t cols,ssize_t m_bitdepth);
|
Frame(ssize_t rows, ssize_t cols, ssize_t m_bitdepth);
|
||||||
Frame(std::byte *fp, ssize_t rows, ssize_t cols,ssize_t m_bitdepth);
|
Frame(std::byte *fp, ssize_t rows, ssize_t cols, ssize_t m_bitdepth);
|
||||||
std::byte* get(int row, int col);
|
std::byte *get(int row, int col);
|
||||||
template <typename T>
|
template <typename T> void set(int row, int col, T data);
|
||||||
void set(int row, int col,T data);
|
|
||||||
// std::vector<std::vector<DataType>> get_array();
|
// std::vector<std::vector<DataType>> get_array();
|
||||||
ssize_t rows() const{
|
ssize_t rows() const { return m_rows; }
|
||||||
return m_rows;
|
ssize_t cols() const { return m_cols; }
|
||||||
}
|
ssize_t bitdepth() const { return m_bitdepth; }
|
||||||
ssize_t cols() const{
|
std::byte *_get_data() { return m_data; }
|
||||||
return m_cols;
|
Frame &operator=(Frame &other) {
|
||||||
}
|
m_rows = other.rows();
|
||||||
ssize_t bitdepth() const{
|
m_cols = other.cols();
|
||||||
return m_bitdepth;
|
m_bitdepth = other.bitdepth();
|
||||||
}
|
m_data = new std::byte[m_rows * m_cols * m_bitdepth / 8];
|
||||||
std::byte* _get_data(){
|
std::memcpy(m_data, other.m_data, m_rows * m_cols * m_bitdepth / 8);
|
||||||
return m_data;
|
|
||||||
}
|
|
||||||
Frame& operator=(Frame& other){
|
|
||||||
m_rows = other.rows();
|
|
||||||
m_cols = other.cols();
|
|
||||||
m_bitdepth = other.bitdepth();
|
|
||||||
m_data = new std::byte[m_rows*m_cols*m_bitdepth/8];
|
|
||||||
std::memcpy(m_data, other.m_data, m_rows*m_cols*m_bitdepth/8);
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
NDView<T> view() {
|
||||||
|
std::vector<ssize_t> shape = {m_rows, m_cols};
|
||||||
|
T* data = reinterpret_cast<T *>(m_data);
|
||||||
|
return NDView<T>(data, shape);
|
||||||
|
}
|
||||||
|
|
||||||
~Frame() { delete[] m_data; }
|
~Frame() { delete[] m_data; }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
#include<aare/Frame.hpp>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
@ -31,18 +30,18 @@ template <ssize_t Ndim> std::array<ssize_t, Ndim> make_array(const std::vector<s
|
|||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, ssize_t Ndim> class DataSpan {
|
template <typename T, ssize_t Ndim=2> class NDView {
|
||||||
public:
|
public:
|
||||||
DataSpan(){};
|
NDView(){};
|
||||||
|
|
||||||
DataSpan(T* buffer, std::array<ssize_t, Ndim> shape) {
|
NDView(T* buffer, std::array<ssize_t, Ndim> shape) {
|
||||||
buffer_ = buffer;
|
buffer_ = buffer;
|
||||||
strides_ = c_strides<Ndim>(shape);
|
strides_ = c_strides<Ndim>(shape);
|
||||||
shape_ = shape;
|
shape_ = shape;
|
||||||
size_ = std::accumulate(std::begin(shape), std::end(shape), 1, std::multiplies<ssize_t>());
|
size_ = std::accumulate(std::begin(shape), std::end(shape), 1, std::multiplies<ssize_t>());
|
||||||
}
|
}
|
||||||
|
|
||||||
DataSpan(T *buffer, const std::vector<ssize_t> &shape) {
|
NDView(T *buffer, const std::vector<ssize_t> &shape) {
|
||||||
buffer_ = buffer;
|
buffer_ = buffer;
|
||||||
strides_ = c_strides<Ndim>(make_array<Ndim>(shape));
|
strides_ = c_strides<Ndim>(make_array<Ndim>(shape));
|
||||||
shape_ = make_array<Ndim>(shape);
|
shape_ = make_array<Ndim>(shape);
|
||||||
@ -50,14 +49,6 @@ template <typename T, ssize_t Ndim> class DataSpan {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DataSpan(Frame& frame) {
|
|
||||||
assert(Ndim == 2);
|
|
||||||
buffer_ = reinterpret_cast<T*>(frame._get_data());
|
|
||||||
strides_ = c_strides<Ndim>({frame.rows(), frame.cols()});
|
|
||||||
shape_ = {frame.rows(), frame.cols()};
|
|
||||||
size_ = frame.rows() * frame.cols();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename... Ix> typename std::enable_if<sizeof...(Ix) == Ndim, T &>::type operator()(Ix... index) {
|
template <typename... Ix> typename std::enable_if<sizeof...(Ix) == Ndim, T &>::type operator()(Ix... index) {
|
||||||
return buffer_[element_offset(strides_, index...)];
|
return buffer_[element_offset(strides_, index...)];
|
||||||
}
|
}
|
||||||
@ -68,28 +59,28 @@ template <typename T, ssize_t Ndim> class DataSpan {
|
|||||||
|
|
||||||
ssize_t size() const { return size_; }
|
ssize_t size() const { return size_; }
|
||||||
|
|
||||||
DataSpan(const DataSpan &) = default;
|
NDView(const NDView &) = default;
|
||||||
DataSpan(DataSpan &&) = default;
|
NDView(NDView &&) = default;
|
||||||
|
|
||||||
T *begin() { return buffer_; }
|
T *begin() { return buffer_; }
|
||||||
T *end() { return buffer_ + size_; }
|
T *end() { return buffer_ + size_; }
|
||||||
T &operator()(ssize_t i) { return buffer_[i]; }
|
T &operator()(ssize_t i) { return buffer_[i]; }
|
||||||
T &operator[](ssize_t i) { return buffer_[i]; }
|
T &operator[](ssize_t i) { return buffer_[i]; }
|
||||||
|
|
||||||
DataSpan &operator+=(const T val) { return elemenwise(val, std::plus<T>()); }
|
NDView &operator+=(const T val) { return elemenwise(val, std::plus<T>()); }
|
||||||
DataSpan &operator-=(const T val) { return elemenwise(val, std::minus<T>()); }
|
NDView &operator-=(const T val) { return elemenwise(val, std::minus<T>()); }
|
||||||
DataSpan &operator*=(const T val) { return elemenwise(val, std::multiplies<T>()); }
|
NDView &operator*=(const T val) { return elemenwise(val, std::multiplies<T>()); }
|
||||||
DataSpan &operator/=(const T val) { return elemenwise(val, std::divides<T>()); }
|
NDView &operator/=(const T val) { return elemenwise(val, std::divides<T>()); }
|
||||||
|
|
||||||
DataSpan &operator/=(const DataSpan &other) { return elemenwise(other, std::divides<T>()); }
|
NDView &operator/=(const NDView &other) { return elemenwise(other, std::divides<T>()); }
|
||||||
|
|
||||||
DataSpan &operator=(const T val) {
|
NDView &operator=(const T val) {
|
||||||
for (auto it = begin(); it != end(); ++it)
|
for (auto it = begin(); it != end(); ++it)
|
||||||
*it = val;
|
*it = val;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
DataSpan &operator=(const DataSpan &other) {
|
NDView &operator=(const NDView &other) {
|
||||||
shape_ = other.shape_;
|
shape_ = other.shape_;
|
||||||
strides_ = other.strides_;
|
strides_ = other.strides_;
|
||||||
size_ = other.size_;
|
size_ = other.size_;
|
||||||
@ -107,13 +98,13 @@ template <typename T, ssize_t Ndim> class DataSpan {
|
|||||||
std::array<ssize_t, Ndim> shape_{};
|
std::array<ssize_t, Ndim> shape_{};
|
||||||
ssize_t size_{};
|
ssize_t size_{};
|
||||||
|
|
||||||
template <class BinaryOperation> DataSpan &elemenwise(T val, BinaryOperation op) {
|
template <class BinaryOperation> NDView &elemenwise(T val, BinaryOperation op) {
|
||||||
for (ssize_t i = 0; i != size_; ++i) {
|
for (ssize_t i = 0; i != size_; ++i) {
|
||||||
buffer_[i] = op(buffer_[i], val);
|
buffer_[i] = op(buffer_[i], val);
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
template <class BinaryOperation> DataSpan &elemenwise(const DataSpan &other, BinaryOperation op) {
|
template <class BinaryOperation> NDView &elemenwise(const NDView &other, BinaryOperation op) {
|
||||||
for (ssize_t i = 0; i != size_; ++i) {
|
for (ssize_t i = 0; i != size_; ++i) {
|
||||||
buffer_[i] = op(buffer_[i], other.buffer_[i]);
|
buffer_[i] = op(buffer_[i], other.buffer_[i]);
|
||||||
}
|
}
|
||||||
@ -121,5 +112,5 @@ template <typename T, ssize_t Ndim> class DataSpan {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template class DataSpan<uint16_t, 2>;
|
template class NDView<uint16_t, 2>;
|
||||||
|
|
@ -30,12 +30,12 @@ template <typename T> class ClusterFinder {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
const std::array<ssize_t, 2> shape_;
|
const std::array<ssize_t, 2> shape_;
|
||||||
DataSpan<T, 2> original_;
|
NDView<T, 2> original_;
|
||||||
ImageData<int, 2> labeled_;
|
ImageData<int, 2> labeled_;
|
||||||
ImageData<int, 2> peripheral_labeled_;
|
ImageData<int, 2> peripheral_labeled_;
|
||||||
ImageData<bool, 2> binary_; // over threshold flag
|
ImageData<bool, 2> binary_; // over threshold flag
|
||||||
T threshold_;
|
T threshold_;
|
||||||
DataSpan<T, 2> noiseMap;
|
NDView<T, 2> noiseMap;
|
||||||
bool use_noise_map = false;
|
bool use_noise_map = false;
|
||||||
int peripheralThresholdFactor_ = 5;
|
int peripheralThresholdFactor_ = 5;
|
||||||
int current_label;
|
int current_label;
|
||||||
@ -58,12 +58,12 @@ template <typename T> class ClusterFinder {
|
|||||||
|
|
||||||
ImageData<int, 2> labeled() { return labeled_; }
|
ImageData<int, 2> labeled() { return labeled_; }
|
||||||
|
|
||||||
void set_noiseMap(DataSpan<T, 2> noise_map) { noiseMap = noise_map; use_noise_map = true; }
|
void set_noiseMap(NDView<T, 2> noise_map) { noiseMap = noise_map; use_noise_map = true; }
|
||||||
void set_peripheralThresholdFactor(int factor) { peripheralThresholdFactor_ = factor; }
|
void set_peripheralThresholdFactor(int factor) { peripheralThresholdFactor_ = factor; }
|
||||||
void find_clusters(DataSpan<T, 2> img);
|
void find_clusters(NDView<T, 2> img);
|
||||||
void find_clusters_X(DataSpan<T, 2> img);
|
void find_clusters_X(NDView<T, 2> img);
|
||||||
void rec_FillHit(int clusterIndex, int i, int j);
|
void rec_FillHit(int clusterIndex, int i, int j);
|
||||||
void single_pass(DataSpan<T, 2> img);
|
void single_pass(NDView<T, 2> img);
|
||||||
void first_pass();
|
void first_pass();
|
||||||
void second_pass();
|
void second_pass();
|
||||||
void store_clusters();
|
void store_clusters();
|
||||||
@ -144,7 +144,7 @@ template <typename T> int ClusterFinder<T>::check_neighbours(int i, int j) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> void ClusterFinder<T>::find_clusters(DataSpan<T, 2> img) {
|
template <typename T> void ClusterFinder<T>::find_clusters(NDView<T, 2> img) {
|
||||||
original_ = img;
|
original_ = img;
|
||||||
labeled_ = 0;
|
labeled_ = 0;
|
||||||
peripheral_labeled_ = 0;
|
peripheral_labeled_ = 0;
|
||||||
@ -156,7 +156,7 @@ template <typename T> void ClusterFinder<T>::find_clusters(DataSpan<T, 2> img) {
|
|||||||
store_clusters();
|
store_clusters();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> void ClusterFinder<T>::find_clusters_X(DataSpan<T, 2> img) {
|
template <typename T> void ClusterFinder<T>::find_clusters_X(NDView<T, 2> img) {
|
||||||
original_ = img;
|
original_ = img;
|
||||||
int clusterIndex = 0;
|
int clusterIndex = 0;
|
||||||
for (int i = 0; i < shape_[0]; ++i) {
|
for (int i = 0; i < shape_[0]; ++i) {
|
||||||
@ -214,7 +214,7 @@ template <typename T> void ClusterFinder<T>::rec_FillHit(int clusterIndex, int i
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> void ClusterFinder<T>::single_pass(DataSpan<T, 2> img) {
|
template <typename T> void ClusterFinder<T>::single_pass(NDView<T, 2> img) {
|
||||||
original_ = img;
|
original_ = img;
|
||||||
labeled_ = 0;
|
labeled_ = 0;
|
||||||
current_label = 0;
|
current_label = 0;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <aare/DataSpan.hpp>
|
#include <aare/NDView.hpp>
|
||||||
#include <aare/Frame.hpp>
|
#include <aare/Frame.hpp>
|
||||||
#include <catch2/catch_test_macros.hpp>
|
#include <catch2/catch_test_macros.hpp>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
@ -22,20 +22,20 @@ TEST_CASE("Frame") {
|
|||||||
delete[] data;
|
delete[] data;
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("DataSpan") {
|
TEST_CASE("NDView") {
|
||||||
auto data = new uint16_t[100];
|
auto data = new uint16_t[100];
|
||||||
for (int i = 0; i < 100; i++) {
|
for (int i = 0; i < 100; i++) {
|
||||||
data[i] = i;
|
data[i] = i;
|
||||||
}
|
}
|
||||||
SECTION("constructors") {
|
SECTION("constructors") {
|
||||||
DataSpan<uint16_t, 2> ds(data, std::vector<ssize_t>({10, 10}));
|
NDView<uint16_t, 2> ds(data, std::vector<ssize_t>({10, 10}));
|
||||||
for (int i = 0; i < 100; i++) {
|
for (int i = 0; i < 100; i++) {
|
||||||
REQUIRE(ds(i / 10, i % 10) == data[i]);
|
REQUIRE(ds(i / 10, i % 10) == data[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SECTION("from Frame") {
|
SECTION("from Frame") {
|
||||||
Frame f(reinterpret_cast<std::byte *>(data), 10, 10, 16);
|
Frame f(reinterpret_cast<std::byte *>(data), 10, 10, 16);
|
||||||
DataSpan<uint16_t, 2> ds(f);
|
NDView<uint16_t> ds = f.view<uint16_t>();
|
||||||
for (int i = 0; i < 100; i++) {
|
for (int i = 0; i < 100; i++) {
|
||||||
REQUIRE(ds(i / 10, i % 10) == data[i]);
|
REQUIRE(ds(i / 10, i % 10) == data[i]);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user