Welcome to K230 Vision Module repository
16. Histogram and Pixel Transformation
Histogram and Pixel Transformation
Histogram and Pixel TransformationPrefaceExample ResultsTheoryHistogram EqualizationNormalization and Linear TransformationLookup Table (LUT)Code OverviewInitialize Camera and Display ModuleCreate Inverse Lookup Table and Set ModeConvert to Grayscale ImageExecute Pixel TransformationPackage Results and DisplayResource ReleaseParameter Adjustment Notes
Preface
This tutorial applies to firmware version: CanMV_K230_YAHBOOM_micropython_v1.8-13. You can download it from the Kendryte official website at https://download.kendryte.com/developer/releases/canmv_k230_micropython/daily_build/

Example Results
Run the example code for this section: 14.camera_equalize_normalize.py.
This section converts a live color image to grayscale and demonstrates histogram equalization, min-max normalization, linear scaling with absolute value, and LUT inverse mapping.
x<img src="Snipaste_2026-07-27_21-53-11.png" alt="Histogram and Pixel Transformation Results" style="zoom:50%;" /> <img src="Snipaste_2026-07-27_21-54-28.png" style="zoom:49%;" />equalizeHist normalize<img src="Snipaste_2026-07-27_21-56-20.png" style="zoom:50%;" /> <img src="Snipaste_2026-07-27_21-57-20.png" style="zoom:50%;" />convertScaleAbs LUT
Theory
Histogram Equalization
A grayscale histogram counts occurrences of each gray level from 0 to 255. equalizeHist() remaps gray levels based on cumulative distribution to make better use of the available gray range. It is commonly used to enhance low-contrast images.
The title of this example contains "histogram," but the source code does not directly call calcHist() or draw a histogram. equalizeHist() internally uses the grayscale distribution to perform the mapping.
Normalization and Linear Transformation
normalize(..., NORM_MINMAX) maps the current image's minimum and maximum values to a specified range. This example maps to 0 to 255.
convertScaleAbs() performs approximately the following operation and outputs 8-bit absolute values:
xxxxxxxxxxdst = saturate_uint8(abs(alpha × src + beta))Lookup Table (LUT)
LUT prepares output values in advance for each possible input gray level. Processing only requires looking up the table by input value, making it suitable for fixed pixel transformations like inversion, gamma correction, and piecewise mapping.
Code Overview
Initialize Camera and Display Module
xxxxxxxxxxW, H = 640, 480sensor = Sensor(width=1280, height=960, fps=90)sensor.reset()sensor.set_framesize(width=W, height=H)sensor.set_pixformat(Sensor.RGB888)Display.init(Display.ST7701, width=640, height=480, to_ide=True)sensor.run()time.sleep(0.5)Create Inverse Lookup Table and Set Mode
xxxxxxxxxxlut = np.zeros((256, 1), dtype=np.uint8)for i in range(256): lut[i, 0] = 255 - imode = 3LUT is created outside the loop. Input 0 maps to 255, and input 255 maps to 0. The source code runs in inverse mode by default.
| mode | Function | Current Parameter |
|---|---|---|
| 0 | equalizeHist | Grayscale equalization |
| 1 | normalize | 0~255, NORM_MINMAX |
| 2 | convertScaleAbs | alpha=1.5, beta=-30 |
| 3 | LUT | Inverse table, source default |
Convert to Grayscale Image
xxxxxxxxxxgray = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)equalizeHist() requires standard single-channel 8-bit input, so this example first converts to grayscale.
Execute Pixel Transformation
xxxxxxxxxxif mode == 0: out = cv2.equalizeHist(gray)elif mode == 1: out = cv2.normalize( gray, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX )elif mode == 2: out = cv2.convertScaleAbs(gray, alpha=1.5, beta=-30)else: out = cv2.LUT(gray, lut)Equalization is a nonlinear global mapping. Min-max normalization depends on the current frame's extremes. convertScaleAbs() is a fixed linear transformation. LUT is entirely defined by the lookup table.
Package Results and Display
xxxxxxxxxximg_out = image.Image( W, H, image.GRAYSCALE, alloc=image.ALLOC_REF, data=out)Display.show_image(img_out)Resource Release
xxxxxxxxxxfinally: sensor.stop() Display.deinit() os.exitpoint(os.EXITPOINT_ENABLE_SLEEP) time.sleep_ms(100)Parameter Adjustment Notes
- Equalization scenarios: Low-contrast grayscale images usually improve significantly, but noise and local overexposure may also be enhanced.
- Normalization stability: When minimum and maximum values change each frame, image brightness may jump. For real-time projects, first calculate a stable range and then fix the mapping.
- alpha: Greater than 1 increases contrast, less than 1 decreases contrast.
- beta: Controls offset; this example uses -30. Since absolute value is also taken, the behavior in negative regions differs from ordinary brightness subtraction.
- LUT shape and type: The current firmware example uses a 256x1 uint8 array. Table entries must cover all 8-bit inputs.
- Gamma transformation: You can use output = 255 × (input/255)^gamma to generate LUT in advance, avoiding per-pixel power operations.