Blocks.ContourDetector

 1import cv2
 2import numpy as np
 3
 4def main(inputs, outputs, parameters, synchronise):
 5    '''
 6    ## Detects Contours in an Image\n
 7    The image in which contours are to be detected is read through the inputs.
 8    First the image is converted from `BGR` to `Grayscale`, the thresholding values are `60, 255`.
 9    The function used is `cv2.threshold()`.
10    Once it is thersholded, the contours are detected in the image using `cv2.findContours()`
11
12    The program then detects the biggest contour present in the image and finds the co-ordinates of its center
13    using the `cv2.moments()` function. 
14
15    This co-ords of the center alongwith the contour characteristics are part of the output array.
16    THis array is shared through `share_array()`
17
18    `while` loop is the part of the program that is executed continuously.
19    It is enabled by default but can be disabled by passing in 0 through the enable wire.
20
21    [Further reading](https://docs.opencv.org/4.x/d4/d73/tutorial_py_contours_begin.html)
22    
23    **Inputs**: BGR Image
24
25    **Outputs**: Array [x, y, width, height, angle of rotation], BGR Image
26
27    **Parameters**: None
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        img = inputs.read_image("Img")
37        if img is None:
38            continue
39
40        img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
41        ret, thresh = cv2.threshold(img_gray, 60, 255, cv2.THRESH_BINARY)
42        contours, hierarchy = cv2.findContours(thresh, 1, 2)
43
44        # Find the biggest contour (if detected)
45        if len(contours) > 0:
46            cont = max(contours, key=cv2.contourArea)
47            rect = cv2.minAreaRect(cont) # Get the minimum area bounding rectangle for the contour
48            # ( center (x,y), (width, height), angle of rotation )
49
50            # Get the moments of the image
51            M = cv2.moments(cont)
52            if M['m00'] != 0:
53                # Get the coords of the center of the moments
54                cx = int(M['m10']/M['m00'])
55                cy = int(M['m01']/M['m00'])
56
57            data = [cx, cy, rect[1][0], rect[1][1], rect[2]]
58            # Send the data to the wire in the form of an array 
59            outputs.share_array('Out', data)
60
61        synchronise()
def main(inputs, outputs, parameters, synchronise)
 5def main(inputs, outputs, parameters, synchronise):
 6    '''
 7    ## Detects Contours in an Image\n
 8    The image in which contours are to be detected is read through the inputs.
 9    First the image is converted from `BGR` to `Grayscale`, the thresholding values are `60, 255`.
10    The function used is `cv2.threshold()`.
11    Once it is thersholded, the contours are detected in the image using `cv2.findContours()`
12
13    The program then detects the biggest contour present in the image and finds the co-ordinates of its center
14    using the `cv2.moments()` function. 
15
16    This co-ords of the center alongwith the contour characteristics are part of the output array.
17    THis array is shared through `share_array()`
18
19    `while` loop is the part of the program that is executed continuously.
20    It is enabled by default but can be disabled by passing in 0 through the enable wire.
21
22    [Further reading](https://docs.opencv.org/4.x/d4/d73/tutorial_py_contours_begin.html)
23    
24    **Inputs**: BGR Image
25
26    **Outputs**: Array [x, y, width, height, angle of rotation], BGR Image
27
28    **Parameters**: None
29    '''
30    auto_enable = False
31    try:
32        enable = inputs.read_number("Enable")
33    except Exception:
34        auto_enable = True
35
36    while(auto_enable or inputs.read_number('Enable')):
37        img = inputs.read_image("Img")
38        if img is None:
39            continue
40
41        img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
42        ret, thresh = cv2.threshold(img_gray, 60, 255, cv2.THRESH_BINARY)
43        contours, hierarchy = cv2.findContours(thresh, 1, 2)
44
45        # Find the biggest contour (if detected)
46        if len(contours) > 0:
47            cont = max(contours, key=cv2.contourArea)
48            rect = cv2.minAreaRect(cont) # Get the minimum area bounding rectangle for the contour
49            # ( center (x,y), (width, height), angle of rotation )
50
51            # Get the moments of the image
52            M = cv2.moments(cont)
53            if M['m00'] != 0:
54                # Get the coords of the center of the moments
55                cx = int(M['m10']/M['m00'])
56                cy = int(M['m01']/M['m00'])
57
58            data = [cx, cy, rect[1][0], rect[1][1], rect[2]]
59            # Send the data to the wire in the form of an array 
60            outputs.share_array('Out', data)
61
62        synchronise()

Block Description

Detects Contours in an Image

The image in which contours are to be detected is read through the inputs. First the image is converted from BGR to Grayscale, the thresholding values are 60, 255. The function used is cv2.threshold(). Once it is thersholded, the contours are detected in the image using cv2.findContours()

The program then detects the biggest contour present in the image and finds the co-ordinates of its center using the cv2.moments() function.

This co-ords of the center alongwith the contour characteristics are part of the output array. THis array is shared through share_array()

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.

Further reading

Inputs: BGR Image

Outputs: Array [x, y, width, height, angle of rotation], BGR Image

Parameters: None