Skip to content

Commit 74c1832

Browse files
committed
Clean up Hello OpenCV example
1 parent be186c2 commit 74c1832

File tree

1 file changed

+47
-24
lines changed

1 file changed

+47
-24
lines changed

examples/hello_opencv.py

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,64 @@
11
# Import OpenCV, just as you would in any other Python environment!
22
import cv2
33

4-
# Import NumPy. Note that we use ulab's NumPy, which is a lightweight version of
5-
# standard NumPy
4+
# Import NumPy, almost like any other Python environment! The only difference is
5+
# the addition of `from ulab` since MicroPython does not have a full NumPy
6+
# implementation; ulab NumPy is a lightweight version of standard NumPy
67
from ulab import numpy as np
78

8-
# Import a display driver. Any display driver can be used, as long as it
9-
# implements an `imshow()` function that takes an NumPy array as input
10-
import st7789_spi as st7789
9+
# Standard OpenCV leverages the host operating system to display images, but we
10+
# don't have that luxury in MicroPython. Instead, we need to import a display
11+
# driver. Any display driver can be used, as long as it implements an `imshow()`
12+
# method that takes an NumPy array as input
13+
from st7789_spi import ST7789_SPI
1114

12-
# Create display object
13-
display = st7789.ST7789_SPI(width=240,
14-
height=320,
15-
spi_id=0,
16-
pin_cs=17,
17-
pin_dc=16,
18-
rotation=1,)
15+
# Create a display object. This will depend on the display driver you are using,
16+
# and you may need to adjust the parameters based on your specific display and
17+
# board configuration
18+
display = ST7789_SPI(width=240,
19+
height=320,
20+
spi_id=0,
21+
pin_cs=17,
22+
pin_dc=16,
23+
rotation=1)
1924

20-
# Initialize an image (NumPy array) to be displayed
25+
# Initialize an image (NumPy array) to be displayed, just like in any other
26+
# Python environment! Here we create a 240x320 pixel image with 3 color channels
27+
# (BGR order, like standard OpenCV) and a data type of `uint8` (you should
28+
# always specify the data type, because NumPy defaults to `float`)
2129
img = np.zeros((240, 320, 3), dtype=np.uint8)
2230

23-
# Images can be modified directly if desired. Here we set the top 50 rows of the
24-
# image to blue (255, 0, 0) in BGR format
31+
# Images can be accessed and modified directly if desired with array slicing.
32+
# Here we set the top 50 rows of the image to blue (remember, BGR order!)
2533
img[0:50, :] = (255, 0, 0)
2634

2735
# OpenCV's drawing functions can be used to modify the image as well. For
28-
# example, we can draw a green ellipse on the image. Note that many OpenCV
29-
# functions return the output image, meaning the entire array will be printed
30-
# if it's not assigned to a variable. In this case, we assign the output to the
31-
# same variable `img`, which has almost no overhead
36+
# example, we can draw a green ellipse at the center of the image
3237
img = cv2.ellipse(img, (160, 120), (100, 50), 0, 0, 360, (0, 255, 0), -1)
3338

34-
# And the obligatory "Hello OpenCV" text, this time in red
39+
# Note - Most OpenCV functions return the resulting image. It's redundant for
40+
# the drawing functions and often ignored, but if you call those functions from
41+
# the REPL without assigning it to a variable, the entire array will be printed.
42+
# To avoid this, you can simply re-assign the image, which has no effect other
43+
# than preventing the output from being printed
44+
45+
# And the obligatory "Hello OpenCV" text! This time in red
3546
img = cv2.putText(img, "Hello OpenCV!", (50, 200), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
3647

37-
# Once we have an image ready to show, just call `imshow()` as you would in
38-
# any other Python environment! However it's a bit different here, as we
39-
# don't have a window to show the image in. Instead, we pass the display object
40-
# to the `imshow()` function, which will show the image on the screen
48+
# Once we have an image ready to show, just call `cv2.imshow()`, almost like any
49+
# other Python environment! The only difference is that we need to pass the
50+
# display object we created earlier as the first argument, instead of a window
51+
# name string. Alternatively, you can call `display.imshow(img)` directly
4152
cv2.imshow(display, img)
53+
54+
# Standard OpenCV requires a call to `cv2.waitKey()` to process events and
55+
# actually display the image. However the display driver shows the image
56+
# immediately, so it's not necessary to call `cv2.waitKey()` in MicroPython.
57+
# But it is available, and behaves almost like any other Python environment! The
58+
# only difference is that it requires a key to be pressed in the REPL instead of
59+
# a window. It will wait for up to the specified number of milliseconds (0 for
60+
# indefinite), and return the ASCII code of the key pressed (-1 if no key press)
61+
#
62+
# Note - Some MicroPython IDEs (like Thonny) don't actually send any key presses
63+
# until you hit Enter on your keyboard
64+
key = cv2.waitKey(1) # Not necessary to display image, can remove if desired

0 commit comments

Comments
 (0)