Functions documentation

This file presents the different functions and Enums Image Processing available for image processing.

These functions are divided in subsections:

  • Pure image processing are used to deal with the thresholding, contours detection, ..

  • Workspaces wise section contains functions to extract workspace and deal with the relative position in the workspace

  • The section Show allows to display images

  • Image Editing contains lot of function which can compress images, add text to image, …

Pure image processing

image_functions.threshold_hsv(list_min_hsv, list_max_hsv, reverse_hue=False, use_s_prime=False)

Take BGR image (OpenCV imread result) and return thresholded image according to values on HSV (Hue, Saturation, Value) Pixel will worth 1 if a pixel has a value between min_v and max_v for all channels

Parameters
  • img (numpy.array) – image BGR if rgb_space = False

  • list_min_hsv (list[int]) – list corresponding to [min_value_H,min_value_S,min_value_V]

  • list_max_hsv (list[int]) – list corresponding to [max_value_H,max_value_S,max_value_V]

  • use_s_prime (bool) – True if you want to use S channel as S’ = S x V else classic

  • reverse_hue (bool) – Useful for Red color cause it is at both extremum

Returns

threshold image

Return type

numpy.array

image_functions.debug_threshold_color(color_hsv)

Return masked image to see the effect of color threshold

Parameters
  • img (numpy.array) – OpenCV image

  • color_hsv (ColorHSV) – Color used for debug

Returns

Masked image

Return type

numpy.array

image_functions.morphological_transformations(morpho_type=<MorphoType.CLOSE: 3>, kernel_shape=(5, 5), kernel_type=<KernelType.ELLIPSE: 2>)

Take black & white image and apply morphological transformation

Parameters
  • im_thresh (numpy.array) – Black & White Image

  • morpho_type (MorphoType) – CLOSE/OPEN/ERODE/DILATE => See on OpenCV/Google if you do not know these words

  • kernel_shape (tuple[float]) – tuple corresponding to the size of the kernel

  • kernel_type (KernelType) – RECT/ELLIPSE/CROSS => see on OpenCV

Returns

image after processing

Return type

numpy.array

image_functions.get_contour_barycenter()

Return barycenter of an OpenCV Contour

Parameters

contour (OpenCV Contour) –

Returns

Barycenter X, Barycenter Y

Return type

int, int

image_functions.get_contour_angle()

Return orientation of a contour according to the smallest side in order to be well oriented for gripper

Parameters

contour (OpenCV Contour) – contour

Returns

Angle in radians

Return type

float

image_functions.biggest_contour_finder()
image_functions.biggest_contours_finder(nb_contours_max=3)

Function to find the biggest contour in an binary image

Parameters
  • img (numpy.array) – Binary Image

  • nb_contours_max (int) – maximal number of contours which will be returned

Returns

biggest contours found

Return type

list[OpenCV Contour]

image_functions.draw_contours(contours)

Draw a list of contour on an image and return the drawing image

Parameters
  • img (numpy.array) – Image

  • contours (list[OpenCV Contour]) – contours list

Returns

Image with drawing

Return type

numpy.array

Workspaces wise

image_functions.extract_img_workspace(workspace_ratio=1.0)

Extract working area from an image thanks to 4 Niryo’s markers

Parameters
  • img (numpy.array) – OpenCV image which contain 4 Niryo’s markers

  • workspace_ratio (float) – Ratio between the width and the height of the area represented by the markers

Returns

extracted and warped working area image

Return type

numpy.array

image_functions.debug_markers(workspace_ratio=1.0)

Display detected markers on an image

Parameters
  • img (numpy.array) – OpenCV image which contain Niryo’s markers

  • workspace_ratio (float) – Ratio between the width and the height of the area represented by the markers

Returns

(status, annotated image)

Return type

numpy.array

image_functions.relative_pos_from_pixels(x_pixels, y_pixels)

Transform a pixels position to a relative position

Parameters
  • img (numpy.array) – Image where the object is detected

  • x_pixels (int) – coordinate X

  • y_pixels (int) – coordinate Y

Returns

X relative, Y relative

Return type

float, float

Show

image_functions.show_img_and_check_close(img)

Display an image and check whether the user want to close

Parameters
  • window_name (str) – window’s name

  • img (numpy.array) – Image

Returns

boolean indicating if the user wanted to leave

Return type

bool

image_functions.show_img(img, wait_ms=1)

Display an image during a certain time

Parameters
  • window_name (str) – window’s name

  • img (numpy.array) – Image

  • wait_ms (int) – Wait time in milliseconds

Returns

value of the key pressed during the display

Return type

int

image_functions.show_img_and_wait_close(img)

Display an image and wait that the user close it

Parameters
  • window_name (str) – window’s name

  • img (numpy.array) – Image

Returns

None

Return type

None

Image Editing

image_functions.compress_image(quality=90)

Compress OpenCV image

Parameters
  • img (numpy.array) – OpenCV Image

  • quality (int) – integer between 1 - 100. The higher it is, the less information will be lost, but the heavier the compressed image will be

Returns

status & string representing compressed image

Return type

bool, str

image_functions.uncompress_image()

Take a compressed img and return an OpenCV image

Parameters

compressed_image (str) – compressed image

Returns

OpenCV image

Return type

numpy.array

image_functions.add_annotation_to_image(text, write_on_top=True)

Add Annotation to an image

Parameters
  • img (numpy.array) – Image

  • text (str) – text string

  • write_on_top (bool) – if you write the text on top

Returns

img with text written on it

Return type

numpy.array

image_functions.undistort_image(mtx, dist)

Use camera intrinsics to undistort raw image

Parameters
  • img (numpy.array) – Raw Image

  • mtx (list[list[float]]) – Camera Intrinsics matrix

  • dist (list[list[float]]) – Distortion Coefficient

Returns

Undistorted image

Return type

numpy.array

image_functions.resize_img(width=None, height=None, inter=3)

Resize an image. The user should precise only width or height if he wants to keep image’s ratio

Parameters
  • img (numpy.array) – OpenCV Image

  • width (int) – Target Width

  • height (int) – Target Height

  • inter (long) – OpenCV interpolation flag

Returns

resized image

Return type

numpy.array

image_functions.concat_imgs(axis=1)

Concat multiple images along 1 axis

Parameters
  • tuple_imgs (tuple[numpy.array]) – tuple of images

  • axis (int) – 0 means vertically and 1 means horizontally

Returns

Concat image

Return type

numpy.array

Enums Image Processing

Enums are used to pass specific parameters to functions.

List of enums:

class ColorHSV(value)[source]

MIN HSV, MAX HSV, Invert Hue (bool)

BLUE = ([90, 50, 85], [125, 255, 255], False)
RED = ([15, 80, 75], [170, 255, 255], True)
GREEN = ([40, 60, 75], [85, 255, 255], False)
ANY = ([0, 50, 100], [179, 255, 255], False)
class ColorHSVPrime(value)[source]

MIN HSV, MAX HSV, Invert Hue (bool)

BLUE = ([90, 70, 100], [115, 255, 255], False)
RED = ([15, 70, 100], [170, 255, 255], True)
GREEN = ([40, 70, 100], [85, 255, 255], False)
ANY = ([0, 70, 140], [179, 255, 255], False)
class ObjectType(value)[source]

An enumeration.

SQUARE = 4
TRIANGLE = 3
CIRCLE = -1
ANY = 0
class MorphoType(value)[source]

An enumeration.

ERODE = 0
DILATE = 1
OPEN = 2
CLOSE = 3
class KernelType(value)[source]

An enumeration.

RECT = 0
ELLIPSE = 2
CROSS = 1