Ocean
BlackPointDetector.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_CV_ADVANCED_BLACK_POINT_DETECTOR_H
9 #define META_OCEAN_CV_ADVANCED_BLACK_POINT_DETECTOR_H
10 
12 
13 #include "ocean/base/Frame.h"
14 #include "ocean/base/Worker.h"
15 
16 #include "ocean/cv/Histogram.h"
17 
18 namespace Ocean
19 {
20 
21 namespace CV
22 {
23 
24 namespace Advanced
25 {
26 
27 /**
28  * Provides functions for black point detection in a frame.
29  * @ingroup cvadvanced
30  */
31 class OCEAN_CV_ADVANCED_EXPORT BlackPointDetector
32 {
33  public:
34 
35  /**
36  * Definition of a vector holding 32 bit floating point values.
37  */
38  typedef std::vector<float> Color;
39 
40  public:
41 
42  /**
43  * Attempts to detect the black point in the given frame with three channels.
44  * @param frame The frame in which the black-point will be detected, must be valid
45  * @param worker Optional worker object to distribute the computation
46  * @return The resulting color of the black points, one for each channel
47  * @return True, if the detection was successful
48  */
49  static Color detectBlackPointHistogram(const Frame& frame, Worker* worker = nullptr);
50 
51  /**
52  * Attempts to detect the black point in the given frame with three channels.
53  * @param frame The frame in which the black-point will be detected, must be valid
54  * @param width The width of the frame in pixel, with range [1, infinity)
55  * @param height The height of the frame in pixel, with range [1, infinity)
56  * @param framePaddingElements The number of padding elements at the end of each frame row, in elements, with range [0, infinity)
57  * @param worker Optional worker object to distribute the computation
58  * @return The resulting color of the black points, one for each channel
59  * @return True, if the detection was successful
60  */
61  template <unsigned int tChannels>
62  static Color detectBlackPointHistogram8BitPerChannel(const uint8_t* frame, const unsigned int width, const unsigned int height, const unsigned int framePaddingElements, Worker* worker = nullptr);
63 };
64 
65 template <unsigned int tChannels>
66 BlackPointDetector::Color BlackPointDetector::detectBlackPointHistogram8BitPerChannel(const uint8_t* frame, const unsigned int width, const unsigned int height, const unsigned int framePaddingElements, Worker* worker)
67 {
68  static_assert(tChannels != 0u, "Invalid channel number!");
69  ocean_assert(frame && width != 0u && height != 0u);
70 
71  Color blackPointColor(tChannels);
72 
73  const CV::Histogram::Histogram8BitPerChannel<tChannels> histogram(CV::Histogram::determineHistogram8BitPerChannel<tChannels>(frame, width, height, framePaddingElements, worker));
74 
75  const unsigned int threshold = max(1u, (width * height + 1000u) / 2000u); // 0.05 % of number of pixels
76 
77  constexpr float inv255 = 1.0f / 255.0f;
78 
79  for (unsigned int c = 0u; c < tChannels; c++)
80  {
81  const unsigned int* bins = histogram.bins(c);
82 
83  // we iterates through the histogram from (channel-wise) lowest to highest bin to find the first bin that has more votes than the specified threshold
84 
85  for (unsigned int i = 0u; i < 256u; ++i)
86  {
87  if (bins[i] >= threshold)
88  {
89  blackPointColor[c] = float(i) * inv255;
90  break;
91  }
92  }
93  }
94 
95  return blackPointColor;
96 }
97 
98 }
99 
100 }
101 
102 }
103 
104 #endif // META_OCEAN_CV_ADVANCED_BLACK_POINT_DETECTOR_H
Provides functions for black point detection in a frame.
Definition: BlackPointDetector.h:32
static Color detectBlackPointHistogram(const Frame &frame, Worker *worker=nullptr)
Attempts to detect the black point in the given frame with three channels.
static Color detectBlackPointHistogram8BitPerChannel(const uint8_t *frame, const unsigned int width, const unsigned int height, const unsigned int framePaddingElements, Worker *worker=nullptr)
Attempts to detect the black point in the given frame with three channels.
Definition: BlackPointDetector.h:66
std::vector< float > Color
Definition of a vector holding 32 bit floating point values.
Definition: BlackPointDetector.h:38
This class implements a standard histogram object storing 8 bit per channel.
Definition: Histogram.h:208
const unsigned int * bins() const
Returns the 256 histogram values for a specific channel.
Definition: Histogram.h:1012
This class implements Ocean's image class.
Definition: Frame.h:1792
This class implements a worker able to distribute function calls over different threads.
Definition: Worker.h:33
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15