STM32F4 image recognition

1. Hardware introduction
(1) The main chip STM32F407VET6
1. Supports single-precision floating-point operations. Chip hardware resources include 192KB internal RAM, 512KB-1MB FLASH (depending on the chip model), using a decentralized memory architecture 128KB internal SRAM, 64KB high-speed RAM , the CPU can directly access this part of the memory (the access speed is the CPU main frequency speed), so the internal SRAM image acquisition and display is usually used, and the CCM-RAM is used for image processing and recognition, which can give full play to the advantages of the M4 core. 2.
Has DCMI digital image acquisition interface, directly collects parallel camera images and uses DMA transmission, including USB, PWM, USRT, SPI, I2C, CAN and many other peripherals, can also use FMC to drive parallel screen and expand external SRAM, use SPI, QSPI plug-in FLASH or SD card.
Internal SRAM start address 0x2000000 length 0x20000 (128KB)
CCM RAM start address 0x1000000 length 0x10000 (64KB)
QVGA image size: RGB5656: 320x240x2/1024=150KB
Gray image Y8: 320x240/1024=75KB
F407 main chip maximum Can handle QVGA High-resolution images, when the RGB format is used, the sub-fast memory is used for processing, and the grayscale image can be directly collected and processed by the internal SRAM.
(2) Camera
OV series cameras such as OV7670, OV7725, and OV2640 are all CMOS digital cameras of OV Company. The hardware interface adopts parallel timing. Generally, the FPC interface adopts the DVP standard, and the 2.54 interface depends on the interface sequence of the actual manufacturer. The definition generally includes VSYNC (field signal), HSYNC (row signal), PCLK (pixel clock signal), XCLK (external clock), D0-D7 data signal, SCL/SDA (SCCB interface compatible with I2C protocol), PWDN (connected to GND) , RST (pull high), the general STM32 image acquisition method is: take the external clock as an example, configure PA8 as an external clock output, and output the acquisition clock that conforms to the camera, and use the SCCB interface to configure the registers of the camera (including the clock times of image acquisition) Frequency division, image acquisition resolution, start and end positions of image acquisition rows and columns, image effects, 3A, DSP, gamma curve, lens correction, etc.), and then perform image acquisition according to the timing of the camera (specifically For timing and register configuration, see the data sheet of the corresponding camera).
(3) LCD screens
ST7735, ST7789, and ILI series all have SPI-driven LCD screens. The most important thing is LCD read-write timing configuration and LCD parameter configuration. For details, you can check the corresponding LCD IC data manual (4)
Helm The steering
gear generally has SG90 (plastic gear), MG90S (metal gear), MG995/996, etc., and the driving method is driven by PWM signal. The control of the steering gear generally requires a time base pulse of about 20ms. The high level part of the pulse is generally the angle control pulse part in the range of 0.5ms-2.5ms, and the total interval is 2ms.
Take the 180 degree angle servo as an example, then the corresponding control relationship:
0.5ms------------0 degree;
1.0ms------------45 degree;
1.5ms------------90 degrees;
2.0ms-----------135 degrees;
2.5ms---------180 degrees;
use the F407 main chip with a main frequency of 168M to control the servo, first set a PWM wave with a period of 20ms, the main frequency of the system is 168M, and the
frequency of the TIM peripheral clock is divided by 2// The automatic reload value is 2000,
the frequency division factor is 840168/2/840=0.1M 0.1M/2000=50Hz 20ms period PWM wave
0 degree----0.5/20 2000=50;
45 degree----1.0/ 20
2000=100;
90 degrees----1.5/20 2000=150;
135 degrees----2.0/20
2000=200;
180 degrees----2.5/20*2000=250;

2. Image acquisition and processing

1. Hardware configuration process
(1) DCMI_GPIO configuration DCMI configuration
includes line HSYNC, field VSYNC, and pixel signal PCLK, as well as data signals, which are collected in 8bit mode, that is, each pixel clock is transmitted to DCMI_DR data register 8-bit data. Configure the corresponding GPIO port as DCMI multiplexing mode, and configure synchronous clock, line and field and pixel synchronization, enable DCMI frame interrupt, and configure interrupt priority. The smaller the value, the higher the priority. DMA configuration, configure the DMA channel as a DCMI acquisition channel, configure the peripheral address, target transfer address, memory type, incremental mode, transfer data length and other parameters, if double buffering is used, configure the DMA as a double buffering mode, and enable the DMA transfer completion interrupt , and configure the interrupt priority.
(2) Camera configuration clock configuration
An external clock is used, that is, the output clock of STM32F4 is used for the camera to work. The basic clock is 12MHz, and the camera will configure the clock for its work according to the clock register. Register configuration, use i2c communication to configure the camera register so that it can output images normally. Image resolution configuration, by modifying the start position and end position of the camera output screen, configure the window position of the row and column respectively, so that it can output the image of the target size.
(3) PWM configuration
Configure the steering gear control port as PWM output mode, and configure it as 20ms PWM output according to the prescaler system and reload value, and output PWM according to the steering gear control method to control the steering gear 2. Interrupt processing
flow
( 1) The DCMI line field interrupt acquisition process
uses DMA to transfer data from the data register, and uses word as the unit (4 bytes) for FIFO transfer (first in, first out). The target image must be in RGB565 format. In the DCMI interrupt processing function, Change the transfer address of DMA each time a line interrupt occurs (transfer one row of data each time), end the acquisition in the field interrupt and set the acquisition end flag to 1, and judge and process in the main function.
(2) DCMI&DMA double buffer image acquisition process
Use DMA to transfer data from the data register, and use word as the unit for transmission, and use double buffering (each buffer is set to the length of a row of image data, so that only one row of data is transferred at a time to ensure accuracy) for DMA transfer, that is, a buffer After the middle data is transmitted, switch to another buffer for transmission, forming a ping-pong operation. When a buffer is transferred, a DMA interrupt will be generated. In the DMA interrupt, the image data will be copied from this buffer to the target image buffer (it must be judged according to DMA whether another buffer is in use before copying), so that when the frame interrupt arrives, the entire All the images are transferred to the target image buffer. In this way, RGB5656 images can be collected or grayscale images can be directly collected (only the Y component is extracted in line copying). If the data is stored in blocks, the data needs to be copied to the corresponding cache block. DCMI interrupt processing function When a DCMI interrupt is detected, first close the DCMI acquisition and DMA transmission, enter the image display and image processing function, and then open the DCMI and DMA to continue image acquisition after the image recognition is completed.
3. The clock configuration of the main function process
adopts the RCC clock configuration to configure the main frequency of the system to 192MHz. The specific method is that the system clock source input is the HSE external crystal clock 12MHz, and the initial frequency multiplier PLL is configured as 192MHz (the internal calculation method is the basic clock/minute frequency parameter * multiplier parameter, the specific need to see the clock configuration process) and then divided by 2 to the bus clock, and finally divided by 8 to the peripheral clock, when the peripheral is used, it will obtain its own working clock according to the frequency division of the bus clock. Peripheral initialization, serial port, GPIO, LCD, I2C, OV7670, TIME, DCMI, DMA initialization, start image acquisition after initialization is completed. The main loop judges the image acquisition completion flag, and if the acquisition is completed, image processing is performed, including LCD display, image recognition, output information, PID tracking, etc.
3. Introduction to image recognition algorithm
1. Color recognition tracking algorithm
(1) RGB to HSL color space
The color recognition used in this module is in HSL (hue, saturation, brightness), converted according to the RGB to HSL color formula
(2) color threshold
Judging In the HSL color space, each color has a corresponding color threshold range. According to the threshold range of each color, it can be judged whether the threshold of each pixel in the image satisfies the condition. The color threshold can output the HSL value of the center point of each frame of image. Calibrate (or calculate the HSL value of the area range for calibration)
(3) The calculation of the target position
adopts the method of interlacing rows and columns to speed up the calculation of the target position, counting the sum of all pixel coordinates in the image that meet the color threshold, and then dividing by the total number of pixels (note is the pixel that satisfies the color threshold) to get the center point position (X, Y) of the target
(4) The servo PID calculation
is based on the actual center point (X, Y) of the target and the expected target position (centerX, centerY) image In the center, the PID error feedback calculation is performed to obtain the PWM value that the steering gear needs to output. Here, the comparison value that the timer needs to output is directly used to simplify the calculation and speed up the processing.
(5) Control tracking
Process the PWM output value in the timer interrupt to control the movement of the steering gear, and the position of the steering gear can be controlled in real time for target tracking. The color recognition of multiple targets can use the same method, the difference is that different names are added when judging the color threshold Statistical values, and then calculate the center positions of different targets according to the statistical values.
2. Two-dimensional code recognition
Here, ATK's two-dimensional code recognition library is directly used and slightly modified. It only supports English and character two-dimensional codes, and does not support Chinese. No specific principles are introduced. The F4 single board does not expand the memory. It can realize QR code recognition with 240x240 resolution. For the specific method, please refer to the implementation process in the
code . The binarization matrix can divide the whole image into several ROI areas for processing, generally divided into 3 areas from top to bottom, and calculate the line segment position of each area separately ; The number of white dots (white line 0xff) (or black line 0x00) is used to determine the upper and lower boundaries of the line as the upper and lower boundaries of the target




(3) Target column statistics
Count the number of white points (white line 0xff) (or black line 0x00) contained in each column in the image, and determine the left and right boundaries of the column as the left and right boundary lines of the target (4) Output target information using
boundary
diagonal The line calculates the center position and deflection angle of each line segment, and the forward and left and right of the car can be calculated according to the position of the line segment.

3. Human body detection and tracking
(1) The human body detection trigger mechanism
uses an infrared pyroelectric module (GPIO is set as a signal input to detect pyroelectric signal triggers). If a high level is detected, it means that someone is within the range of the device (1s clock read Take it once, because the pyroelectric module will be delayed for a period of time and then shut down, resulting in a low level, so it can only be used as a trigger switch, and cannot be used as a credential that people have been in the equipment area), there is a pyroelectric module (set in the program) A flag bit) to trigger the human motion detection algorithm

(2) Human motion detection
adopts the method of frame-by-frame difference. Each time the frame ID is calculated in the image capture completion flag, the frame ID is reset every two frames, and the image of the previous frame is saved in a cache each time. Frame difference is performed when the frame arrives (if there is environmental interference and noise, a large part has been removed in this difference, because the time between the two frames is short enough, it can be considered that the environment has not changed much), and the image after the difference is processed again. Filter once, then perform corrosion, dilation and corrosion to further remove noise points, and perform binarization segmentation after denoising (the OSTU adaptive binarization algorithm can effectively remove environmental interference and camera factors. Previously, mean value segmentation and fixed Threshold segmentation, and finally found that this effect is better), after the segmentation, the binarization matrix is ​​obtained

(3) Human body area detection and target tracking.
The binarization matrix is ​​used for pixel statistics. If it exceeds the threshold of the target pixel, it is considered that there is human body movement in the area, and movement tracking can be performed (the white point is used as the effective target of the human body during binarization). Due to the background Part of the change will be the target area brought by the background, so the next step is to use every pixel to scan and count the white point coordinates in the pixel area, and finally the center point position of the human target can be calculated, according to the current position of the steering gear and expectations The position (center of the image) is calculated by PID to get the position where the steering gear needs to move, and convert it into a PWM value to control the tracking of the steering gear.

Guess you like

Origin blog.csdn.net/weixin_40672861/article/details/119359268