|
6 | 6 | # implementation; ulab NumPy is a lightweight version of standard NumPy |
7 | 7 | from ulab import numpy as np |
8 | 8 |
|
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 |
14 | | - |
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) |
24 | | - |
25 | 9 | # Initialize an image (NumPy array) to be displayed, just like in any other |
26 | 10 | # Python environment! Here we create a 240x320 pixel image with 3 color channels |
27 | 11 | # (BGR order, like standard OpenCV) and a data type of `uint8` (you should |
|
39 | 23 | # Note - Most OpenCV functions return the resulting image. It's redundant for |
40 | 24 | # the drawing functions and often ignored, but if you call those functions from |
41 | 25 | # 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 |
| 26 | +# To avoid this, you can simply re-assign the image variable (for example, |
| 27 | +# `img = cv2.function(...)`) |
44 | 28 |
|
45 | 29 | # And the obligatory "Hello OpenCV" text! This time in red |
46 | 30 | img = cv2.putText(img, "Hello OpenCV!", (50, 200), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) |
47 | 31 |
|
48 | 32 | # 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 |
52 | | -cv2.imshow(display, img) |
| 33 | +# other Python environment! However, there is one important difference: |
| 34 | +# |
| 35 | +# Standard OpenCV leverages the host operating system to display images in |
| 36 | +# windows, but we don't have that luxury in MicroPython. So there is an API |
| 37 | +# change to `cv2.imshow()` to accommodate this: instead of passing a window name |
| 38 | +# string as the first argument to `cv2.imshow()`, we pass a display driver. Any |
| 39 | +# display driver can be used, as long as it implements an `imshow()` method that |
| 40 | +# takes a NumPy array as input |
| 41 | +# |
| 42 | +# This example assumes a display driver called `display` has been initialized by |
| 43 | +# a `boot.py` script. See the example `boot.py` script for more details |
| 44 | +cv2.imshow(display, img) # Can alternatively call `display.imshow(img)` |
53 | 45 |
|
54 | 46 | # Standard OpenCV requires a call to `cv2.waitKey()` to process events and |
55 | 47 | # actually display the image. However the display driver shows the image |
|
61 | 53 | # |
62 | 54 | # Note - Some MicroPython IDEs (like Thonny) don't actually send any key presses |
63 | 55 | # until you hit Enter on your keyboard |
64 | | -key = cv2.waitKey(1) # Not necessary to display image, can remove if desired |
| 56 | +key = cv2.waitKey(0) # Not necessary to display image, can remove if desired |
0 commit comments