Blocks.Erosion

 1import cv2
 2import numpy as np
 3
 4def main(inputs, outputs, parameters, synchronise):
 5    '''
 6    ## Erodes an Image\n
 7    You can specify the kernel dimensions and number of iterations in the parameters.
 8
 9    We first convert the colour of the image from `BGR` to `GRAY` then we apply erosion on it 
10    using the `cv2.erode()` function.\n
11    Finaly we convert from `GRAY` back to `BGR` and output the image through the `share_image()` function.
12    
13    [Further reading]([Further reading](https://docs.opencv.org/4.x/d9/d61/tutorial_py_morphological_ops.html))
14    
15    **Inputs**: BGR Image
16
17    **Outputs**: BGR Image
18
19    **Parameters**: Kernel, Iterations
20    '''
21    kernel = np.array([int(x.strip()) for x in parameters.read_string("Kernel").split(",")])
22    kernel = np.ones(kernel, np.uint8)
23    iters = int(parameters.read_string("Iterations"))
24    auto_enable = False
25    try:
26        enable = inputs.read_number("Enable")
27    except Exception:
28        auto_enable = True
29
30    while(auto_enable or inputs.read_number('Enable')):
31        frame = inputs.read_image("Img")
32        if frame is None:
33            continue
34
35        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
36        dilated = cv2.erode(frame, kernel, iterations = iters)
37        dilated = cv2.cvtColor(dilated, cv2.COLOR_GRAY2BGR)
38        
39        outputs.share_image('Out', dilated)
40
41        synchronise()
def main(inputs, outputs, parameters, synchronise)
 5def main(inputs, outputs, parameters, synchronise):
 6    '''
 7    ## Erodes an Image\n
 8    You can specify the kernel dimensions and number of iterations in the parameters.
 9
10    We first convert the colour of the image from `BGR` to `GRAY` then we apply erosion on it 
11    using the `cv2.erode()` function.\n
12    Finaly we convert from `GRAY` back to `BGR` and output the image through the `share_image()` function.
13    
14    [Further reading]([Further reading](https://docs.opencv.org/4.x/d9/d61/tutorial_py_morphological_ops.html))
15    
16    **Inputs**: BGR Image
17
18    **Outputs**: BGR Image
19
20    **Parameters**: Kernel, Iterations
21    '''
22    kernel = np.array([int(x.strip()) for x in parameters.read_string("Kernel").split(",")])
23    kernel = np.ones(kernel, np.uint8)
24    iters = int(parameters.read_string("Iterations"))
25    auto_enable = False
26    try:
27        enable = inputs.read_number("Enable")
28    except Exception:
29        auto_enable = True
30
31    while(auto_enable or inputs.read_number('Enable')):
32        frame = inputs.read_image("Img")
33        if frame is None:
34            continue
35
36        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
37        dilated = cv2.erode(frame, kernel, iterations = iters)
38        dilated = cv2.cvtColor(dilated, cv2.COLOR_GRAY2BGR)
39        
40        outputs.share_image('Out', dilated)
41
42        synchronise()

Block Description

Erodes an Image

You can specify the kernel dimensions and number of iterations in the parameters.

We first convert the colour of the image from BGR to GRAY then we apply erosion on it using the cv2.erode() function.

Finaly we convert from GRAY back to BGR and output the image through the share_image() function.

Further reading

Inputs: BGR Image

Outputs: BGR Image

Parameters: Kernel, Iterations