Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the visual car effect of OpenCV object tracking raspberry pie

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to achieve the visual car effect of OpenCV object tracking raspberry pie". The content of the article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve the visual car effect of OpenCV object tracking raspberry pie".

Catalogue

I. initialization

2. Motion control function

Third, steering gear angle control

IV. Camera & & Image processing

1. Turn on the camera

2. Convert the image to a grayscale image

3. Gaussian filtering (denoising)

4. Brightness enhancement

5. Convert to binary

6. Closed operation processing

7. Get the outline

Code

Fifth, obtain the maximum contour coordinates

VI. Exercise

1. No outline is recognized (still)

2. Go forward

3. Turn left

4. Turn right

Code

General code

Initialize def Motor_Init (): global L_Motor, R_Motor motorcycle = GPIO.PWM R_Motor = GPIO.PWM L_Motor.start (0) R_Motor.start (0) def Direction_Init (): GPIO.setup (left_back,GPIO.OUT) GPIO.setup (left_front,GPIO.OUT) GPIO.setup (l_motor GPIO.OUT) GPIO.setup (right_front,GPIO.OUT) GPIO.setup (right_back,GPIO.OUT) GPIO.setup (r_motor GPIO.OUT) def Servo_Init (): global pwm_servo pwm_servo=Adafruit_PCA9685.PCA9685 () def Init (): GPIO.setwarnings (False) GPIO.setmode (GPIO.BCM) Direction_Init () Servo_Init () Motor_Init () II. Motion control function def Front (speed): L_Motor.ChangeDutyCycle (speed) GPIO.output (left_front,1) # left_front GPIO.output (left_back 0) # left_back R_Motor.ChangeDutyCycle (speed) GPIO.output (right_front,1) # right_front GPIO.output (right_back,0) # right_back def Back (speed): L_Motor.ChangeDutyCycle (speed) GPIO.output (left_front,0) # left_front GPIO.output (left_back,1) # left_back R_Motor.ChangeDutyCycle (speed) GPIO.output (right_front 0) # right_front GPIO.output (right_back,1) # right_back def Left (speed): L_Motor.ChangeDutyCycle (speed) GPIO.output (left_front,0) # left_front GPIO.output (left_back,1) # left_back R_Motor.ChangeDutyCycle (speed) GPIO.output (right_front,1) # right_front GPIO.output (right_back 0) # right_backdef Right (speed): L_Motor.ChangeDutyCycle (speed) GPIO.output (left_front,1) # left_front GPIO.output (left_back,0) # left_back R_Motor.ChangeDutyCycle (speed) GPIO.output (right_front,0) # right_front GPIO.output (right_back,1) # right_backdef Stop (): L_Motor.ChangeDutyCycle (0) GPIO.output (left_front 0) # left_front GPIO.output (left_back,0) # left_back R_Motor.ChangeDutyCycle (0) GPIO.output (right_front,0) # right_front GPIO.output (right_back,0) # right_ back3, steering gear angle control def set_servo_angle (channel) Angle): angle=4096* ((angle*11) + 500) / 20000 pwm_servo.set_pwm_freq (50) # frequency==50Hz (servo) pwm_servo.set_pwm (channel,0,int (angle)) set_servo_angle (4,110) # top servo lengthwise # 0:back 180:front set_servo_angle (5,90) # bottom servo crosswise # 0:left 180:right

Above (4): the steering gear at the top (the steering gear with the camera swinging up and down)

The following (5): is the steering gear at the bottom (the steering gear with the camera swinging left and right)

4. Camera & & Image processing # 1 Image Process img, contours = Image_Processing () width, height = 160,120 camera = cv2.VideoCapture (0) camera.set (3je width) camera.set (4m height) 1. Turn on the camera.

Turn on the camera and set the window size.

The reason for setting the small window: the small window is better in real-time.

# Capture the frames ret, frame = camera.read ()

2. Convert the image to a grayscale image # to graygray = cv2.cvtColor (frame, cv2.COLOR_BGR2GRAY) cv2.imshow ('gray',gray)

3. Gaussian filtering (denoising) # Gausi blur blur = cv2.GaussianBlur (gray, (5Power5), 0) 4, brightness enhancement # brighten blur = cv2.convertScaleAbs (blur, None, 1.5,30) 5, conversion to binary # to binary ret,binary = cv2.threshold (blur,150,255,cv2.THRESH_BINARY_INV) cv2.imshow ('binary',binary)

6. Closed operation processing # Close kernel = cv2.getStructuringElement (cv2.MORPH_RECT, (172.17)) close = cv2.morphologyEx (binary, cv2.MORPH_CLOSE, kernel) cv2.imshow ('close',close)

7. Get the outline # get contours binary_c,contours,hierarchy = cv2.findContours (close, 1, cv2.CHAIN_APPROX_NONE) cv2.drawContours (image, contours,-1, (255j0255), 2) cv2.imshow ('image', image)

Code def Image_Processing (): # Capture the frames ret, frame = camera.read () # Crop the image image = frame cv2.imshow ('frame',frame) # to gray gray = cv2.cvtColor (frame, cv2.COLOR_BGR2GRAY) cv2.imshow (' gray',gray) # Gausi blur blur = cv2.GaussianBlur (gray, (5) 5), 0) # brighten blur = cv2.convertScaleAbs (blur, None, 1.5,30) # to binary ret Binary = cv2.threshold (blur,150,255,cv2.THRESH_BINARY_INV) cv2.imshow ('binary',binary) # Close kernel = cv2.getStructuringElement (cv2.MORPH_RECT, (17Magne17)) close = cv2.morphologyEx (binary, cv2.MORPH_CLOSE, kernel) cv2.imshow (' close',close) # get contours binary_c,contours,hierarchy = cv2.findContours (close, 1, cv2.CHAIN_APPROX_NONE) cv2.drawContours (image, contours,-1, (255Power0) Cv2.imshow ('image', image) return frame, contours 5. Get the maximum contour coordinates.

Due to the possibility of multiple objects, we only recognize the largest objects here (deep learning can classify them, but we haven't learned this yet, so we'll do it again) and get its coordinates.

# 2 get coordinates x, y = Get_Coord (img, contours) def Get_Coord (img, contours): image = img.copy () try: contour = max (contours, key=cv2.contourArea) cv2.drawContours (image, contour,-1, (255 new_frame' 0255), 2) cv2.imshow ('new_frame' Image) # get coord M = cv2.moments (contour) x = int (M ['m10'] / M [' m00']) y = int (M ['m01'] / M [' m00']) print (XMagi y) return xMagol y except: print'no objects' return 0Cool 0

Returns the coordinates of the largest outline:

VI. Exercise

Judge its position and move according to the coordinates returned by the feedback.

# 3 Move Move (xQuery) 1. No outline is recognized (still) if Xerox / 0 and yearly / 0: Stop () 2. Move forward.

Recognize the object and let it move forward in the middle (in the middle of the 1x2 area).

# go ahead elif width/4

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report