OpenCV Development Example
The archive contains an environment installer, a still-image example and the two existing video demos, but not OpenCV itself or test media. Install the environment in section 3 and prepare media in section 4 before running an example.
1. Overview
OpenCV is an open-source computer vision library used for image processing, contour extraction and video analysis. In Python, images are usually NumPy arrays: an operation on a still image can also be applied to successive video frames.
This guide provides still-image edges, grayscale video, video edges and motion-region previews. System GStreamer uses Rockchip MPP for hardware decoding; OpenCV processes and displays the frames. The default is the existing NV12 example, which reads the luma plane directly to avoid an additional color conversion.
1.1 Board and OS configurations
The setup script targets ARM64 Debian 12 and Ubuntu 22.04. It checks the OS and dependencies rather than selecting a CM5-specific configuration. Use an official desktop image, connect HDMI, log in to the desktop and connect to the network. For GNOME, use X11 to match the tested setup.
ArmSoM SDK board configurations share Debian and Ubuntu rootfs build trees. The configurations below are available in the SDK. Video examples also require the image's matching MPP plugin and drivers. Setup checks plugin availability; running a video verifies decoding on the target board.
| SoC | SDK board configurations | SDK OS configurations |
|---|---|---|
| RK3528 | Sige1 | Debian |
| RK3576 | AIM5, CM5 (CM5-IO and CM4 Nano B IO), Sige5 | Debian, Ubuntu |
| RK3588 / RK3588S | AIM7, CM7S, LM7, Sige7, Sige7S | Debian, Ubuntu |
1.2 Tested environments
| Board | OS and desktop | Kernel | Python |
|---|---|---|---|
| CM5 + CM5-IO (RK3576) | Debian 12 arm64 XFCE | 6.1.118-rk3576 | 3.11 |
| AIM7 (RK3588) | Ubuntu 22.04.5 arm64 GNOME/X11 | 6.1.118-rk3588 | 3.10.12 |
Both setups used OpenCV 5.0.0. Other boards can follow the same checks and examples; measure performance on the actual target. Still-image processing only needs OpenCV and an input image, while the complete setup in this guide also checks hardware-video dependencies.
2. OpenCV application scenarios
OpenCV can process individual images or provide the building blocks for real-time camera applications. It offers image transforms, feature extraction, camera calibration and video analysis, and can work with trained models for detection and recognition.
| Scenario | OpenCV's role | Typical uses |
|---|---|---|
| Image processing and enhancement | Resizing, filtering, color conversion, thresholding and morphology | Preprocessing, noise reduction, foreground extraction |
| Industrial vision | Edge and contour extraction, template matching, geometric measurement | Part positioning, visual inspection, dimension measurement; usually requires calibration and stable lighting |
| Object detection and tracking | Detect objects with traditional methods or models and analyze their positions across frames | Face detection, vehicle analysis, target following |
| Text and document processing | Deskewing, perspective correction, binarization and region extraction | Preprocessing receipts, meter readings and scanned documents for an OCR engine |
| Camera calibration and robot vision | Camera parameter estimation, distortion correction, feature matching and stereo vision | Stereo ranging, visual localization, robot perception |
| Video analysis | Frame differences, background modeling, optical flow and motion estimation | Fixed-camera change detection, motion analysis, vision prototypes |
The image/video edge and motion-region examples here are introductory building blocks, rather than complete detection or recognition systems. ArmSoM applications can use MPP for video decoding and RKNN for further NPU inference development. Using OpenCV does not automatically run its algorithms on the NPU.
3. Download and setup
Create the project directory in the board's desktop terminal:
mkdir -p ~/cvproj
Click Download OpenCV examples at the top of this page and place the archive in ~/cvproj/ on the board, named opencv-examples.tar.gz. If the browser saves it in Downloads, move it into the project directory with the file manager. If you download on a computer, transfer it to that directory on the board using a USB drive or SFTP.
Then extract the archive and install the environment. Run bash setup.sh as your desktop login user; it requests sudo only to install system packages. A desktop logged in as root can run it directly too.
Setup installs APT dependencies. For clock or repository errors on a freshly flashed image, see Update the APT package source list. Do not run sudo bash setup.sh to create an environment for a regular desktop user.
cd ~/cvproj
tar -xzf opencv-examples.tar.gz
bash setup.sh
setup.sh installs missing dependencies, creates a local venv, installs opencv-python==5.0.0.93 (imported version 5.0.0), and checks MPP and Python GStreamer bindings. It only installs and checks the environment: it does not download video, extract clips or generate images.
ENV_READY means the environment is installed; next, prepare media in section 4. If setup reports an inaccessible display, run the examples in the logged-in HDMI desktop terminal. After an interrupted installation, rerun bash setup.sh; installed dependencies and a matching OpenCV installation are reused.
| File | Purpose |
|---|---|
setup.sh | Install and check the runtime environment |
image_edges.py | The guide's original still-image processing and display code, packaged as a runnable file |
mpp_nv12_preview.py | Existing NV12 video demo with three modes |
mpp_preview.py | Existing basic grayscale demo for comparison |
The project is in ~/cvproj/; the next section saves media to ~/cvproj/samples/. ~ is the setup user's home directory, such as /root for root. In a new terminal, run cd ~/cvproj before choosing an example.
4. Prepare test media
Prepare the video and image separately after installing the environment. Run these commands on the board; there is no need to rerun setup.sh. If you only want to run video examples with your own H.264 MP4, skip this section and replace the input path in the video commands.
4.1 Download the video
Download the 30-second 1080p source to ~/cvproj/samples/bbb-source.mp4:
cd ~/cvproj
mkdir -p samples
curl -fL --retry 3 https://raw.githubusercontent.com/renesas-rz/media/main/Big_Buck_Bunny/big_buck_bunny_1080p_30fps_30s.mp4 \
-o samples/bbb-source.mp4
4.2 Extract the test clip
Extract eight seconds starting at 12 seconds, encode as H.264 and remove audio. This produces the same 1920×1080, 30 fps, 240-frame clip used in the measurements. -ss 12 sets the start, -t 8 sets the duration, and -an removes audio.
ffmpeg -y -nostdin -ss 12 -i samples/bbb-source.mp4 -t 8 -an \
-c:v libx264 -preset ultrafast -pix_fmt yuv420p samples/bbb-1080p.mp4
4.3 Extract the test image
Extract one frame at two seconds into the clip for the still-image edge example:
ffmpeg -y -nostdin -ss 2 -i samples/bbb-1080p.mp4 -frames:v 1 -update 1 samples/bbb-frame.jpg
The source is ~/cvproj/samples/bbb-source.mp4, the test clip is ~/cvproj/samples/bbb-1080p.mp4, and the test image is ~/cvproj/samples/bbb-frame.jpg. Open them in the desktop file manager to inspect the source. Prepare media once; repeating the FFmpeg commands overwrites output files with the same names.
Big Buck Bunny © 2008 Blender Foundation, CC BY 3.0, downloaded from the test media repository. Screenshots show selected or processed frames.
5. Still-image edge detection
Read a frame extracted from the clip, convert it to grayscale, reduce noise with a Gaussian filter and extract contours with Canny. One command saves the result and opens a preview:
./venv/bin/python image_edges.py
The terminal prints IMAGE_OK (1080, 1920), the nonzero pixel count and the output path. Focus the image window and press any key to close it.
| Content | File path |
|---|---|
| Input image | ~/cvproj/samples/bbb-frame.jpg |
| Output edge map | ~/cvproj/samples/bbb-edges.png |

Save the image without opening a window
./venv/bin/python image_edges.py --no-display
Open ~/cvproj/samples/bbb-edges.png in the desktop file manager after processing.
6. Video processing
All three examples use the existing bundled NV12 script. Choose one mode to run. Press q or Esc to exit early; let the clip finish when verifying the frame count.
6.1 Grayscale preview
Display the Y luma plane of the NV12 frames decoded by MPP:
./venv/bin/python mpp_nv12_preview.py samples/bbb-1080p.mp4 --mode gray
6.2 Edge detection
Apply Gaussian smoothing and Canny to each frame's luma data to display contours:
./venv/bin/python mpp_nv12_preview.py samples/bbb-1080p.mp4 --mode edges
6.3 Motion-region detection
Compute differences between consecutive smoothed frames and threshold at 25 to highlight changes:
./venv/bin/python mpp_nv12_preview.py samples/bbb-1080p.mp4 --mode motion
White pixels indicate large changes. This works best with a fixed camera; moving the camera can highlight the background too. This example does not recognize or track objects.
After playing the standard clip to completion, the terminal should print frames=240, elapsed time and the average frame submission rate. Closing early produces fewer frames; performance depends on the board and runtime environment.
The source download is ~/cvproj/samples/bbb-source.mp4; the test clip is ~/cvproj/samples/bbb-1080p.mp4. Open the clip in the desktop file manager to watch the color source. All three video effects display live in a window and do not save a processed video.
Use your own video
Replace the path with a local H.264-encoded MP4. Quote paths that contain spaces:
./venv/bin/python mpp_nv12_preview.py "/home/user/Videos/test.mp4" --mode edges
This is the NV12 demo's input requirement: an .mp4 extension alone does not identify its codec. This demo does not handle H.265 or other codecs.
7. Test results and implementation
7.1 Board results and preview rates
The table retains the existing demos' board test records. Both boards used the same 1080p30, 240-frame clip and 1080p, 60 Hz HDMI output. Both generated and displayed a 1920×1080 edge image.
| Preview mode | CM5 · Debian 12 XFCE | AIM7 · Ubuntu 22.04 GNOME/X11 |
|---|---|---|
| Basic GRAY8 grayscale | 11.97 fps | 5.68 fps |
| NV12 grayscale | 29.94 fps | 29.95 fps |
| NV12 edges | 29.93 fps | 29.95 fps |
| NV12 motion regions | 29.94 fps | 29.96 fps |
These are average frame submissions to the OpenCV window, including playback pacing. They are not measured physical display rates or maximum algorithm throughput. The boards used different operating systems and desktops; the results describe each tested configuration.
The following screenshot is from the original basic grayscale demo on the CM5 HDMI desktop. It illustrates the effect, rather than a test of the newly added setup script.

In the previous CM5 retest after reflashing Debian 12, basic grayscale averaged 11.91 fps. NV12 grayscale, edges and motion averaged 29.90, 29.92 and 29.93 fps respectively. All four previews processed 240 frames and exited normally.
7.2 Why use NV12 by default?
The basic script uses videoconvert to convert decoded frames to GRAY8. CM5 stage tests identified this conversion path as the main bottleneck. NV12 already includes luma data: the optimized script pulls buffers through an in-process GStreamer appsink, reads the Y plane with NumPy using its actual row stride, and passes it to OpenCV.
For example, the CM5 frame width was 1920 while its row stride was 1984 bytes; assuming tightly packed 1920-byte rows would read the wrong offsets. The existing NV12 demo handles stride and follows the source frame rate. No SDK changes or OpenCV rebuild are needed.
Original stage measurements and the basic comparison command
Each stage test ran one pipeline on the same 240-frame input. fakesink sync=false discards output without a window or playback-clock pacing.
| Test path | Elapsed | Average rate |
|---|---|---|
| MPP decode → fakesink | 0.694 s | 345.82 fps |
| MPP decode → GRAY8 → fakesink | 19.043 s | 12.60 fps |
| GRAY8 pipe → Python (window disabled) | 20.076 s | 11.95 fps |
| GRAY8 pipe → OpenCV window | 20.045 s | 11.97 fps |
| NV12 luma plane → OpenCV window | 8.018 s | 29.93 fps |
In another CM5 mode comparison, GRAY8 edges and motion averaged 13.29 and 12.59 fps; NV12 averaged 29.93 and 29.94 fps. Each processed 240 frames. Separate runs do not establish a performance ranking between modes.
The basic script remains in the archive for optional comparison. It is not a prerequisite for normal use:
./venv/bin/python mpp_preview.py samples/bbb-1080p.mp4
8. Troubleshooting
| Symptom | Action |
|---|---|
| Setup fails at the APT stage | Resolve clock or repository problems using the APT guide, then rerun bash setup.sh |
| Sample download fails | Check board networking and access to the source, then repeat the download in section 4.1; extract the clip and image after a successful download |
Missing mppvideodec | Check the official desktop image and matching Rockchip MPP plugin for your board; installing OpenCV alone does not provide it |
| Script or sample not found | Run cd ~/cvproj and confirm extraction, environment installation and section 4 media preparation completed |
| Qt, display or window errors | Run in the logged-in HDMI desktop terminal; GNOME can use an Xorg/X11 session. Add --no-display to the image example to test file output first |
Commands for further diagnosis
Show the OpenCV version:
./venv/bin/python -c 'import cv2; print(cv2.__version__)'
Check whether the system provides the MPP decoder. This prints plugin information; it does not play a video:
gst-inspect-1.0 mppvideodec
Inspect display outputs and resolutions from the desktop terminal:
xrandr --current
When launching over serial or SSH, first log in to the board's graphical desktop and use its actual display address with a user permitted to access that desktop. Run the following command in the board's desktop terminal to check the address. Running the examples directly in that terminal usually requires no manual display configuration.
echo $DISPLAY
