IDS peak IPL  v1.10.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:
49  Sharpness();
50 
57  ~Sharpness();
58 
70  Sharpness(const Sharpness& other);
71 
83  Sharpness& operator=(const Sharpness& other);
84 
93  Sharpness(Sharpness&& other) noexcept;
94 
103  Sharpness& operator=(Sharpness&& other) noexcept;
104 
106  enum class SharpnessAlgorithm : uint16_t
107  {
108  Tenengrad,
109  MeanScore,
111  Sobel
112  };
113 
117  enum class SharpnessROIWeight : uint16_t
118  {
119  Weak = 0x0021,
120  Medium = 0x0042,
121  Strong = 0x0063
122  };
123 
125  {
126 #if !defined(__cpp_aggregate_nsdmi)
127  SharpnessROI() = default;
128  SharpnessROI(Rect2D rect, uint8_t xSamplingInterval, uint8_t ySamplingInterval, SharpnessROIWeight weight) :
129  rect(std::move(rect)), xSamplingInterval(xSamplingInterval), ySamplingInterval(ySamplingInterval), weight(weight) {}
130 #endif
131  Rect2D rect{ 0, 0, 0, 0 };
132  uint8_t xSamplingInterval{ 1 };
133  uint8_t ySamplingInterval{ 1 };
135  };
136 
137  using SharpnessROIList = std::vector<SharpnessROI>;
138 
150  inline double Measure(const Image& image);
151 
160  PEAK_IPL_NO_DISCARD inline bool IsPixelFormatSupported(PixelFormatName pixelFormatName) const;
161 
168  PEAK_IPL_NO_DISCARD inline std::vector<PixelFormatName> SupportedPixelFormats() const;
169 
175  inline void SetAlgorithm(SharpnessAlgorithm algorithm);
176 
183  PEAK_IPL_NO_DISCARD inline SharpnessAlgorithm Algorithm() const;
184 
191  PEAK_IPL_NO_DISCARD inline Size2D ROIMinSize() const;
192 
199  inline void SetROIs(const SharpnessROIList& rois);
200 
207  inline void SetROI(const SharpnessROI& roi);
208 
215  inline void SetROI(const Rect2D& roi);
216 
223  PEAK_IPL_NO_DISCARD inline SharpnessROIList ROIs() const;
224 
225 private:
226  PEAK_IPL_SHARPNESS_HANDLE m_backendHandle{};
227 };
228 
230 {
232 }
233 
235 {
236  if (m_backendHandle)
237  {
239  }
240 }
241 
242 inline Sharpness::Sharpness(const Sharpness& other)
243 {
244  if (other.m_backendHandle)
245  {
247  }
248 
249  m_backendHandle = other.m_backendHandle;
250 }
251 
253 {
254  if (this != &other)
255  {
256  if (m_backendHandle)
257  {
259  }
260 
261  if (other.m_backendHandle)
262  {
264  [&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_ImageSharpness_Acquire(other.m_backendHandle); });
265  }
266  }
267 
268  m_backendHandle = other.m_backendHandle;
269 
270  return *this;
271 }
272 
273 inline Sharpness::Sharpness(Sharpness&& other) noexcept
274 {
275  *this = std::move(other);
276 }
277 
278 inline Sharpness& Sharpness::operator=(Sharpness&& other) noexcept
279 {
280  if (this != &other)
281  {
282  if (m_backendHandle != nullptr)
283  {
285  }
286 
287  m_backendHandle = other.m_backendHandle;
288  other.m_backendHandle = nullptr;
289  }
290 
291  return *this;
292 }
293 
294 inline double Sharpness::Measure(const Image& image)
295 {
296  double calculated_value;
297 
300  m_backendHandle, ImageBackendAccessor::BackendHandle(image), &calculated_value);
301  });
302 
303  return calculated_value;
304 }
305 
306 inline bool Sharpness::IsPixelFormatSupported(PixelFormatName pixelFormatName) const
307 {
308  PEAK_IPL_BOOL8 isPixelFormatSupported = 0;
309 
312  static_cast<PEAK_IPL_PIXEL_FORMAT>(pixelFormatName),
313  reinterpret_cast<PEAK_IPL_BOOL8*>(&isPixelFormatSupported));
314  });
315 
316  return isPixelFormatSupported > 0;
317 }
318 
319 inline std::vector<PixelFormatName> Sharpness::SupportedPixelFormats() const
320 {
321  size_t sizeOfData{};
324  m_backendHandle, nullptr, &sizeOfData);
325  });
326 
327  std::vector<PixelFormatName> formats(sizeOfData);
330  m_backendHandle, reinterpret_cast<PEAK_IPL_PIXEL_FORMAT*>(formats.data()), &sizeOfData);
331  });
332 
333  return formats;
334 }
335 
337 {
340  m_backendHandle, static_cast<PEAK_IPL_SHARPNESS_ALGORITHM>(algorithm));
341  });
342 }
343 
344 inline Sharpness::SharpnessAlgorithm Sharpness::Algorithm() const
345 {
346  SharpnessAlgorithm algorithm;
347 
350  m_backendHandle, reinterpret_cast<PEAK_IPL_SHARPNESS_ALGORITHM*>(&algorithm));
351  });
352 
353  return algorithm;
354 }
355 
356 inline Size2D Sharpness::ROIMinSize() const
357 {
358  PEAK_IPL_SIZE_2D size{};
359 
361  [&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_ImageSharpness_GetROIMinimumSize(m_backendHandle, &size); });
362 
363  return Size2D{ size.width, size.height };
364 }
365 
366 inline void Sharpness::SetROIs(const SharpnessROIList& rois)
367 {
369  std::vector<PEAK_IPL_SHARPNESS_ROI> sharpness_rois{};
370 
371  std::transform(std::cbegin(rois), std::cend(rois), std::back_inserter(sharpness_rois), [](const auto roi) {
372  return PEAK_IPL_SHARPNESS_ROI{ PEAK_IPL_RECT_2D{ { roi.rect.topLeft().x, roi.rect.topLeft().y },
373  { roi.rect.size().width, roi.rect.size().height } },
374  roi.xSamplingInterval, roi.ySamplingInterval,
375  static_cast<std::underlying_type_t<SharpnessROIWeight>>(roi.weight) };
376  });
377 
379  m_backendHandle, sharpness_rois.data(), sharpness_rois.size());
380  });
381 }
382 
383 inline void Sharpness::SetROI(const SharpnessROI& roi)
384 {
385  SharpnessROIList rois{ roi };
386  SetROIs(rois);
387 }
388 
389 inline void Sharpness::SetROI(const Rect2D& roi)
390 {
391  SharpnessROI sharpnessRoi{ roi, 1, 1, SharpnessROIWeight::Medium };
392  SetROI(sharpnessRoi);
393 }
394 
395 inline Sharpness::SharpnessROIList Sharpness::ROIs() const
396 {
397  size_t roiListSize{};
399  [&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_ImageSharpness_GetROIs(m_backendHandle, nullptr, &roiListSize); });
400 
401  std::vector<PEAK_IPL_SHARPNESS_ROI> sharpness_rois(roiListSize);
404  m_backendHandle, sharpness_rois.data(), &roiListSize);
405  });
406 
407  SharpnessROIList rois{};
408 
409  std::transform(
410  std::cbegin(sharpness_rois), std::cend(sharpness_rois), std::back_inserter(rois), [](const auto roi) {
411  return SharpnessROI{ Rect2D{ roi.rect.position.x, roi.rect.position.y, roi.rect.size.width,
412  roi.rect.size.height },
413  roi.xSamplingInterval, roi.ySamplingInterval, static_cast<SharpnessROIWeight>(roi.weight) };
414  });
415 
416  return rois;
417 }
418 
419 inline bool operator==(const Sharpness::SharpnessROI& lhs, const Sharpness::SharpnessROI& rhs)
420 {
421  return lhs.rect == rhs.rect && lhs.weight == rhs.weight && lhs.xSamplingInterval == rhs.xSamplingInterval
422  && lhs.ySamplingInterval == rhs.ySamplingInterval;
423 }
424 
425 inline bool operator!=(const Sharpness::SharpnessROI& lhs, const Sharpness::SharpnessROI& rhs)
426 {
427  return !(lhs == rhs);
428 }
429 
430 } /* namespace ipl */
431 } /* namespace peak */
#define PEAK_IPL_NO_DISCARD
Definition: peak_ipl_backend.h:26
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:383
struct PEAK_IPL_SIZE_2D size
Definition: peak_ipl_backend.h:280
PEAK_IPL_C_API PEAK_IPL_ImageSharpness_GetROIs(PEAK_IPL_SHARPNESS_HANDLE handle, struct PEAK_IPL_SHARPNESS_ROI *roiList, size_t *roiListSize)
Returns the ROIs in which the sharpness is measured.
~Sharpness()
Destructor for the Sharpness instance.
Definition: peak_ipl_sharpness.hpp:234
SharpnessAlgorithm
Measure Algorithms.
Definition: peak_ipl_sharpness.hpp:106
#define PEAK_IPL_C_ABI_PREFIX
Definition: peak_ipl_dll_defines.h:16
Sharpness()
Constructor for a new Sharpness instance.
Definition: peak_ipl_sharpness.hpp:229
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:294
The "peak::ipl" namespace contains the whole image processing library.
Definition: peak_ipl_binning.hpp:22
Rect2D rect
Sharpness Rect.
Definition: peak_ipl_sharpness.hpp:131
PixelFormatName
Enum holding the possible pixel format names.
Definition: peak_ipl_simple_types.hpp:38
STL namespace.
void SetROIs(const SharpnessROIList &rois)
Sets the region of interests in which the sharpness is measured.
Definition: peak_ipl_sharpness.hpp:366
void ExecuteAndMapReturnCodes(const CallableType &callableObject)
Definition: peak_ipl_exception.hpp:182
2D size of an image.
Definition: peak_ipl_simple_types.hpp:207
Stores the pixel format, width and height of an image and the pointer to the image buffer...
Definition: peak_ipl_image.hpp:39
bool operator!=(const Sharpness::SharpnessROI &lhs, const Sharpness::SharpnessROI &rhs)
Definition: peak_ipl_sharpness.hpp:425
PEAK_IPL_C_API PEAK_IPL_ImageSharpness_Acquire(PEAK_IPL_SHARPNESS_HANDLE handle)
Increase the reference count of a sharpness measurement object.
PEAK_IPL_C_API PEAK_IPL_ImageSharpness_GetROIMinimumSize(PEAK_IPL_SHARPNESS_HANDLE handle, struct PEAK_IPL_SIZE_2D *min_size)
Returns the minimum size of a ROI to use for measurement calculation.
Definition: peak_ipl_sharpness.hpp:124
struct PEAK_IPL_SHARPNESS * PEAK_IPL_SHARPNESS_HANDLE
Definition: peak_ipl_backend.h:377
Sharpness & operator=(const Sharpness &other)
Copy assigment for Sharpness instance.
Definition: peak_ipl_sharpness.hpp:252
Normal use of roi for the measurement.
uint8_t ySamplingInterval
vertical sampling interval
Definition: peak_ipl_sharpness.hpp:133
SharpnessROIWeight
Definition: peak_ipl_sharpness.hpp:117
contrast-based sharpness algorithm (convolution)
SharpnessROIWeight weight
weight for the roi
Definition: peak_ipl_sharpness.hpp:134
Definition: peak_ipl_backend.h:277
PEAK_IPL_C_API PEAK_IPL_ImageSharpness_SetAlgorithm(PEAK_IPL_SHARPNESS_HANDLE handle, PEAK_IPL_SHARPNESS_ALGORITHM algorithm)
Sets the algorithm used 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:128
int8_t PEAK_IPL_BOOL8
Definition: peak_ipl_backend.h:41
size_t width
Definition: peak_ipl_backend.h:273
PEAK_IPL_C_API PEAK_IPL_ImageSharpness_SetROIs(PEAK_IPL_SHARPNESS_HANDLE handle, struct PEAK_IPL_SHARPNESS_ROI *roiList, size_t roiListSize)
Sets ROIs in which the sharpness is measured.
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:312
std::vector< SharpnessROI > SharpnessROIList
Definition: peak_ipl_sharpness.hpp:137
bool operator==(const Sharpness::SharpnessROI &lhs, const Sharpness::SharpnessROI &rhs)
Definition: peak_ipl_sharpness.hpp:419
void SetAlgorithm(SharpnessAlgorithm algorithm)
Sets the algorithm used for measurement calculation.
Definition: peak_ipl_sharpness.hpp:336
PEAK_IPL_C_API PEAK_IPL_ImageSharpness_Destruct(PEAK_IPL_SHARPNESS_HANDLE handle)
Destroys the sharpness measurement associated with the given handle.
contrast-based sharpness algorithm (mean value)
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.
size_t width
Width of size.
Definition: peak_ipl_simple_types.hpp:209
contrast-based sharpness algorithm (convolution)
uint8_t xSamplingInterval
horizontal sampling interval
Definition: peak_ipl_sharpness.hpp:132
Rectangle type.
Definition: peak_ipl_rect.hpp:29
Definition: peak_ipl_backend.h:271
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.