diff --git a/docs/doc/en/projects/line_tracking_robot.md b/docs/doc/en/projects/line_tracking_robot.md index 7b8f8458..60564eee 100644 --- a/docs/doc/en/projects/line_tracking_robot.md +++ b/docs/doc/en/projects/line_tracking_robot.md @@ -1,5 +1,70 @@ --- title: MaixPy Line Tracking Robot (/Car) +update: + - date: 2024-05-09 + author: lxowalle + version: 1.0.0 + content: Initial documentation --- +Before reading this article, make sure you know how to develop with MaixCAM. For details, please read [Quick Start](../README.md). + +## Introduction + +This article describes how to implement a line tracking robot using MaixPy. + +## How to implement line tracking robot using MaixPy + +1. Preparation of MaixCAM and trolley +2. Implementing the line tracking function +3. Implement the trolley control function + +### Preparation of MaixCAM and trolley + +TODO + +### Implementing the line tracking function + +You can quickly find straight lines using the `get_regression` of the `image` module, see [Line tracking](. /line_tracking.md). + +Code: + +```python +from maix import camera, display, image + +cam = camera.Camera(320, 240) +disp = display.Display() + +# thresholds = [[0, 80, 40, 80, 10, 80]] # red +thresholds = [[0, 80, -120, -10, 0, 30]] # green +# thresholds = [[0, 80, 30, 100, -120, -60]] # blue + +while 1: + img = cam.read() + + lines = img.get_regression(thresholds, area_threshold = 100) + for a in lines: + img.draw_line(a.x1(), a.y1(), a.x2(), a.y2(), image.COLOR_GREEN, 2) + theta = a.theta() + rho = a.rho() + if theta > 90: + theta = 270 - theta + else: + theta = 90 - theta + img.draw_string(0, 0, "theta: " + str(theta) + ", rho: " + str(rho), image.COLOR_BLUE) + + disp.show(img) + +``` + +The above code implements the function of finding a straight line, note: + +- Use `a.theta()` to get the angle of the line. +- Use `a.rho()` to get the distance between the line and the origin (the origin is in the upper left corner). + +After find the straight line with reference to the above code, you can use `a.theta()` and `a.rho()` to control the direction of the cart. + +### Implement the trolley control function + +TODO diff --git a/docs/doc/en/vision/apriltag.md b/docs/doc/en/vision/apriltag.md index ee164986..c31644c7 100644 --- a/docs/doc/en/vision/apriltag.md +++ b/docs/doc/en/vision/apriltag.md @@ -7,7 +7,7 @@ update: content: Initial documentation --- -Before reading this article, make sure you are familiar with how to develop with MaixPy. For more details, please read [MaixVision -- MaixPy Programming + Graphical Block Programming](../basic/maixvision.md). +Before reading this article, make sure you are familiar with how to develop with MaixCAM. For more details, please read [Quick Start](../README.md). ## Introduction diff --git a/docs/doc/en/vision/find_blobs.md b/docs/doc/en/vision/find_blobs.md index 17eaac9d..bcca15fb 100644 --- a/docs/doc/en/vision/find_blobs.md +++ b/docs/doc/en/vision/find_blobs.md @@ -10,7 +10,7 @@ update: version: 1.0.1 content: Added detailed usage for finding blobs --- -Before reading this article, make sure you know how to develop with MaixPy. For details, please read [MaixVision -- MaixPy Programming + Graphical Block Programming](../basic/maixvision.md). +Before reading this article, make sure you know how to develop with MaixCAM. For details, please read [Quick Start](../README.md). ## Introduction diff --git a/docs/doc/en/vision/line_tracking.md b/docs/doc/en/vision/line_tracking.md index 6f38c51f..155a8436 100644 --- a/docs/doc/en/vision/line_tracking.md +++ b/docs/doc/en/vision/line_tracking.md @@ -8,7 +8,7 @@ update: --- -Before reading this article, make sure you already know how to develop MaixPy. For details, please read [MaixVision -- MaixPy Programming + Graphical Block Programming](../basic/maixvision.md). +Before reading this article, make sure you already know how to develop MaixCAM. For details, please read [Quick Start](../README.md). ## Introduction @@ -37,19 +37,20 @@ disp = display.Display() thresholds = [[0, 80, -120, -10, 0, 30]] # green # thresholds = [[0, 80, 30, 100, -120, -60]] # blue -while 1. +while 1: img = cam.read() lines = img.get_regression(thresholds, area_threshold = 100) - for a in lines. + for a in lines: img.draw_line(a.x1(), a.y1(), a.x2(), a.y2(), image.COLOR_GREEN, 2) theta = a.theta() - if theta > 90. + rho = a.rho() + if theta > 90: theta = 270 - theta - if theta > 90: theta = 270 - theta + else: theta = 90 - theta - img.draw_string(0, 0, ‘theta: ’ + str(theta), image.COLOR_BLUE) - + img.draw_string(0, 0, "theta: " + str(theta) + ", rho: " + str(rho), image.COLOR_BLUE) + disp.show(img) ``` @@ -71,30 +72,31 @@ Steps: 3. Get the image from the camera and display it ```python - while 1. + while 1: img = cam.read() disp.show(img) ``` 4. Call the `get_regression` method to find the straight line in the camera image and draw it to the screen - ``` python - lines = img.get_regression(thresholds, pixels_threshold = 100) - for a in lines. - img.draw_line(a.x1(), a.y1(), a.x2(), a.y2(), image.COLOR_GREEN, 2) - theta = a.theta() - if theta > 90. - theta = 270 - theta - if theta > 90: theta = 270 - theta - theta = 90 - theta - img.draw_string(0, 0, ‘theta: ’ + str(theta), image.COLOR_BLUE) + ```python + lines = img.get_regression(thresholds, area_threshold = 100) + for a in lines: + img.draw_line(a.x1(), a.y1(), a.x2(), a.y2(), image.COLOR_GREEN, 2) + theta = a.theta() + rho = a.rho() + if theta > 90: + theta = 270 - theta + else: + theta = 90 - theta + img.draw_string(0, 0, "theta: " + str(theta) + ", rho: " + str(rho), image.COLOR_BLUE) ``` - `img` is the camera image read via `cam.read()`, when initialised as `cam = camera.Camera(320, 240)`, the `img` object is an RGB image with a resolution of 320x240. - `img.get_regression` is used to find straight lines, `thresholds` is a list of colour thresholds, each element is a colour threshold, multiple thresholds are passed in if multiple thresholds are found at the same time, and each colour threshold has the format `[L_MIN, L_MAX, A_MIN, A_MAX, B_MIN, B_MAX]`, where ` L`, `A`, `B` are the three channels of `LAB` colour space, `L` channel is the luminance, `A` channel is the red-green channel, `B` channel is the blue-yellow channel. `pixels_threshold` is a pixel area threshold used to filter some unwanted straight lines. - `for a in lines` is used to iterate through the returned `Line` objects, where `a` is the current `Line` object. Normally the `get_regression` function will only return one `Line` object, but if you need to find more than one line, try the `find_line` method. - Use `img.draw_line` to draw the found line, `a.x1(), a.y1(), a.x2(), a.y2()` represent the coordinates of the ends of the line. - - Use `img.draw_string` to show the angle between the line and the x-axis in the upper left corner, and `a.theta()` is the angle between the line and the y-axis, which is converted to `theta` for easier understanding. + - Use `img.draw_string` to show the angle between the line and the x-axis in the upper left corner, and `a.theta()` is the angle between the line and the y-axis, which is converted to `theta` for easier understanding, `a.rho()` is the length of the vertical line from the origin to the line. 5. Run the code through the maixvision, you can find the line, look at the effect! diff --git a/docs/doc/en/vision/qrcode.md b/docs/doc/en/vision/qrcode.md index 4e221df6..b3d45476 100644 --- a/docs/doc/en/vision/qrcode.md +++ b/docs/doc/en/vision/qrcode.md @@ -7,7 +7,7 @@ update: content: Initial document --- -Before reading this article, make sure you are familiar with how to develop with MaixPy. For details, please read [MaixVision -- MaixPy Programming + Graphical Block Programming](../basic/maixvision.md) +Before reading this article, make sure you are familiar with how to develop with MaixCAM. For details, please read [Quick Start](../README.md). ## Introduction diff --git a/docs/doc/zh/projects/line_tracking_robot.md b/docs/doc/zh/projects/line_tracking_robot.md index 06893cb9..57e714db 100644 --- a/docs/doc/zh/projects/line_tracking_robot.md +++ b/docs/doc/zh/projects/line_tracking_robot.md @@ -1,5 +1,71 @@ --- title: MaixPy 小车巡线 +update: + - date: 2024-05-09 + author: lxowalle + version: 1.0.0 + content: 初版文档 --- +阅读本文前,确保已经知晓如何开发MaixCAM,详情请阅读[快速开始](../README.md) +## 简介 + +本文将介绍如何使用MaixPy实现寻线小车 + +## 如何使用MaixPy实现寻线小车 + +1. 准备MaixCAM与小车 +2. 实现寻线功能 +3. 实现小车控制功能 + +### 准备MaixCAM与小车 + +TODO + +### 实现寻线功能 + +使用`image`模块的`get_regression`可以快速寻找到直线,详情见[寻找直线](./line_tracking.md) + +代码实现: + +```python +from maix import camera, display, image + +cam = camera.Camera(320, 240) +disp = display.Display() + +# thresholds = [[0, 80, 40, 80, 10, 80]] # red +thresholds = [[0, 80, -120, -10, 0, 30]] # green +# thresholds = [[0, 80, 30, 100, -120, -60]] # blue + +while 1: + img = cam.read() + + lines = img.get_regression(thresholds, area_threshold = 100) + for a in lines: + img.draw_line(a.x1(), a.y1(), a.x2(), a.y2(), image.COLOR_GREEN, 2) + theta = a.theta() + rho = a.rho() + if theta > 90: + theta = 270 - theta + else: + theta = 90 - theta + img.draw_string(0, 0, "theta: " + str(theta) + ", rho: " + str(rho), image.COLOR_BLUE) + + disp.show(img) + +``` + +上述代码实现了寻线功能, 上述参数中需注意: + +- 设置合适的thresholds值来寻找到对应的直线 +- 设置合适的area_threshold值来过滤环境干扰,可以过滤一些面积小的直线 +- 使用`a.theta()`获取直线的角度 +- 使用`a.rho()`获取直线与原点(原点在左上角)的距离 + +根据实际环境调试好寻线参数后, 就可以利用`a.theta()`和`a.rho()`控制小车方向了。 + +### 实现小车控制功能 + +TODO diff --git a/docs/doc/zh/vision/apriltag.md b/docs/doc/zh/vision/apriltag.md index ac5706f1..ffbd29e4 100644 --- a/docs/doc/zh/vision/apriltag.md +++ b/docs/doc/zh/vision/apriltag.md @@ -7,7 +7,7 @@ update: content: 初版文档 --- -阅读本文前,确保已经知晓如何开发MaixPy,详情请阅读[MaixVision -- MaixPy 编程 + 图形化积木编程](../basic/maixvision.md) +阅读本文前,确保已经知晓如何开发MaixCAM,详情请阅读[快速开始](../README.md) ## 简介 diff --git a/docs/doc/zh/vision/find_blobs.md b/docs/doc/zh/vision/find_blobs.md index 0ada5318..50d8206f 100644 --- a/docs/doc/zh/vision/find_blobs.md +++ b/docs/doc/zh/vision/find_blobs.md @@ -11,7 +11,7 @@ update: content: 添加寻找色块的详细用法 --- -阅读本文前,确保已经知晓如何开发MaixPy,详情请阅读[MaixVision -- MaixPy 编程 + 图形化积木编程](../basic/maixvision.md) +阅读本文前,确保已经知晓如何开发MaixCAM,详情请阅读[快速开始](../README.md) ## 简介 diff --git a/docs/doc/zh/vision/line_tracking.md b/docs/doc/zh/vision/line_tracking.md index 342f2bd9..ea389b18 100644 --- a/docs/doc/zh/vision/line_tracking.md +++ b/docs/doc/zh/vision/line_tracking.md @@ -7,15 +7,15 @@ update: content: 初版文档 --- -阅读本文前,确保已经知晓如何开发MaixPy,详情请阅读[MaixVision -- MaixPy 编程 + 图形化积木编程](../basic/maixvision.md) +阅读本文前,确保已经知晓如何开发MaixCAM,详情请阅读[快速开始](../README.md) ## 简介 在视觉应用中,在巡迹小车、巡线机器人等应用中经常需要寻找线条的功能。本文将介绍: -- 如何使用MaixPy来寻找直线 +- 如何使用MaixPy来实现巡线功能 -- 如何使用MaixCam的默认应用程序寻找直线 +- 如何使用MaixCam的默认应用程序巡线 ## 如何使用MaixPy来寻找直线 @@ -43,14 +43,14 @@ while 1: for a in lines: img.draw_line(a.x1(), a.y1(), a.x2(), a.y2(), image.COLOR_GREEN, 2) theta = a.theta() + rho = a.rho() if theta > 90: theta = 270 - theta else: theta = 90 - theta - img.draw_string(0, 0, "theta: " + str(theta), image.COLOR_BLUE) + img.draw_string(0, 0, "theta: " + str(theta) + ", rho: " + str(rho), image.COLOR_BLUE) disp.show(img) - ``` 步骤: @@ -79,22 +79,23 @@ while 1: 4. 调用`get_regression`方法寻找摄像头图片中的直线,并画到屏幕上 ```python - lines = img.get_regression(thresholds, pixels_threshold = 100) + lines = img.get_regression(thresholds, area_threshold = 100) for a in lines: - img.draw_line(a.x1(), a.y1(), a.x2(), a.y2(), image.COLOR_GREEN, 2) - theta = a.theta() - if theta > 90: - theta = 270 - theta - else: - theta = 90 - theta - img.draw_string(0, 0, "theta: " + str(theta), image.COLOR_BLUE) + img.draw_line(a.x1(), a.y1(), a.x2(), a.y2(), image.COLOR_GREEN, 2) + theta = a.theta() + rho = a.rho() + if theta > 90: + theta = 270 - theta + else: + theta = 90 - theta + img.draw_string(0, 0, "theta: " + str(theta) + ", rho: " + str(rho), image.COLOR_BLUE) ``` - `img`是通过`cam.read()`读取到的摄像头图像,当初始化的方式为`cam = camera.Camera(320, 240)`时,`img`对象是一张分辨率为320x240的RGB图。 - `img.get_regression`用来寻找直线, `thresholds` 是一个颜色阈值列表,每个元素是一个颜色阈值,同时找到多个阈值就传入多个,每个颜色阈值的格式为 `[L_MIN, L_MAX, A_MIN, A_MAX, B_MIN, B_MAX]`,这里的 `L`、`A`、`B` 是`LAB`颜色空间的三个通道,`L` 通道是亮度,`A` 通道是红绿通道,`B` 通道是蓝黄通道。`pixels_threshold`是一个像素面积的阈值,用来过滤一些不需要直线。 - `for a in lines`用来遍历返回的`Line`对象, 其中`a`就是当前的`Line`对象。通常`get_regression`函数只会返回一个`Line`对象,如果需要寻找多条直线,可以尝试使用`find_line`方法 - 使用`img.draw_line`来画出找到的线条,`a.x1(), a.y1(), a.x2(), a.y2()`分别代表直线两端的坐标 - - 使用`img.draw_string`在左上角显示直线与x轴的夹角, `a.theta()`是直线与y轴的夹角, 这里为了方便理解转换成直线与x轴的夹角`theta` + - 使用`img.draw_string`在左上角显示直线与x轴的夹角, `a.theta()`是直线与y轴的夹角, 这里为了方便理解转换成直线与x轴的夹角`theta`,`a.rho()`是原点与直线的垂线的长度. 5. 通过maixvision运行代码,就可以寻线啦,看看效果吧 diff --git a/docs/doc/zh/vision/qrcode.md b/docs/doc/zh/vision/qrcode.md index 7ce0ac93..386ea6d4 100644 --- a/docs/doc/zh/vision/qrcode.md +++ b/docs/doc/zh/vision/qrcode.md @@ -7,7 +7,7 @@ update: content: 初版文档 --- -阅读本文前,确保已经知晓如何开发MaixPy,详情请阅读[MaixVision -- MaixPy 编程 + 图形化积木编程](../basic/maixvision.md) +阅读本文前,确保已经知晓如何开发MaixCAM,详情请阅读[快速开始](../README.md) ## 简介 diff --git a/examples/vision/image_basic/line_tracking.py b/examples/vision/image_basic/line_tracking.py index fbabae35..673d5785 100644 --- a/examples/vision/image_basic/line_tracking.py +++ b/examples/vision/image_basic/line_tracking.py @@ -14,10 +14,11 @@ for a in lines: img.draw_line(a.x1(), a.y1(), a.x2(), a.y2(), image.COLOR_GREEN, 2) theta = a.theta() + rho = a.rho() if theta > 90: theta = 270 - theta else: theta = 90 - theta - img.draw_string(0, 0, "theta: " + str(theta), image.COLOR_BLUE) + img.draw_string(0, 0, "theta: " + str(theta) + ", rho: " + str(rho), image.COLOR_BLUE) - disp.show(img) + disp.show(img) \ No newline at end of file