Welcome to DOFBOT-JetsonNANO repository
2.Color block positioning
Color Block Positioning Experiment
The main purpose of color block positioning is to achieve advanced functionality for color block following. The principle is to determine the distance and position information of the color block relative to the camera by calculating the center point coordinates of the color block in the camera's field of view, thereby achieving color block positioning. The experimental results show that it continuously finds and follows the center point of the color block.
1. Main Code
- Start Docker
After entering the Raspberry Pi 5 desktop, open the terminal and run the following command to start the corresponding Dofbot container:
./Docker_Ros.sh
Then, in the Docker terminal, input the following command to start jupyter-lab:
xxxxxxxxxxcdjupyter-lab --allow-root
Then, in the browser, input the main board's IP address plus :9999:
xxxxxxxxxxIP:9999 // Example: 192.168.1.11:9999
Code path: /root/Dofbot_2D/src/dofbot_color_follow/Color block positioning.ipynb
- Import header files
xxxxxxxxxximport cv2 as cvimport threadingimport randomfrom time import sleepimport ipywidgets as widgetsfrom IPython.display import displayfrom positioning import color_follow- Main recognition function, simultaneously obtaining the target center point of the color block (color_x, color_y)
xxxxxxxxxxdef follow_function(self, img, HSV_config): (color_lower, color_upper) = HSV_config self.img = cv.resize(img, (640, 480), ) self.img = cv.GaussianBlur(self.img, (5, 5), 0) hsv = cv.cvtColor(self.img, cv.COLOR_BGR2HSV) mask = cv.inRange(hsv, color_lower, color_upper) mask = cv.erode(mask, None, iterations=2) mask = cv.dilate(mask, None, iterations=2) mask = cv.GaussianBlur(mask, (5, 5), 0) cnts = cv.findContours(mask.copy(), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)[-2] if len(cnts) > 0: cnt = max(cnts, key=cv.contourArea) (color_x, color_y), color_radius = cv.minEnclosingCircle(cnt) if color_radius > 10: # Mark the detected color with the prototype coil # 将检测到的颜色用原形线圈标记出来 cv.circle(self.img, (int(color_x), int(color_y)), int(color_radius), (255, 0, 255), 3) print(color_x,color_y) return self.img- Create controls
xbutton_layout = widgets.Layout(width='200px', height='100px', align_self='center')# 输出控件 Output widgetoutput = widgets.Output()# 颜色追踪 Color trackingcolor_follow = widgets.Button(description='color_follow', button_style="success", layout=butt