OpenCV实现机器人对物体进行移动跟随的方法实例
程序员文章站
2022-03-21 13:07:00
1.物体识别本案例实现对特殊颜色物体的识别,并实现根据物体位置的改变进行控制跟随。import cv2 as cv# 定义结构元素kernel = cv.getstructuringelement(c...
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, contours, maxindex, (255, 255, 0), 2) # 获取外切矩形 x, y, w, h = cv.boundingrect(contours[maxindex]) cv.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2) # 获取中心像素点 center_x = int(x + w/2) center_y = int(y + h/2) cv.circle(frame, (center_x, center_y), 5, (0, 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()
实际效果图
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, 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 not rospy.is_shutdown(): # 将图像转成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, contours, maxindex, (255, 255, 0), 2) # 获取外切矩形 x, y, w, h = cv.boundingrect(contours[maxindex]) cv.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) # 获取中心像素点 center_x = int(x + w / 2) center_y = int(y + h / 2) cv.circle(frame, (center_x, center_y), 5, (0, 0, 255), -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机器人对物体移动跟随内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!