通过cv2 python脚本发送ROS消息

2024-04-27 05:11:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试实现一个机器人解决方案,用户将通过一个区域的摄像头点击一个点,移动4轮机器人将通过路径到达该位置。 我已经创建了通过使用同形图变换将视频像素坐标转换为地面坐标的部分,并希望实现将地面坐标发送到RViz的部分。 计算地面坐标的部分如下所示:

global h
font = cv2.FONT_HERSHEY_SIMPLEX #Loading neccessary fonts

with open("save.h", "rb") as f:
    h = load(f)


print "Homographic matrix loaded successfully."

def draw_circle2(event,x,y,flags,param):
    global h,mouseX,mouseY, num_of_points, complete,np_coord_on_screen,np_coord_on_ground, myImage, emptyFrame, coord_on_screen, coord_on_ground

    if event == cv2.EVENT_LBUTTONDBLCLK:

        a = np.array([[x, y]], dtype='float32')
        a = np.array([a])
        pointOut = cv2.perspectiveTransform(a, h) #Calculation of new point location

        loc_pointOut = tuple(pointOut)

        pointOut=(loc_pointOut[0][0][0],loc_pointOut[0][0][1]) #Point on ground

        print "Current Location: "+str(pointOut)
        cv2.imshow('Video',emptyFrame) #Showing emptyFrame
        cv2.circle(myImage,(x,y),4,(255,0,0),-1) #Draw a circle on myImage
        cv2.putText(myImage,(str((round(pointOut[0],2)))+","+str(round(pointOut[1],2))), (x-5,y-15),font, 0.4,(0,255,0)) #Draw the text
        cv2.imshow('Video',myImage) #Showing resulting myImage

        myImage = emptyFrame.copy()



# Initial code 
# Showing first frame on screen
raw_input("Press any key...")
clear_all()
cv2.namedWindow('Video') #Naming the window to use.
cv2.setMouseCallback('Video',draw_circle2) #Set mouse callBack function.
ret, frame = cap.read() #Get image from camera
if (ret): #If frame has image, show the image on screen
    global myImage, emptyFrame
    myImage = frame.copy()
    emptyFrame = frame.copy()
    cv2.imshow('Video',myImage) # Show the image on screen



while True:  # making a loop

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    if (cv2.waitKey(1) & 0xFF == ord('c')):
        #Deleting points from image

        cv2.imshow('Video',emptyFrame) #Show the image again, deleting all graphical overlays like text and shapes
        coord_on_screen = []        #Resetting coordinate lists
        coord_on_ground=[]          #Resetting coordinate lists
    if (cv2.waitKey(1) & 0xFF == ord('s')):
        cap.release()
        cv2.destroyAllWindows()
        init()


# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

所以,pointOut就是地面上的点(例如:(2,4)m) 我需要一些关于如何在我的代码中实现创建节点和将pointOut标记消息传输到ROS主题的指导

我的配置是ROS Kinetic Kame,Python 2.7


Tags: theimageifonvideonpcv2screen