Contouring

Low to mid level functions and classes that mostly involve contouring. For more info on how they work, visit OpenCV’s documentation on contours:

http://docs.opencv.org/trunk/d3/d05/tutorial_py_table_of_contents_contours.html

psbeam.contouring.get_contours(image, thresh_mode='otsu', *args, **kwargs)

Returns the contours of an image according to the inputted threshold.

Parameters:
  • image (np.ndarray) – Image to extract the contours from.
  • thresh_mode (str, optional) –

    Thresholding mode to use. For extended documentation see preprocessing.threshold_image. Valid modes are:

    [‘mean’, ‘top’, ‘bottom’, ‘adaptive’, ‘otsu’]
Returns:

contours – A list of the contours found in the image.

Return type:

list

Raises:

NoContoursDetected – The returned contours list was empty.

psbeam.contouring.get_largest_contour(image=None, contours=None, thresh_mode='otsu', **kwargs)

Returns largest contour of the contour list. Either an image or a contour must be passed. If both are passed, a warning will be logged and the contours of the image will be computed and used to find the largest contour.

Function is making an implicit assumption that there will only be one (large) contour in the image.

Parameters:
  • image (np.ndarray, optional) – Image to extract the contours from.
  • contours (np.ndarray, optional) – Contours found on an image.
  • thresh_mode (str, optional) –

    Thresholding mode to use. For extended documentation see preprocessing.threshold_image. Valid modes are:

    [‘mean’, ‘top’, ‘bottom’, ‘adaptive’, ‘otsu’]
Returns:

(contour_largest, area_largest) – Contour that encloses the largest area and the area it encloses

Return type:

tuple

Raises:

InputError – If neither an image nor contours are inputted, or largest area is zero

psbeam.contouring.get_moments(image=None, contour=None, **kwargs)

Returns the moments of an image.

Attempts to find the moments using an inputted contours first, but if it isn’t inputted it will compute the contours of the image then compute the moments.

Parameters:
  • image (np.ndarray) – Image to calculate moments from.
  • contour (np.ndarray) – Beam contour.
Returns:

moments – Dictionary with all the calculated moments of the image.

Return type:

dict

Raises:

InputError – If neither an image nor contours are inputted.

psbeam.contouring.get_centroid(M)

Returns the centroid using the inputted image moments.

Centroid is computed as being the first moment in x and y divided by the zeroth moment.

Parameters:M (list) – List of image moments.
Returns:Centroid of the image moments.
Return type:tuple
psbeam.contouring.get_contour_size(image=None, contour=None, **kwargs)

Returns the length and width of the contour, or the contour of the image inputted.

Parameters:
  • image (np.ndarray) – Image to calculate moments from.
  • contour (np.ndarray) – Beam contour.
Returns:

Length and width of the inputted contour.

Return type:

tuple

Raises:

InputError – If neither an image nor contours are inputted.

psbeam.contouring.get_similarity(contour, template='circle', method=1, **kwargs)

Returns a score of how similar a contour is to a selected template image.

Parameters:
Returns:

Value that is 0.0 or larger, with 0.0 denoting a perfect matching

Return type:

float

Raises:

InputError – If string for template image is not in template images, np.ndarray for template image is not the right shape, invalid type passed for template.