|
1 | 1 | # Import OpenCV |
2 | | -import cv2 |
| 2 | +import cv2 as cv |
3 | 3 | from cv2_hardware_init import * |
4 | 4 | from ulab import numpy as np |
5 | 5 | import time |
|
53 | 53 | # method to create a binary image. This means it will only detect a dark |
54 | 54 | # logo on a light background (or vice versa), but you can modify this to |
55 | 55 | # find specific colors or use other methods if desired |
56 | | - gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) |
57 | | - thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) |
| 56 | + gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) |
| 57 | + thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) |
58 | 58 |
|
59 | 59 | # Find contours in the binary image, which represent the boundaries of |
60 | 60 | # shapes. Contours are a powerful tool in OpenCV for shape analysis and |
61 | 61 | # object detection |
62 | | - contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) |
| 62 | + contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) |
63 | 63 |
|
64 | 64 | # It's possible that no contours were found, so first check if any were |
65 | 65 | # found before proceeding |
66 | 66 | if contours: |
67 | 67 | # We'll compare the contours found in the image to the reference logo |
68 | | - # contour defined earlier. We will use the `cv2.matchShapes()` function |
| 68 | + # contour defined earlier. We will use the `cv.matchShapes()` function |
69 | 69 | # to compare the shapes to pick the best match, so we need to initialize |
70 | 70 | # variables to keep track of the best match found so far |
71 | 71 | best_contour = None |
|
74 | 74 | # Loop through each contour found in the image to find the best match |
75 | 75 | for i in range(len(contours)): |
76 | 76 | # If the image is noisy, the binarized image may contain many tiny |
77 | | - # contours that are obviously not the logo. `cv2.matchShapes()` can |
| 77 | + # contours that are obviously not the logo. `cv.matchShapes()` can |
78 | 78 | # take some time, so we can be more efficient by skipping obviously |
79 | 79 | # wrong contours. In this example, the logo we're looking for is |
80 | 80 | # fairly complex, so we can skip contours that have too few points |
81 | 81 | # since they will definitely be too simple to match the logo |
82 | 82 | if len(contours[i]) < 20: |
83 | 83 | continue |
84 | 84 |
|
85 | | - # Now we call `cv2.matchShapes()` which returns a "similarity" score |
| 85 | + # Now we call `cv.matchShapes()` which returns a "similarity" score |
86 | 86 | # between the two shapes. The lower the score, the more similar the |
87 | 87 | # shapes are |
88 | | - similarity = cv2.matchShapes(logo_contour, contours[i], cv2.CONTOURS_MATCH_I2, 0) |
| 88 | + similarity = cv.matchShapes(logo_contour, contours[i], cv.CONTOURS_MATCH_I2, 0) |
89 | 89 |
|
90 | 90 | # Check if this contour is a better match than the best so far |
91 | 91 | if similarity < best_similarity: |
|
100 | 100 | # higher threshold of 1.0 |
101 | 101 | if best_similarity < 1.0: |
102 | 102 | # Now we'll draw the best contour found on the original image |
103 | | - frame = cv2.drawContours(frame, [best_contour], -1, (0, 0, 255), 2) |
| 103 | + frame = cv.drawContours(frame, [best_contour], -1, (0, 0, 255), 2) |
104 | 104 |
|
105 | 105 | # All processing is done! Calculate the frame rate and display it |
106 | 106 | current_time = time.ticks_us() |
107 | 107 | fps = 1000000 / (current_time - loop_time) |
108 | 108 | loop_time = current_time |
109 | | - frame = cv2.putText(frame, f"FPS: {fps:.2f}", (40, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) |
| 109 | + frame = cv.putText(frame, f"FPS: {fps:.2f}", (40, 30), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) |
110 | 110 |
|
111 | 111 | # Draw the reference logo contour in the top left corner of the frame |
112 | 112 | frame[0:50, 0:40] = (0,0,0) |
113 | | - frame = cv2.drawContours(frame, [logo_contour], -1, (255, 255, 255), 1, offset=(2, 2)) |
| 113 | + frame = cv.drawContours(frame, [logo_contour], -1, (255, 255, 255), 1, offset=(2, 2)) |
114 | 114 |
|
115 | 115 | # Display the frame |
116 | | - cv2.imshow(display, frame) |
| 116 | + cv.imshow(display, frame) |
117 | 117 |
|
118 | 118 | # Check for key presses |
119 | | - key = cv2.waitKey(1) |
| 119 | + key = cv.waitKey(1) |
120 | 120 |
|
121 | 121 | # If any key is pressed, exit the loop |
122 | 122 | if key != -1: |
|
0 commit comments