IDS peak IPL  v1.7.0
peak_ipl_sharpness.hpp
Go to the documentation of this file.
1 
11 #pragma once
12 
18 
19 #include <type_traits>
20 #include <algorithm>
21 #include <cstdint>
22 #include <iterator>
23 #include <vector>
24 
28 namespace peak
29 {
30 namespace ipl
31 {
32 enum class PixelFormatName;
40 class Sharpness final
41 {
42 public:
43  Sharpness();
44  ~Sharpness();
45 
46  Sharpness(const Sharpness&) = delete;
47  Sharpness& operator=(const Sharpness&) = delete;
48 
49  Sharpness(Sharpness&&) noexcept;
50  Sharpness& operator=(Sharpness&&) noexcept;
51 
52  enum class SharpnessAlgorithm : uint16_t
53  {
54  Tenengrad,
55  MeanScore,
57  Sobel
58  };
59 
60  enum class SharpnessROIWeight : uint16_t
61  {
62  Weak = 0x0021,
63  Medium = 0x0042,
64  Strong = 0x0063
65  };
66 
67  struct SharpnessROI
68  {
69 #if !defined(__cpp_aggregate_nsdmi)
70  SharpnessROI() = default;
71  SharpnessROI(Rect2D rect, uint8_t xSamplingInterval, uint8_t ySamplingInterval, SharpnessROIWeight weight) :
72  rect(std::move(rect)), xSamplingInterval(xSamplingInterval), ySamplingInterval(ySamplingInterval), weight(weight) {}
73 #endif
74  Rect2D rect{ 0, 0, 0, 0 };
75  uint8_t xSamplingInterval{ 1 };
76  uint8_t ySamplingInterval{ 1 };
78  };
79 
80  using SharpnessROIList = std::vector<SharpnessROI>;
81 
82  inline double Measure(const Image& image);
83 
84  inline bool IsPixelFormatSupported(PixelFormatName pixelFormatName) const;
85  inline std::vector<PixelFormatName> SupportedPixelFormats() const;
86 
87  inline void SetAlgorithm(SharpnessAlgorithm algorithm);
88  inline SharpnessAlgorithm Algorithm() const;
89 
90  inline Size2D ROIMinSize() const;
91  inline void SetROIs(const SharpnessROIList& rois);
92  inline void SetROI(const SharpnessROI& roi);
93  inline void SetROI(const Rect2D& roi);
94  inline SharpnessROIList ROIs() const;
95 
96 private:
97  PEAK_IPL_SHARPNESS_HANDLE m_backendHandle{};
98 };
99 
101 {
103 }
104 
106 {
107  if (m_backendHandle)
108  {
110  }
111 }
112 
113 inline Sharpness::Sharpness(Sharpness&& other) noexcept
114 {
115  *this = std::move(other);
116 }
117 
118 inline Sharpness& Sharpness::operator=(Sharpness&& other) noexcept
119 {
120  if (this != &other)
121  {
123  m_backendHandle = other.m_backendHandle;
124  other.m_backendHandle = nullptr;
125  }
126 
127  return *this;
128 }
129 
139 inline double Sharpness::Measure(const Image& image)
140 {
141  double calculated_value;
142 
145  m_backendHandle, ImageBackendAccessor::BackendHandle(image), &calculated_value);
146  });
147 
148  return calculated_value;
149 }
150 
159 inline bool Sharpness::IsPixelFormatSupported(PixelFormatName pixelFormatName) const
160 {
161  PEAK_IPL_BOOL8 isPixelFormatSupported = 0;
162 
165  static_cast<PEAK_IPL_PIXEL_FORMAT>(pixelFormatName),
166  reinterpret_cast<PEAK_IPL_BOOL8*>(&isPixelFormatSupported));
167  });
168 
169  return isPixelFormatSupported > 0;
170 }
171 
178 inline std::vector<PixelFormatName> Sharpness::SupportedPixelFormats() const
179 {
180  size_t sizeOfData{};
183  m_backendHandle, nullptr, &sizeOfData);
184  });
185 
186  std::vector<PixelFormatName> formats(sizeOfData);
189  m_backendHandle, reinterpret_cast<PEAK_IPL_PIXEL_FORMAT*>(formats.data()), &sizeOfData);
190  });
191 
192  return formats;
193 }
194 
201 {
204  m_backendHandle, static_cast<PEAK_IPL_SHARPNESS_ALGORITHM>(algorithm));
205  });
206 }
207 
215 {
216  SharpnessAlgorithm algorithm;
217 
220  m_backendHandle, reinterpret_cast<PEAK_IPL_SHARPNESS_ALGORITHM*>(&algorithm));
221  });
222 
223  return algorithm;
224 }
225 
233 {
234  PEAK_IPL_SIZE_2D size{};
235 
237  [&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_ImageSharpness_GetROIMinimumSize(m_backendHandle, &size); });
238 
239  return Size2D{ size.width, size.height };
240 }
241 
248 inline void Sharpness::SetROIs(const SharpnessROIList& rois)
249 {
251  std::vector<PEAK_IPL_SHARPNESS_ROI> sharpness_rois{};
252 
253  std::transform(std::cbegin(rois), std::cend(rois), std::back_inserter(sharpness_rois), [](const auto roi) {
254  return PEAK_IPL_SHARPNESS_ROI{ PEAK_IPL_RECT_2D{ { roi.rect.topLeft().x, roi.rect.topLeft().y },
255  { roi.rect.size().width, roi.rect.size().height } },
256  roi.xSamplingInterval, roi.ySamplingInterval,
257  static_cast<std::underlying_type_t<SharpnessROIWeight>>(roi.weight) };
258  });
259 
261  m_backendHandle, sharpness_rois.data(), sharpness_rois.size());
262  });
263 }
264 
271 inline void Sharpness::SetROI(const SharpnessROI& roi)
272 {
273  SharpnessROIList rois{ roi };
274  SetROIs(rois);
275 }
276 
283 inline void Sharpness::SetROI(const Rect2D& rect)
284 {
285  SharpnessROI roi{ rect, 1, 1, SharpnessROIWeight::Medium };
286  SetROI(roi);
287 }
288 
296 {
297  size_t roiListSize{};
299  [&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_ImageSharpness_GetROIs(m_backendHandle, nullptr, &roiListSize); });
300 
301  std::vector<PEAK_IPL_SHARPNESS_ROI> sharpness_rois(roiListSize);
304  m_backendHandle, sharpness_rois.data(), &roiListSize);
305  });
306 
307  SharpnessROIList rois{};
308 
309  std::transform(
310  std::cbegin(sharpness_rois), std::cend(sharpness_rois), std::back_inserter(rois), [](const auto roi) {
311  return SharpnessROI{ Rect2D{ roi.rect.position.x, roi.rect.position.y, roi.rect.size.width,
312  roi.rect.size.height },
313  roi.xSamplingInterval, roi.ySamplingInterval, static_cast<SharpnessROIWeight>(roi.weight) };
314  });
315 
316  return rois;
317 }
318 
319 inline bool operator==(const Sharpness::SharpnessROI& lhs, const Sharpness::SharpnessROI& rhs)
320 {
321  return lhs.rect == rhs.rect && lhs.weight == rhs.weight && lhs.xSamplingInterval == rhs.xSamplingInterval
322  && lhs.ySamplingInterval == rhs.ySamplingInterval;
323 }
324 
325 inline bool operator!=(const Sharpness::SharpnessROI& lhs, const Sharpness::SharpnessROI& rhs)
326 {
327  return !(lhs == rhs);
328 }
329 
330 } /* namespace ipl */
331 } /* namespace peak */
Configure and measure the sharpness of a given image.
Definition: peak_ipl_sharpness.hpp:40
void SetROI(const SharpnessROI &roi)
Set ROI in which the sharpness is measured.
Definition: peak_ipl_sharpness.hpp:271
Size2D ROIMinSize() const
Gets the minimum size of a ROI, in which the sharpness is measured.
Definition: peak_ipl_sharpness.hpp:232
~Sharpness()
Definition: peak_ipl_sharpness.hpp:105
SharpnessAlgorithm
Definition: peak_ipl_sharpness.hpp:52
PEAK_IPL_SIZE_2D size
Definition: peak_ipl_backend.h:245
#define PEAK_IPL_C_ABI_PREFIX
Definition: peak_ipl_dll_defines.h:16
Sharpness()
Definition: peak_ipl_sharpness.hpp:100
PixelFormatName
Enum holding the possible pixel format names.
Definition: peak_ipl_simple_types.hpp:38
double Measure(const Image &image)
Measures the sharpness of the given image. The higher the value, the better the sharpness.
Definition: peak_ipl_sharpness.hpp:139
The "peak::ipl" namespace contains the whole image processing library.
Definition: peak_ipl_binning.hpp:22
Rect2D rect
Definition: peak_ipl_sharpness.hpp:74
STL namespace.
void SetROIs(const SharpnessROIList &rois)
Sets region of interests in which the sharpness is measured.
Definition: peak_ipl_sharpness.hpp:248
void ExecuteAndMapReturnCodes(const CallableType &callableObject)
Definition: peak_ipl_exception.hpp:182
2D size of an image.
Definition: peak_ipl_simple_types.hpp:177
Stores the pixel format, width and height of an image and the pointer to the image buffer...
Definition: peak_ipl_image.hpp:39
PEAK_IPL_C_API PEAK_IPL_ImageSharpness_SetROIs(PEAK_IPL_SHARPNESS_HANDLE handle, PEAK_IPL_SHARPNESS_ROI *roiList, size_t roiListSize)
Sets ROIs in which the sharpness is measured.
bool operator!=(const Sharpness::SharpnessROI &lhs, const Sharpness::SharpnessROI &rhs)
Definition: peak_ipl_sharpness.hpp:325
Definition: peak_ipl_sharpness.hpp:67
struct PEAK_IPL_SHARPNESS * PEAK_IPL_SHARPNESS_HANDLE
Definition: peak_ipl_backend.h:342
SharpnessROIList ROIs() const
Gets ROIs in which the sharpness is measured.
Definition: peak_ipl_sharpness.hpp:295
uint8_t ySamplingInterval
Definition: peak_ipl_sharpness.hpp:76
SharpnessROIWeight
Definition: peak_ipl_sharpness.hpp:60
SharpnessROIWeight weight
Definition: peak_ipl_sharpness.hpp:77
Definition: peak_ipl_backend.h:242
PEAK_IPL_C_API PEAK_IPL_ImageSharpness_SetAlgorithm(PEAK_IPL_SHARPNESS_HANDLE handle, PEAK_IPL_SHARPNESS_ALGORITHM algorithm)
Sets the algorithm to use for measurement calculation.
PEAK_IPL_C_API PEAK_IPL_ImageSharpness_Measure(PEAK_IPL_SHARPNESS_HANDLE handle, PEAK_IPL_IMAGE_HANDLE imageHandle, double *calculated_value)
Measures the relative sharpness of the given image.
SharpnessROI(Rect2D rect, uint8_t xSamplingInterval, uint8_t ySamplingInterval, SharpnessROIWeight weight)
Definition: peak_ipl_sharpness.hpp:71
int8_t PEAK_IPL_BOOL8
Definition: peak_ipl_backend.h:25
size_t width
Definition: peak_ipl_backend.h:238
std::vector< PixelFormatName > SupportedPixelFormats() const
Returns a list of supported pixel formats which can be used for shaprness measurement.
Definition: peak_ipl_sharpness.hpp:178
PEAK_IPL_C_API PEAK_IPL_ImageSharpness_GetIsPixelFormatSupported(PEAK_IPL_SHARPNESS_HANDLE handle, PEAK_IPL_PIXEL_FORMAT pixelFormat, PEAK_IPL_BOOL8 *isPixelFormatSupported)
Asks the given sharpness measurement whether it supports the given pixel format.
PEAK_IPL_C_API PEAK_IPL_ImageSharpness_Construct(PEAK_IPL_SHARPNESS_HANDLE *handle)
Creates a sharpness measurement.
Definition: peak_ipl_backend.h:277
std::vector< SharpnessROI > SharpnessROIList
Definition: peak_ipl_sharpness.hpp:80
bool operator==(const Sharpness::SharpnessROI &lhs, const Sharpness::SharpnessROI &rhs)
Definition: peak_ipl_sharpness.hpp:319
void SetAlgorithm(SharpnessAlgorithm algorithm)
Sets the algorithm to use for measurement calculation.
Definition: peak_ipl_sharpness.hpp:200
Sharpness & operator=(const Sharpness &)=delete
PEAK_IPL_C_API PEAK_IPL_ImageSharpness_Destruct(PEAK_IPL_SHARPNESS_HANDLE handle)
Destroys the sharpness measurement associated with the given handle.
PEAK_IPL_C_API PEAK_IPL_ImageSharpness_GetSupportedPixelFormatList(PEAK_IPL_SHARPNESS_HANDLE handle, PEAK_IPL_PIXEL_FORMAT *pixelFormatList, size_t *pixelFormatListSize)
Queries the supported pixel formats.
SharpnessAlgorithm Algorithm() const
Returns the used algorithm for the sharpness measurement.
Definition: peak_ipl_sharpness.hpp:214
size_t width
Definition: peak_ipl_simple_types.hpp:179
bool IsPixelFormatSupported(PixelFormatName pixelFormatName) const
Checks if the given pixel format can be used for sharpness measurement.
Definition: peak_ipl_sharpness.hpp:159
PEAK_IPL_C_API PEAK_IPL_ImageSharpness_GetROIs(PEAK_IPL_SHARPNESS_HANDLE handle, PEAK_IPL_SHARPNESS_ROI *roiList, size_t *roiListSize)
Returns the ROIs in which the sharpness is measured.
uint8_t xSamplingInterval
Definition: peak_ipl_sharpness.hpp:75
Rectangle.
Definition: peak_ipl_rect.hpp:30
PEAK_IPL_C_API PEAK_IPL_ImageSharpness_GetROIMinimumSize(PEAK_IPL_SHARPNESS_HANDLE handle, PEAK_IPL_SIZE_2D *min_size)
Returns the minimum size of a ROI to use for measurement calculation.
Definition: peak_ipl_backend.h:236
PEAK_IPL_C_API PEAK_IPL_ImageSharpness_GetAlgorithm(PEAK_IPL_SHARPNESS_HANDLE handle, PEAK_IPL_SHARPNESS_ALGORITHM *algorithm)
Returns the algorithm to use for measurement calculation.
constexpr Size2D size() const
Definition: peak_ipl_rect.hpp:52