Scope
ScopeHistogram.cpp
1 #include "StdAfx.h"
2 #include "ScopeHistogram.h"
3 #include "Pixel.h"
4 #include "ScopeImage.h"
5 #include "helpers.h"
6 
7 namespace scope {
8 
9 ScopeHistogram::ScopeHistogram(const uint32_t& _no_of_bins, const uint16_t& _range)
10  : range(_range)
11  , binsize(static_cast<double>(range-0+1)/_no_of_bins)
12  , hist(_no_of_bins, 0){
13 }
14 
16  : range(_h.range)
17  , binsize(_h.Binsize())
18  , hist(*_h.GetHistConst()) {
19  _h.ReleaseHistConst();
20 }
21 
22 double ScopeHistogram::Binsize() const {
23  std::lock_guard<std::mutex> lock(mutex);
24  return binsize;
25 }
26 
27 void ScopeHistogram::Calculate(ScopeImageU16CPtr const _img, const bool& _loghistogram) {
28  std::lock_guard<std::mutex> lock(mutex);
29 
30  // Initialize histogram with 0s
31  std::fill(std::begin(hist), std::end(hist), 0);
32 
33  const uint16_t low(0);
34  const uint16_t high(range);
35 
36  // Do the histogram
37  // With const we have shared access to the image data, good for parallelism...
38  {
39  ScopeImageConstAccessU16 imagedata(*_img);
40  std::for_each(std::begin(*imagedata.GetConstData()), std::end(*imagedata.GetConstData()), [&](const uint16_t& val) {
41  // The static_cast<size_t> does floor double -> fill in the lowest bin
42  if ( (val >= low) && (val <= high) )
43  ++( hist[ static_cast<size_t>( static_cast<double>(val-low)/binsize ) ] );
44  } );
45  }
46 
47  // Max count is 2^32 (~4E9), log of this is ~9.6. To put the (double) log values back into the uint32_t histogram
48  // we can safely multiply them with 100000 (we could go up to ~4E8)
49  if ( _loghistogram )
50  std::transform(std::begin(hist), std::end(hist), std::begin(hist), [](uint32_t h) {
51  return static_cast<uint32_t>(100000 * std::log10(static_cast<double>(h)));
52  } );
53 }
54 
55 void ScopeHistogram::Resize(const uint32_t& _no_of_bins) {
56  std::lock_guard<std::mutex> lock(mutex);
57  assert(_no_of_bins <= static_cast<uint32_t>(range-0));
58  // There are +1 number of possible values (including 0)
59  binsize = static_cast<double>(range-0+1)/_no_of_bins;
60  hist.resize(_no_of_bins, 0);
61 }
62 
63 uint32_t ScopeHistogram::MaxCount() const {
64  std::lock_guard<std::mutex> lock(mutex);
65  return *std::max_element(std::begin(hist), std::end(hist));
66 }
67 
69  std::lock_guard<std::mutex> lock(mutex);
70  auto first = std::find_if(std::begin(hist), std::end(hist), [&](const uint32_t& c) { return c > 0; } );
71  if ( first != hist.end() ) {
72  // get position from iterator, multiply to get corresponding value
73  return static_cast<uint16_t>(binsize * (first - std::begin(hist)));
74  }
75  return 0;
76 }
77 
79  std::lock_guard<std::mutex> lock(mutex);
80  auto last = std::find_if(hist.rbegin(), hist.rend(), [&](const uint32_t& c) { return c > 0; } );
81  if ( last != hist.rend() ) {
82  // get position from iterator, multiply to get corresponding value. Note: -1 because uint16_t starts at zero...
83  return static_cast<uint16_t>(binsize * static_cast<double>(hist.rend() - last) - 1 );
84  }
85  return 0;
86 }
87 
88 const std::vector<uint32_t>* ScopeHistogram::GetHistConst() const {
89  mutex.lock();
90  return &hist;
91 }
92 
94  mutex.unlock();
95 }
96 
97 
98 }
A histogram for a uint16_t image with uint32_t counts.
Definition: ScopeHistogram.h:9
const uint16_t range
range of uint16 to do histogram of
double Binsize() const
uint16_t FirstCountPosition() const
double binsize
size of each bin
The BGRA8Pixel struct and various related helpers for color conversions.
This is the include file for standard system include files, or project specific include files that ar...
void ReleaseHistConst() const
uint16_t LastCountPosition() const
std::vector< uint32_t > hist
data vector
const std::vector< uint32_t > * GetHistConst() const
ScopeHistogram(const uint32_t &_no_of_bins=512, const uint16_t &_range=UINT16_MAX)
Initialize to binsize 1 (histogram size is thus UINT16_MAX+1) and zero counts.
const std::vector< T > * GetConstData() const
Definition: ScopeImage.h:247
void Resize(const uint32_t &_no_of_bins)
Resize the histogram to a new number of bins.
void Calculate(ScopeImageU16CPtr const _img, const bool &_loghistogram=false)
Calculate the histogram.
std::mutex mutex
for data protection
Various helper functions and classes for Scope.
Gives RAII safe const access (read-only) to the pixeldata of a ScopeImage.
Definition: ScopeImage.h:14
uint32_t MaxCount() const