Welcome to K230 Vision Module repository
22.Image Corrosion
Image Corrosion
Image CorrosionExample ResultsPrinciple ExplanationWhat is Image Erosion?Code OverviewImporting ModulesSetting the Image SizeInitialize the camera (RGB888 format)Initialize the display moduleInitialize the media manager and start the cameraSet erosion parametersImage processing and erosion operationResource releaseParameter adjustment instructions
Example Results
Run this section's example code [Source Code/06.cv_lite/16.rgb888_erode.py]
In this section, we will use the cv_lite extension module to implement erosion of an RGB888 image on the K230.

Principle Explanation
What is Image Erosion?
Image erosion is a morphological image processing operation used to reduce the white area (foreground) in an image, thereby removing small white noise, separating connected objects, or refining object boundaries. Erosion is one of the basic operations in morphological processing and is commonly used on binary or grayscale images.
- Operation Principle: Erosion uses a structuring element (often called a convolution kernel or kernel) to slide across the image. Only when all pixels within the kernel's coverage area are white (or have a high value) are the pixels at the kernel's center set to white (or have their maximum value). This causes white areas to shrink inward, thinning their boundaries and removing small isolated white regions.
- Effect: The erosion operation can reduce the size of white objects in an image, remove small white noise (such as tiny white dots), and separate adjacent but connected regions. In practical applications, erosion is often used to remove noise, separate objects, or serve as the basis for other complex morphological operations (such as opening).
- Application Scenarios: Erosion is very common in image preprocessing, for example, for separating contiguous objects in object detection, removing small noise dots in medical image processing, and thinning text lines in character recognition.
In the erosion operation, the size of the convolution kernel and the number of iterations significantly affect the results: larger kernels and more iterations result in a more pronounced shrinking effect. Furthermore, the binarization threshold (if any) determines which pixels are considered foreground (white) or background (black).
Code Overview
Importing Modules
import time, os, sys, gcfrom machine import Pinfrom media.sensor import * # Camera interfacefrom media.display import * # Display interfacefrom media.media import * # Media managerimport _threadimport cv_lite # cv_lite extension module (including erosion interface) / AI CV extension (erode function)import ulab.numpy as np # NumPy-like ndarray for MicroPythonSetting the Image Size
xxxxxxxxxximage_shape = [480, 640] # Height x WidthDefine the image resolution to 480x640 (height x width). The camera and display modules will be initialized based on this size later.
Initialize the camera (RGB888 format)
xxxxxxxxxxsensor = Sensor(id=2, width=1280, height=960, fps=30)sensor.reset()sensor.set_framesize(width=image_shape[1], height=image_shape[0])sensor.set_pixformat(Sensor.RGB888) # Set pixel format to RGB888- Initialize the camera, set the resolution to 1280x960 and the frame rate to 30 FPS.
- Resize the output frame to 640x480 and set it to RGB888 format (three-channel color image, suitable for morphological operations on color images).
Initialize the display module
xxxxxxxxxxDisplay.init(Display.ST7701, width=image_shape[1], height=image_shape[0], to_ide=True, quality=50)Initialize t