日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

树莓派视觉小车 -- 物体跟踪(OpenCV)

發布時間:2023/11/27 生活经验 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 树莓派视觉小车 -- 物体跟踪(OpenCV) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

物體跟蹤效果展示

過程:

一、初始化

二、運動控制函數

三、舵機角度控制

?四、攝像頭&&圖像處理

1、打開攝像頭

2、把圖像轉換為灰度圖

3、 高斯濾波(去噪)

4、亮度增強

5、轉換為二進制

6、閉運算處理

7、獲取輪廓

代碼

五、獲取最大輪廓坐標

六、運動

1、沒有識別到輪廓(靜止)

2、向前走

3、向左轉

4、向右轉

?代碼

總代碼


物體跟蹤效果展示

??

?

?

過程:

一、初始化

def Motor_Init():global L_Motor, R_MotorL_Motor= GPIO.PWM(l_motor,100)R_Motor = GPIO.PWM(r_motor,100)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_servopwm_servo=Adafruit_PCA9685.PCA9685()def Init():GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM)Direction_Init()Servo_Init()Motor_Init()

二、運動控制函數

def Front(speed):L_Motor.ChangeDutyCycle(speed)GPIO.output(left_front,1)   #left_frontGPIO.output(left_back,0)    #left_backR_Motor.ChangeDutyCycle(speed)GPIO.output(right_front,1)  #right_frontGPIO.output(right_back,0)   #right_backdef Back(speed):L_Motor.ChangeDutyCycle(speed)GPIO.output(left_front,0)   #left_frontGPIO.output(left_back,1)    #left_backR_Motor.ChangeDutyCycle(speed)GPIO.output(right_front,0)  #right_frontGPIO.output(right_back,1)   #right_backdef Left(speed):L_Motor.ChangeDutyCycle(speed)GPIO.output(left_front,0)   #left_frontGPIO.output(left_back,1)    #left_backR_Motor.ChangeDutyCycle(speed)GPIO.output(right_front,1)  #right_frontGPIO.output(right_back,0)   #right_backdef Right(speed):L_Motor.ChangeDutyCycle(speed)GPIO.output(left_front,1)   #left_frontGPIO.output(left_back,0)    #left_backR_Motor.ChangeDutyCycle(speed)GPIO.output(right_front,0)  #right_frontGPIO.output(right_back,1)   #right_backdef Stop():L_Motor.ChangeDutyCycle(0)GPIO.output(left_front,0)   #left_frontGPIO.output(left_back,0)    #left_backR_Motor.ChangeDutyCycle(0)GPIO.output(right_front,0)  #right_frontGPIO.output(right_back,0)   #right_back

三、舵機角度控制

def set_servo_angle(channel,angle):angle=4096*((angle*11)+500)/20000pwm_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  

上面的(4):是頂部的舵機(攝像頭上下擺動的那個舵機)

下面的(5):是底部的舵機(攝像頭左右擺動的那個舵機)

?四、攝像頭&&圖像處理

# 1 Image Processimg, contours = Image_Processing()
width, height = 160, 120camera = cv2.VideoCapture(0)camera.set(3,width) camera.set(4,height) 

1、打開攝像頭

打開攝像頭,并設置窗口大小。

設置小窗口的原因:?小窗口實時性比較好

# Capture the framesret, frame = camera.read()

2、把圖像轉換為灰度圖

# to gray
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('gray',gray)

3、 高斯濾波(去噪)

# Gausi blurblur = cv2.GaussianBlur(gray,(5,5),0)

4、亮度增強

#brightenblur = cv2.convertScaleAbs(blur, None, 1.5, 30)

5、轉換為二進制

#to binaryret,binary = cv2.threshold(blur,150,255,cv2.THRESH_BINARY_INV)cv2.imshow('binary',binary)

?

6、閉運算處理

#Closekernel = cv2.getStructuringElement(cv2.MORPH_RECT, (17,17))close = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)cv2.imshow('close',close)

7、獲取輪廓

#get contoursbinary_c,contours,hierarchy = cv2.findContours(close, 1, cv2.CHAIN_APPROX_NONE)cv2.drawContours(image, contours, -1, (255,0,255), 2)cv2.imshow('image', image)

代碼

def Image_Processing():# Capture the framesret, frame = camera.read()# Crop the imageimage = framecv2.imshow('frame',frame)# to graygray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)cv2.imshow('gray',gray)# Gausi blurblur = cv2.GaussianBlur(gray,(5,5),0)#brightenblur = cv2.convertScaleAbs(blur, None, 1.5, 30)#to binaryret,binary = cv2.threshold(blur,150,255,cv2.THRESH_BINARY_INV)cv2.imshow('binary',binary)#Closekernel = cv2.getStructuringElement(cv2.MORPH_RECT, (17,17))close = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)cv2.imshow('close',close)#get contoursbinary_c,contours,hierarchy = cv2.findContours(close, 1, cv2.CHAIN_APPROX_NONE)cv2.drawContours(image, contours, -1, (255,0,255), 2)cv2.imshow('image', image)return frame, contours

五、獲取最大輪廓坐標

由于有可能出現多個物體,我們這里只識別最大的物體(深度學習可以搞分類,還沒學到這,學到了再做),得到它的坐標。

# 2 get coordinatesx, 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,0,255), 2)cv2.imshow('new_frame', image)# get coordM = cv2.moments(contour)x = int(M['m10']/M['m00'])y = int(M['m01']/M['m00'])print(x, y) return x,yexcept:print 'no objects'return 0,0

返回最大輪廓的坐標:?

六、運動

根據反饋回來的坐標,判斷它的位置,進行運動。

# 3 MoveMove(x,y)

1、沒有識別到輪廓(靜止)

    if x==0 and y==0:Stop()

2、向前走

識別到物體,且在正中央(中間1/2區域),讓物體向前走。

#go aheadelif width/4 <x and x<(width-width/4):Front(70)

3、向左轉

物體在左邊1/4區域。

#leftelif x < width/4:Left(50)

4、向右轉

物體在右邊1/4區域。

#Rightelif x > (width-width/4):Right(50)

?代碼

def Move(x,y):global second#stopif x==0 and y==0:Stop()#go aheadelif width/4 <x and x<(width-width/4):Front(70)#leftelif x < width/4:Left(50)#Rightelif x > (width-width/4):Right(50)

總代碼

#Object Tracking
import  RPi.GPIO as GPIO
import time
import Adafruit_PCA9685
import numpy as np
import cv2second = 0width, height = 160, 120
camera = cv2.VideoCapture(0)
camera.set(3,width) 
camera.set(4,height) l_motor = 18
left_front   =  22
left_back   =  27r_motor = 23
right_front   = 25
right_back  =  24def Motor_Init():global L_Motor, R_MotorL_Motor= GPIO.PWM(l_motor,100)R_Motor = GPIO.PWM(r_motor,100)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_servopwm_servo=Adafruit_PCA9685.PCA9685()def Init():GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM)Direction_Init()Servo_Init()Motor_Init()def Front(speed):L_Motor.ChangeDutyCycle(speed)GPIO.output(left_front,1)   #left_frontGPIO.output(left_back,0)    #left_backR_Motor.ChangeDutyCycle(speed)GPIO.output(right_front,1)  #right_frontGPIO.output(right_back,0)   #right_backdef Back(speed):L_Motor.ChangeDutyCycle(speed)GPIO.output(left_front,0)   #left_frontGPIO.output(left_back,1)    #left_backR_Motor.ChangeDutyCycle(speed)GPIO.output(right_front,0)  #right_frontGPIO.output(right_back,1)   #right_backdef Left(speed):L_Motor.ChangeDutyCycle(speed)GPIO.output(left_front,0)   #left_frontGPIO.output(left_back,1)    #left_backR_Motor.ChangeDutyCycle(speed)GPIO.output(right_front,1)  #right_frontGPIO.output(right_back,0)   #right_backdef Right(speed):L_Motor.ChangeDutyCycle(speed)GPIO.output(left_front,1)   #left_frontGPIO.output(left_back,0)    #left_backR_Motor.ChangeDutyCycle(speed)GPIO.output(right_front,0)  #right_frontGPIO.output(right_back,1)   #right_backdef Stop():L_Motor.ChangeDutyCycle(0)GPIO.output(left_front,0)   #left_frontGPIO.output(left_back,0)    #left_backR_Motor.ChangeDutyCycle(0)GPIO.output(right_front,0)  #right_frontGPIO.output(right_back,0)   #right_backdef set_servo_angle(channel,angle):angle=4096*((angle*11)+500)/20000pwm_servo.set_pwm_freq(50)                #frequency==50Hz (servo)pwm_servo.set_pwm(channel,0,int(angle))def Image_Processing():# Capture the framesret, frame = camera.read()# Crop the imageimage = framecv2.imshow('frame',frame)# to graygray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)cv2.imshow('gray',gray)# Gausi blurblur = cv2.GaussianBlur(gray,(5,5),0)#brightenblur = cv2.convertScaleAbs(blur, None, 1.5, 30)#to binaryret,binary = cv2.threshold(blur,150,255,cv2.THRESH_BINARY_INV)cv2.imshow('binary',binary)#Closekernel = cv2.getStructuringElement(cv2.MORPH_RECT, (17,17))close = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)cv2.imshow('close',close)#get contoursbinary_c,contours,hierarchy = cv2.findContours(close, 1, cv2.CHAIN_APPROX_NONE)cv2.drawContours(image, contours, -1, (255,0,255), 2)cv2.imshow('image', image)return frame, contoursdef Get_Coord(img, contours):image = img.copy()try:contour = max(contours, key=cv2.contourArea)cv2.drawContours(image, contour, -1, (255,0,255), 2)cv2.imshow('new_frame', image)# get coordM = cv2.moments(contour)x = int(M['m10']/M['m00'])y = int(M['m01']/M['m00'])print(x, y) return x,yexcept:print 'no objects'return 0,0def Move(x,y):global second#stopif x==0 and y==0:Stop()#go aheadelif width/4 <x and x<(width-width/4):Front(70)#leftelif x < width/4:Left(50)#Rightelif x > (width-width/4):Right(50)if __name__ == '__main__':Init()set_servo_angle(4, 110)     #top servo     lengthwise#0:back    180:front    set_servo_angle(5, 90)     #bottom servo  crosswise#0:left    180:right  while 1:# 1 Image Processimg, contours = Image_Processing()# 2 get coordinatesx, y = Get_Coord(img, contours)# 3 MoveMove(x,y)# must include this codes(otherwise you can't open camera successfully)if cv2.waitKey(1) & 0xFF == ord('q'):Stop()GPIO.cleanup()    break#Front(50)#Back(50)#$Left(50)#Right(50)#time.sleep(1)#Stop()

檢測原理是基于最大輪廓的檢測,沒有用深度學習的分類,所以容易受到干擾,后期學完深度學習會繼續優化。有意見或者想法的朋友歡迎交流。

總結

以上是生活随笔為你收集整理的树莓派视觉小车 -- 物体跟踪(OpenCV)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。