Masking utilities

Simple methods to help in manipulating images.

library.utilities.utilities_mask.apply_mask(img, mask, infile)

Apply image mask to image.

Parameters:
  • img – numpy array of image

  • mask – numpy array of mask

  • infile – path to file

Returns:

numpy array of cleaned image

library.utilities.utilities_mask.clean_and_rotate_image(file_key)

The main function that uses the user edited mask to crop out the tissue from surrounding debris. It also rotates the image to a usual orientation (where the olfactory bulb is facing left and the cerebellum is facing right. The hippocampus is facing up and the brainstem is facing down) Normalization needs adjusting, for section 064 of DK101, the cells get an uwanted outline that is far too bright. This happens with the scaled method. An affected area on 064.tif full resolution is top left corner of 32180x19665, and bottom right corner at: 33500x20400 on the full cleaned version For the regular tif, look at 15812x43685, 16816x44463

Parameters:

file_key – is a tuple of the following: infile, outfile, maskfile, rotation, flip, max_width, max_height, self.channel, bgcolor

Returns:

nothing. we write the image to disk

library.utilities.utilities_mask.clean_and_rotate_imageOLD(file_key)

The main function that uses the user edited mask to crop out the tissue from surrounding debris. It also rotates the image to a usual orientation (where the olfactory bulb is facing left and the cerebellum is facing right. The hippocampus is facing up and the brainstem is facing down)

Parameters:

file_key – is a tuple of the following:

  • infile file path of image to read

  • outpath file path of image to write

  • mask binary mask image of the image

  • rotation number of 90 degree rotations

  • flip either flip or flop

  • max_width width of image

  • max_height height of image

  • scale used in scaling. Gotten from the histogram

Returns:

nothing. we write the image to disk

library.utilities.utilities_mask.combine_dims(a)

Combines dimensions of a numpy array

Parameters:

a – numpy array

Returns:

numpy array

library.utilities.utilities_mask.compare_directories(dir1: str, dir2: str) None

Compares the contents of two directories to ensure they have the same files and that the images within those files have the same dimensions. Args: :param dir1 (str): The path to the first directory. :param dir2 (str): The path to the second directory. :raise: AssertionError: If the number of files in the directories are not equal or if any directory is empty. SystemExit: If there are any mismatches in file names or image dimensions, the function prints the errors and exits the program.

library.utilities.utilities_mask.create_mask(image)
library.utilities.utilities_mask.crop_imageDEPRECATED(img, mask)

Crop image to remove parts of image not in mask

Parameters:
  • img – numpy array of image

  • mask – numpy array of mask

Returns:

numpy array of cropped image

library.utilities.utilities_mask.equalized(fixed, cliplimit=5)

Takes an image that has already been scaled and uses opencv adaptive histogram equalization. This cases uses 5 as the clip limit and splits the image into rows and columns. A higher cliplimit will make the image brighter. A cliplimit of 1 will do nothing.

Parameters:

fixed – image we are working on

Returns:

a better looking image

library.utilities.utilities_mask.get_box_corners(arr)
library.utilities.utilities_mask.get_image_box(img)

Computes the bounding box coordinates of the non-zero regions in a binary image.

Args:

img (numpy.ndarray): Input binary image where non-zero pixels represent the object.

Returns:
tuple: A tuple (x1, y1, x2, y2) representing the coordinates of the bounding box

where (x1, y1) is the top-left corner and (x2, y2) is the bottom-right corner.

library.utilities.utilities_mask.mask_with_background(img, mask)

Masks the image with the given mask and replaces the masked region with the background color.

Args:

img (numpy.ndarray): The input image. mask (numpy.ndarray): The mask to be applied on the image.

Returns:

numpy.ndarray: The masked image with the background color.

library.utilities.utilities_mask.mask_with_contours(img)
library.utilities.utilities_mask.match_histograms(cleaned, reference)
library.utilities.utilities_mask.match_histogramsXXX(source, template)

Adjust the pixel values of a grayscale image such that its histogram matches that of a target image

Arguments: source – a grayscale image which histogram will be modified template – a grayscale image which histogram will be used as a reference

Returns: a grayscale image with the same size as source

library.utilities.utilities_mask.merge_mask(image, mask)

Merge image with mask [so user can edit] stack 3 channels on single image (black background, image, then mask)

Parameters:
  • image – numpy array of the image

  • mask – numpy array of the mask

Returns:

merged numpy array

library.utilities.utilities_mask.normalize16(img)
library.utilities.utilities_mask.normalize8(img)
library.utilities.utilities_mask.normalize_image(img)

This is a simple opencv image normalization for 16 bit images.

Parameters:

img – the numpy array of the 16bit image

Return img:

the normalized image

library.utilities.utilities_mask.place_image(img, file: str, max_width, max_height, bgcolor)

Places the image in a padded one size container with the correct background

Parameters:
  • img – image we are working on.

  • file – file name and path location

  • max_width – width to pad

  • max_height – height to pad

  • bgcolor – background color of image, 0 for NTB, white for thionin

Returns:

placed image centered in the correct size.

library.utilities.utilities_mask.place_imageV1(file_key: tuple, bgcolor: int = 0)
library.utilities.utilities_mask.rescaler(img)
library.utilities.utilities_mask.rotate_image(img, file: str, rotation: int)

Rotate the image by the number of rotation(s)

Rotate the image by the number of rotation :param img: image to work on :param file: file name and path :param rotation: number of rotations, 1 = 90degrees clockwise :return: rotated image

library.utilities.utilities_mask.scaled(img, scale=32000)

Stretch values out to scale Used to be 45000, but changing it down to 32000 as of 7 Aug 2024

library.utilities.utilities_mask.smooth_image(gray)