yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
MicroROS Self-balancing Robot Car📖
01.Introduction
  • 0.Unboxing and Reviewing
  • 1. Must read before use - learning route
  • 2. Introduction to LiDAR
  • 3. Balance car interface description
  • 4. Analysis of common problems
  • 5. Robot charging and battery precautions
02.Assembly tutorial
  • Robot assembly steps
03.Preparation
  • 0.Quick Start (Must Read)
  • 1.Write firmware
  • 2.How to install and use VM
  • 3.ESP32 communication board configuration parameters
  • 4.Connect to microROS agent
04.ROS-WiFi module configuration
  • 0.Serial port configuration WiFi instructions
  • 1.Use ROS-WiFi cam module(must read)
  • 2.Connect to ROS-WiFi cam module agent
  • 3.Camera image correction (optional)
05.Quick start control
  • 1.Keyboard remote control
  • 2.Handle remote control
06.ROS+OpenCV course
  • 1.OpenCV Getting started
  • 2.Geometric transformation
  • 3.Image processing and drawing text line segments
  • 4.Image beautification
07.Mediapipe course
  • 1. Hand detection
  • 2. Posture detection
  • 3. Overall inspection
  • 4. Face detection
  • 5. Face recognition
  • 6. Facial effects
  • 7. 3D object recognition
  • 8. Brush
  • 9. Finger control
  • 10. Gesture recognition
08.Robot basic course
  • 1.Robot information release
  • 2.Robot keyboard control
  • 3.Robot handle control
  • 4.Robot state estimation
  • 5.Line speed calibration
  • 6.Angular velocity calibration
  • 7.Robot URDF model
09.Lidar course
  • 1.Lidar avoid
  • 2.Lidar following
  • 3.Lidar guard
  • 5.Gmapping mapping
  • 6.Cartographer mapping
  • 7.Navigation2 navigation avoid
  • 8.Navigation2 fast relocation navigation
  • 9.ROS Robot APP mapping
  • 10.ROS Robot APP navigation
10.Robot visual interaction
  • 1.Color tracking
  • 2.Object recognition and tracking
  • 3.Face recognition tracking
  • 4.Autonomous driving line patrol
  • 5.QR code motion control
  • 6.Gesture control car movement
  • 7.Palm control car movement
  • 8.Gesture control car action group
  • 9.Palm recognition and tracking
  • 10.Human body posture recognition and tracking
11.Multi-machine course
  • 1.Multi-machine handle control
  • 2.Multi-machine keyboard control
  • 3.Multi-machine navigation
  • 4.Multi-machine rapid relocation navigation
12.STM32 balancing case
01.Environment construction and development
  • 13.Window Watchdog (WWDG)
  • 01.MDK-ARM installation
  • 02.STM32CubeIDE installation
  • 03.Common driver installation
  • 04.Download the program
  • 05.MDK-ARM project usage
  • 06.Program simulation
02.STM32 getting started course
  • 01.Project creation
  • 02.LED Control(GPIO)
  • 03.Buzzer control(GPIO)
  • 04.Button control(EXTI)
  • 05.Serial communication(USART)
  • 06.Voltage detection(ADC)
  • 07.Basic timer(TIM)
  • 08.Universal timer(TIM)
  • 09.Advanced Timer(TIM)
  • 10.I2C Communication(I2C)-OLED
  • 11.SPI communication(SPI)-2.4G handle
  • 12.Independent Watchdog (IWDG)
  • 14.DMA-USART
  • 15.DMA-I2C
03.STM32 expansion course
  • 01.Battery voltage detection (ADC)
  • 02.Ultrasonic module-measuring distance (TIM)
  • 03.Motor drive+encoder (TIM)
  • 04.OLED Data-Display(I2C)
  • 05.MPU6050-Data Read (I2C)
04.Robot car PID control course
  • 01.PID basic concept
  • 02.PID example analysis
  • 03.P, PI, PD controller theory
  • 04.Position PID
  • 05.Incremental PID
  • 06.Cascade PID
  • 07.Balance principle of car
  • 08.Car upright control (PD)
  • 09.Car speed ​​control (PI)
  • 10.Car steering control (PD)
  • 11.Got angle and angular velocity (DMP algorithm)
  • 12.Got angle and angular velocity (Kalman filter algorithm)
  • 13.Got angle and angular velocity(Complementary filter algorithm)
05.Robot car basic course
  • 01.Car parameter adjustment
  • 02.Ultrasonic obstacle avoidance
  • 03.Ultrasonic follow
  • 04.Bluetooth remote control
  • 05.Load balance
  • 06.Radar obstacle avoidance
  • 07.Radar guard
  • 08.Radar follow
  • 09.Radar patrol
  • 10.Radar wall following go in a straight line
  • 11.Radar tracking wall multiple walls
06.Robot car LQR control
  • Balance car LQR high-level control
07.Circuit design
  • 01.Overall circuit design
  • 02.STM32 minimum system
  • 03.Serial port circuit design
  • 04.Power circuit design
  • 05.MPU6050 Circuit Design
  • 06.Motor drive circuit design
08.Mathematical model
  • Balance car mathematical model
13.Linux basic course
  • 1.Introduction to Linux system
  • 2.Ubuntu file system
  • 3.Ubuntu common commands
  • 4.Ubuntu common editors
  • 5.Ubuntu software operation commands
  • 6.Virtual machine installation
  • 7.SSH remote control
  • 8.VNC remote control
  • 9.Transfer files remotely
  • 10.Driver library and communication
  • 11.Static IP and hotspot mode
  • 12.Bind device ID
  • 13.Capacity expansion and resource allocation
  • 14.Update system software sources
  • 15.Set root user password
  • 16.sudo free password
  • 17.Connect to WiFi network
  • 18.View system version
  • 19.Customized service management
  • 20.Back up system image
14.Docker course
  • 1. Overview and installation
  • 2. Common commands
  • 3. Docker images deeply understand and publish images
  • 4. Docker hardware interaction and data processing
  • 5. Enter the bot's docker container
15.ROS2 basic course
  • 1.Introduction to ROS2
  • 2.ROS2 install Humble
  • 3.ROS2 development environment
  • 4.ROS2 workspace
  • 5.ROS2 function package
  • 6.ROS2 node
  • 7.ROS2 topic communication
  • 8.ROS2 service communication
  • 9.ROS2 action communication
  • 10.ROS2 custom interface message
  • 11.ROS2 parameter service case
  • 12.ROS2 meta-function package
  • 13.ROS2 distributed communication
  • 14.ROS2 DDS
  • 15.ROS2 time related API
  • 16.ROS2 common command tools
  • 17.ROS2 rviz2 use
  • 18.ROS2 rqt toolbox
  • 19.ROS2 Launch startup file configuration
  • 20.ROS2 recording and playback tool
  • 21.ROS2 URDF model
  • 22.ROS2 Gazebo simulation platform
  • 23.ROS2 TF2 coordinate transformation
Download
  • RemoteCtrl_APP(Android)
  • Mapping_APP(iOS_Android)
  • Code
  • Hardware Information
  • software tools
  • 3D model file
  • Instruction manual
  • VM image file
  • ROS2_Basic_VM
Download ZIP

Welcome to MicroROS Self-balancing Robot Car repository

3.Image processing and drawing text line segments

3.Image processing and drawing text line segments

1、OpenCV image grayscale processing

1.1、Grayscale image

The process of converting color images into grayscale images is the grayscale processing of the images. The color of each pixel in a color image is determined by three components: R, G, and B, and each component can take a value of 0-255. This allows a pixel to have a color range of over 16 million (256 * 256 * 256=1677256). A grayscale image is a special type of color image with the same components of R, G, and B, and one pixel has a range of 256 variations. Therefore, in digital image processing, various formats of images are generally converted into grayscale images to reduce the computational complexity of subsequent images. The description of grayscale images, like color images, still reflects the distribution and characteristics of the overall and local chromaticity and brightness levels of the entire image.

1.2、Image grayscale processing

Grayscale processing is the process of converting a color image into a grayscale image. Color images are divided into three components: R, G, and B, each displaying various colors such as red, green, and blue. Grayscale is the process of making the R, G, and B components of colors equal. Pixels with higher grayscale values are brighter (with a maximum pixel value of 255, indicating white), while those with lower grayscale values are darker (with a minimum pixel value of 0, indicating black).

The core idea of image grayscale is that R=G=B, which is also known as grayscale value.

  1. Maximum method: Make the converted values of R, G, and B equal to the largest of the three values before conversion, that is, R=G=B=max (R, G, B). The grayscale image converted by this method has high brightness.
  2. Average method: The values of R, G, and B after conversion are the average values of R, G, and B before conversion. That is: R=G=B=(R+G+B)/3. This method produces a softer grayscale image.

In OpenCV, use cv2. cvtColor (img1, cv2. COLOR_BGR2GRAY) to achieve grayscale processing of images

1.3、Code and actual effect display

cd ~/yahboomcar_ws/src/yahboom_esp32ai_car/scripts/opencv/
python3 3_1.py
xxxxxxxxxx
import cv2
import numpy as np
if __name__ == '__main__':
    img = cv2.imread('yahboom.jpg')
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    while True :
        cv2.imshow("frame",img)
        cv2.imshow('gray', gray)
        action = cv2.waitKey(10) & 0xFF
        if action == ord('q') or action == 113:
            break
    img.release()
    cv2.destroyAllWindows()

After running the program, the effect image,

image-20240123161926746

2、OpenCV image binarization processing

2.1、The core idea of binarization

Set a threshold, with values greater than 0 (black) or 255 (white), to make the image a black and white image. The threshold can be fixed or adaptive. The adaptive threshold is generally a comparison between the average value of a pixel in a region and the pixel in the middle order, or the weighted sum of Gaussian distributions. A difference can be set or not set.

2.2、The threshold function is provided in Python OpenCV:

cv2.threshold(src, threshold, maxValue, thresholdType)

Parameter meanings:

src:Original image

threshold:Current threshold

maxVal:Maximum threshold, usually 255

thresholdType:Threshold types generally have the following values

enum ThresholdTypes { THRESH_BINARY = 0, #The grayscale value of pixels larger than the threshold is set to maxValue (such as a maximum of 255 for 8-bit grayscale values), and the grayscale value of pixels smaller than the threshold is set to 0. THRESH-BINARY-INV=1, # Pixels greater than the threshold have their grayscale value set to 0, while pixels less than the threshold have their grayscale value set to maxValue. THRESH-TRUNC=2, # Pixels above the threshold have a grayscale value set to 0, while pixels below the threshold have a grayscale value set to maxValue. THRESH-TOZERO=3, # Pixels with grayscale values below this threshold do not undergo any changes, while those with grayscale values above this threshold all become 0. THRESH-TOZERO-INV=4 # Pixels with grayscale values greater than this threshold will not be changed, while pixels with grayscale values less than this threshold will all have grayscale values set to 0. }

return:

retval:Consistent with the threshold parameter

dst: result image

notes:Before binarizing, we need to grayscale the color image to obtain a grayscale image.

2.3、Code and actual effect display

xxxxxxxxxx
cd ~/yahboomcar_ws/src/yahboom_esp32ai_car/scripts/opencv/
python3 3_2.py
xxxxxxxxxx
import cv2
import numpy as np
if __name__ == '__main__':
    img = cv2.imread('yahboom.jpg')
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    ret,thresh1=cv2.threshold(gray,180,255,cv2.THRESH_BINARY_INV) 
    while True :
        cv2.imshow("frame",img)
        cv2.imshow('gray', gray)
        cv2.imshow("binary",thresh1)
        action = cv2.waitKey(10) & 0xFF
        if action == ord('q') or action == 113:
            break
    img.release()
    cv2.destroyAllWindows()

After running the program, the effect image,

image-20240123162405598

3、OpenCV Image Edge Detection

3.1、The purpose of image edge detection

Significantly reduce the data size of the image while retaining its original image attributes. At present, there are multiple algorithms available for edge detection. Although the Canny algorithm is ancient, it can be said to be a standard algorithm for edge detection and is still widely used in research.

3.2、Canny edge detection algorithm

Among the currently commonly used edge detection methods, the Canny edge detection algorithm is one of the methods with strict definitions that can provide good and reliable detection. Due to its advantages of meeting the three standards of edge detection and simple implementation process, it has become one of the most popular algorithms for edge detection.

The Canny edge detection algorithm can be divided into the following 5 steps:

(1). Using Gaussian filters to smooth images and filter out noise

(2). Calculate the gradient intensity and direction of each pixel in the image

(3). Apply Non Maximum Suppression to eliminate spurious responses caused by edge detection

(4). Applying Double Threshold Detection to Determine True and Potential Edges

(5). Edge detection is ultimately achieved by suppressing isolated weak edges

3.3、How do we implement it in OpenCV? It's very simple, divided into three steps

(1). Grayscale image:gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

(2). Gaussian filtering (denoising) image:GaussianBlur(src,ksize,sigmaX [,dst [,sigmaY [,borderType]]])-> dst

Parameter meanings:

src:The input image is usually a grayscale image

ksize:Gaussian kernel size

sigmaX :Gaussian kernel standard deviation in the X direction

sigmaY :Gaussian kernel standard deviation in the Y direction

dst:Processed image

(3). Canny method for processing images:edges=cv2.Canny( image, threshold1, threshold2[, apertureSize[, L2gradient]])

Parameter meanings:

edges:Calculated edge image

image :Calculated edge image,Generally, the image obtained after Gaussian processing

threshold1 :The first threshold during the processing

threshold2 :Second threshold during processing

apertureSize :Sobel The aperture size of the operator

L2gradient :The default value for identifying the gradient magnitude of an image is False. If true, use a more accurate L2 norm for calculation (i.e. the sum of squared derivatives in both directions and then the root), otherwise use the L1 norm (directly adding the absolute values of derivatives in both directions).

3.4、Code and actual effect display

xxxxxxxxxx
cd ~/yahboomcar_ws/src/yahboom_esp32ai_car/scripts/opencv/
python3 3_3.py
xxxxxxxxxx
import cv2
import numpy as np
if __name__ == '__main__':
    img = cv2.imread('yahboom.jpg')
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    imgG = cv2.GaussianBlur(gray,(3,3),0)
    dst = cv2.Canny(imgG,50,50)
    while True :
        cv2.imshow("frame",img)
        cv2.imshow('gray', gray)
        cv2.imshow("canny",dst)
        action = cv2.waitKey(10) & 0xFF
        if action == ord('q') or action == 113:
            break
    img.release()
    cv2.destroyAllWindows()

After running the program, the effect image,

image-20240123162631861

4、OpenCV line segment drawing

4.1、When using OpenCV to process images, we sometimes need to draw line segments, rectangles, etc. on the image. Used in OpenCV

cv2.line(dst,pt1,pt2,color,thickness=None,lineType=None,shift=None)Draw line segments using functions.

Parameter meanings:

dst:Output Image。

pt1,pt2:Required parameter. The coordinate points of a line segment represent the starting and ending points, respectively

color:Required parameter. Used to set the color of line segments

thickness:Optional parameters. Used to set the width of line segments

lineType:Optional parameters. Used to set the type of line segment, optional 8 (8 adjacency connectors - default), 4 (4 adjacency connectors), and cv2. LINE.AA for anti aliasing

4.2、Code and actual effect display

xxxxxxxxxx
cd ~/yahboomcar_ws/src/yahboom_esp32ai_car/scripts/opencv/
python3 3_4.py
xxxxxxxxxx
import cv2
import numpy as np
if __name__ == '__main__':
    img = cv2.imread('yahboom.jpg')
    line = cv2.line(img, (50,20), (20,100), (255,0,255), 10)
    while True :
        cv2.imshow("line",line)
        action = cv2.waitKey(10) & 0xFF
        if action == ord('q') or action == 113:
            break
    img.release()
    cv2.destroyAllWindows()

After running the program, the effect image,

image-20240123162858472

5、OpenCV drawing rectangles

5.1、In OpenCV, drawing rectangles is done using

cv2.rectangle(img,pt1,pt2,color,thickness=None,lineType=None,shift=None)

Parameter meanings:

img:Canvas or carrier images

pt1,pt2:Required parameter. The vertices of a rectangle represent the vertices and diagonal vertices respectively, namely the upper left corner and lower right corner of the rectangle (these two vertices can determine a unique rectangle), which can be understood as diagonals.

color:Required parameter. Used to set the color of the rectangle

thickness:Optional parameters. Used to set the width of the rectangular edge. When the value is negative, it indicates filling the rectangle

lineType:Optional parameters. Used to set the type of line segment, optional 8 (8 adjacency connectors - default), 4 (4 adjacency connectors), and cv2. LINE.AA for anti aliasing

5.2、Code and Effect Display

xxxxxxxxxx
cd ~/yahboomcar_ws/src/yahboom_esp32ai_car/scripts/opencv/
python3 3_5.py
xxxxxxxxxx
import cv2
import numpy as np
if __name__ == '__main__':
    img = cv2.imread('yahboom.jpg')
    rect = cv2.rectangle(img, (50,20), (100,100), (255,0,255), 10)
    while True :
        cv2.imshow("line",rect)
        action = cv2.waitKey(10) & 0xFF
        if action == ord('q') or action == 113:
            break
    img.release()
    cv2.destroyAllWindows()

image-20240123163107652

6、OpenCV drawing circles

6.1、In OpenCV, drawing a circle is done using

cv2.circle(img, center, radius, color[,thickness[,lineType]])

Parameter meanings:

img:Painting or carrier image cloth

center:For center coordinates, format: (50,50)

radius:radius

thickness:The thickness of the lines. Default is 1. If -1, it is filled with solid

lineType:Line type. The default is 8, connection type. The following table explains

parameterexplain
cv2.FILLEDfilling
cv2.LINE_44 Connection types
cv2.LINE_88 Connection types
cv2.LINE_AAAnti aliasing, this parameter will make the lines smoother

6.2、Code and actual effect display

xxxxxxxxxx
cd ~/yahboomcar_ws/src/yahboom_esp32ai_car/scripts/opencv/
python3 3_6.py
xxxxxxxxxx
import cv2
import numpy as np
if __name__ == '__main__':
    img = cv2.imread('yahboom.jpg')
    circle = cv2.circle(img, (80,80), 50, (255,0,255), 10)
    while True :
        cv2.imshow("circle",circle)
        action = cv2.waitKey(10) & 0xFF
        if action == ord('q') or action == 113:
            break
    img.release()
    cv2.destroyAllWindows()

image-20240123163223746

7、OpenCV drawing ellipses

7.1、In OpenCV, drawing ellipses is done using

cv2.ellipse(img, center, axes, angle, StartAngle, endAngle, color[,thickness[,lineType]

Parameter meanings:

center:The center point of an ellipse,(x,y)

axes:Refers to short radius and long radius,(x,y)

StartAngle:The angle of the starting angle of the arc

endAngle:The angle of the ending angle of the arc

img、color、thickness、lineType You can refer to the explanation of the circle

7.2、Code and actual effect display

xxxxxxxxxx
cd ~/yahboomcar_ws/src/yahboom_esp32ai_car/scripts/opencv/
python3 3_7.py
xxxxxxxxxx
import cv2
import numpy as np
if __name__ == '__main__':
    img = cv2.imread('yahboom.jpg')
    ellipse = cv2.ellipse(img, (80,80), (20,50),0,0, 360,(255,0,255), 5)
    while True :
        cv2.imshow("ellipse",ellipse)
        action = cv2.waitKey(10) & 0xFF
        if action == ord('q') or action == 113:
            break
    img.release()
    cv2.destroyAllWindows()

image-20240123163410508

8、OpenCV drawing polygons

8.1、In OpenCV, polygons are drawn using

cv2.polylines(img,[pts],isClosed, color[,thickness[,lineType]])

Parameter meanings:

pts:The vertices of a polygon

isClosed:Is it closed。(True/False)

Other parameters refer to the drawing parameters of the circle

8.2、Code and actual effect display

xxxxxxxxxx
cd ~/yahboomcar_ws/src/yahboom_esp32ai_car/scripts/opencv/
python3 3_8.py
xxxxxxxxxx
import cv2
import numpy as np
if __name__ == '__main__':
    img = cv2.imread('yahboom.jpg')
    points = np.array([[120,50], [40,140], [120,70], [110,110], [50,50]], np.int32)
    polylines = cv2.polylines(img, [points],True,(255,0,255), 5)
    while True :
        cv2.imshow("polylines",polylines)
        action = cv2.waitKey(10) & 0xFF
        if action == ord('q') or action == 113:
            break
    img.release()
    cv2.destroyAllWindows()

image-20240123163558331

9、OpenCV drawing text

9.1、In OpenCV, drawing text is done using

cv2.putText(img, str, origin, font, size,color,thickness)

Parameter meanings:

img:input image

str:Text drawn

origin:The upper left corner coordinate (integer) can be understood as where the text starts

font:font

size:font size

color:font color

thickness:font thickness

Among them, the font is optional

image-20220215141713129

9.2、Code and actual effect display

xxxxxxxxxx
cd ~/yahboomcar_ws/src/yahboom_esp32ai_car/scripts/opencv/
python3 3_9.py
xxxxxxxxxx
import cv2
import numpy as np
if __name__ == '__main__':
    img = cv2.imread('yahboom.jpg')
    cv2.putText(img,'This is Yahboom!',(50,50),cv2.FONT_HERSHEY_SIMPLEX,1,(0,200,0),2)
    while True :
        cv2.imshow("img",img)
        action = cv2.waitKey(10) & 0xFF
        if action == ord('q') or action == 113:
            break
    img.release()
    cv2.destroyAllWindows()

image-20240123165331409

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Geometric transformation
Image beautification

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文