Skip to content

Commit 0bcb62d

Browse files
committed
Change examples to import cv2 as cv
Seems to be more common
1 parent 2067566 commit 0bcb62d

File tree

5 files changed

+43
-43
lines changed

5 files changed

+43
-43
lines changed

examples/ex01_hello_opencv.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Import OpenCV, just as you would in any other Python environment!
2-
import cv2
2+
import cv2 as cv
33

44
# Standard OpenCV leverages the host operating system to access hardware, but we
55
# don't have that luxury in MicroPython. Instead, drivers are provided for
@@ -26,30 +26,30 @@
2626

2727
# OpenCV's drawing functions can be used to modify the image as well. For
2828
# example, we can draw a green ellipse at the center of the image
29-
img = cv2.ellipse(img, (160, 120), (100, 50), 0, 0, 360, (0, 255, 0), -1)
29+
img = cv.ellipse(img, (160, 120), (100, 50), 0, 0, 360, (0, 255, 0), -1)
3030

3131
# Note - Most OpenCV functions return the resulting image. It's redundant for
3232
# the drawing functions and often ignored, but if you call those functions from
3333
# the REPL without assigning it to a variable, the entire array will be printed.
3434
# To avoid this, you can simply re-assign the image variable (for example,
35-
# `img = cv2.function(...)`)
35+
# `img = cv.function(...)`)
3636

3737
# And the obligatory "Hello OpenCV" text! This time in red
38-
img = cv2.putText(img, "Hello OpenCV!", (50, 200), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
38+
img = cv.putText(img, "Hello OpenCV!", (50, 200), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
3939

40-
# Once we have an image ready to show, just call `cv2.imshow()`, almost like any
40+
# Once we have an image ready to show, just call `cv.imshow()`, almost like any
4141
# other Python environment! However, there is one important difference:
4242
#
43-
# Standard OpenCV takes a window name string in `cv2.imshow()`, which is used
43+
# Standard OpenCV takes a window name string in `cv.imshow()`, which is used
4444
# to display the image in a window. We don't have windows in MicroPython, so
4545
# there is an API change where the first argument must be a display driver. Any
4646
# display driver can be used, as long as it implements an `imshow()` method that
4747
# takes a NumPy array as input
48-
cv2.imshow(display, img) # Can alternatively call `display.imshow(img)`
48+
cv.imshow(display, img) # Can alternatively call `display.imshow(img)`
4949

50-
# Standard OpenCV requires a call to `cv2.waitKey()` to process events and
50+
# Standard OpenCV requires a call to `cv.waitKey()` to process events and
5151
# actually display the image. However the display driver shows the image
52-
# immediately, so it's not necessary to call `cv2.waitKey()` in MicroPython.
52+
# immediately, so it's not necessary to call `cv.waitKey()` in MicroPython.
5353
# But it is available, and behaves almost like any other Python environment! The
5454
# only difference is that it requires a key to be pressed in the REPL instead of
5555
# a window. It will wait for up to the specified number of milliseconds (0 for
@@ -58,7 +58,7 @@
5858
# Note - Some MicroPython IDEs (like Thonny) don't actually send any key presses
5959
# until you hit Enter on your keyboard
6060
print("Press any key to continue")
61-
key = cv2.waitKey(0) # Not necessary to display image, can remove if desired
61+
key = cv.waitKey(0) # Not necessary to display image, can remove if desired
6262

6363
# Print the key pressed
6464
print("Key pressed:", chr(key))

examples/ex02_camera.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Import OpenCV and hardware initialization module
2-
import cv2
2+
import cv2 as cv
33
from cv2_hardware_init import *
44

55
# Open a camera, similar to any other Python environment! In standard OpenCV,
6-
# you would use `cv2.VideoCapture(0)` or similar, and OpenCV would leverage the
6+
# you would use `cv.VideoCapture(0)` or similar, and OpenCV would leverage the
77
# host operating system to open a camera object and return it as a
8-
# `cv2.VideoCapture` object. However, we don't have that luxury in MicroPython,
8+
# `cv.VideoCapture` object. However, we don't have that luxury in MicroPython,
99
# so a camera driver is required instead. Any camera driver can be used, as long
10-
# as it implements the same methods as the standard OpenCV `cv2.VideoCapture`
10+
# as it implements the same methods as the standard OpenCV `cv.VideoCapture`
1111
# class, such as `open()`, `read()`, and `release()`
1212
camera.open()
1313

@@ -27,10 +27,10 @@
2727
break
2828

2929
# Display the frame
30-
cv2.imshow(display, frame)
30+
cv.imshow(display, frame)
3131

3232
# Check for key presses
33-
key = cv2.waitKey(1)
33+
key = cv.waitKey(1)
3434

3535
# If any key is pressed, exit the loop
3636
if key != -1:

examples/ex03_touch_screen.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Import OpenCV and hardware initialization module
2-
import cv2
2+
import cv2 as cv
33
from cv2_hardware_init import *
44

55
# Import NumPy
@@ -9,7 +9,7 @@
99
img = np.zeros((240, 320, 3), dtype=np.uint8)
1010

1111
# Prompt the user to draw on the screen
12-
img = cv2.putText(img, "Touch to draw!", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
12+
img = cv.putText(img, "Touch to draw!", (10, 30), cv.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
1313

1414
# Prompt the user to press a key to continue
1515
print("Press any key to continue")
@@ -42,13 +42,13 @@
4242

4343
# Draw a line if touching
4444
if touching:
45-
img = cv2.line(img, (x0, y0), (x1, y1), (255, 255, 255), 2)
45+
img = cv.line(img, (x0, y0), (x1, y1), (255, 255, 255), 2)
4646

4747
# Display the frame
4848
display.imshow(img)
4949

5050
# Check for key presses
51-
key = cv2.waitKey(1)
51+
key = cv.waitKey(1)
5252

5353
# If any key is pressed, exit the loop
5454
if key != -1:

examples/ex04_imread_imwrite.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Import OpenCV and hardware initialization module
2-
import cv2
2+
import cv2 as cv
33
from cv2_hardware_init import *
44

5-
# Call `cv2.imread()` to read an image from the MicroPython filesystem, just
5+
# Call `cv.imread()` to read an image from the MicroPython filesystem, just
66
# like in any other Python environment! Make sure to copy the image to the
77
# MicroPython filesystem first, and set the path to the image file as needed
88
#
@@ -11,35 +11,35 @@
1111
#
1212
# Note - only BMP and PNG formats are currently supported in MicroPython OpenCV
1313
print("Loading image...")
14-
img = cv2.imread("test_images/sparkfun_logo.png")
14+
img = cv.imread("test_images/sparkfun_logo.png")
1515

1616
# Show the image for 1 second
1717
#
1818
# Note - If the image is larger or smaller than the display, the behavior will
1919
# depend on the display driver. For example, the default ST7789 display driver
2020
# will crop large images, and show small images in the top-left corner
21-
cv2.imshow(display, img)
21+
cv.imshow(display, img)
2222

2323
# Prompt the user to press a key to continue
2424
print("Press any key to continue")
25-
key = cv2.waitKey(0)
25+
key = cv.waitKey(0)
2626

27-
# Let's modify the image! Here we use `cv2.Canny()` to perform edge detection
27+
# Let's modify the image! Here we use `cv.Canny()` to perform edge detection
2828
# on the image, which is a common operation in computer vision
2929
print("Performing edge detection...")
30-
edges = cv2.Canny(img, 100, 200)
30+
edges = cv.Canny(img, 100, 200)
3131

3232
# Display the modified image
33-
cv2.imshow(display, edges)
33+
cv.imshow(display, edges)
3434

3535
# Now we'll save the modified image to the MicroPython filesystem using
36-
# `cv2.imwrite()`, just like in any other Python environment!
36+
# `cv.imwrite()`, just like in any other Python environment!
3737
#
3838
# Again, SD cards are supported, just change the path to point to the SD card
3939
#
4040
# Note - only BMP and PNG formats are currently supported in MicroPython OpenCV
4141
print("Saving modified image...")
42-
success = cv2.imwrite("test_images/sparkfun_logo_edges.png", edges)
42+
success = cv.imwrite("test_images/sparkfun_logo_edges.png", edges)
4343

4444
# Check if the image was saved successfully
4545
if success:

examples/ex05_detect_sfe_logo.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Import OpenCV
2-
import cv2
2+
import cv2 as cv
33
from cv2_hardware_init import *
44
from ulab import numpy as np
55
import time
@@ -53,19 +53,19 @@
5353
# method to create a binary image. This means it will only detect a dark
5454
# logo on a light background (or vice versa), but you can modify this to
5555
# 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)
5858

5959
# Find contours in the binary image, which represent the boundaries of
6060
# shapes. Contours are a powerful tool in OpenCV for shape analysis and
6161
# 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)
6363

6464
# It's possible that no contours were found, so first check if any were
6565
# found before proceeding
6666
if contours:
6767
# 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
6969
# to compare the shapes to pick the best match, so we need to initialize
7070
# variables to keep track of the best match found so far
7171
best_contour = None
@@ -74,18 +74,18 @@
7474
# Loop through each contour found in the image to find the best match
7575
for i in range(len(contours)):
7676
# 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
7878
# take some time, so we can be more efficient by skipping obviously
7979
# wrong contours. In this example, the logo we're looking for is
8080
# fairly complex, so we can skip contours that have too few points
8181
# since they will definitely be too simple to match the logo
8282
if len(contours[i]) < 20:
8383
continue
8484

85-
# Now we call `cv2.matchShapes()` which returns a "similarity" score
85+
# Now we call `cv.matchShapes()` which returns a "similarity" score
8686
# between the two shapes. The lower the score, the more similar the
8787
# 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)
8989

9090
# Check if this contour is a better match than the best so far
9191
if similarity < best_similarity:
@@ -100,23 +100,23 @@
100100
# higher threshold of 1.0
101101
if best_similarity < 1.0:
102102
# 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)
104104

105105
# All processing is done! Calculate the frame rate and display it
106106
current_time = time.ticks_us()
107107
fps = 1000000 / (current_time - loop_time)
108108
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)
110110

111111
# Draw the reference logo contour in the top left corner of the frame
112112
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))
114114

115115
# Display the frame
116-
cv2.imshow(display, frame)
116+
cv.imshow(display, frame)
117117

118118
# Check for key presses
119-
key = cv2.waitKey(1)
119+
key = cv.waitKey(1)
120120

121121
# If any key is pressed, exit the loop
122122
if key != -1:

0 commit comments

Comments
 (0)