当前位置:   article > 正文

ROS实现无人机目标跟踪/物体跟随/循迹_跟随 ros

跟随 ros

1. 物体跟踪

1.1 实现思路

调用无人机的图像:

cv_image = self.bridge.imgmsg_to_cv2(data, “bgr8”)

之后同OpenCV实现机器人对物体进行移动跟随一样,获取所要跟踪的物体

节点的发布和接收见:ROS学习: Topic通讯

1.2 代码示例

import rospy
import cv2 as cv
from geometry_msgs.msg import Twist
from cv_bridge import CvBridge, CvBridgeError
from sensor_msgs.msg import Image


class image_converter:
    def __init__(self):
        self.cmd_pub = rospy.Publisher("/bebop/cmd_vel", Twist, queue_size=1)			# 发布运动控制信息
        self.bridge = CvBridge()
        self.image_sub = rospy.Subscriber("/bebop/image_raw", Image, self.callback)		# 订阅摄像头信息

    def callback(self, data):
        try:
            cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")			# 获取订阅的摄像头图像
        except CvBridgeError as e:
            print e

        # 对图像进行处理
        kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))		# 定义结构元素
        height, width = cv_image.shape[0:2]
        screen_center = width / 2
        screen_center_h = height / 2
        
        offset = 50
        offset_h = 30
        lower_b = (75, 43, 46)
        upper_b = (110, 255, 255)
        
        hsv_frame = cv.cvtColor(cv_image, cv.COLOR_BGR2HSV)				# 转成HSV颜色空间
        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)			# 闭运算去噪
        cv.imshow("mask", mask3)
        
        # 找出面积最大的区域
        _, 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(cv_image, contours, maxIndex, (255, 255, 0), 2)			

        # 获取外切矩形
        x, y, w, h = cv.boundingRect(contours[maxIndex])
        cv.rectangle(cv_image, (x, y), (x + w, y + h), (255, 0, 0), 2)
        # 获取中心像素点
        center_x = int(x + w / 2)
        center_y = int(y + h / 2)
        cv.circle(cv_image, (center_x, center_y), 5, (0, 0, 255), -1)
		# 显示图像
        cv.imshow("Image", cv_image)

        # 运动控制
        twist = Twist()
        # 左右转向和移动
        if center_x < screen_center - offset:
            twist.linear.x = 0.0
            twist.linear.y = 0.2
            twist.angular.z = 0.2
            print "turn left"
        elif screen_center - offset <= center_x <= screen_center + offset:
            twist.linear.x = 0.0
            twist.linear.y = 0.0
            twist.angular.z = 0
            print "keep"
        elif center_x > screen_center + offset:
            twist.linear.x = 0.0
            twist.linear.y = -0.2
            twist.angular.z = -0.2
            print "turn right"
        else:
            twist.linear.x = 0
            twist.angular.z = 0
            print "stop"
		# 上下移动
        if center_y < screen_center_h - offset_h:
            twist.linear.z = 0.2
            print "up up up"
        elif screen_center_h - offset_h <= center_y <= screen_center_h + offset_h:
            twist.linear.z = 0
            print "keep"
        elif center_y > screen_center_h + offset_h:
            twist.linear.z = -0.2
            print "down down down"
        else:
            twist.linear.z = 0
            print "stop"
        cv.waitKey(3)
		# 发布运动指令
        try:
            self.cmd_pub.publish(twist)
        except CvBridgeError as e:
            print e


if __name__ == '__main__':
    try:
        rospy.init_node("cv_bridge_test")
        rospy.loginfo("Starting cv_bridge_test node")
        image_converter()
        rospy.spin()

    except KeyboardInterrupt:
        print "Shutting down cv_bridge_test node."
        cv.destroyAllWindows()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111

效果图
在这里插入图片描述
在这里插入图片描述

2. 自主寻线

将上节的物体识别改为所寻线,运动控制左右移动/转向,剩下就是调参的事情了

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号