improutils.preprocessing package

Submodules

improutils.preprocessing.calibration module

improutils.preprocessing.calibration.calibration_stats(reprojection_error: float, camera_matrix: ndarray, dist_coeffs: ndarray, std_deviations_intrinsics: ndarray | None = None, per_view_errors: ndarray | None = None, view_names: List[str] | None = None, pixel_size: float | Tuple[float, float] | None = None) None[source]

Print calibration statistics.

RMS re-projection error, estimated intrinsics and distortion parameters, with standard deviations, focal length in millimeters, and per-view reprojection errors.

Parameters:
  • reprojection_error (float) – Re-projection error from cv2.calibrateCamera.

  • camera_matrix (np.ndarray) – Camera matrix from cv2.calibrate.

  • dist_coeffs (np.ndarray) – Distortion coefficients from cv2.calibrateCamera.

  • std_deviations_intrinsics (np.ndarray, optional) – Standard deviations of intrinsics from cv2.calibrateCameraExtended. Defaults to None.

  • per_view_errors (np.ndarray, optional) – Per-view errors from cv2.calibrateCameraExtended. Defaults to None.

  • view_names (List[str], optional) – Image names for which the chessboard was detected. Defaults to None.

  • pixel_size (Union[float, Tuple[float, float]], optional) – Size of physical pixels of a camera in micrometers (e.g., 4.8, 5.86, or [5.86, 4.8] for non-square pixels). Defaults to None.

improutils.preprocessing.calibration.camera_calibration(calib_path: str, chess_shape: Tuple[int, int], cv2_flags: int = 0, extensions: List[str] = ['jpg', 'jpeg', 'png', 'tiff', 'bmp']) Tuple[float, ndarray, ndarray, List[ndarray], List[ndarray], ndarray, ndarray, ndarray, Dict[str, ndarray]][source]

Calibrate camera from images with chessboard pattern, using OpenCV’s cv2.calibrateCameraExtended function.

Parameters:
  • calib_path (str) – Path to the folder containing chessboard pattern images.

  • chess_shape (Tuple[int, int]) – Interior corner count in the format of rows, columns.

  • cv2_flags (int, optional) – Additional OpenCV flags for cv2.calibrateCameraExtended. Defaults to 0.

  • extensions (List[str], optional) – Allowed image extensions. Defaults to [“jpg”, “jpeg”, “png”, “tiff”].

Returns:

Returns the output from cv2.calibrateCameraExtended and a dictionary with image names as keys and images with drawn chessboard corners as values.

Return type:

Tuple[float, np.ndarray, np.ndarray, Tuple[np.ndarray], Tuple[np.ndarray], np.ndarray, np.ndarray, np.ndarray, Dict[str,np.ndarray]]

Raises:
  • ValueError – If calibration images have different sizes.

  • ValueError – If no calibration images were found or could not be read from the provided path.

  • ValueError – If no chessboard patterns were detected in the images.

improutils.preprocessing.calibration.correct_frame(frame, camera_matrix, dist_coeffs)[source]

Return an undistorted frame.

improutils.preprocessing.calibration.load_camera_calib(input_file)[source]

Load camera calibration from specified input file.

Parameters:

input_file (string) – Input file with calibration data in YAML format.

Returns:

Returns a tuple where first element is camera matrix array and second element is dist coefficients array. These arrays might be empty if the file isn’t found or in correct format.

Return type:

tuple(ndarray, ndarray)

improutils.preprocessing.calibration.save_camera_calib(output_file, camera_matrix, dist_coefs)[source]

Save camera calibration data to a YAML file.

This function stores the camera matrix and distortion coefficients in the specified output file. If the parent directory does not exist, it is created automatically.

Parameters:
  • output_file (str) – Path to the output YAML file where calibration data will be saved.

  • camera_matrix (ndarray) – Camera intrinsic matrix.

  • dist_coefs (ndarray) – Distortion coefficients of the camera.

Return type:

None

improutils.preprocessing.contours module

improutils.preprocessing.contours.contour_to_image(contour, img_bin, size=None)[source]

Create a new image from the contour.

Parameters:
  • contour (ndarray) – Contour that represents the area from image to be cropped.

  • img_bin (ndarray) – Input binary image.

  • size (tuple) – Optional size of the created image. If it’s not used, the image’s size is the same as the size of bounding rectangle of the input contour.

Return type:

Output cropped image.

improutils.preprocessing.contours.fill_holes(img_bin, close=False, size=5)[source]

Fill the holes in found contours. It could merge the contour using close input with appropriate size.

Parameters:
  • img_bin (ndarray) – Input binary image.

  • close (boolean) – If it should merge contours with missing points using close operation.

  • size (int) – Size of the close operation element.

  • fill (bool, optional) – If True, filled contours are drawn; if False, contours are drawn as outlines. Default is True.

Return type:

Output binary image.

improutils.preprocessing.contours.find_contours(img_bin, min_area=0, max_area=inf, fill=True, external=True)[source]

Find contours in a binary image and filter them by area.

The function counts the filtered contours and draws them on a new binary image. Contours can be optionally filled or just outlined, and either external or all contours can be considered.

Parameters:
  • img_bin (ndarray) – Input binary image.

  • min_area (int, optional) – Minimum contour area to keep (smaller contours are discarded). Default is 0.

  • max_area (int or float, optional) – Maximum contour area to keep (larger contours are discarded). Default is np.inf.

  • fill (bool, optional) – If True, filled contours are drawn; if False, contours are drawn as outlines. Default is True.

  • external (bool, optional) – If True, only external contours are considered; if False, all contours are considered. Default is True.

Returns:

  • contour_drawn (ndarray) – Output binary image with drawn filtered contours.

  • count (int) – Number of filtered contours.

  • contours (list of ndarray) – List of filtered contour arrays.

improutils.preprocessing.contours.find_holes(img_bin, min_area=0, max_area=inf, fill=True)[source]

Find inner contours (holes) in a binary image and filter them by area.

The function identifies contours that are inside other contours (holes), filters them based on the specified minimum and maximum area, and draws them on a new binary image. Contours can be optionally filled or outlined.

Parameters:
  • img_bin (ndarray) – Input binary image.

  • min_area (int, optional) – Minimum contour area to keep (smaller contours are discarded). Default is 0.

  • max_area (int or float, optional) – Maximum contour area to keep (larger contours are discarded). Default is np.inf.

  • fill (bool, optional) – If True, filled contours are drawn; if False, contours are drawn as outlines. Default is True.

Returns:

  • contours_drawn (ndarray) – Output binary image with drawn filtered hole contours.

  • count (int) – Number of filtered hole contours.

  • contours (list of ndarray) – List of filtered hole contour arrays.

improutils.preprocessing.contours.get_center(contour)[source]

Get the center of a contour in pixels in tuple format.

Parameters:

contour (ndarray) – input contour.

Return type:

A tuple with x and y coordinates of the contour’s center.

improutils.preprocessing.morphology module

improutils.preprocessing.morphology.black_hat(img, kernel_size=3, iterations=1, kernel=None)[source]

Apply the black hat morphological operation on the input image.

Parameters:
  • img (numpy.ndarray) – The input image.

  • kernel_size (int, optional) – The size of the kernel used for the operation. Default is 3.

  • iterations (int, optional) – The number of times the operation is applied. Default is 1.

  • kernel (numpy.ndarray, optional) – The kernel used for the operation. If not provided, a square kernel of size kernel_size will be used.

Return type:

The image after applying the black hat operation.

improutils.preprocessing.morphology.close(img, kernel_size=3, iterations=1, kernel=None)[source]

Apply morphological closing operation on the input image.

Parameters:
  • img (numpy.ndarray) – The input image.

  • kernel_size (int, optional) – The size of the kernel used for the closing operation. Default is 3.

  • iterations (int, optional) – The number of times the closing operation is applied. Default is 1.

  • kernel (numpy.ndarray, optional) – The custom kernel used for the closing operation. If not provided, a square kernel of size kernel_size will be used.

Return type:

The image after applying the closing operation.

improutils.preprocessing.morphology.dilate(img, kernel_size=3, iterations=1, kernel=None)[source]

Dilate an image using the specified kernel.

Parameters:
  • img (numpy.ndarray) – The input image to be dilated.

  • kernel_size (int, optional) – The size of the kernel used for dilation. Default is 3.

  • iterations (int, optional) – The number of times the dilation is applied. Default is 1.

  • kernel (numpy.ndarray, optional) – The custom kernel to be used for dilation. If not provided, a square kernel of size kernel_size will be used.

Return type:

The dilated image.

improutils.preprocessing.morphology.erode(img, kernel_size=3, iterations=1, kernel=None)[source]

Erode an image using the specified kernel.

Parameters:
  • img (numpy.ndarray) – The input image to be eroded.

  • kernel_size (int, optional) – The size of the kernel used for erosion. Default is 3.

  • iterations (int, optional) – The number of times erosion is applied. Default is 1.

  • kernel (numpy.ndarray, optional) – The custom kernel to be used for erosion. If not provided, a square kernel of size kernel_size will be used.

Return type:

The eroded image.

improutils.preprocessing.morphology.morphological_gradient(img, kernel_size=3, iterations=1, kernel=None)[source]

Apply morphological gradient operation on the input image.

Parameters:
  • img (numpy.ndarray) – The input image.

  • kernel_size (int, optional) – The size of the kernel used for morphological operations. Defaults to 3.

  • iterations (int, optional) – The number of times the morphological operation is applied. Defaults to 1.

  • kernel (numpy.ndarray, optional) – The custom kernel used for morphological operations. If not provided, a square kernel of size kernel_size will be used.

Return type:

The image after applying the morphological gradient operation.

improutils.preprocessing.morphology.open(img, kernel_size=3, iterations=1, kernel=None)[source]

Applz the morphological opening operation on the input image.

Parameters:
  • img (numpy.ndarray) – The input image.

  • kernel_size (int, optional) – The size of the kernel used for the opening operation. Default is 3.

  • iterations (int, optional) – The number of times the opening operation is applied. Default is 1.

  • kernel (numpy.ndarray, optional) – The custom kernel used for the opening operation. If not provided, a square kernel of size kernel_size will be used.

Return type:

The image after applying the morphological opening operation.

improutils.preprocessing.morphology.top_hat(img, kernel_size=3, iterations=1, kernel=None)[source]

Apply the top hat morphological operation on the input image.

Parameters:
  • img (numpy.ndarray) – The input image.

  • kernel_size (int, optional) – The size of the kernel used for the operation. Default is 3.

  • iterations (int, optional) – The number of times the operation is applied. Default is 1.

  • kernel (numpy.ndarray, optional) – The custom kernel to be used for the operation. If not provided, a square kernel of size kernel_size will be used.

Return type:

The image after applying the top hat operation.

improutils.preprocessing.preprocessing module

improutils.preprocessing.preprocessing.crop(img, tl_x, tl_y, br_x, br_y)[source]

Crop an image by added coordinates.

Parameters:
  • img (ndarray) – Input image.

  • tl_x (int) – TOP-LEFT corner’s x-coordinate

  • tl_y (int) – TOP-LEFT corner’s y-coordinate

  • br_x (int) – BOTTOM-RIGHT corner’s x-coordinate

  • br_y (int) – BOTTOM-RIGHT corner’s y-coordinate

Return type:

Output image.

improutils.preprocessing.preprocessing.crop_by_bounding_rect(img_bin)[source]

Crop binary image by ONE bounding rectangle corresponding to ALL objects in the binary image.

Parameters:

img_bin (ndarray) – Input binary image.

Return type:

Output cropped image.

improutils.preprocessing.preprocessing.crop_contour(contour, img)[source]

Crop contour in respect to its bounding rectangle.

It’s the fastest method, but could include other parts of image than just contour if the contour is irregulary shaped.

Parameters:
  • contour (ndarray) – Contour that represents the area from image to be cropped. The bounding rectangle of contour is used.

  • img (ndarray) – Input image.

Return type:

Output cropped image.

improutils.preprocessing.preprocessing.negative(img)[source]

Convert image to its negative.

Parameters:

img (ndarray) – Input image.

Return type:

Output image.

improutils.preprocessing.preprocessing.normalize(img)[source]

Normalize image using min-max normalization from its values to values 0 - 255.

Parameters:

img (ndarray) – Input image.

improutils.preprocessing.preprocessing.normalize2BGR_image(img)[source]

Normalize image using min-max and converts it to BGR.

Parameters:

img (ndarray) – Input image

Returns:

_ – Normalized image in BGR

Return type:

ndarray

improutils.preprocessing.preprocessing.polar_warp(img, size=None, full_radius=True, inverse=False)[source]

Apply a polar warp to an image.

This function performs a polar coordinate transformation on the input image. It can optionally produce a full-radius warp or use an inverse mapping.

Parameters:
  • img (ndarray) – Input image to be warped.

  • size (tuple of int, optional) – Size of the destination image as (height, width). Default is None, which transforms size to (pi*Rm,Rm), see cv2.warpPolar documentation.

  • full_radius (bool, optional) – If True, the warp uses the full radius of the image (diagonal from center to corner). If False, only the vertical radius is used. Default is True.

  • inverse (bool, optional) – If True, applies the inverse warp mapping. Default is False.

Returns:

Warped image in polar coordinates.

Return type:

ndarray

improutils.preprocessing.preprocessing.resize(image, size, method=3)[source]

Resize the image to the preffered size.

Method of resizing is well suited for making the images smaller rather than larger (cv2.INTER_AREA). For making images larger, use other cv2.INTER_### instead.

Parameters:
  • image (ndarray) – Contour that represents the area from image to be cropped.

  • size (tuple) – New size of the resized image.

  • method (int) – Optional argument. For more information see cv2.INTER_### parameters.

Return type:

Output resized image.

improutils.preprocessing.preprocessing.rotate(image, angle, image_center=None)[source]

Rotate the input image by specified angle.

Parameters:
  • image (ndarray) – Image to be rotated.

  • angle (float) – Rotation angle.

  • image_center (Optional[tuple(int, int)]) – Center of rotation.

Returns:

Returns the rotated input image by specified angle.

Return type:

ndarray

improutils.preprocessing.preprocessing.to_gray(img_bgr)[source]

Convert image to monochrome.

Parameters:

img_bgr (ndarray) – Input image.

Return type:

Output image.

improutils.preprocessing.preprocessing.to_hsv(img_bgr)[source]

Convert image to HSV (hue, saturation, value) color space.

Parameters:

img_bgr (ndarray) – Input image.

Return type:

Output image.

improutils.preprocessing.preprocessing.to_rgb(img_bgr)[source]

Convert image to RGB (red, green, blue) color space from BGR.

Parameters:

img_bgr (ndarray) – Input image.

Return type:

Output image.

improutils.preprocessing.preprocessing.warp_to_cartesian(img, size=None, full_radius=True)[source]

Warp an image from polar coordinates back to Cartesian coordinates.

This function applies an inverse polar coordinate transformation to the input image using the polar_warp helper function.

Parameters:
  • img (ndarray) – Input image to be warped.

  • size (tuple of int, optional) – Size of the output image as (height, width). Default is None, which preserves the original image size.

  • full_radius (bool, optional) – If True, uses the full image diagonal as the radius for the warp. If False, uses only the vertical radius. Default is True.

Returns:

Image warped back to Cartesian coordinates.

Return type:

ndarray

improutils.preprocessing.preprocessing.warp_to_polar(img, size=None, full_radius=True)[source]

Warp an image to polar coordinates.

This function applies a polar coordinate transformation to the input image using the polar_warp helper function.

Parameters:
  • img (ndarray) – Input image to be warped.

  • size (tuple of int, optional) – Size of the output image as (height, width). Default is None, which preserves the original image size.

  • full_radius (bool, optional) – If True, uses the full image diagonal as the radius for the warp. If False, uses only the vertical radius. Default is True.

Returns:

Image warped to polar coordinates.

Return type:

ndarray

Module contents

improutils.preprocessing.black_hat(img, kernel_size=3, iterations=1, kernel=None)[source]

Apply the black hat morphological operation on the input image.

Parameters:
  • img (numpy.ndarray) – The input image.

  • kernel_size (int, optional) – The size of the kernel used for the operation. Default is 3.

  • iterations (int, optional) – The number of times the operation is applied. Default is 1.

  • kernel (numpy.ndarray, optional) – The kernel used for the operation. If not provided, a square kernel of size kernel_size will be used.

Return type:

The image after applying the black hat operation.

improutils.preprocessing.calibration_stats(reprojection_error: float, camera_matrix: ndarray, dist_coeffs: ndarray, std_deviations_intrinsics: ndarray | None = None, per_view_errors: ndarray | None = None, view_names: List[str] | None = None, pixel_size: float | Tuple[float, float] | None = None) None[source]

Print calibration statistics.

RMS re-projection error, estimated intrinsics and distortion parameters, with standard deviations, focal length in millimeters, and per-view reprojection errors.

Parameters:
  • reprojection_error (float) – Re-projection error from cv2.calibrateCamera.

  • camera_matrix (np.ndarray) – Camera matrix from cv2.calibrate.

  • dist_coeffs (np.ndarray) – Distortion coefficients from cv2.calibrateCamera.

  • std_deviations_intrinsics (np.ndarray, optional) – Standard deviations of intrinsics from cv2.calibrateCameraExtended. Defaults to None.

  • per_view_errors (np.ndarray, optional) – Per-view errors from cv2.calibrateCameraExtended. Defaults to None.

  • view_names (List[str], optional) – Image names for which the chessboard was detected. Defaults to None.

  • pixel_size (Union[float, Tuple[float, float]], optional) – Size of physical pixels of a camera in micrometers (e.g., 4.8, 5.86, or [5.86, 4.8] for non-square pixels). Defaults to None.

improutils.preprocessing.camera_calibration(calib_path: str, chess_shape: Tuple[int, int], cv2_flags: int = 0, extensions: List[str] = ['jpg', 'jpeg', 'png', 'tiff', 'bmp']) Tuple[float, ndarray, ndarray, List[ndarray], List[ndarray], ndarray, ndarray, ndarray, Dict[str, ndarray]][source]

Calibrate camera from images with chessboard pattern, using OpenCV’s cv2.calibrateCameraExtended function.

Parameters:
  • calib_path (str) – Path to the folder containing chessboard pattern images.

  • chess_shape (Tuple[int, int]) – Interior corner count in the format of rows, columns.

  • cv2_flags (int, optional) – Additional OpenCV flags for cv2.calibrateCameraExtended. Defaults to 0.

  • extensions (List[str], optional) – Allowed image extensions. Defaults to [“jpg”, “jpeg”, “png”, “tiff”].

Returns:

Returns the output from cv2.calibrateCameraExtended and a dictionary with image names as keys and images with drawn chessboard corners as values.

Return type:

Tuple[float, np.ndarray, np.ndarray, Tuple[np.ndarray], Tuple[np.ndarray], np.ndarray, np.ndarray, np.ndarray, Dict[str,np.ndarray]]

Raises:
  • ValueError – If calibration images have different sizes.

  • ValueError – If no calibration images were found or could not be read from the provided path.

  • ValueError – If no chessboard patterns were detected in the images.

improutils.preprocessing.close(img, kernel_size=3, iterations=1, kernel=None)[source]

Apply morphological closing operation on the input image.

Parameters:
  • img (numpy.ndarray) – The input image.

  • kernel_size (int, optional) – The size of the kernel used for the closing operation. Default is 3.

  • iterations (int, optional) – The number of times the closing operation is applied. Default is 1.

  • kernel (numpy.ndarray, optional) – The custom kernel used for the closing operation. If not provided, a square kernel of size kernel_size will be used.

Return type:

The image after applying the closing operation.

improutils.preprocessing.contour_to_image(contour, img_bin, size=None)[source]

Create a new image from the contour.

Parameters:
  • contour (ndarray) – Contour that represents the area from image to be cropped.

  • img_bin (ndarray) – Input binary image.

  • size (tuple) – Optional size of the created image. If it’s not used, the image’s size is the same as the size of bounding rectangle of the input contour.

Return type:

Output cropped image.

improutils.preprocessing.correct_frame(frame, camera_matrix, dist_coeffs)[source]

Return an undistorted frame.

improutils.preprocessing.crop(img, tl_x, tl_y, br_x, br_y)[source]

Crop an image by added coordinates.

Parameters:
  • img (ndarray) – Input image.

  • tl_x (int) – TOP-LEFT corner’s x-coordinate

  • tl_y (int) – TOP-LEFT corner’s y-coordinate

  • br_x (int) – BOTTOM-RIGHT corner’s x-coordinate

  • br_y (int) – BOTTOM-RIGHT corner’s y-coordinate

Return type:

Output image.

improutils.preprocessing.crop_by_bounding_rect(img_bin)[source]

Crop binary image by ONE bounding rectangle corresponding to ALL objects in the binary image.

Parameters:

img_bin (ndarray) – Input binary image.

Return type:

Output cropped image.

improutils.preprocessing.crop_contour(contour, img)[source]

Crop contour in respect to its bounding rectangle.

It’s the fastest method, but could include other parts of image than just contour if the contour is irregulary shaped.

Parameters:
  • contour (ndarray) – Contour that represents the area from image to be cropped. The bounding rectangle of contour is used.

  • img (ndarray) – Input image.

Return type:

Output cropped image.

improutils.preprocessing.dilate(img, kernel_size=3, iterations=1, kernel=None)[source]

Dilate an image using the specified kernel.

Parameters:
  • img (numpy.ndarray) – The input image to be dilated.

  • kernel_size (int, optional) – The size of the kernel used for dilation. Default is 3.

  • iterations (int, optional) – The number of times the dilation is applied. Default is 1.

  • kernel (numpy.ndarray, optional) – The custom kernel to be used for dilation. If not provided, a square kernel of size kernel_size will be used.

Return type:

The dilated image.

improutils.preprocessing.erode(img, kernel_size=3, iterations=1, kernel=None)[source]

Erode an image using the specified kernel.

Parameters:
  • img (numpy.ndarray) – The input image to be eroded.

  • kernel_size (int, optional) – The size of the kernel used for erosion. Default is 3.

  • iterations (int, optional) – The number of times erosion is applied. Default is 1.

  • kernel (numpy.ndarray, optional) – The custom kernel to be used for erosion. If not provided, a square kernel of size kernel_size will be used.

Return type:

The eroded image.

improutils.preprocessing.fill_holes(img_bin, close=False, size=5)[source]

Fill the holes in found contours. It could merge the contour using close input with appropriate size.

Parameters:
  • img_bin (ndarray) – Input binary image.

  • close (boolean) – If it should merge contours with missing points using close operation.

  • size (int) – Size of the close operation element.

  • fill (bool, optional) – If True, filled contours are drawn; if False, contours are drawn as outlines. Default is True.

Return type:

Output binary image.

improutils.preprocessing.find_contours(img_bin, min_area=0, max_area=inf, fill=True, external=True)[source]

Find contours in a binary image and filter them by area.

The function counts the filtered contours and draws them on a new binary image. Contours can be optionally filled or just outlined, and either external or all contours can be considered.

Parameters:
  • img_bin (ndarray) – Input binary image.

  • min_area (int, optional) – Minimum contour area to keep (smaller contours are discarded). Default is 0.

  • max_area (int or float, optional) – Maximum contour area to keep (larger contours are discarded). Default is np.inf.

  • fill (bool, optional) – If True, filled contours are drawn; if False, contours are drawn as outlines. Default is True.

  • external (bool, optional) – If True, only external contours are considered; if False, all contours are considered. Default is True.

Returns:

  • contour_drawn (ndarray) – Output binary image with drawn filtered contours.

  • count (int) – Number of filtered contours.

  • contours (list of ndarray) – List of filtered contour arrays.

improutils.preprocessing.find_holes(img_bin, min_area=0, max_area=inf, fill=True)[source]

Find inner contours (holes) in a binary image and filter them by area.

The function identifies contours that are inside other contours (holes), filters them based on the specified minimum and maximum area, and draws them on a new binary image. Contours can be optionally filled or outlined.

Parameters:
  • img_bin (ndarray) – Input binary image.

  • min_area (int, optional) – Minimum contour area to keep (smaller contours are discarded). Default is 0.

  • max_area (int or float, optional) – Maximum contour area to keep (larger contours are discarded). Default is np.inf.

  • fill (bool, optional) – If True, filled contours are drawn; if False, contours are drawn as outlines. Default is True.

Returns:

  • contours_drawn (ndarray) – Output binary image with drawn filtered hole contours.

  • count (int) – Number of filtered hole contours.

  • contours (list of ndarray) – List of filtered hole contour arrays.

improutils.preprocessing.get_center(contour)[source]

Get the center of a contour in pixels in tuple format.

Parameters:

contour (ndarray) – input contour.

Return type:

A tuple with x and y coordinates of the contour’s center.

improutils.preprocessing.load_camera_calib(input_file)[source]

Load camera calibration from specified input file.

Parameters:

input_file (string) – Input file with calibration data in YAML format.

Returns:

Returns a tuple where first element is camera matrix array and second element is dist coefficients array. These arrays might be empty if the file isn’t found or in correct format.

Return type:

tuple(ndarray, ndarray)

improutils.preprocessing.morphological_gradient(img, kernel_size=3, iterations=1, kernel=None)[source]

Apply morphological gradient operation on the input image.

Parameters:
  • img (numpy.ndarray) – The input image.

  • kernel_size (int, optional) – The size of the kernel used for morphological operations. Defaults to 3.

  • iterations (int, optional) – The number of times the morphological operation is applied. Defaults to 1.

  • kernel (numpy.ndarray, optional) – The custom kernel used for morphological operations. If not provided, a square kernel of size kernel_size will be used.

Return type:

The image after applying the morphological gradient operation.

improutils.preprocessing.negative(img)[source]

Convert image to its negative.

Parameters:

img (ndarray) – Input image.

Return type:

Output image.

improutils.preprocessing.normalize(img)[source]

Normalize image using min-max normalization from its values to values 0 - 255.

Parameters:

img (ndarray) – Input image.

improutils.preprocessing.normalize2BGR_image(img)[source]

Normalize image using min-max and converts it to BGR.

Parameters:

img (ndarray) – Input image

Returns:

_ – Normalized image in BGR

Return type:

ndarray

improutils.preprocessing.open(img, kernel_size=3, iterations=1, kernel=None)[source]

Applz the morphological opening operation on the input image.

Parameters:
  • img (numpy.ndarray) – The input image.

  • kernel_size (int, optional) – The size of the kernel used for the opening operation. Default is 3.

  • iterations (int, optional) – The number of times the opening operation is applied. Default is 1.

  • kernel (numpy.ndarray, optional) – The custom kernel used for the opening operation. If not provided, a square kernel of size kernel_size will be used.

Return type:

The image after applying the morphological opening operation.

improutils.preprocessing.polar_warp(img, size=None, full_radius=True, inverse=False)[source]

Apply a polar warp to an image.

This function performs a polar coordinate transformation on the input image. It can optionally produce a full-radius warp or use an inverse mapping.

Parameters:
  • img (ndarray) – Input image to be warped.

  • size (tuple of int, optional) – Size of the destination image as (height, width). Default is None, which transforms size to (pi*Rm,Rm), see cv2.warpPolar documentation.

  • full_radius (bool, optional) – If True, the warp uses the full radius of the image (diagonal from center to corner). If False, only the vertical radius is used. Default is True.

  • inverse (bool, optional) – If True, applies the inverse warp mapping. Default is False.

Returns:

Warped image in polar coordinates.

Return type:

ndarray

improutils.preprocessing.resize(image, size, method=3)[source]

Resize the image to the preffered size.

Method of resizing is well suited for making the images smaller rather than larger (cv2.INTER_AREA). For making images larger, use other cv2.INTER_### instead.

Parameters:
  • image (ndarray) – Contour that represents the area from image to be cropped.

  • size (tuple) – New size of the resized image.

  • method (int) – Optional argument. For more information see cv2.INTER_### parameters.

Return type:

Output resized image.

improutils.preprocessing.rotate(image, angle, image_center=None)[source]

Rotate the input image by specified angle.

Parameters:
  • image (ndarray) – Image to be rotated.

  • angle (float) – Rotation angle.

  • image_center (Optional[tuple(int, int)]) – Center of rotation.

Returns:

Returns the rotated input image by specified angle.

Return type:

ndarray

improutils.preprocessing.save_camera_calib(output_file, camera_matrix, dist_coefs)[source]

Save camera calibration data to a YAML file.

This function stores the camera matrix and distortion coefficients in the specified output file. If the parent directory does not exist, it is created automatically.

Parameters:
  • output_file (str) – Path to the output YAML file where calibration data will be saved.

  • camera_matrix (ndarray) – Camera intrinsic matrix.

  • dist_coefs (ndarray) – Distortion coefficients of the camera.

Return type:

None

improutils.preprocessing.to_gray(img_bgr)[source]

Convert image to monochrome.

Parameters:

img_bgr (ndarray) – Input image.

Return type:

Output image.

improutils.preprocessing.to_hsv(img_bgr)[source]

Convert image to HSV (hue, saturation, value) color space.

Parameters:

img_bgr (ndarray) – Input image.

Return type:

Output image.

improutils.preprocessing.to_rgb(img_bgr)[source]

Convert image to RGB (red, green, blue) color space from BGR.

Parameters:

img_bgr (ndarray) – Input image.

Return type:

Output image.

improutils.preprocessing.top_hat(img, kernel_size=3, iterations=1, kernel=None)[source]

Apply the top hat morphological operation on the input image.

Parameters:
  • img (numpy.ndarray) – The input image.

  • kernel_size (int, optional) – The size of the kernel used for the operation. Default is 3.

  • iterations (int, optional) – The number of times the operation is applied. Default is 1.

  • kernel (numpy.ndarray, optional) – The custom kernel to be used for the operation. If not provided, a square kernel of size kernel_size will be used.

Return type:

The image after applying the top hat operation.

improutils.preprocessing.warp_to_cartesian(img, size=None, full_radius=True)[source]

Warp an image from polar coordinates back to Cartesian coordinates.

This function applies an inverse polar coordinate transformation to the input image using the polar_warp helper function.

Parameters:
  • img (ndarray) – Input image to be warped.

  • size (tuple of int, optional) – Size of the output image as (height, width). Default is None, which preserves the original image size.

  • full_radius (bool, optional) – If True, uses the full image diagonal as the radius for the warp. If False, uses only the vertical radius. Default is True.

Returns:

Image warped back to Cartesian coordinates.

Return type:

ndarray

improutils.preprocessing.warp_to_polar(img, size=None, full_radius=True)[source]

Warp an image to polar coordinates.

This function applies a polar coordinate transformation to the input image using the polar_warp helper function.

Parameters:
  • img (ndarray) – Input image to be warped.

  • size (tuple of int, optional) – Size of the output image as (height, width). Default is None, which preserves the original image size.

  • full_radius (bool, optional) – If True, uses the full image diagonal as the radius for the warp. If False, uses only the vertical radius. Default is True.

Returns:

Image warped to polar coordinates.

Return type:

ndarray