Image Preprocessing

Functions for preprocessing images.

psbeam.preprocessing.to_gray(image, color_space='RGB', cv2_color=None)

Converts the inputted image to gray scale.

This function should serve as a wrapper for cv2.cvtColor with basic input checking. Its main use is to convert color images to gray scale but it can be used as an alias to cv2.cvtColor if a color conversion code is passed into cv2_color

Parameters:
  • image (np.ndarray) – Image to convert to grayscale.
  • color_space (str, optional) – Color space of the image. Valid entries are ‘RGB’, ‘BGR’.
  • cv2_color (cv2.ColorConversionCodes) – OpenCV color conversion code. Bypasses image array checks if used.
Returns:

image_gray – The image converted to gray scale (ie len(image.shape) is 2)

Return type:

np.ndarray

Raises:

InputError – If input is not an image or color_space is invalid.

psbeam.preprocessing.to_uint8(image, mode='scale')

Correctly converts an image to uint8 type.

Running ‘image.astype(np.uint8)’ on its own applies a mod(256) to handle values over 256. The correct way is to either clip (implemented here) or normalize to the the max and min possible values of the array. Below are the available conversion modes:

clip
Truncates the image at 0 and 255, then returns the resulting array.
norm
Normalizes the image so that the maximum value of the input array is set to 255 and the minimum value is 0.
scale
Scales the image so that the maximum and minimum values of the resulting array corresponds to the maximum and minimum possible values of the input array.
Parameters:
  • image (np.ndarray) – Image to be converted to uint8.
  • mode (str, optional) – Conversion mode to use. See conversion modes for more details.
Returns:

Image that is a np.ndarray with dtype uint8.

Return type:

np.ndarray

psbeam.preprocessing.uint_resize_gauss(image, mode='scale', fx=1.0, fy=1.0, kernel=(11, 11), sigma=0)

Preprocess the image by converting to uint8, resizing and running a gaussian blur.

Parameters:
  • image (np.ndarray) – The image to be preprocessed.
  • mode (str, optional) – Conversion mode that either clips the image or normalizes to the range of the original image type.
  • fx (float, optional) – Percent to resize the image by in x.
  • fy (float, optional) – Percent to resize the image by in y.
  • kernel (tuple, optional) – Kernel to use when running the gaussian filter.
Returns:

Image of type uint8 that has been resized and blurred.

Return type:

np.ndarray

psbeam.preprocessing.threshold_image(image, binary=True, mode='top', factor=1, **kwargs)

Thresholds the image, creating a binary image.

The thresholding can be done using one of the following modes:

mean
Set the threshold line to be image.mean + image.std*factor.
top
Sets the threshold line to be image.max - image.std*factor, leaving just the highest intensity pixels.
bottom
Sets the threshold line to be image.min + image.std*factor, removing just the lowest intensity pixels
adaptive
Sets threshold line according to a weighed sum of neughborhood values using a gaussian window. See ‘Adaptive Thresholding’ in the following link for more details. http://docs.opencv.org/trunk/d7/d4d/tutorial_py_thresholding.html
otsu
Sets the threshold to be between the histogram peaks of a bimodal image. See “Otsu’s Binarization” in the following for more details. http://docs.opencv.org/trunk/d7/d4d/tutorial_py_thresholding.html
Parameters:
  • image (np.ndarray) – The image to threshold.
  • binary (bool, optional) – Use binary thresholding or to_zero thresholding.
  • mode (str, optional) – Thresholding mode to use. See docstring for more information.
  • factor (int, optional) – Number of times to multiply the std by before adding to the mean for thresholding.
Returns:

th – Image that has been thresholded.

Return type:

np.ndarray