IDS peak IPL v1.13.1
Loading...
Searching...
No Matches
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
28namespace peak
29{
30namespace ipl
31{
32enum class PixelFormatName;
40class Sharpness final
41{
42public:
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
136
137 using SharpnessROIList = std::vector<SharpnessROI>;
138
152 inline double Measure(const Image& image);
153
162 PEAK_IPL_NO_DISCARD inline bool IsPixelFormatSupported(PixelFormatName pixelFormatName) const;
163
170 PEAK_IPL_NO_DISCARD inline std::vector<PixelFormatName> SupportedPixelFormats() const;
171
177 inline void SetAlgorithm(SharpnessAlgorithm algorithm);
178
186
194
201 inline void SetROIs(const SharpnessROIList& rois);
202
209 inline void SetROI(const SharpnessROI& roi);
210
217 inline void SetROI(const Rect2D& roi);
218
226
227private:
228 PEAK_IPL_SHARPNESS_HANDLE m_backendHandle{};
229};
230
235
237{
238 if (m_backendHandle)
239 {
241 }
242}
243
244inline Sharpness::Sharpness(const Sharpness& other)
245{
246 if (other.m_backendHandle)
247 {
249 }
250
251 m_backendHandle = other.m_backendHandle;
252}
253
255{
256 if (this != &other)
257 {
258 if (m_backendHandle)
259 {
261 }
262
263 if (other.m_backendHandle)
264 {
266 [&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_ImageSharpness_Acquire(other.m_backendHandle); });
267 }
268 }
269
270 m_backendHandle = other.m_backendHandle;
271
272 return *this;
273}
274
275inline Sharpness::Sharpness(Sharpness&& other) noexcept
276{
277 *this = std::move(other);
278}
279
280inline Sharpness& Sharpness::operator=(Sharpness&& other) noexcept
281{
282 if (this != &other)
283 {
284 if (m_backendHandle != nullptr)
285 {
287 }
288
289 m_backendHandle = other.m_backendHandle;
290 other.m_backendHandle = nullptr;
291 }
292
293 return *this;
294}
295
296inline double Sharpness::Measure(const Image& image)
297{
298 double calculated_value;
299
302 m_backendHandle, ImageBackendAccessor::BackendHandle(image), &calculated_value);
303 });
304
305 return calculated_value;
306}
307
308inline bool Sharpness::IsPixelFormatSupported(PixelFormatName pixelFormatName) const
309{
310 PEAK_IPL_BOOL8 isPixelFormatSupported = 0;
311
314 static_cast<PEAK_IPL_PIXEL_FORMAT>(pixelFormatName),
315 reinterpret_cast<PEAK_IPL_BOOL8*>(&isPixelFormatSupported));
316 });
317
318 return isPixelFormatSupported > 0;
319}
320
321inline std::vector<PixelFormatName> Sharpness::SupportedPixelFormats() const
322{
323 size_t sizeOfData{};
326 m_backendHandle, nullptr, &sizeOfData);
327 });
328
329 std::vector<PixelFormatName> formats(sizeOfData);
332 m_backendHandle, reinterpret_cast<PEAK_IPL_PIXEL_FORMAT*>(formats.data()), &sizeOfData);
333 });
334
335 return formats;
336}
337
339{
342 m_backendHandle, static_cast<PEAK_IPL_SHARPNESS_ALGORITHM>(algorithm));
343 });
344}
345
347{
348 SharpnessAlgorithm algorithm;
349
352 m_backendHandle, reinterpret_cast<PEAK_IPL_SHARPNESS_ALGORITHM*>(&algorithm));
353 });
354
355 return algorithm;
356}
357
359{
360 PEAK_IPL_SIZE_2D size{};
361
363 [&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_ImageSharpness_GetROIMinimumSize(m_backendHandle, &size); });
364
365 return Size2D{ size.width, size.height };
366}
367
368inline void Sharpness::SetROIs(const SharpnessROIList& rois)
369{
371 std::vector<PEAK_IPL_SHARPNESS_ROI> sharpness_rois{};
372
373 std::transform(std::cbegin(rois), std::cend(rois), std::back_inserter(sharpness_rois), [](const auto roi) {
374 return PEAK_IPL_SHARPNESS_ROI{ PEAK_IPL_RECT_2D{ { roi.rect.topLeft().x, roi.rect.topLeft().y },
375 { roi.rect.size().width, roi.rect.size().height } },
376 roi.xSamplingInterval, roi.ySamplingInterval,
377 static_cast<std::underlying_type_t<SharpnessROIWeight>>(roi.weight) };
378 });
379
381 m_backendHandle, sharpness_rois.data(), sharpness_rois.size());
382 });
383}
384
385inline void Sharpness::SetROI(const SharpnessROI& roi)
386{
387 SharpnessROIList rois{ roi };
388 SetROIs(rois);
389}
390
391inline void Sharpness::SetROI(const Rect2D& roi)
392{
393 SharpnessROI sharpnessRoi{ roi, 1, 1, SharpnessROIWeight::Medium };
394 SetROI(sharpnessRoi);
395}
396
398{
399 size_t roiListSize{};
401 [&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_ImageSharpness_GetROIs(m_backendHandle, nullptr, &roiListSize); });
402
403 std::vector<PEAK_IPL_SHARPNESS_ROI> sharpness_rois(roiListSize);
406 m_backendHandle, sharpness_rois.data(), &roiListSize);
407 });
408
409 SharpnessROIList rois{};
410
411 std::transform(
412 std::cbegin(sharpness_rois), std::cend(sharpness_rois), std::back_inserter(rois), [](const auto roi) {
413 return SharpnessROI{ Rect2D{ roi.rect.position.x, roi.rect.position.y, roi.rect.size.width,
414 roi.rect.size.height },
415 roi.xSamplingInterval, roi.ySamplingInterval, static_cast<SharpnessROIWeight>(roi.weight) };
416 });
417
418 return rois;
419}
420
422{
423 return lhs.rect == rhs.rect && lhs.weight == rhs.weight && lhs.xSamplingInterval == rhs.xSamplingInterval
425}
426
428{
429 return !(lhs == rhs);
430}
431
432} /* namespace ipl */
433} /* namespace peak */
Stores the pixel format, width and height of an image and the pointer to the image buffer.
Definition peak_ipl_image.hpp:40
Rectangle type.
Definition peak_ipl_rect.hpp:30
constexpr Size2D size() const
Definition peak_ipl_rect.hpp:51
Configure and measure the sharpness of a given image.
Definition peak_ipl_sharpness.hpp:41
Sharpness()
Constructor for a new Sharpness instance.
Definition peak_ipl_sharpness.hpp:231
Sharpness & operator=(const Sharpness &other)
Copy assigment for Sharpness instance.
Definition peak_ipl_sharpness.hpp:254
void SetROIs(const SharpnessROIList &rois)
Sets the region of interests in which the sharpness is measured.
Definition peak_ipl_sharpness.hpp:368
Size2D ROIMinSize() const
Gets the minimum size of a ROI, in which the sharpness is measured.
Definition peak_ipl_sharpness.hpp:358
void SetROI(const SharpnessROI &roi)
Set ROI in which the sharpness is measured.
Definition peak_ipl_sharpness.hpp:385
SharpnessAlgorithm
Measure Algorithms.
Definition peak_ipl_sharpness.hpp:107
@ HistogramVariance
statistics-based sharpness algorithm
@ Tenengrad
contrast-based sharpness algorithm (convolution)
@ Sobel
contrast-based sharpness algorithm (convolution)
@ MeanScore
contrast-based sharpness algorithm (mean value)
SharpnessROIList ROIs() const
Gets ROIs in which the sharpness is measured.
Definition peak_ipl_sharpness.hpp:397
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:296
SharpnessROIWeight
Definition peak_ipl_sharpness.hpp:118
@ Weak
Weak use of roi for the measurement.
@ Medium
Normal use of roi for the measurement.
@ Strong
Strong use of roi for the measurement.
std::vector< SharpnessROI > SharpnessROIList
Definition peak_ipl_sharpness.hpp:137
bool IsPixelFormatSupported(PixelFormatName pixelFormatName) const
Checks if the given pixel format can be used for sharpness measurement.
Definition peak_ipl_sharpness.hpp:308
~Sharpness()
Destructor for the Sharpness instance.
Definition peak_ipl_sharpness.hpp:236
void SetAlgorithm(SharpnessAlgorithm algorithm)
Sets the algorithm used for measurement calculation.
Definition peak_ipl_sharpness.hpp:338
SharpnessAlgorithm Algorithm() const
Returns the used algorithm for the sharpness measurement.
Definition peak_ipl_sharpness.hpp:346
std::vector< PixelFormatName > SupportedPixelFormats() const
Returns a list of supported pixel formats which can be used for sharpness measurement.
Definition peak_ipl_sharpness.hpp:321
bool operator!=(const Sharpness::SharpnessROI &lhs, const Sharpness::SharpnessROI &rhs)
Definition peak_ipl_sharpness.hpp:427
PixelFormatName
Enum holding the possible pixel format names.
Definition peak_ipl_simple_types.hpp:39
void ExecuteAndMapReturnCodes(const CallableType &callableObject)
Definition peak_ipl_exception.hpp:182
bool operator==(const Sharpness::SharpnessROI &lhs, const Sharpness::SharpnessROI &rhs)
Definition peak_ipl_sharpness.hpp:421
The "peak::ipl" namespace contains the whole image processing library.
Definition peak_ipl_binning.hpp:23
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.
uint16_t PEAK_IPL_SHARPNESS_ALGORITHM
Definition peak_ipl_backend.h:298
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.
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.
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_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_Destruct(PEAK_IPL_SHARPNESS_HANDLE handle)
Destroys the sharpness measurement associated with the given handle.
struct PEAK_IPL_SHARPNESS * PEAK_IPL_SHARPNESS_HANDLE
Definition peak_ipl_backend.h:385
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.
int8_t PEAK_IPL_BOOL8
Definition peak_ipl_backend.h:41
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.
#define PEAK_IPL_NO_DISCARD
Definition peak_ipl_backend.h:26
int32_t PEAK_IPL_PIXEL_FORMAT
Definition peak_ipl_backend.h:188
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.
PEAK_IPL_C_API PEAK_IPL_ImageSharpness_Construct(PEAK_IPL_SHARPNESS_HANDLE *handle)
Creates a sharpness measurement.
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.
#define PEAK_IPL_C_ABI_PREFIX
Definition peak_ipl_dll_defines.h:16
Definition peak_ipl_backend.h:286
struct PEAK_IPL_SIZE_2D size
Definition peak_ipl_backend.h:288
Definition peak_ipl_backend.h:321
Definition peak_ipl_backend.h:280
size_t height
Definition peak_ipl_backend.h:282
Definition peak_ipl_sharpness.hpp:125
uint8_t ySamplingInterval
vertical sampling interval
Definition peak_ipl_sharpness.hpp:133
SharpnessROIWeight weight
weight for the roi
Definition peak_ipl_sharpness.hpp:134
uint8_t xSamplingInterval
horizontal sampling interval
Definition peak_ipl_sharpness.hpp:132
SharpnessROI(Rect2D rect, uint8_t xSamplingInterval, uint8_t ySamplingInterval, SharpnessROIWeight weight)
Definition peak_ipl_sharpness.hpp:128
Rect2D rect
Sharpness Rect.
Definition peak_ipl_sharpness.hpp:131
2D size of an image.
Definition peak_ipl_simple_types.hpp:216
size_t width
Width of size.
Definition peak_ipl_simple_types.hpp:217