|
1 | 1 | # Import OpenCV, just as you would in any other Python environment!
|
2 | 2 | import cv2
|
3 | 3 |
|
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 |
6 | 7 | from ulab import numpy as np
|
7 | 8 |
|
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 |
11 | 14 |
|
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) |
19 | 24 |
|
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`) |
21 | 29 | img = np.zeros((240, 320, 3), dtype=np.uint8)
|
22 | 30 |
|
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!) |
25 | 33 | img[0:50, :] = (255, 0, 0)
|
26 | 34 |
|
27 | 35 | # 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 |
32 | 37 | img = cv2.ellipse(img, (160, 120), (100, 50), 0, 0, 360, (0, 255, 0), -1)
|
33 | 38 |
|
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 |
35 | 46 | img = cv2.putText(img, "Hello OpenCV!", (50, 200), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
|
36 | 47 |
|
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 |
41 | 52 | 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