Color Recognition Example
Color recognition with HUSKYLENS 2 lets Arduino read color block IDs, counts and positions in real time. It’s like giving your robot eyes that see labeled colored stickers. The examples show how to switch to color mode, display center and first block IDs, and then query a specific learned color (ID1) for its name, total count and XY coordinates on the UNIHIKER K10 screen.
1. Color Recognition Code Example
Before You Start:
1.1 Output Related Data for Color Recognition
HUSKYLENS 2 can recognize color blocks within its field of view and output color block-related data. Readable data includes: the ID number of the color block closest to the center of HUSKYLENS 2's camera view, the total number of detected color blocks, the ID number of the first detected color block, and more.
The example program belows:
#include "unihiker_k10.h"
#include "DFRobot_HuskylensV2.h"
// Create objects (Huskylens for color recognition, UNIHIKER K10 for screen)
HuskylensV2 huskylens;
UNIHIKER_K10 k10;
uint8_t screen_dir = 2; // Screen display direction
// Main program initialization
void setup() {
k10.begin(); // Initialize UNIHIKER K10
Wire.begin(); // Initialize I2C communication
// Wait for Huskylens connection (retry every 100ms)
while (!huskylens.begin(Wire)) {
delay(100);
}
k10.initScreen(screen_dir); // Init screen with set direction
k10.creatCanvas(); // Create drawing canvas
huskylens.switchAlgorithm(ALGORITHM_COLOR_RECOGNITION); // Set to color recognition mode
}
void loop() {
huskylens.getResult(ALGORITHM_COLOR_RECOGNITION); // Get latest color recognition data
// Check if color recognition data is available
if (huskylens.available(ALGORITHM_COLOR_RECOGNITION)) {
// Simplify index: 1-1 → 0 (same meaning, more intuitive)
k10.canvas->canvasText(String("Center color ID: ") + String(RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_COLOR_RECOGNITION), Result, ID)), 1, 0x0000FF);
k10.canvas->canvasText(String("Total colors: ") + String(huskylens.getCachedResultNum(ALGORITHM_COLOR_RECOGNITION)), 3, 0x0000FF);
k10.canvas->canvasText(String("First color ID: ") + String(RET_ITEM_NUM(huskylens.getCachedResultByIndex(ALGORITHM_COLOR_RECOGNITION, 0), Result, ID)), 5, 0x0000FF);
}
k10.canvas->updateCanvas(); // Refresh screen
delay(50); // 50ms delay for stable data
}
Upload the program and wait for the upload to complete.
Align the crosshair of HUSKYLENS 2 with the color block to learn it. For detailed steps on learning colors, see: Color Recognition.
After learning is complete, aim the HUSKYLENS 2 camera at the color block and observe the output.
Running result:The total number of detected color blocks is output. Regardless of whether the color block has been learned, it will be counted if the bounding box outlines it. Output the corresponding color block ID number as required. The color block near the center of the screen is framed with a white box, indicating it is an unlearned color block, so its ID number is 0.
1.2 Acquire Relevant Data of the Specified Color
After HUSKYLENS 2 recognizes a color, it can retrieve relevant data about the specified color in the image. For example, it can: determine if a specified color exists in the image; retrieve the name of the specified color; and count the number of identical color blocks of the specified color in the image. When multiple blocks of the same specified color appear in the image, you can specify to retrieve parameters of one of the blocks, including name, X/Y coordinates, width, and height.
The example program as follows:
#include "unihiker_k10.h"
#include "DFRobot_HuskylensV2.h"
// Create objects (Huskylens: color recognition; UNIHIKER K10: screen)
HuskylensV2 huskylens;
UNIHIKER_K10 k10;
uint8_t screen_dir = 2; // Screen display direction
// Main program initialization
void setup() {
k10.begin(); // Initialize UNIHIKER K10
Wire.begin(); // Initialize I2C communication
// Wait for Huskylens connection (retry every 100ms)
while (!huskylens.begin(Wire)) {
delay(100);
}
k10.initScreen(screen_dir); // Init screen with set direction
k10.creatCanvas(); // Create drawing canvas
huskylens.switchAlgorithm(ALGORITHM_COLOR_RECOGNITION); // Set to color recognition mode
}
void loop() {
huskylens.getResult(ALGORITHM_COLOR_RECOGNITION); // Get latest color recognition data
// Check if data is available and ID1 color block exists
if (huskylens.available(ALGORITHM_COLOR_RECOGNITION)) {
if (huskylens.getCachedResultByID(ALGORITHM_COLOR_RECOGNITION, 1) != NULL) {
// Simplify index: 1-1 → 0 (same meaning, more intuitive)
k10.canvas->canvasText(String("ID1 color count: ") + String(huskylens.getCachedResultNumByID(ALGORITHM_COLOR_RECOGNITION, 1)), 1, 0x0000FF);
k10.canvas->canvasText(String("ID1 color name: ") + String(RET_ITEM_STR(huskylens.getCachedResultByID(ALGORITHM_COLOR_RECOGNITION, 1), Result, name)), 3, 0x0000FF);
k10.canvas->canvasText("First ID1 color", 5, 0x0000FF);
// Fix line conflict: move coords to row 6 (original row 5 overlapped)
k10.canvas->canvasText(String("Coords: ") + String(RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_COLOR_RECOGNITION, 1, 0), Result, xCenter)) + String(","), 6, 0x0000FF);
k10.canvas->canvasText(String(RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_COLOR_RECOGNITION, 1, 0), Result, yCenter)), 7, 0x0000FF);
}
}
k10.canvas->updateCanvas(); // Refresh screen
delay(50); // 50ms delay for stable data
}
Running Result:As shown in the figure, you can obtain the total number of ID1 color blocks, their names, and the coordinates of the first detected ID1 color block in the image. (Color names can be customized, defaulting to "color").
Was this article helpful?
