1. 程式人生 > 程式設計 >OpenCV實現機器人對物體進行移動跟隨的方法例項

OpenCV實現機器人對物體進行移動跟隨的方法例項

1.物體識別

本案例實現對特殊顏色物體的識別,並實現根據物體位置的改變進行控制跟隨。

import cv2 as cv

# 定義結構元素
kernel = cv.getStructuringElement(cv.MORPH_RECT,(3,3))
# print kernel

capture = cv.VideoCapture(0)		
print capture.isOpened()
ok,frame = capture.read()
lower_b = (65,43,46)
upper_b = (110,255,255)

height,width = frame.shape[0:2]
screen_center = width / 2
offset = 50

while ok:
 # 將影象轉成HSV顏色空間
 hsv_frame = cv.cvtColor(frame,cv.COLOR_BGR2HSV)
 # 基於顏色的物體提取
 mask = cv.inRange(hsv_frame,lower_b,upper_b)
 mask2 = cv.morphologyEx(mask,cv.MORPH_OPEN,kernel)
 mask3 = cv.morphologyEx(mask2,cv.MORPH_CLOSE,kernel)
 
 # 找出面積最大的區域
 _,contours,_ = cv.findContours(mask3,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)

 maxArea = 0
 maxIndex = 0
 for i,c in enumerate(contours):
 area = cv.contourArea(c)
 if area > maxArea:
 maxArea = area
 maxIndex = i
	# 繪製
 cv.drawContours(frame,maxIndex,(255,0),2)
 # 獲取外切矩形
 x,y,w,h = cv.boundingRect(contours[maxIndex])
 cv.rectangle(frame,(x,y),(x+w,y+h),2)
 # 獲取中心畫素點
 center_x = int(x + w/2)
 center_y = int(y + h/2)
 cv.circle(frame,(center_x,center_y),5,(0,255),-1)

 # 簡單的列印反饋資料,之後補充運動控制
 if center_x < screen_center - offset:
 print "turn left"
 elif screen_center - offset <= center_x <= screen_center + offset:
 print "keep"
 elif center_x > screen_center + offset:
 print "turn right"

 cv.imshow("mask4",mask3)
 cv.imshow("frame",frame)
 cv.waitKey(1)
 ok,frame = capture.read()

實際效果圖

OpenCV實現機器人對物體進行移動跟隨的方法例項

2.移動跟隨

結合ROS控制turtlebot3或其他機器人運動,turtlebot3機器人的教程見我另一個博文:ROS控制Turtlebot3

首先啟動turtlebot3,如下程式碼可以放在機器人的樹莓派中,將相機插在USB口即可

程式碼示例:

import rospy
import cv2 as cv
from geometry_msgs.msg import Twist


def shutdown():
 twist = Twist()
 twist.linear.x = 0
 twist.angular.z = 0
 cmd_vel_Publisher.publish(twist)
 print "stop"


if __name__ == '__main__':
 rospy.init_node("follow_node")
 rospy.on_shutdown(shutdown)
 rate = rospy.Rate(100)

 cmd_vel_Publisher = rospy.Publisher("/cmd_vel",Twist,queue_size=1)
 # 定義結構元素
 kernel = cv.getStructuringElement(cv.MORPH_RECT,3))
 # print kernel

 capture = cv.VideoCapture(0)
 print capture.isOpened()
 ok,frame = capture.read()
 lower_b = (65,46)
 upper_b = (110,255)

 height,width = frame.shape[0:2]
 screen_center = width / 2
 offset = 50

 while not rospy.is_shutdown():
 # 將影象轉成HSV顏色空間
 hsv_frame = cv.cvtColor(frame,kernel)

 # 找出面積最大的區域
 _,c in enumerate(contours):
 area = cv.contourArea(c)
 if area > maxArea:
 maxArea = area
 maxIndex = i
 # 繪製
 cv.drawContours(frame,(x + w,y + h),2)
 # 獲取中心畫素點
 center_x = int(x + w / 2)
 center_y = int(y + h / 2)
 cv.circle(frame,-1)

 # 簡單的列印反饋資料,之後補充運動控制
 twist = Twist()
 if center_x < screen_center - offset:
 twist.linear.x = 0.1
 twist.angular.z = 0.5
 print "turn left"
 elif screen_center - offset <= center_x <= screen_center + offset:
 twist.linear.x = 0.3
 twist.angular.z = 0
 print "keep"
 elif center_x > screen_center + offset:
 twist.linear.x = 0.1
 twist.angular.z = -0.5
 print "turn right"
 else:
 twist.linear.x = 0
 twist.angular.z = 0
 print "stop"

 # 將速度發出
 cmd_vel_Publisher.publish(twist)

 # cv.imshow("mask4",mask3)
 # cv.imshow("frame",frame)
 cv.waitKey(1)
 rate.sleep()
 ok,frame = capture.read()

總結

到此這篇關於OpenCV實現機器人對物體進行移動跟隨的文章就介紹到這了,更多相關OpenCV機器人對物體移動跟隨內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!