Masking utilities

Simple methods to help in manipulating images.

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 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.create_mask(image)
library.utilities.utilities_mask.crop_image(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(mask)

Find new max width and height

Parameters
  • img – numpy array of image

  • mask – numpy array of mask

Returns

numpy array of cropped image

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(file_key: tuple, bgcolor: int = 0)

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.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)