EN2550 Assignment 05
EN2550 Assignment 05
In this assignment, you will be counting and tracking the hexagonal nuts on a moving convey belt.
%matplotlib inline
1.0.2 Let’s load and visualize the template image and the convey belt snapshot at a
given time.
1.1 Part-I :
Before going into the implementation, let’s play with some functions.
1
1.1.1 Otsu’s thresholding
Please read thresholding to get an idea about different types of thresholding and how to use
them.(Please use cv.THRESH_BINARY_INV).
Carry out morphological closing to remove small holes inside the foreground. Use a 3 × 3 kernel.
See closing for a guide.
[ ]: kernel = #"< 3x3 matrix with all ones, with uint8 dtype>"
closing_t = cv.morphologyEx(img_t, cv.MORPH_CLOSE, kernel)
#closing_b = "< Your code to apply morphological closing for belt >"
Use findContours function to retrieve the extreme outer contours. (see for help and see for
information.)
Display these countours.
[ ]: contours_t, hierarchy_t = cv.findContours(closing_t, cv.RETR_TREE, cv.
,→CHAIN_APPROX_SIMPLE)
[ ]: # Visualizing contours
im_contours_belt = np.zeros((belt_im.shape[0],belt_im.shape[1],3), np.uint8)
2
conts = cv.drawContours(im_contours_belt, contours_b, -1, (0,255,0), 3).
,→astype('uint8')
plt.imshow(conts)
Use the matchShapes function as shown in examples to match contours in the belt image with that
in the template.
Get an idea about the value output by the cv.matchShapes when both the template and the
reference image have the same shape. Understand the given code snippet.
[ ]: label = 1 # remember that the label of the background is 0
belt = ((labels_b >= label)*255).astype('uint8')
belt_cont, template_hierarchy = cv.findContours(belt, cv.RETR_EXTERNAL, cv.
,→CHAIN_APPROX_SIMPLE)
1.2 Part - II
Use the cv.contourArea(), see this and calculate the the area of the contours_b[1]
[ ]: ca = '<Your code goes here>'
Use the cv.moments to extract the x and y coordinates of the centroid of contours_b[1].
[ ]: M = '<Your code goes here>'
cx, cy = '<Your code goes here>'
Make a variable called count to represent the number of contours and set it to the value 1. Make
an np array [cx, cy, ca, count] and name this as object_prev_frame
Similarly, you can create the object_curr_frame(to describe the current values) and define the
threshold delta_x to check whether the corresponding element of both the object_curr_frame
and object_prev_frame are less than the delta_x. You can set delta_x as 15 or so. (Here the
delta_x can be thought of as the movement of the cx from frame to frame)
3
1.3 Part - III
[ ]: def get_indexed_image(im):
""" Thresholding, closing, and connected component analysis lumped
"""
1.3.2 2. Implement the function is_new, which checks the dissimilarity between 2
vectors. (Grading)
'Check whether the absolute different between all the elements of ith␣
,→column of each array is greater than the ith delta value (See thee example␣
return None
4
1.3.3 3. If the array a is in the shape of (number of nuts , len(object_prev_frame))
( i.e. array a is made by stacking all the object_prev_frame for each frame. If
b is in the form of [cx, cy, ca, count], write the function prev_index to find the
index of a particular nut in the previous frame. (Grading)
You can use following code snippet load and access each frame of a video
[ ]: cap = cv.VideoCapture('conveyor_many_frame.mp4') # give the correct path here
while cap.isOpened():
ret, frame = cap.read()
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break
cv2_imshow(frame)
if cv.waitKey(1) == ord('q'):
break
cap.release()
cv.destroyAllWindows()
1.3.4 3. Implement a code to detect hexagonal nuts in a moving convey belt. (Grad-
ing)
1.4 Steps:
1. Use the above code snippet to access each frame and remember to convert the frame into
grey scale. Name the variable as grey
5
2. Call get_indexed_image and extract retval, labels, stats, centroids.
3. Find contours of all nuts present in a given frame of the belt.
4. Initiate a 3-D array with zeros to draw contours. Call this im_contours_belt
5. Draw each contour. Use cv.drawContours. See this