xrf_explorer.server.image_register package

Submodules

xrf_explorer.server.image_register.register_image module

apply_perspective_transformation(image_to_transform: Mat | ndarray, points_src: Mat | ndarray, points_dest: Mat | ndarray) Mat | ndarray[source]

Applies a perspective transformation on an image based on source and destination points.

Parameters:
  • image_to_transform – A MatLike representation of the image to be transformed

  • points_src – A MatLike representation of the source points

  • points_dest – A MatLike representation of the destination points

compute_fitting_dimensions_by_aspect(image_to_resize_height: int, image_to_resize_width: int, image_reference_height: int, image_reference_width: int) tuple[int, int][source]

Computes the dimensions to which an image (specified by its dimensions image_to_resize_height image_to_resize_width) should be resized to fit the aspect ratio of a reference image.

Parameters:
  • image_to_resize_height – The height of the image for which the dimensions are computed

  • image_to_resize_width – The width of the image for which the dimensions are computed

  • image_reference_height – The height of the reference image

  • image_reference_width – The width of the reference image

Returns:

A tuple (height, width) specifying the computed dimensions

getPerspectiveTransform(src, dst[, solveMethod]) retval

. @brief Calculates a perspective transform from four pairs of the corresponding points. . . The function calculates the f$3 times 3f$ matrix of a perspective transform so that: . . f[begin{bmatrix} t_i x’_i \ t_i y’_i \ t_i end{bmatrix} = texttt{map_matrix} cdot begin{bmatrix} x_i \ y_i \ 1 end{bmatrix}f] . . where . . f[dst(i)=(x’_i,y’_i), src(i)=(x_i, y_i), i=0,1,2,3f] . . @param src Coordinates of quadrangle vertices in the source image. . @param dst Coordinates of the corresponding quadrangle vertices in the destination image. . @param solveMethod method passed to cv::solve (#DecompTypes) . . @sa findHomography, warpPerspective, perspectiveTransform

get_image_registered_to_data_cube(data_source: str, image_name: str) Mat | ndarray | None[source]

Registers an image to align with the dimensions of the data cube.

Parameters:
  • data_source – The name of the data source

  • image_name – The name of the image to be registered

Returns:

The registered image in BGR format or None in case of an error

imread(filename[, flags]) retval

. @brief Loads an image from a file. . . @anchor imread . . The function imread loads an image from the specified file and returns it. If the image cannot be . read (because of missing file, improper permissions, unsupported or invalid format), the function . returns an empty matrix ( Mat::data==NULL ). . . Currently, the following file formats are supported: . . - Windows bitmaps - *.bmp, *.dib (always supported) . - JPEG files - *.jpeg, *.jpg, *.jpe (see the Note section) . - JPEG 2000 files - *.jp2 (see the Note section) . - Portable Network Graphics - *.png (see the Note section) . - WebP - *.webp (see the Note section) . - AVIF - *.avif (see the Note section) . - Portable image format - *.pbm, *.pgm, *.ppm *.pxm, *.pnm (always supported) . - PFM files - *.pfm (see the Note section) . - Sun rasters - *.sr, *.ras (always supported) . - TIFF files - *.tiff, *.tif (see the Note section) . - OpenEXR Image files - *.exr (see the Note section) . - Radiance HDR - *.hdr, *.pic (always supported) . - Raster and Vector geospatial data supported by GDAL (see the Note section) . . @note . - The function determines the type of an image by the content, not by the file extension. . - In the case of color images, the decoded images will have the channels stored in B G R order. . - When using IMREAD_GRAYSCALE, the codec’s internal grayscale conversion will be used, if available. . Results may differ to the output of cvtColor() . - On Microsoft Windows* OS and MacOSX*, the codecs shipped with an OpenCV image (libjpeg, . libpng, libtiff, and libjasper) are used by default. So, OpenCV can always read JPEGs, PNGs, . and TIFFs. On MacOSX, there is also an option to use native MacOSX image readers. But beware . that currently these native image loaders give images with different pixel values because of . the color management embedded into MacOSX. . - On Linux*, BSD flavors and other Unix-like open-source operating systems, OpenCV looks for . codecs supplied with an OS image. Install the relevant packages (do not forget the development . files, for example, “libjpeg-dev”, in Debian* and Ubuntu*) to get the codec support or turn . on the OPENCV_BUILD_3RDPARTY_LIBS flag in CMake. . - In the case you set WITH_GDAL flag to true in CMake and @ref IMREAD_LOAD_GDAL to load the image, . then the [GDAL](http://www.gdal.org) driver will be used in order to decode the image, supporting . the following formats: [Raster](http://www.gdal.org/formats_list.html), . [Vector](http://www.gdal.org/ogr_formats.html). . - If EXIF information is embedded in the image file, the EXIF orientation will be taken into account . and thus the image will be rotated accordingly except if the flags @ref IMREAD_IGNORE_ORIENTATION . or @ref IMREAD_UNCHANGED are passed. . - Use the IMREAD_UNCHANGED flag to keep the floating point values from PFM image. . - By default number of pixels must be less than 2^30. Limit can be set using system . variable OPENCV_IO_MAX_IMAGE_PIXELS . . @param filename Name of file to be loaded. . @param flags Flag that can take values of cv::ImreadModes

imwrite(filename, img[, params]) retval

. @brief Saves an image to a specified file. . . The function imwrite saves the image to the specified file. The image format is chosen based on the . filename extension (see cv::imread for the list of extensions). In general, only 8-bit unsigned (CV_8U) . single-channel or 3-channel (with ‘BGR’ channel order) images . can be saved using this function, with these exceptions: . . - With OpenEXR encoder, only 32-bit float (CV_32F) images can be saved. . - 8-bit unsigned (CV_8U) images are not supported. . - With Radiance HDR encoder, non 64-bit float (CV_64F) images can be saved. . - All images will be converted to 32-bit float (CV_32F). . - With JPEG 2000 encoder, 8-bit unsigned (CV_8U) and 16-bit unsigned (CV_16U) images can be saved. . - With PAM encoder, 8-bit unsigned (CV_8U) and 16-bit unsigned (CV_16U) images can be saved. . - With PNG encoder, 8-bit unsigned (CV_8U) and 16-bit unsigned (CV_16U) images can be saved. . - PNG images with an alpha channel can be saved using this function. To do this, create . 8-bit (or 16-bit) 4-channel image BGRA, where the alpha channel goes last. Fully transparent pixels . should have alpha set to 0, fully opaque pixels should have alpha set to 255/65535 (see the code sample below). . - With PGM/PPM encoder, 8-bit unsigned (CV_8U) and 16-bit unsigned (CV_16U) images can be saved. . - With TIFF encoder, 8-bit unsigned (CV_8U), 16-bit unsigned (CV_16U), . 32-bit float (CV_32F) and 64-bit float (CV_64F) images can be saved. . - Multiple images (vector of Mat) can be saved in TIFF format (see the code sample below). . - 32-bit float 3-channel (CV_32FC3) TIFF images will be saved . using the LogLuv high dynamic range encoding (4 bytes per pixel) . . If the image format is not supported, the image will be converted to 8-bit unsigned (CV_8U) and saved that way. . . If the format, depth or channel order is different, use . Mat::convertTo and cv::cvtColor to convert it before saving. Or, use the universal FileStorage I/O . functions to save the image to XML or YAML format. . . The sample below shows how to create a BGRA image, how to set custom compression parameters and save it to a PNG file. . It also demonstrates how to save multiple images in a TIFF file: . @include snippets/imgcodecs_imwrite.cpp . @param filename Name of the file. . @param img (Mat or vector of Mat) Image or Images to be saved. . @param params Format-specific parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, … .) see cv::ImwriteFlags

inverse_register_image(image: Mat | ndarray, new_width: int, new_height: int, points_source: ndarray, points_destination: ndarray) Mat | ndarray[source]

Inverse register the given image to match the new width and height with transformation given by the source and destination points.

Parameters:
  • image – The image to be inverse registered

  • new_width – The new width of the image

  • new_height – The new height of the image

  • points_source – The source points

  • points_destination – The destination points

Returns:

The inverse registered image

load_image_to_register(path_image_to_register: str) Mat | ndarray | None[source]

Loads an image from the specified path. Preserves the alpha channel of .png files.

Parameters:

path_image_to_register – Path of the image to be loaded for registering

Returns:

A MatLike representation of the image. If the image cannot be read, it returns None

load_points(path_points_csv_file: str) tuple[ndarray[float32], ndarray[float32]][source]

Loads the control points for the transformation from a CSV file, as generated by the butterfly_registrator.

Parameters:

path_points_csv_file – Path of the csv file

Returns:

A tuple containing a numpy array with the source points at index 0 and a numpy array with the destination points at index 1

load_points_dict(path_points_csv_file: str) dict[str, list[float32]] | None[source]

Loads the control points for the transformation from a CSV file, as generated by the butterfly_registrator.

Parameters:

path_points_csv_file – Path of the csv file

Returns:

A dict containing the registration points

pad_image_to_match_size(image_to_pad: Mat | ndarray, image_reference_height: int, image_reference_width: int) Mat | ndarray[source]

Pads the image or removes padding to match the size of the reference image.

Parameters:
  • image_to_pad – A MatLike representation of the image to be padded

  • image_reference_height – The height of the reference image

  • image_reference_width – The width of the reference image

Returns:

A MatLike representation of the padded image

register_image(image: Mat | ndarray, new_width: int, new_height: int, points_source: ndarray, points_destination: ndarray) Mat | ndarray[source]

Register the given image to match the new width and height with transformation given by the source and destination points.

Parameters:
  • image – The image to be registered

  • new_width – The new width of the image

  • new_height – The new height of the image

  • points_source – The source points

  • points_destination – The destination points

Returns:

The registered image

register_image_to_image(path_image_reference: str, path_image_register: str, path_csv_points: str, path_result_registered_image: str) bool[source]

Registers an image to align with a reference image by resizing, padding, and applying perspective transformation. It uses control points from a CSV, generated by the butterfly_registrator, to apply the perspective transformation.

Parameters:
  • path_image_reference – The path of the reference image

  • path_image_register – The path of the image to be registered

  • path_csv_points – The path of the .csv file

  • path_result_registered_image – The path where the registered image will be uploaded to

Returns:

True if the registered image has been written to the specified path successfully and false otherwise

resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) dst

. @brief Resizes an image. . . The function resize resizes the image src down to or up to the specified size. Note that the . initial dst type or size are not taken into account. Instead, the size and type are derived from . the src,`dsize`,`fx`, and fy. If you want to resize src so that it fits the pre-created dst, . you may call the function as follows: . @code . // explicitly specify dsize=dst.size(); fx and fy will be computed from that. . resize(src, dst, dst.size(), 0, 0, interpolation); . @endcode . If you want to decimate the image by factor of 2 in each direction, you can call the function this . way: . @code . // specify fx and fy and let the function compute the destination image size. . resize(src, dst, Size(), 0.5, 0.5, interpolation); . @endcode . To shrink an image, it will generally look best with #INTER_AREA interpolation, whereas to . enlarge an image, it will generally look best with #INTER_CUBIC (slow) or #INTER_LINEAR . (faster but still looks OK). . . @param src input image. . @param dst output image; it has the size dsize (when it is non-zero) or the size computed from . src.size(), fx, and fy; the type of dst is the same as of src. . @param dsize output image size; if it equals zero (None in Python), it is computed as: . f[texttt{dsize = Size(round(fx*src.cols), round(fy*src.rows))}f] . Either dsize or both fx and fy must be non-zero. . @param fx scale factor along the horizontal axis; when it equals 0, it is computed as . f[texttt{(double)dsize.width/src.cols}f] . @param fy scale factor along the vertical axis; when it equals 0, it is computed as . f[texttt{(double)dsize.height/src.rows}f] . @param interpolation interpolation method, see #InterpolationFlags . . @sa warpAffine, warpPerspective, remap

resize_image_fit_aspect_ratio(image_resize: Mat | ndarray, image_reference_height: int, image_reference_width: int) Mat | ndarray[source]

Resizes an image to the aspect ratio calculated by the reference image width and height (image_reference_width, image_reference_height).

Parameters:
  • image_resize – A MatLike representation of the image to be resized

  • image_reference_height – The height of the reference image (in number of pixels)

  • image_reference_width – The width of the reference image (in number of pixels)

Returns:

A MatLike representation of the resized image

warpPerspective(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]]) dst

. @brief Applies a perspective transformation to an image. . . The function warpPerspective transforms the source image using the specified matrix: . . f[texttt{dst} (x,y) = texttt{src} left ( frac{M_{11} x + M_{12} y + M_{13}}{M_{31} x + M_{32} y + M_{33}} , . frac{M_{21} x + M_{22} y + M_{23}}{M_{31} x + M_{32} y + M_{33}} right )f] . . when the flag #WARP_INVERSE_MAP is set. Otherwise, the transformation is first inverted with invert . and then put in the formula above instead of M. The function cannot operate in-place. . . @param src input image. . @param dst output image that has the size dsize and the same type as src . . @param M f$3times 3f$ transformation matrix. . @param dsize size of the output image. . @param flags combination of interpolation methods (#INTER_LINEAR or #INTER_NEAREST) and the . optional flag #WARP_INVERSE_MAP, that sets M as the inverse transformation ( . f$texttt{dst}rightarrowtexttt{src}f$ ). . @param borderMode pixel extrapolation method (#BORDER_CONSTANT or #BORDER_REPLICATE). . @param borderValue value used in case of a constant border; by default, it equals 0. . . @sa warpAffine, resize, remap, getRectSubPix, perspectiveTransform

Module contents