Functions Documentation

Important

Section is not finished yet !

This file presents the different functions and Enums 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

threshold_hsv(img, list_min_hsv, list_max_hsv, reverse_hue=False, use_s_prime=False)[source]

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

debug_threshold_color(img, color_hsv)[source]

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

morphological_transformations(im_thresh, morpho_type=<MorphoType.CLOSE: 3>, kernel_shape=(5, 5), kernel_type=<KernelType.ELLIPSE: 2>)[source]

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

get_contour_barycenter(contour)[source]

Return barycenter of an OpenCV Contour

Parameters:contour (OpenCV Contour) –
Returns:Barycenter X, Barycenter Y
Return type:int, int
get_contour_angle(contour)[source]

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
biggest_contours_finder(img, nb_contours_max=3)[source]

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]

draw_contours(img, contours)[source]

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

extract_img_workspace(img, workspace_ratio=1.0)[source]

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

debug_markers(img, workspace_ratio=1.0)[source]

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

relative_pos_from_pixels(img, x_pixels, y_pixels)[source]

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

show_img_and_check_close(window_name, img)[source]

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

show_img(window_name, img, wait_ms=1)[source]

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

show_img_and_wait_close(window_name, img)[source]

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

compress_image(img, quality=90)[source]

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

uncompress_image(compressed_image)[source]

Take a compressed img and return an OpenCV image

Parameters:compressed_image (str) – compressed image
Returns:OpenCV image
Return type:numpy.array
add_annotation_to_image(img, text, write_on_top=True)[source]

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

undistort_image(img, mtx, dist)[source]

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

resize_img(img, width=None, height=None, inter=3)[source]

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

concat_imgs(tuple_imgs, axis=1)[source]

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[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[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[source]

An enumeration.

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

An enumeration.

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

An enumeration.

RECT = 0
ELLIPSE = 2
CROSS = 1