DiatomTrack.utils package

Submodules

DiatomTrack.utils.DataUtils module

class DiatomTrack.utils.DataUtils.InitializeDataPredict(image_path)[source]

Bases: object

Initialize data for segmentation.

class DiatomTrack.utils.DataUtils.SegDatasetPredict(data: InitializeDataPredict, transform=None)[source]

Bases: Dataset

Create data sample for each image containing the resized RGB image and a single channel equivalent for segmentation.

DiatomTrack.utils.DataUtils.cv_imread()

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

DiatomTrack.utils.DataUtils.cvtColor(src, code[, dst[, dstCn]]) dst

. @brief Converts an image from one color space to another. . . The function converts an input image from one color space to another. In case of a transformation . to-from RGB color space, the order of the channels should be specified explicitly (RGB or BGR). Note . that the default color format in OpenCV is often referred to as RGB but it is actually BGR (the . bytes are reversed). So the first byte in a standard (24-bit) color image will be an 8-bit Blue . component, the second byte will be Green, and the third byte will be Red. The fourth, fifth, and . sixth bytes would then be the second pixel (Blue, then Green, then Red), and so on. . . The conventional ranges for R, G, and B channel values are: . - 0 to 255 for CV_8U images . - 0 to 65535 for CV_16U images . - 0 to 1 for CV_32F images . . In case of linear transformations, the range does not matter. But in case of a non-linear . transformation, an input RGB image should be normalized to the proper value range to get the correct . results, for example, for RGB f$rightarrowf$ L*u*v* transformation. For example, if you have a . 32-bit floating-point image directly converted from an 8-bit image without any scaling, then it will . have the 0..255 value range instead of 0..1 assumed by the function. So, before calling #cvtColor , . you need first to scale the image down: . @code . img *= 1./255; . cvtColor(img, img, COLOR_BGR2Luv); . @endcode . If you use #cvtColor with 8-bit images, the conversion will have some information lost. For many . applications, this will not be noticeable but it is recommended to use 32-bit images in applications . that need the full range of colors or that convert an image before an operation and then convert . back. . . If conversion adds the alpha channel, its value will set to the maximum of corresponding channel . range: 255 for CV_8U, 65535 for CV_16U, 1 for CV_32F. . . @param src input image: 8-bit unsigned, 16-bit unsigned ( CV_16UC… ), or single-precision . floating-point. . @param dst output image of the same size and depth as src. . @param code color space conversion code (see #ColorConversionCodes). . @param dstCn number of channels in the destination image; if the parameter is 0, the number of the . channels is derived automatically from src and code. . . @see @ref imgproc_color_conversions

DiatomTrack.utils.DataUtils.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

DiatomTrack.utils.Transforms module

class DiatomTrack.utils.Transforms.Normalize[source]

Bases: object

Normalizes an array.

class DiatomTrack.utils.Transforms.ToTensor[source]

Bases: object

Creates tensor from array.

DiatomTrack.utils.contours module

DiatomTrack.utils.contours.contour_filter(single_contours, aggregate_contours, dim=(1536, 2048))[source]

A filter function for the contours that are output by the segmentation. Combine contours that are directly adjacent into an aggregate contour. Filter contours by size and ratio of length to area.

Parameters:
  • single_contours (list) – Contours of single cells.

  • aggregate_contours (list) – Contours of aggregates.

  • dim (tuple, optional) – The image size. The default is (1536,2048).

Returns:

  • single_contours_filtered (list) – The filtered contours for the single classification.

  • new_aggregate_contours (list) – The updated contours of the aggregate classification.

DiatomTrack.utils.contours.contour_filter_process(cnts, limit)[source]

Filter contours, combine adjacent ones, extract key parameters, and transform them into rotated bounding boxes.

Parameters:
  • cnts (dict) – The dictionary containing the contour data.

  • limit (int) – The number of frames to process.

Returns:

  • filtered_contours (dict) – The dictionary containing the filtered and combined contours.

  • properties (list) – Tuples of areas and rotated bounding boxes.

DiatomTrack.utils.contours.contours_to_properties(key, contours_single, contours_aggregate)[source]

Transform a list of single and aggregate contours into a into a list of tuples of the key, the properties (minAreaRect, label, contour) and the areas of the contours.

DiatomTrack.utils.contours.contours_to_rectangles(contours, label)[source]

Take a list of contours and a label and extract the minAreaRect properties.

DiatomTrack.utils.contours.convert_array_to_rectangle(array)[source]

Convert a flat numpy array to format of a minAreaRect of opencv.

DiatomTrack.utils.contours.convert_contour_to_array(contour)[source]

Convert an opencv contour to flat numpy array containing the information of a minAreaRect.

DiatomTrack.utils.contours.rotation_boundary(angle)[source]

Convert degree to radian and enforce the angle to be in range 0 to pi.

Module contents