IDSpeakIPL  v1.8.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 
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 
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  {
283  m_backendHandle = other.m_backendHandle;
284  other.m_backendHandle = nullptr;
285  }
286 
287  return *this;
288 }
289 
290 inline double Sharpness::Measure(const Image& image)
291 {
292  double calculated_value;
293 
296  m_backendHandle, ImageBackendAccessor::BackendHandle(image), &calculated_value);
297  });
298 
299  return calculated_value;
300 }
301 
302 inline bool Sharpness::IsPixelFormatSupported(PixelFormatName pixelFormatName) const
303 {
304  PEAK_IPL_BOOL8 isPixelFormatSupported = 0;
305 
308  static_cast<PEAK_IPL_PIXEL_FORMAT>(pixelFormatName),
309  reinterpret_cast<PEAK_IPL_BOOL8*>(&isPixelFormatSupported));
310  });
311 
312  return isPixelFormatSupported > 0;
313 }
314 
315 inline std::vector<PixelFormatName> Sharpness::SupportedPixelFormats() const
316 {
317  size_t sizeOfData{};
320  m_backendHandle, nullptr, &sizeOfData);
321  });
322 
323  std::vector<PixelFormatName> formats(sizeOfData);
326  m_backendHandle, reinterpret_cast<PEAK_IPL_PIXEL_FORMAT*>(formats.data()), &sizeOfData);
327  });
328 
329  return formats;
330 }
331 
333 {
336  m_backendHandle, static_cast<PEAK_IPL_SHARPNESS_ALGORITHM>(algorithm));
337  });
338 }
339 
341 {
342  SharpnessAlgorithm algorithm;
343 
346  m_backendHandle, reinterpret_cast<PEAK_IPL_SHARPNESS_ALGORITHM*>(&algorithm));
347  });
348 
349  return algorithm;
350 }
351 
353 {
354  PEAK_IPL_SIZE_2D size{};
355 
357  [&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_ImageSharpness_GetROIMinimumSize(m_backendHandle, &size); });
358 
359  return Size2D{ size.width, size.height };
360 }
361 
362 inline void Sharpness::SetROIs(const SharpnessROIList& rois)
363 {
365  std::vector<PEAK_IPL_SHARPNESS_ROI> sharpness_rois{};
366 
367  std::transform(std::cbegin(rois), std::cend(rois), std::back_inserter(sharpness_rois), [](const auto roi) {
368  return PEAK_IPL_SHARPNESS_ROI{ PEAK_IPL_RECT_2D{ { roi.rect.topLeft().x, roi.rect.topLeft().y },
369  { roi.rect.size().width, roi.rect.size().height } },
370  roi.xSamplingInterval, roi.ySamplingInterval,
371  static_cast<std::underlying_type_t<SharpnessROIWeight>>(roi.weight) };
372  });
373 
375  m_backendHandle, sharpness_rois.data(), sharpness_rois.size());
376  });
377 }
378 
379 inline void Sharpness::SetROI(const SharpnessROI& roi)
380 {
381  SharpnessROIList rois{ roi };
382  SetROIs(rois);
383 }
384 
385 inline void Sharpness::SetROI(const Rect2D& roi)
386 {
387  SharpnessROI sharpnessRoi{ roi, 1, 1, SharpnessROIWeight::Medium };
388  SetROI(sharpnessRoi);
389 }
390 
392 {
393  size_t roiListSize{};
395  [&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_ImageSharpness_GetROIs(m_backendHandle, nullptr, &roiListSize); });
396 
397  std::vector<PEAK_IPL_SHARPNESS_ROI> sharpness_rois(roiListSize);
400  m_backendHandle, sharpness_rois.data(), &roiListSize);
401  });
402 
403  SharpnessROIList rois{};
404 
405  std::transform(
406  std::cbegin(sharpness_rois), std::cend(sharpness_rois), std::back_inserter(rois), [](const auto roi) {
407  return SharpnessROI{ Rect2D{ roi.rect.position.x, roi.rect.position.y, roi.rect.size.width,
408  roi.rect.size.height },
409  roi.xSamplingInterval, roi.ySamplingInterval, static_cast<SharpnessROIWeight>(roi.weight) };
410  });
411 
412  return rois;
413 }
414 
415 inline bool operator==(const Sharpness::SharpnessROI& lhs, const Sharpness::SharpnessROI& rhs)
416 {
417  return lhs.rect == rhs.rect && lhs.weight == rhs.weight && lhs.xSamplingInterval == rhs.xSamplingInterval
418  && lhs.ySamplingInterval == rhs.ySamplingInterval;
419 }
420 
421 inline bool operator!=(const Sharpness::SharpnessROI& lhs, const Sharpness::SharpnessROI& rhs)
422 {
423  return !(lhs == rhs);
424 }
425 
426 } /* namespace ipl */
427 } /* 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:379
struct PEAK_IPL_SIZE_2D size
Definition: peak_ipl_backend.h:273
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.
PEAK_IPL_NO_DISCARD Size2D ROIMinSize() const
Gets the minimum size of a ROI, in which the sharpness is measured.
Definition: peak_ipl_sharpness.hpp:352
~Sharpness()
Destructor for the Sharpness instance.
Definition: peak_ipl_sharpness.hpp:234
SharpnessAlgorithm
Measure Algorithms.
Definition: peak_ipl_sharpness.hpp:106
PEAK_IPL_NO_DISCARD constexpr Size2D size() const
Definition: peak_ipl_rect.hpp:51
#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
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:290
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
STL namespace.
void SetROIs(const SharpnessROIList &rois)
Sets the region of interests in which the sharpness is measured.
Definition: peak_ipl_sharpness.hpp:362
void ExecuteAndMapReturnCodes(const CallableType &callableObject)
Definition: peak_ipl_exception.hpp:182
2D size of an image.
Definition: peak_ipl_simple_types.hpp:200
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:421
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:370
Sharpness & operator=(const Sharpness &other)
Copy assigment for Sharpness instance.
Definition: peak_ipl_sharpness.hpp:252
PEAK_IPL_NO_DISCARD SharpnessROIList ROIs() const
Gets ROIs in which the sharpness is measured.
Definition: peak_ipl_sharpness.hpp:391
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:270
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:266
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_NO_DISCARD std::vector< PixelFormatName > SupportedPixelFormats() const
Returns a list of supported pixel formats which can be used for sharpness measurement.
Definition: peak_ipl_sharpness.hpp:315
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:305
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:415
void SetAlgorithm(SharpnessAlgorithm algorithm)
Sets the algorithm used for measurement calculation.
Definition: peak_ipl_sharpness.hpp:332
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.
PEAK_IPL_NO_DISCARD SharpnessAlgorithm Algorithm() const
Returns the used algorithm for the sharpness measurement.
Definition: peak_ipl_sharpness.hpp:340
size_t width
Width of size.
Definition: peak_ipl_simple_types.hpp:202
PEAK_IPL_NO_DISCARD bool IsPixelFormatSupported(PixelFormatName pixelFormatName) const
Checks if the given pixel format can be used for sharpness measurement.
Definition: peak_ipl_sharpness.hpp:302
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:264
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.