Skip to content

Commit

Permalink
* update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
lxowalle committed May 10, 2024
1 parent 5d616dc commit 508a0d8
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 41 deletions.
65 changes: 65 additions & 0 deletions docs/doc/en/projects/line_tracking_robot.md
Original file line number Diff line number Diff line change
@@ -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

2 changes: 1 addition & 1 deletion docs/doc/en/vision/apriltag.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion docs/doc/en/vision/find_blobs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
40 changes: 21 additions & 19 deletions docs/doc/en/vision/line_tracking.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
```

Expand All @@ -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!

Expand Down
2 changes: 1 addition & 1 deletion docs/doc/en/vision/qrcode.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
66 changes: 66 additions & 0 deletions docs/doc/zh/projects/line_tracking_robot.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion docs/doc/zh/vision/apriltag.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ update:
content: 初版文档
---

阅读本文前,确保已经知晓如何开发MaixPy,详情请阅读[MaixVision -- MaixPy 编程 + 图形化积木编程](../basic/maixvision.md)
阅读本文前,确保已经知晓如何开发MaixCAM,详情请阅读[快速开始](../README.md)

## 简介

Expand Down
2 changes: 1 addition & 1 deletion docs/doc/zh/vision/find_blobs.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ update:
content: 添加寻找色块的详细用法
---

阅读本文前,确保已经知晓如何开发MaixPy,详情请阅读[MaixVision -- MaixPy 编程 + 图形化积木编程](../basic/maixvision.md)
阅读本文前,确保已经知晓如何开发MaixCAM,详情请阅读[快速开始](../README.md)

## 简介

Expand Down
29 changes: 15 additions & 14 deletions docs/doc/zh/vision/line_tracking.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ update:
content: 初版文档
---

阅读本文前,确保已经知晓如何开发MaixPy,详情请阅读[MaixVision -- MaixPy 编程 + 图形化积木编程](../basic/maixvision.md)
阅读本文前,确保已经知晓如何开发MaixCAM,详情请阅读[快速开始](../README.md)

## 简介

在视觉应用中,在巡迹小车、巡线机器人等应用中经常需要寻找线条的功能。本文将介绍:

- 如何使用MaixPy来寻找直线
- 如何使用MaixPy来实现巡线功能

- 如何使用MaixCam的默认应用程序寻找直线
- 如何使用MaixCam的默认应用程序巡线


## 如何使用MaixPy来寻找直线
Expand Down Expand Up @@ -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)

```

步骤:
Expand Down Expand Up @@ -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运行代码,就可以寻线啦,看看效果吧

Expand Down
2 changes: 1 addition & 1 deletion docs/doc/zh/vision/qrcode.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ update:
content: 初版文档
---

阅读本文前,确保已经知晓如何开发MaixPy,详情请阅读[MaixVision -- MaixPy 编程 + 图形化积木编程](../basic/maixvision.md)
阅读本文前,确保已经知晓如何开发MaixCAM,详情请阅读[快速开始](../README.md)

## 简介

Expand Down
5 changes: 3 additions & 2 deletions examples/vision/image_basic/line_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 508a0d8

Please sign in to comment.