Welcome to DOFBOT Pro repository
2. Color Calibration
Color Calibration
For Orin board users, directly open a web page and enter the IP address:8888 to access jupyter-lab and run directly. For Jetson-Nano board users, you need to first enter the docker container, then enter the following command in docker:
cdjupyter-lab --allow-root
Then open a web page and enter the IP address:9999 to access jupyter-lab and run the following program.
By adjusting the HSV high and low thresholds, you can filter out interfering colors, allowing blocks to be ideally recognized in complex environments. When using color-based functions for the first time, it's best to calibrate first.
The calibrated color values will be saved to a file for easy reading and use by multiple programs. Color positioning, color tracking, and snake-leading functions use front-view perspective, so HSV calibration values are different, thus corresponding functions have separate calibration files. Other routines using top-down perspective can use this program for calibration.
Here is the color calibration file path for calibration:
xxxxxxxxxx#Jetson-Nano users need to enter the docker container to view~/dofbot_pro/dofbot_basic_visual/scripts/02.HSV_Calibration.ipynb
1. HSV Introduction
HSV (Hue, Saturation, Value) is a color space created by A. R. Smith in 1978 based on the intuitive characteristics of colors, also known as the Hexcone Model. The parameters of colors in this model are: Hue (H), Saturation (S), and Value (V).
H: 0 — 180
S: 0 — 255
V: 0 — 255
- HSV Parameter Table:
| Black | Gray | White | Red | Orange | Yellow | Green | Cyan | Blue | Purple | ||
|---|---|---|---|---|---|---|---|---|---|---|---|
| H_min | 0 | 0 | 0 | 0 | 156 | 11 | 26 | 35 | 78 | 100 | 125 |
| H_max | 180 | 180 | 180 | 10 | 180 | 25 | 34 | 77 | 99 | 124 | 155 |
| S_min | 0 | 0 | 0 | 43 | 43 | 43 | 43 | 43 | 43 | 43 | |
| S_max | 255 | 43 | 30 | 255 | 255 | 255 | 255 | 255 | 255 | 255 | |
| V_min | 0 | 0 | 0 | 46 | 46 | 46 | 46 | 46 | 46 | 46 | |
| V_max | 46 | 220 | 255 | 255 | 255 | 255 | 255 | 255 | 255 | 255 |
- HSV Hexagonal Pyramid
(1) Hue H. Represents color information, i.e., the position of the spectral color. This parameter is represented by an angle measure, ranging from 0° to 360°, calculated counterclockwise starting from red. Red is 0°, green is 120°, blue is 240°. Their complementary colors are: yellow is 60°, cyan is 180°, purple is 300°.
(2) Saturation S. Saturation S: represents the ratio between the purity of the selected color and the maximum purity of that color. When S=0, there is only grayscale. Complementary colors are 120° apart. Complementary colors differ by 180°. A color can be seen as the result of mixing a certain spectral color with white. The larger the proportion of spectral color, the closer the color is to the spectral color, and the higher the saturation. High saturation means the color is deep and vivid. The white light component of spectral colors is 0, reaching maximum saturation. The usual range is 0% to 100%, with larger values indicating more saturated colors.
(3) Value V. Value represents the brightness of the color. For light source colors, the value is related to the brightness of the emitter; for object colors, this value is related to the transmittance or reflectance of the object. The usual range is 0% (black) to 100% (white). One thing to note: there is no direct relationship between it and light intensity. The 3D representation of the HSV model evolves from the RGB cube. Imagine viewing from the white vertex to the black vertex along the diagonal of the RGB cube, and you can see the hexagonal appearance of the cube. The hexagonal boundary represents color, the horizontal axis represents purity, and value is measured along the vertical axis.

2. Code Design
- Import Header Files
xxxxxxxxxx#!/usr/bin/env python# coding: utf-8import threadingimport cv2 as cvimport ipywidgets as widgetsfrom IPython.display import displayfrom dofbot_utils.dofbot_config import *- Create Instance, Initialize Parameters
xxxxxxxxxx# Create and update HSV instanceupdate_hsv = update_hsv()# Initialize the num parameternum=0# Initialization modemodel = "General"# Initialize HSV nameHSV_name=None# Initialize HSV valuecolor_hsv = {"red" : ((0, 43, 46), (10, 255, 255)), "green" : ((35, 43, 46), (77, 255, 255)), "blue" : ((100, 43, 46), (124, 255, 255)), "yellow": ((26, 43, 46), (34, 255, 255))}# Set random colorscolor = [[random.randint(0, 255) for _ in range(3)] for _ in range(255)]# HSV parameter pathHSV_path="/home/jetson/dofbot_pro/dofbot_color_identify/scripts/HSV_config.txt"# Read HSV configuration file, update HSV valuetry: read_HSV(HSV_path,color_hsv)except Exception: print("Read HSV_config Error!!!")- Initialize Robotic Arm Position
x#Orin boardXYT_path="/home/jetson/dofbot_pro/dofbot_color_identify/scripts/XYT_config.txt"#Jetson-Nano boardXYT_path="/root/dofbot_pro/dofbot_color_identify/scripts/XYT_config.txt"try: xy, threshold = read_XYT(XYT_path)except Exception: print("Read XYT_config Error !!!")import Arm_LibArm = Arm_Lib.Arm_Device()joints_0 = [xy[0], xy[1], 0, 0, 90, 30]Arm.Arm_serial_servo_write6_array(joints_0, 1000)
- Create Controls
xxxxxxxxxx# Create layoutbutton_layout = widgets.Layout(width='260px', height='40px', align_self='center')# Output partoutput = widgets.Output()# Enter color update modeHSV_update_red = widgets.Button(description='HSV_update_red', button_style='success', layout=button_layout)HSV_update_green = widgets.Button(description='HSV_update_green', button_style='success', layout=button_layout)HSV_update_blue = widgets.Button(description='HSV_update_blue', button_style='success', layout=button_layout)HSV_update_yellow = widgets.Button(description='HSV_update_yellow', button_style='success', layout=button_layout)HSV_write_file = widgets.Button(description='HSV_write_file', button_style='primary', layout=button_layout)Color_Binary = widgets.Button(description='Color/Binary', button_style='info', layout=button_layout)# Adjust the sliderH_min_slider = widgets.IntSlider(description='H_min :', value=0, min=0, max=255, step=1, orientation='horizontal')S_min_slider = widgets.IntSlider(description='S_min :', value=43, min=0, max=255, step=1, orientation='horizontal')V_min_slider = widgets.IntSlider(description='V_min :', value=46, min=0, max=255, step=1, orientation='horizontal')H_max_slider = widgets.IntSlider(description='H_max :', value=10, min=0, max=255, step=1, orientation='horizontal')S_max_slider = widgets.IntSlider(description='S_max :', value=255, min=0, max=255, step=1, orientation='horizontal')V_ma