Robotics StackExchange | Archived questions

Extracting output data from launch file node

Hi, I am using ROS hydro 12.04 LTS. Currently I successfully launching the motion tracking node based on openni and kinect. There is the data shown at the terminal whenever there is a motion in front of the kinect camera. However, when I look at rqt_graph , there is no published topic for that data and I am wondering why.

Can anyone suggest on how to extract those data to matlab or other program? I can share the developed package if u want to try it. Thanks for your concern.

I had tried to write a code publishing to a topic on the "blob-parameter" line below but there is an error where the global name for std_msgs not defined and error in code syntax :

!/usr/bin/env python

-- coding: utf-8 --

import rospy import sys import cv2 import cv2.cv as cv from sensormsgs.msg import Image, CameraInfo from cvbridge import CvBridge, CvBridgeError import numpy as np from numpy import * import os.path import time from std_msgs.msg import String

def bgdiff(fnbg,im_in,th,blur):

im_bg = fn_bg

diff = cv2.absdiff(im_in,im_bg)

mask = diff < th

hight = im_bg.shape[0]
width = im_bg.shape[1]

im_mask = np.zeros((hight,width),np.uint8)

im_mask[mask]=255

im_mask = cv2.medianBlur(im_mask,blur)

im_edge = cv2.Canny(im_mask,100,200)


return im_bg, im_in, im_mask, im_edge

def nothing(x): pass

class bgsub2(): def init(self): self.node_name = "bgsub"

    rospy.init_node(self.node_name)

    self.th = rospy.get_param("~move_threshold", "60")        

    rospy.on_shutdown(self.cleanup)

    cv2.namedWindow("Motion Edge")
    cv2.createTrackbar("threshold", "Motion Edge", self.th, 255, nothing)

    self.isStart = True

    self.cv_window_name = self.node_name
    cv.NamedWindow(self.cv_window_name, cv.CV_WINDOW_NORMAL)
    cv.MoveWindow(self.cv_window_name, 25, 75)

    cv.NamedWindow("Depth Image", cv.CV_WINDOW_NORMAL)
    cv.MoveWindow("Depth Image", 25, 350)

    self.bridge = CvBridge()

    self.image_sub = rospy.Subscriber("input_rgb_image", Image, self.image_callback, queue_size=1)

    self.depth_sub = rospy.Subscriber("input_depth_image", Image, self.depth_callback, queue_size=1)        

    rospy.loginfo("Waiting for image topics...")
    rospy.wait_for_message("input_rgb_image", Image)
    rospy.wait_for_message("input_depth_image", Image)      
    rospy.loginfo("Ready.")



def image_callback(self, ros_image):

    try:
        frame = self.bridge.imgmsg_to_cv2(ros_image, "bgr8")
    except CvBridgeError, e:
        print e

    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)        
    self.th = cv2.getTrackbarPos("threshold", "Motion Edge")       
    if self.isStart == True:
        self.frame_prev = frame
        self.isStart = False
        return 

    im_bg,im_in,im_mask,im_edge = bg_diff(self.frame_prev,frame,self.th,blur=7)       
    cv2.imshow("Input",im_in)
    cv2.imshow("Background",im_bg)      
    img_size = im_edge.shape      
    params = cv2.SimpleBlobDetector_Params()      
    params.filterByArea = True
    params.minArea = 10

Blob-parameter detector

    detector = cv2.SimpleBlobDetector(params)        
    keypoints = detector.detect(im_edge)
    print len(keypoints)
    if len(keypoints) != 0:
        ptx=0
        pty=0    
        for key in keypoints:
            ptx+=key.pt[0]
            pty+=key.pt[1]
        pt= ( int(ptx/ len(keypoints)), int(pty/ len(keypoints)))

        print pt
        self.pt = rospy.Publisher('pointer', std_msgs.msg.String, queue_size=1)

    else:
        pass

    im_edge = cv2.drawKeypoints(im_edge, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

    cv2.imshow("Motion Edge",im_edge)

    self.frame_prev = frame

    self.keystroke = cv2.waitKey(5)
    if self.keystroke != -1:
        cc = chr(self.keystroke & 255).lower()
        if cc == 'q':

            rospy.signal_shutdown("User hit q key to quit.")


def depth_callback(self, ros_image):

    try:

        depth_image = self.bridge.imgmsg_to_cv2(ros_image, "passthrough")
    except CvBridgeError, e:
        print e


    depth_array = np.array(depth_image, dtype=np.float32)


    cv2.normalize(depth_array, depth_array, 0, 1, cv2.NORM_MINMAX)


    depth_display_image = self.process_depth_image(depth_array)


    cv2.imshow("Depth Image", depth_display_image)


def process_image(self, frame):

    grey = cv2.cvtColor(frame, cv.CV_BGR2GRAY)


    grey = cv2.blur(grey, (7, 7))


    edges = cv2.Canny(grey, 15.0, 30.0)

    return edges


def process_depth_image(self, frame):

    return frame


def cleanup(self):
    print "Shutting down vision node."
    cv2.destroyAllWindows()   

def main(args):
try: bgsub2() rospy.spin() except KeyboardInterrupt: print "Shutting down vision node." cv.DestroyAllWindows()

if name == 'main': main(sys.argv)

Asked by hilmi on 2016-04-29 04:01:08 UTC

Comments

Answers

You'll need to create a node that subscribes to the topic you're interested in. Once you're node has that data, you can do anything you like with it. If you haven't yet, I'd recommend going through the beginners tutorials.

Asked by Airuno2L on 2016-04-29 09:41:41 UTC

Comments

i had some problems on publishing the topic itself. Once I can publish the topic then it will be no problem for me to create a node that subscribe to a topic. My lack experience in python code make it harder for me to convert output to a topic. Any chance of helping me on the code syntax?

Asked by hilmi on 2016-05-03 02:42:29 UTC

I can share the coding on your email for syntax checking. I had tried on my own for the coding but lots of error occur on the output.

Asked by hilmi on 2016-05-03 02:43:47 UTC