mt.opencv.polygon

Extra functions dealing with polygons via OpenCV.

Functions

  • render_mask(): Renders a mask array from a list of contours.

  • polygon2mask(): Converts the interior of a polygon into an uint8 mask image with padding.

  • morph_open(): Applies a morphological opening operation on the interior of a polygon to form a more human-like polygon.

mt.opencv.polygon.render_mask(contours, out_imgres, thickness=-1, debug=False)

Renders a mask array from a list of contours.

Parameters:
  • contours (list) – a list of numpy arrays, each of which is a list of 2D points, not necessarily in integers

  • out_imgres (list) – the [width, height] image resolution of the output mask.

  • thickness (int32) – negative to fill interior, positive for thickness of the boundary

  • debug (bool) – If True, output an uint8 mask image with 0 being negative and 255 being positive. Otherwise, output a float32 mask image with 0.0 being negative and 1.0 being positive.

Returns:

a 2D array of resolution out_imgres representing the mask

Return type:

numpy.ndarray

mt.opencv.polygon.polygon2mask(polygon, padding=0)

Converts the interior of a polygon into an uint8 mask image with padding.

Parameters:
  • polygon (numpy.array) – list of 2D integer points (x,y)

  • padding (int) – number of pixels for padding at all sides

Returns:

  • img (numpy.array of shape (height, width)) – an uint8 2D image with 0 being zero and 255 being one representing the interior of the polygon, plus padding

  • offset (numpy.array(shape=(2,))) – (offset_x, offset_y). Each polygon’s interior pixel is located at img[offset_y+y,m offset_x+x] and with value 255

mt.opencv.polygon.morph_open(polygon, ksize=3)

Applies a morphological opening operation on the interior of a polygon to form a more human-like polygon.

Parameters:
  • polygon (numpy.array) – list of 2D integer points (x,y)

  • ksize (int) – size of morphological square kernel

Returns:

polygons – list of output polygons, because morphological opening can split a thin polygon into a few parts

Return type:

list of numpy arrays