Commit 07fcf820 authored by Jeeken's avatar Jeeken
Browse files

fix markdown format

parent 957846d8
Loading
Loading
Loading
Loading
+87 −57
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@

## Files
| File Name      | Inform                                   |
|------------         |:----                                          |
| -------------- | :--------------------------------------- |
| car_target.py  | Armour Detection                         |
| circle_test.py | A testing program for circle detection   |
| tool.py        | Supporting file for armour detection program |
@@ -10,21 +10,27 @@

## Tutorial

### For User:
#### Parameters Setting
<b>MODE:  </b> Running mode, 'D' for debug mode, 'R' for running mode.
<b>SRC:   </b> Image source, 'PIC' for reading from picture, 'CAM' for camera, 'VID' for video
<b>TARGET:</b> Color of target, 'BLUE' detecting blue light bar, 'RED' for detecting red light bar
### For Users:

### For Developer:
#### Parameter Settings

**MODE:** Running mode, `D` for debug mode, `R` for running mode.

**SRC:** Image source, `PIC` for reading from picture, `CAM` for camera, `VID` for video

**TARGET:** Color of target, `BLUE` for detecting blue light bar, `RED` for detecting red light bar

### For Developers:

#### Fixing Log
1. Return target coordinate from <b>main(mat)</b> and write to microcomputer from writer

1. Return target coordinate from `main(mat)` and write to microcomputer from serial port
2. Resizing the output image to a formal value
3. Match target from camera coordinate to gun coordinate
4. Converse (x,y) to (pitch, yaw)
4. Convert (x,y) to (pitch, yaw)

#### Improvements:

#### Improving Point:
1. Coordinate conversion
2. Targeting Mode
3. Robustness: in current algorithm, final target selection is based on pixel numbers. Whichever methods we used for tracking, we cannot keep tracking a car in real competition. The gun will keep switching between different cars because of the disturbance. This problem must be fixed!
@@ -34,54 +40,65 @@
## Complete Documentation for Car Target Detection

### API for functions in the program

#### func_undistort
Recover the undistorted image obtained from the camera. </br>

<b>parameter :</b>
* <b>mat</b> - image matrix
Recover the undistorted image obtained from the camera.

##### parameters:

- `mat` - image matrix

#### track

1. color threshold
2. segment image by connected component and find the bar
3. match the bar and get the bar pairs with similar angle and length
4. select the bar pairs</br>
4. select the bar pairs

##### parameters:

- `mat` - image matrix
- `color_threshold` - the value of threshold
- `square_ratio` - the threshold ratio of height/width (any smaler than the threshold will be deleted)
- `angle_rate` - angle factor for matching
- `length_rate` - length factor for matching
- `matrix threshold` - match those with score greater than threshold
- `DIST` - distance of the target

<b>parameters :</b>
* <b>mat</b> - image matrix
* <b>color_threshold</b> - the value of threshold
* <b>square_ratio</b> - the threshold ratio of height/width (any smaller than the threshold will be deleted)
* <b>angle_rate</b> - angle factor for matching
* <b>length_rate</b> - length factor for matching
* <b>matrix threshold</b> - match those with score greater than threshold
* <b>DIST</b> - distance of the target
##### return:

<b> return :</b>
* return: [] when no target, [x, y, pixel count] for target
`[]` when no target, `[x, y, pixel_count]` for target

#### func_get_delete_strict_list

Get a strict list of deleted component. Used when target is near

<b>parameters :</b>
* <b>connect_data</b> - the information list of connected component
* <b>square_ratio</b> - standard ratio for selecting the square
##### parameters:

- `connect_data` - the information list of connected component
- `square_ratio` - standard ratio for selecting the square

#### func_get_delete_loose_list

Get a strict list of deleted component. Used when target is far

<b>parameters :</b>
* <b>connect_data</b> - the information list of connected component
* <b>square_ratio</b> - standard ratio for selecting the square
##### parameters:

- `connect_data` - the information list of connected component
- `square_ratio` - standard ratio for selecting the square

#### main

Main function of detection.

<b>parameter :</b>
* <b>mat</b> - image matrix
##### parameters:
- `mat` - image matrix

#### cv2.connectedComponentsWithStats

connect_output is a tuple :

- int component count,
- ndarray label map : use int to label connect  component, same int value means one component, size equal to "b"
- ndarray connect component info : a n * 5 ndarray to show connect component info, [left_top_x, left_top_y, width, height, pixel number])
@@ -91,6 +108,7 @@ connect_output is a tuple :
### Detail description on Armour Detection

#### Setup

Set up undistorted matrix and some parameters

#### Program
@@ -99,41 +117,53 @@ Set up undistorted matrix and some parameters

##### 2. Judge image source and run corresponding program

###### 2.1 Image source is picture<br>
###### 2.1 Image source is picture

Give a file list

Read pictures in the list one by one.

Store a read picture in `mat`, and run `main(mat)`

Quit the program and close all windows when "q" was pressed

Give a file list<br>
Read pictures in the list one by one.<br>
Store a read picture in <b>mat</b>, and run <b>main(mat)</b> <br>
Quit the program and close all windows when press "q"<br>
###### 2.2 Image source is camera

Capture a picture with camera<br>
If camera is open, read the captured picture, stored it in <b>mat</b>, run <b>func_undistort(mat)</b> to undistort the captured picture, and run <b>main(mat)</b><br>
Capture a picture with camera

If camera is open, read the captured picture, stored it in `mat`, run `func_undistort(mat)`` to undistort the captured picture, and run `main(mat)`

Stop capturing, and close all windows

###### 2.3 Image source is video

Run a video catcher to capture pictures from a video<br>
While catcher is running, read the image captured, store it in <b>mat</b> and run <b>main(mat)</b><br>
Wait for some seconds (the exact number of seconds is decided by variable <b>wait</b>)<br>
When press "q", break; when press "p", set <b>wait</b> to 0; when press "c",set <b>wait</b> to 10.<br>
Run a video capturer to capture pictures from a video

While capturer is running, read the image captured, store it in `mat` and run `main(mat)`

Wait for some seconds (the exact number of seconds is decided by variable `wait`)

When "q" was pressed, break; when "p" was pressed, set `wait` to 0; when "c" was pressed, set `wait` to 10.

Stop capturing and close all windows.

#### 3. Main
1. Get global variable : target_last and MIN_STEP. Copy <b>mat</b> (input image matrix) to <b>origin</b>, set <b>tstart</b> to starting time.
2. Track the near by targets :<b>track</b>(detail information of this function is depicted below)

1. Get global variable: target_last and MIN_STEP. Copy `mat` (input image matrix) to `origin`, set `tstart` to starting time.
2. Track the near by targets: `track`(detail information of this function is depicted below)
3. If no target is found, track the far targets.
4. Calculate the distance of the target
5. Draw a circle on the detected targets.

#### 4. Track
1. Relative threshold: set 255 if b - r < color_threshold. Show the processed image if it's in debug mode.<br>
2. Label connected component: Use <b>cv2.connectedComponentsWithStats</b> (whose detail information is given in function part) to label. Draw rectangles on the selected connected components if in Debug mode.
3. Get delete list according to the ratio of length and height, and area of connected components ( use <b>func_get_delete_strict_list</b> in <b>NEAR</b> mode and with <b>func_get_delete_loose_list</b> in <b>MID</b> mode to get the list of connected components to be deleted ). If not connected components are remained after deletion, return.

1. Relative threshold: set 255 if b - r < color_threshold. Show the processed image if it's in debug mode.
2. Label connected component: Use `cv2.connectedComponentsWithStats` (whose detail information is given in function part) to label. Draw rectangles on the selected connected components if in Debug mode.
3. Get delete list according to the ratio of length and height, and area of connected components ( use `func_get_delete_strict_list` in `NEAR` mode and with `func_get_delete_loose_list` in `MID` mode to get the list of connected components to be deleted ). If not connected components are remained after deletion, return.
4. Get y coordinate and x coordinate of mid point for both top line and bottom line of the light bar. Draw a circle on both points when in debug mode.
5. Calculate bar length, bar angle and get weighted sum.
6. Calculate bar angle difference and bar length difference  matrix
7. Use a weighted sum combining angle difference and length difference, and then threshold the sum to get a primitive match
8. Calculate x difference, y difference and z difference of each pair of light bars. Further distinguish difference between pairs by thresholding cross ratio and angles of crossing line between paris.
9. Times up all three match matrix to get the final match result. (All three matrices are 0-1 matrix and square matrix, it's just like combining 3 relation and keep only those available in all three matrices as real available pairs).
9. Calculate the sum of pixels of matching pairs, pick up pairs with most total pixel to be the target. Calculate and return its x and y coordinate.
10. Calculate the sum of pixels of matching pairs, pick up pairs with most total pixel to be the target. Calculate and return its x and y coordinate.
+2 −1
Original line number Diff line number Diff line
# CV_tools_API
# CV Tools API

Reconstruction of Codes in CV Tools