Welcome to K230 Vision Module repository
20. Pixel Level Transformation
Pixel Level Transformation
Pixel Level TransformationPrefaceExample ResultsTheoryconvertScaleAbsLUT Lookup TableCode OverviewImport Modules and Initialize HardwareCreate LUT and Set ModeCapture Image and Convert to GrayscaleExecute 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: 17.camera_test_imgproc_new.py.
This section focuses on two pixel-level APIs: convertScaleAbs() uses fixed coefficients to adjust grayscale contrast and offset, and LUT() uses a 256-entry lookup table for inverse mapping. Both modes run on 640x480 grayscale live video.


Theory
convertScaleAbs
convertScaleAbs() performs scaling, offset, absolute value, and 8-bit saturation conversion for each pixel:
dst(x, y) = saturate_uint8(abs(alpha × src(x, y) + beta))This example uses alpha=1.5, beta=-30. Mid-to-high grayscale differences are amplified, and darker regions first subtract 30. Since absolute value is taken afterward, it is not equivalent to ordinary "contrast multiplied by 1.5, brightness reduced by 30."
LUT Lookup Table
8-bit input has only 256 possible values, so you can prepare output for each input in advance:
xxxxxxxxxxlut[i] = 255 - iThis table implements grayscale inversion. By replacing with gamma, piecewise, or threshold mapping tables, other fixed transformations can be completed without running Python loops per pixel.
Code Overview
Import Modules and Initialize Hardware
ximport time, os, sys, gcfrom media.sensor import *from media.display import *from media.media import *import cv2import ulab.numpy as np
W, 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 LUT and Set Mode
xxxxxxxxxxlut = np.zeros((256, 1), dtype=np.uint8)for i in range(256): lut[i, 0] = 255 - i
mode = 0mode=0: convertScaleAbs(), source default.
mode=1: LUT() inverse.
LUT is generated once outside the main loop to avoid 256 Python assignments per frame.
Capture Image and Convert to Grayscale
xxxxxxxxxximg = sensor.snapshot()img_np = img.to_numpy_ref()gray = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)Both transformations operate on single-channel 8-bit images for easy direct comparison of grayscale mapping results.
Execute Pixel Transformation
xxxxxxxxxxif mode == 0: out = cv2.convertScaleAbs( gray, alpha=1.5, beta=-30 )else: out = cv2.LUT(gray, lut)convertScaleAbs() is determined by a fixed formula, while LUT() is determined by 256 values in the table, suitable for arbitrary discrete grayscale mappings.
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
alpha: Increasing expands grayscale differences but also more easily saturates bright areas; decreasing compresses contrast.
beta: Controls linear offset, but this function also takes absolute value, so negative offset regions need to be understood in conjunction with the formula.
Inverse LUT: 255-i applies to 8-bit images. When input depth changes, table length and type requirements need to be reconfirmed.
Gamma LUT: Can be generated as 255×(i/255)^gamma; gamma<1 usually brightens dark areas, gamma>1 usually darkens midtones.
Piecewise mapping: Can stretch the desired grayscale range to 0~255 while compressing other ranges, used to highlight specific brightness targets.
Difference from Lesson 14: Lesson 14 compares equalization, normalization, and other global mappings. This lesson specifically explains convertScaleAbs and LUT's per-pixel mapping mechanism.