3from __future__
import annotations
6from typing
import Any, cast
10from ids_peak_ipl
import ids_peak_ipl
17from ids_peak_icv
import lib_loader
20 peak_icv_image_handle,
22 peak_icv_capture_information,
23 peak_icv_binning_factor,
26 execute_and_map_return_codes,
27 check_init_for_classes_with_classmethods_only,
28 metadata_to_capture_information,
29 metadata_from_capture_information,
30 add_metadata_from_capture_information_to_metadata
34from typing
import TYPE_CHECKING
43 Image Class for working with algorithms.
45 @since ids_peak_icv 1.0
46 @ingroup ids_peak_icv_python_types
55 Creates an instance of Image using a size.Size and a pixel format
57 @param size: Image size in pixels
58 @param pixel_format: Pixel format of the image
59 @return Returns an image instance.
61 @since ids_peak_icv 1.0
67 execute_and_map_return_codes(lib_loader.dll().peak_icv_Image_Create,
68 ctypes.pointer(image_handle),
69 ctypes.c_int(pixel_format.value),
72 return Image(_handle=image_handle)
78 Creates an instance of Image using a ids_peak_ipl image.
80 @param ids_peak_ipl_image: ids_peak_ipl image
81 @return Created an image instance.
83 @since ids_peak_icv 1.0
85 numpy_data = (np.array(ids_peak_ipl_image.DataView(), dtype=np.uint8)
if
86 ids_peak_ipl_image.PixelFormat().IsPacked()
else ids_peak_ipl_image.get_numpy())
88 pixel_format=ids_peak_ipl_image.PixelFormat().PixelFormatName(),
89 size=
peak_common_size(ids_peak_ipl_image.Width(), ids_peak_ipl_image.Height()),
90 buffer_size=ids_peak_ipl_image.ByteCount(),
91 buffer=numpy_data.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8))
95 execute_and_map_return_codes(lib_loader.dll().peak_icv_Image_CreateFromImageInfo,
96 ctypes.pointer(image_handle),
98 ctypes.sizeof(image_info))
100 time_stamp=ids_peak_ipl_image.Timestamp())
101 execute_and_map_return_codes(lib_loader.dll().peak_icv_Image_SetCaptureInformation, image_handle,
102 c_capture_information, ctypes.sizeof(c_capture_information))
104 return cls(_handle=image_handle, _ids_peak_ipl_image=ids_peak_ipl_image)
110 Creates an instance of Image using a PixelFormat, size.Size and numpy image buffer.
112 @param pixel_format: Pixel format of the image
113 @param size: Image size in pixels
114 @param data: Numpy array holding the buffer data
116 @return Created an image instance.
118 @exception OutOfRangeException: If the input array is 2D and the expected number of channels is not 1,
119 or if it is 3D and the channel count does not match, or if it has a number of dimensions other than 2 or 3.
121 @since ids_peak_icv 1.0
124 if pixel_format.is_packed:
126 "Creating an image from a packed format is not supported")
134 if pixel_format.number_of_channels != 1:
137 if data.shape[2] != pixel_format.number_of_channels:
139 "Number of channels must be {}".format(pixel_format.number_of_channels))
147 pixel_format=pixel_format.value,
149 buffer_size=data.size * data.itemsize,
150 buffer=data.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8))
153 execute_and_map_return_codes(lib_loader.dll().peak_icv_Image_CreateFromImageInfo,
154 ctypes.pointer(image_handle),
156 ctypes.sizeof(image_info))
158 return cls(_handle=image_handle, _data=data)
161 def create_from_file(cls, image_path: str, forced_pixel_format: PixelFormat |
None =
None) -> Image:
164 Creates an Image from a file path.
166 This method loads an image from the given file path.
168 If a PixelFormat is provided via ``forced_pixel_format``,
169 the method attempts to interpret the file using that format.
170 If interpretation fails, an exception is raised.
171 Explicit conversion between image formats must be handled manually by the user.
173 The following image formats are supported:
176 - Joint Photographic Experts Group (JPEG)
177 - Portable Network Graphics (PNG)
178 - @htmlonly Tag Image File Format (TIFF)@endhtmlonly
179 (Only if ``forced_pixel_format`` is specified)
180 - Raw Binary Format (RAW)
182 ### Supported image and pixel formats
185 @supportedPixelformats{ImageLoad_BMP}
187 @supportedPixelformats{ImageLoad_JPEG}
189 @supportedPixelformats{ImageLoad_PNG}
191 If ``forced_pixel_format`` is ``None`` any raw image can be loaded.
193 If ``forced_pixel_format`` is specified, only the following formats are supported
194 and must match the format used when saving:
195 @supportedPixelformats{ImageLoad_RAW_WithPixelFormat}
197 See the @ref concept_type_pixel_format for a detailed description of the pixel formats.
199 @param image_path: File path on disk, pointing to an image.
200 @param forced_pixel_format: Pixel format the loaded image is forced to use.
201 @return The loaded Image as a new instance
203 @since ids_peak_icv 1.0
206 c_image_path = image_path.encode(
'utf-8')
209 if forced_pixel_format
is None:
210 execute_and_map_return_codes(lib_loader.dll().peak_icv_Image_CreateFromFile,
211 ctypes.pointer(c_handle), ctypes.c_char_p(c_image_path))
213 execute_and_map_return_codes(lib_loader.dll().peak_icv_Image_CreateFromFileWithPixelFormat,
214 ctypes.pointer(c_handle), ctypes.c_char_p(c_image_path),
215 ctypes.c_int(forced_pixel_format.value))
216 return cls(_handle=c_handle)
222 Creates an instance of Image using an IImageView interface.
224 @param image_view: An object implementing the IImageView interface,
225 which can be constructed from a buffer provided by a camera.
226 @return A new `Image` instance created from the given image view.
228 @since ids_peak_icv 1.0
231 image = cls.
_create_from_np_array(image_view.pixel_format, image_view.size, image_view.to_numpy_array())
232 image.metadata = image_view.metadata
235 def __init__(self, *args: Any, **kwargs: Any) ->
None:
238 In order to create an Image, please refer to the classmethods.
240 @since ids_peak_icv 1.0
242 self.
_handle_handle = check_init_for_classes_with_classmethods_only(self.__class__.__name__,
243 peak_icv_image_handle, *args, **kwargs)
249 execute_and_map_return_codes(lib_loader.dll().peak_icv_Image_GetInfo,
250 self.
_handle_handle, ctypes.pointer(image_info), ctypes.sizeof(image_info))
255 self.
_size =
Size(image_info.size.width, image_info.size.height)
258 _shape = [int(self.
_size.height), int(self.
_size.width)]
260 _shape.append(num_channels)
264 if "_data" in kwargs:
265 self.
_data = kwargs[
"_data"]
272 self.
_raw_data = np.ctypeslib.as_array(image_info.buffer, shape=(
286 Destructor for Image class.
288 if hasattr(self,
'_handle'):
291 def save(self, output_file_path: str) ->
None:
294 Saves an instance to a file on disk. The type is specified by the given file ending in file name.
296 The following image formats are supported:
297 - Portable Network Graphics (PNG)
299 - Joint Photographic Experts Group (JPEG)
300 - @htmlonly Tag Image File Format@endhtmlonly (TIFF)
301 - Raw Binary Format (RAW)
303 The compression settings for PNG and JPEG images are as follows:
304 For JPEG, the image quality is set to 75%, and for PNG, the image quality is set to 100%.
306 @note The file extension for the given file name must match the specified image format when an image is saved.
307 Otherwise, an error will occur when saving an image. The file extensions are:
314 ### Supported image and pixel formats
317 @supportedPixelformats{ImageSave_BMP}
319 @supportedPixelformats{ImageSave_JPEG}
321 @supportedPixelformats{ImageSave_PNG}
323 @supportedPixelformats{ImageSave_TIFF}
325 @supportedPixelformats{ImageSave_RAW}
327 See the @ref concept_type_pixel_format for a detailed description of the pixel formats.
329 @param output_file_path: The path of the file to store the image to disk.
331 @since ids_peak_icv 1.0
333 c_output_file_path = output_file_path.encode(
'utf-8')
334 execute_and_map_return_codes(lib_loader.dll().peak_icv_Image_SaveToFile, self.
_handle_handle,
335 ctypes.c_char_p(c_output_file_path))
341 Provides the size.Size of the image.
343 @return Image size in pixels
345 @since ids_peak_icv 1.0
353 Provides the width of the image.
355 @return Image width in pixels
357 @since ids_peak_icv 1.0
359 return int(self.
size.width)
365 Provides the height of the image.
367 @return Image height in pixels
369 @since ids_peak_icv 1.0
371 return int(self.
size.height)
376 Provides a numpy representation of the image.
378 Note that the image is copied by default and the caller must not take care of the original Instance being still
381 @param copy: True: The image is copied, False: The image only references the internal memory.
382 @return Image data as a numpy array. Shape is [height, width] for single channel images.
383 Shape is [height, width, number of channels] for multichannel images.
385 @since ids_peak_icv 1.0
389 return cast(np.ndarray, self.
_data.copy())
396 Converts the image to a different pixel format.
398 If the input image has the same pixel format as the specified pixel format parameter,
399 the image will be copied.
402 - Conversion to Bayer formats is only supported from a bayer format with the same pattern,
403 e.g. from BayerRG10p to BayerRG10 or BayerRG8.
404 - Conversion to packed formats is not supported.
405 - Conversions from RGB or BGR formats to RGBa or BGRa formats is not supported.
406 - RGBa and BGRa formats support only conversions into different bit depths of the same channel layout
408 - Confidence and Coord3D formats can only be converted to other bit depths of the same format type.
411 For a full list of supported conversions see \ref concept_supported_pixel_format_conversion.
413 @param pixel_format: The target pixel format.
415 @return A new Image with the target pixel format.
417 @since ids_peak_icv 1.0
421 execute_and_map_return_codes(lib_loader.dll().peak_icv_Image_Create,
422 ctypes.pointer(output_image_handle),
423 ctypes.c_int(pixel_format.value),
426 execute_and_map_return_codes(lib_loader.dll().peak_icv_Image_ConvertPixelFormat,
430 return Image(_handle=output_image_handle)
435 Converts an image into another pixel format with a factor multiplied to every pixel.
437 Supported conversions:
438 From PixelFormat::Coord3D_C8 or PixelFormat::Coord3D_C16 to PixelFormat::Coord3D_C32f
440 @param pixel_format: The target pixel format.
441 @param factor: The multiplication factor.
442 @return A new instance of Image with the target pixel format.
444 @since ids_peak_icv 1.1
448 execute_and_map_return_codes(lib_loader.dll().peak_icv_Image_Create,
449 ctypes.pointer(output_image_handle),
450 ctypes.c_int(pixel_format.value),
453 execute_and_map_return_codes(lib_loader.dll().peak_icv_Image_ConvertPixelFormatWithFactor,
454 self.
_handle_handle, ctypes.c_int(pixel_format.value), ctypes.c_double(factor),
457 return Image(_handle=output_image_handle)
460 def region(self) -> Region:
463 Retrieves the region.Region associated with the specified image_handle.
464 @return The image region.
466 @since ids_peak_icv 1.0
471 execute_and_map_return_codes(lib_loader.dll().peak_icv_Image_GetRegion, self.
_handle_handle,
472 ctypes.pointer(region._handle))
476 def region(self, region: Region) ->
None:
479 Updates the region.Region on the image.
481 Sets a region of interest (ROI) for the specified image handle,
482 which defines a sub-area of the image where operations or analyses are focused.
483 Any points in the region that fall outside the bounds of the image are automatically discarded (clipped).
485 @param region: The region to be set.
487 @since ids_peak_icv 1.0
489 execute_and_map_return_codes(lib_loader.dll().peak_icv_Image_SetRegion, self.
_handle_handle,
495 Resets the region.Region on the image to the complete image.
497 @since ids_peak_icv 1.0
499 execute_and_map_return_codes(lib_loader.dll().peak_icv_Image_ResetRegion, self.
_handle_handle)
505 Retrieves the metadata.Metadata of an image which contains information about
506 the image capture settings.
508 @return The metadata of the image.
510 @since ids_peak_icv 1.0
517 execute_and_map_return_codes(lib_loader.dll().peak_icv_Image_GetCaptureInformation, self.
_handle_handle,
518 ctypes.pointer(c_capture_information), ctypes.sizeof(c_capture_information))
520 self.
_metadata = metadata_from_capture_information(c_capture_information)
527 Updates the metadata.Metadata of an image which is used to store and
528 manage capture settings.
530 @param metadata: The metadata to be updated.
532 @since ids_peak_icv 1.0
534 c_capture_information = metadata_to_capture_information(metadata, size=self.
_size)
536 execute_and_map_return_codes(lib_loader.dll().peak_icv_Image_SetCaptureInformation, self.
_handle_handle,
537 c_capture_information, ctypes.sizeof(c_capture_information))
538 self.
_metadata = add_metadata_from_capture_information_to_metadata(metadata, c_capture_information)
543 Subtracts the specified subtrahend image pixelwise from the current image and returns the resulting image.
545 This function performs a pixelwise subtraction of the given subtrahend image from the current image.
546 The resulting image is returned as a new `Image` object.
547 The operation requires that both images have the same pixel format and dimensions.
549 @param subtrahend: The image to subtract from the current image.
550 @return The resulting image after pixelwise subtraction.
552 @since ids_peak_icv 1.0
554 difference = Image.create_from_pixel_format_and_size(self.
_pixel_format, self.
size)
555 execute_and_map_return_codes(lib_loader.dll().peak_icv_Image_Subtract, self.
_handle_handle,
556 subtrahend._handle, difference._handle)
559 def __sub__(self, subtrahend: Image) -> Image:
562 Subtracts the specified subtrahend image pixelwise from the current image and returns the resulting image.
564 This function performs a pixelwise subtraction of the given subtrahend image from the current image.
565 The resulting image is returned as a new `Image` object.
566 The operation requires that both images have the same pixel format and dimensions.
568 @param subtrahend: The image to subtract from the current image.
569 @return The resulting image after pixelwise subtraction.
571 @since ids_peak_icv 1.0
576 if not isinstance(other, Image):
580 if self.
size != other.size:
587 is_equal = ctypes.c_bool()
588 execute_and_map_return_codes(lib_loader.dll().peak_icv_Image_Compare, self.
_handle_handle, other._handle,
589 ctypes.pointer(is_equal))
591 return is_equal.value
601 Calculate the byte size of an image buffer for a given size and pixel format.
603 @param size: The image dimensions (width, height) in pixels.
604 @param bits_per_pixel: The number of storage bits per pixel for the pixel format.
605 @param packed_or_yuv: If True, assumes packed or yuv format (truncate bits to full bytes).
606 If False, assumes other format (round up to nearest byte).
607 @return The byte size of the buffer.
609 total_bits = int(size.width) * int(size.height) * bits_per_pixel
611 return math.ceil(total_bits / 8)
613 return (total_bits + 7) // 8
617 if isinstance(image_var, ids_peak_ipl.Image):
618 return Image.create_from_ids_peak_ipl_image(image_var)
624 if not isinstance(image_var, Image):
626 "Parameter images has to be a numpy array of ids_peak_icv images.")
A size consists of width and height where width and height have to be greater than or equal to zero.
Image Class for working with algorithms.
Region region(self)
Retrieves the region.Region associated with the specified image_handle.
Image create_from_ids_peak_ipl_image(cls, ids_peak_ipl.Image ids_peak_ipl_image)
Creates an instance of Image using a ids_peak_ipl image.
Image create_from_np_array(cls, PixelFormat pixel_format, Size size, np.ndarray data)
Creates an instance of Image using a PixelFormat, size.Size and numpy image buffer.
None region(self, Region region)
Updates the region.Region on the image.
Image __sub__(self, Image subtrahend)
Subtracts the specified subtrahend image pixelwise from the current image and returns the resulting i...
None __init__(self, *Any args, **Any kwargs)
In order to create an Image, please refer to the classmethods.
None save(self, str output_file_path)
Saves an instance to a file on disk.
Image create_from_pixel_format_and_size(cls, PixelFormat pixel_format, Size size)
Creates an instance of Image using a size.Size and a pixel format.
bool __eq__(self, object other)
None reset_region(self)
Resets the region.Region on the image to the complete image.
Image convert_pixel_format_with_factor(self, PixelFormat pixel_format, float factor)
Converts an image into another pixel format with a factor multiplied to every pixel.
PixelFormat pixel_format(self)
None __del__(self)
Destructor for Image class.
Metadata metadata(self)
Retrieves the metadata.Metadata of an image which contains information about the image capture settin...
int height(self)
Provides the height of the image.
Image _create_from_np_array(cls, PixelFormat pixel_format, Size size, np.ndarray data)
Image convert_pixel_format(self, PixelFormat pixel_format)
Converts the image to a different pixel format.
Size size(self)
Provides the size.Size of the image.
Image subtract(self, Image subtrahend)
Subtracts the specified subtrahend image pixelwise from the current image and returns the resulting i...
Image create_from_file(cls, str image_path, PixelFormat|None forced_pixel_format=None)
Creates an Image from a file path.
Image create_from_image_view(cls, IImageView image_view)
Creates an instance of Image using an IImageView interface.
np.ndarray to_numpy_array(self, bool copy=True)
Provides a numpy representation of the image.
int width(self)
Provides the width of the image.
Represents a region consisting of a set of points in the image plane.
Thrown when a requested operation is not implemented or supported.
Thrown when a value is outside the allowed range.
int _get_byte_size(Size size, int bits_per_pixel, bool packed_or_yuv)
Calculate the byte size of an image buffer for a given size and pixel format.
Any _check_and_get_icv_image(Any image_var)
Any _check_icv_image(Any image_var)