IDS peak IPL  v1.6.0
peak_ipl_sharpness.hpp
Go to the documentation of this file.
1 
11 #pragma once
12 
17 
18 #include <type_traits>
19 #include <algorithm>
20 #include <cstdint>
21 #include <iterator>
22 #include <vector>
23 
27 namespace peak
28 {
29 namespace ipl
30 {
31 class Image;
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  Rect2D rect{ 0, 0, 0, 0 };
70  uint8_t xSamplingInterval{ 1 };
71  uint8_t ySamplingInterval{ 1 };
73  };
74 
75  using SharpnessROIList = std::vector<SharpnessROI>;
76 
77  inline double Measure(const Image& image);
78 
79  inline bool IsPixelFormatSupported(PixelFormatName pixelFormatName) const;
80  inline std::vector<PixelFormatName> SupportedPixelFormats() const;
81 
82  inline void SetAlgorithm(SharpnessAlgorithm algorithm);
83  inline SharpnessAlgorithm Algorithm() const;
84 
85  inline Size2D ROIMinSize() const;
86  inline void SetROIs(const SharpnessROIList& rois);
87  inline void SetROI(const SharpnessROI& roi);
88  inline void SetROI(const Rect2D& roi);
89  inline SharpnessROIList ROIs() const;
90 
91 private:
92  PEAK_IPL_SHARPNESS_HANDLE m_backendHandle{};
93 };
94 
96 {
98 }
99 
101 {
103 }
104 
105 inline Sharpness::Sharpness(Sharpness&& other) noexcept
106 {
107  *this = std::move(other);
108 }
109 
110 inline Sharpness& Sharpness::operator=(Sharpness&& other) noexcept
111 {
112  if (this != &other)
113  {
115  m_backendHandle = other.m_backendHandle;
116  other.m_backendHandle = nullptr;
117  }
118 
119  return *this;
120 }
121 
131 inline double Sharpness::Measure(const Image& image)
132 {
133  double calculated_value;
134 
137  m_backendHandle, ImageBackendAccessor::BackendHandle(image), &calculated_value);
138  });
139 
140  return calculated_value;
141 }
142 
151 inline bool Sharpness::IsPixelFormatSupported(PixelFormatName pixelFormatName) const
152 {
153  PEAK_IPL_BOOL8 isPixelFormatSupported = 0;
154 
157  static_cast<PEAK_IPL_PIXEL_FORMAT>(pixelFormatName),
158  reinterpret_cast<PEAK_IPL_BOOL8*>(&isPixelFormatSupported));
159  });
160 
161  return isPixelFormatSupported > 0;
162 }
163 
170 inline std::vector<PixelFormatName> Sharpness::SupportedPixelFormats() const
171 {
172  size_t sizeOfData{};
175  m_backendHandle, nullptr, &sizeOfData);
176  });
177 
178  std::vector<PixelFormatName> formats(sizeOfData);
181  m_backendHandle, reinterpret_cast<PEAK_IPL_PIXEL_FORMAT*>(formats.data()), &sizeOfData);
182  });
183 
184  return formats;
185 }
186 
193 {
196  m_backendHandle, static_cast<PEAK_IPL_SHARPNESS_ALGORITHM>(algorithm));
197  });
198 }
199 
207 {
208  SharpnessAlgorithm algorithm;
209 
212  m_backendHandle, reinterpret_cast<PEAK_IPL_SHARPNESS_ALGORITHM*>(&algorithm));
213  });
214 
215  return algorithm;
216 }
217 
225 {
226  PEAK_IPL_SIZE_2D size{};
227 
229  [&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_ImageSharpness_GetROIMinimumSize(m_backendHandle, &size); });
230 
231  return Size2D{ size.width, size.height };
232 }
233 
240 inline void Sharpness::SetROIs(const SharpnessROIList& rois)
241 {
243  std::vector<PEAK_IPL_SHARPNESS_ROI> sharpness_rois{};
244 
245  std::transform(std::cbegin(rois), std::cend(rois), std::back_inserter(sharpness_rois), [](const auto roi) {
246  return PEAK_IPL_SHARPNESS_ROI{ PEAK_IPL_RECT_2D{ { roi.rect.topLeft().x, roi.rect.topLeft().y },
247  { roi.rect.size().width, roi.rect.size().height } },
248  roi.xSamplingInterval, roi.ySamplingInterval,
249  static_cast<std::underlying_type_t<SharpnessROIWeight>>(roi.weight) };
250  });
251 
253  m_backendHandle, sharpness_rois.data(), sharpness_rois.size());
254  });
255 }
256 
263 inline void Sharpness::SetROI(const SharpnessROI& roi)
264 {
265  SharpnessROIList rois{ roi };
266  SetROIs(rois);
267 }
268 
275 inline void Sharpness::SetROI(const Rect2D& rect)
276 {
277  SharpnessROI roi{ rect, 1, 1, SharpnessROIWeight::Medium };
278  SetROI(roi);
279 }
280 
288 {
289  size_t roiListSize{};
291  [&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_ImageSharpness_GetROIs(m_backendHandle, nullptr, &roiListSize); });
292 
293  std::vector<PEAK_IPL_SHARPNESS_ROI> sharpness_rois(roiListSize);
296  m_backendHandle, sharpness_rois.data(), &roiListSize);
297  });
298 
299  SharpnessROIList rois{};
300 
301  std::transform(
302  std::cbegin(sharpness_rois), std::cend(sharpness_rois), std::back_inserter(rois), [](const auto roi) {
303  return SharpnessROI{ Rect2D{ roi.rect.position.x, roi.rect.position.y, roi.rect.size.width,
304  roi.rect.size.height },
305  roi.xSamplingInterval, roi.ySamplingInterval, static_cast<SharpnessROIWeight>(roi.weight) };
306  });
307 
308  return rois;
309 }
310 
311 inline bool operator==(const Sharpness::SharpnessROI& lhs, const Sharpness::SharpnessROI& rhs)
312 {
313  return lhs.rect == rhs.rect && lhs.weight == rhs.weight && lhs.xSamplingInterval == rhs.xSamplingInterval
314  && lhs.ySamplingInterval == rhs.ySamplingInterval;
315 }
316 
317 inline bool operator!=(const Sharpness::SharpnessROI& lhs, const Sharpness::SharpnessROI& rhs)
318 {
319  return !(lhs == rhs);
320 }
321 
322 } /* namespace ipl */
323 } /* 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:263
Size2D ROIMinSize() const
Gets the minimum size of a ROI, in which the sharpness is measured.
Definition: peak_ipl_sharpness.hpp:224
~Sharpness()
Definition: peak_ipl_sharpness.hpp:100
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:95
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:131
The "peak::ipl" namespace contains the whole image processing library.
Definition: peak_ipl_binning.hpp:16
Rect2D rect
Definition: peak_ipl_sharpness.hpp:69
void SetROIs(const SharpnessROIList &rois)
Sets region of interests in which the sharpness is measured.
Definition: peak_ipl_sharpness.hpp:240
void ExecuteAndMapReturnCodes(const CallableType &callableObject)
Definition: peak_ipl_exception.hpp:182
2D size of an image.
Definition: peak_ipl_simple_types.hpp:163
Stores the pixel format, width and height of an image and the pointer to the image buffer...
Definition: peak_ipl_image.hpp:37
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:317
Definition: peak_ipl_sharpness.hpp:67
struct PEAK_IPL_SHARPNESS * PEAK_IPL_SHARPNESS_HANDLE
Definition: peak_ipl_backend.h:322
SharpnessROIList ROIs() const
Gets ROIs in which the sharpness is measured.
Definition: peak_ipl_sharpness.hpp:287
uint8_t ySamplingInterval
Definition: peak_ipl_sharpness.hpp:71
SharpnessROIWeight
Definition: peak_ipl_sharpness.hpp:60
SharpnessROIWeight weight
Definition: peak_ipl_sharpness.hpp:72
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.
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:170
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:265
std::vector< SharpnessROI > SharpnessROIList
Definition: peak_ipl_sharpness.hpp:75
bool operator==(const Sharpness::SharpnessROI &lhs, const Sharpness::SharpnessROI &rhs)
Definition: peak_ipl_sharpness.hpp:311
void SetAlgorithm(SharpnessAlgorithm algorithm)
Sets the algorithm to use for measurement calculation.
Definition: peak_ipl_sharpness.hpp:192
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:206
size_t width
Definition: peak_ipl_simple_types.hpp:165
bool IsPixelFormatSupported(PixelFormatName pixelFormatName) const
Checks if the given pixel format can be used for sharpness measurement.
Definition: peak_ipl_sharpness.hpp:151
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:70
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