1. Single-Color Recognition Experiment
About 2 min
Routine Code
import sensor, image, time, lcd
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 100)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
clock = time.clock()
print("Hold the object you want to track in front of the camera in the box.")
print("MAKE SURE THE COLOR OF THE OBJECT YOU WANT TO TRACK IS FULLY ENCLOSED BY THE BOX!")
# Capture the color thresholds for whatever was in the center of the image.
# 50x50 center of QVGA.
r = [(320//2)-(50//2), (240//2)-(50//2), 50, 50]
for i in range(50):
img = sensor.snapshot()
img.draw_rectangle(r)
lcd.display(img)
print("Learning thresholds...")
threshold = [50, 50, 0, 0, 0, 0] # Middle L, A, B values.
for i in range(50):
img = sensor.snapshot()
hist = img.get_histogram(roi=r)
lo = hist.get_percentile(0.01) # Get the CDF of the histogram at the 1% range (ADJUST AS NECESSARY)!
hi = hist.get_percentile(0.99) # Get the CDF of the histogram at the 99% range (ADJUST AS NECESSARY)!
# Average in percentile values.
threshold[0] = (threshold[0] + lo.l_value()) // 2
threshold[1] = (threshold[1] + hi.l_value()) // 2
threshold[2] = (threshold[2] + lo.a_value()) // 2
threshold[3] = (threshold[3] + hi.a_value()) // 2
threshold[4] = (threshold[4] + lo.b_value()) // 2
threshold[5] = (threshold[5] + hi.b_value()) // 2
for blob in img.find_blobs([threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10):
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
img.draw_rectangle(r, color=(0,255,0))
lcd.display(img)
print("Thresholds learned...")
print("Start Color Recognition...")
while(True):
clock.tick()
img = sensor.snapshot()
for blob in img.find_blobs([threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10):
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
lcd.display(img)
print(clock.fps())Experiment Preparation
- Connect the K210 to your computer via USB.
- Prepare an object with a distinct color.
- Open CanMV IDE and run the routine code above.
Experiment Results
- After running, wait for system initialization to complete. The LCD displays the camera feed with a white box in the center of the screen. Place the color to be recognized inside the white box. The white box remains for about 3 seconds.

- When the white box turns green, the system begins learning the LAB values of the color inside the green box. Additional white boxes may appear as a preview effect. After about 5 seconds, the green box disappears, indicating learning is complete.

- Afterward, when similar colors appear in the camera feed, they are automatically outlined with a white box.

Routine Code Explanation
- Import the required libraries and initialize the camera and LCD display.
import sensor, image, time, lcd
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
clock = time.clock()- Draw a 50×50 white box on the camera feed to prompt the user to place the target color inside the box.
r = [(320//2)-(50//2), (240//2)-(50//2), 50, 50]
for i in range(50):
img = sensor.snapshot()
img.draw_rectangle(r)
lcd.display(img)- When the box changes from white to green, the system begins learning the color's LAB values. Values are read multiple times and averaged as the learned LAB result.
print("Learning thresholds...")
threshold = [50, 50, 0, 0, 0, 0] # Middle L, A, B values.
for i in range(50):
img = sensor.snapshot()
hist = img.get_histogram(roi=r)
lo = hist.get_percentile(0.01) # Get the CDF of the histogram at the 1% range (ADJUST AS NECESSARY)!
hi = hist.get_percentile(0.99) # Get the CDF of the histogram at the 99% range (ADJUST AS NECESSARY)!
# Average in percentile values.
threshold[0] = (threshold[0] + lo.l_value()) // 2
threshold[1] = (threshold[1] + hi.l_value()) // 2
threshold[2] = (threshold[2] + lo.a_value()) // 2
threshold[3] = (threshold[3] + hi.a_value()) // 2
threshold[4] = (threshold[4] + lo.b_value()) // 2
threshold[5] = (threshold[5] + hi.b_value()) // 2
for blob in img.find_blobs([threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10):
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
img.draw_rectangle(r, color=(0,255,0))
lcd.display(img)- After color learning completes, create a while loop to recognize colors in the camera feed. The system compares LAB values against those learned in the previous step and outlines matching color blobs.
print("Thresholds learned...")
print("Start Color Recognition...")
while(True):
clock.tick()
img = sensor.snapshot()
for blob in img.find_blobs([threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10):
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
lcd.display(img)
print(clock.fps())