Blocks.Cropper

 1import cv2
 2import numpy as np
 3
 4def main(inputs, outputs, parameters, synchronise):
 5    '''
 6    ## Crops an Image\n
 7    The image which is to be cropped is read through the inputs using the `inputs.read_image()` function.
 8    The parameters ask for **x**, **y**, **w**, **h**\n
 9    ```
10    x: x co-ordinate of where the crop should start\n
11    y: y co-ordinate of where the crop should start\n
12    w: width of the crop\n
13    h: height of the crop\n
14    ```
15    Image is cropped by simple list slicing.\n
16    `while` loop is the part of the program that is executed continuously.
17    It is enabled by default but can be disabled by passing in 0 through the enable wire.
18    Output is shared via `share_image()`
19
20    **Inputs**: BGR Image
21
22    **Outputs**: Resized BGR Image
23
24    **Parameters**: x, y, width, height
25    '''
26    x, y, w, h = np.array([int(x.strip()) for x in parameters.read_string("xywh").split(",")])
27    
28    auto_enable = False
29    try:
30        enable = inputs.read_number("Enable")
31    except Exception:
32        auto_enable = True
33
34    while(auto_enable or inputs.read_number('Enable')):
35        frame = inputs.read_image("Img")
36        if frame is None:
37            continue
38
39        
40        cropped_img = frame[y:y+h, x:x+w, :]
41        outputs.share_image('Out', cropped_img)
42
43        synchronise()
def main(inputs, outputs, parameters, synchronise)
 5def main(inputs, outputs, parameters, synchronise):
 6    '''
 7    ## Crops an Image\n
 8    The image which is to be cropped is read through the inputs using the `inputs.read_image()` function.
 9    The parameters ask for **x**, **y**, **w**, **h**\n
10    ```
11    x: x co-ordinate of where the crop should start\n
12    y: y co-ordinate of where the crop should start\n
13    w: width of the crop\n
14    h: height of the crop\n
15    ```
16    Image is cropped by simple list slicing.\n
17    `while` loop is the part of the program that is executed continuously.
18    It is enabled by default but can be disabled by passing in 0 through the enable wire.
19    Output is shared via `share_image()`
20
21    **Inputs**: BGR Image
22
23    **Outputs**: Resized BGR Image
24
25    **Parameters**: x, y, width, height
26    '''
27    x, y, w, h = np.array([int(x.strip()) for x in parameters.read_string("xywh").split(",")])
28    
29    auto_enable = False
30    try:
31        enable = inputs.read_number("Enable")
32    except Exception:
33        auto_enable = True
34
35    while(auto_enable or inputs.read_number('Enable')):
36        frame = inputs.read_image("Img")
37        if frame is None:
38            continue
39
40        
41        cropped_img = frame[y:y+h, x:x+w, :]
42        outputs.share_image('Out', cropped_img)
43
44        synchronise()

Block Description

Crops an Image

The image which is to be cropped is read through the inputs using the inputs.read_image() function. The parameters ask for x, y, w, h

x: x co-ordinate of where the crop should start

y: y co-ordinate of where the crop should start

w: width of the crop

h: height of the crop

Image is cropped by simple list slicing.

while loop is the part of the program that is executed continuously. It is enabled by default but can be disabled by passing in 0 through the enable wire. Output is shared via share_image()

Inputs: BGR Image

Outputs: Resized BGR Image

Parameters: x, y, width, height