0

The instructions for the question are below

Question report 2.7. You are now ready to make a ROS node to perform real-time extraction of the centroid of the line from the images acquired by the camera of your robot. File name: image_segment_node.py

Subscribes to topics:

raspicam_node/image (type sensor_msgs/Image )

Publishes to topics:

• /image/segmented (type sensor_msgs/Image )

• /image/centroid (type geometry_msgs/PointStamped )

Description: This node should import the file image_processing.py to use all the functions that you have developed so far. For each new image received from the camera, the node should perform the same operations described in Question provided 2.3, but without visualization of the results. Instead, the node should:

  1. Publish the segmented image with the line on the topic /image/segmented .
  2. Insert the computed horizontal centroid in the x field of a PointStamped message, add the current time to the header.stamp field, and then publish the message on the topic /image/centroid

The code I have for image_segment_node.py is below

#!/usr/bin/env python
"""This is a node to perform real-time extraction of the centroid of the line from images acquired by camera of robot and export data."""

import rospy
import numpy as np
import image_processing as ip
from cv_bridge import CvBridge
from sensor_msgs.msg import Image
from geometry_msgs.msg import PointStamped
import cv2

def callback(msg):
    global pub1
    global pub2
    rospy.loginfo("started")
    img = CvBridge.imgmsg_to_cv2(msg, 'bgr8')
    img = cv2.imread(img)
    lb, ub = ip.classifier_parameters()
    img_seg = cv2.inRange(img, lb, ub)
    x = ip.image_centroid_horizontal(img_seg)
    img_seg = ip.image_one_to_three_channels(img_seg)
    img_seg = ip.image_line_vertical(img_seg, x)
    pub1.publish(img_seg)
    point1 = PointStamped()
    point1.point = x
    point1.header = rospy.Time.now()
    pub2.publish(point1)


def main():
    global pub1
    global pub2
    rospy.init_node('image_segment_node')
    rospy.Subscriber("/raspicam_node/image", Image, callback=callback, queue_size=1)
    pub1 = rospy.Publisher("/image/segmented", Image, queue_size=10)
    pub2 = rospy.Publisher("/image/centroid", PointStamped, queue_size=10)
    rate = rospy.Rate(10)
   # rospy.spin()

if __name__ == '__main__':
    try:
        main()
    finally:
        pass

Terminal tells me this

drand21@roslab:~/ros_ws/src/me416_lab/nodes$ rostopic echo /image/centroid WARNING: topic [/image/centroid] does not appear to be published yet

drand
  • 1

1 Answers1

0

Can you try the code below, I changed only the structure. There doesn't seem any problem with it, but warning is clearly about "pub2" not publishing.

#!/usr/bin/env python
"""This is a node to perform real-time extraction of the centroid of the line from images acquired by camera of robot and export data."""

import rospy
import numpy as np
import image_processing as ip
from cv_bridge import CvBridge
from sensor_msgs.msg import Image
from geometry_msgs.msg import PointStamped
import cv2

class image_segment():
# Class function for creating map relations
def __init__(self):
    rospy.init_node('image_segment_node')
    self.rate = rospy.Rate(10)
    
    # Publishers
    self.pub1 = rospy.Publisher("/image/segmented", Image, queue_size=10)
    self.pub2 = rospy.Publisher("/image/centroid", PointStamped, queue_size=10)
    # Subscribers
    rospy.Subscriber("/raspicam_node/image", Image, callback=self.callback_im, queue_size=1)
    
def callback_im(self, msg):
    rospy.loginfo("started")
    img = CvBridge.imgmsg_to_cv2(msg, 'bgr8')
    img = cv2.imread(img)
    lb, ub = ip.classifier_parameters()
    img_seg = cv2.inRange(img, lb, ub)
    x = ip.image_centroid_horizontal(img_seg)
    img_seg = ip.image_one_to_three_channels(img_seg)
    img_seg = ip.image_line_vertical(img_seg, x)
    self.pub1.publish(img_seg)
    point1 = PointStamped()
    point1.point = x
    point1.header = rospy.Time.now()
    self.pub2.publish(point1)

def main(self):
    while not rospy.is_shutdown():
        ###
        self.rate.sleep()

if __name__ == '__main__':
    try:
        im_class = image_segment()
        im_class.main()
    finally:
        pass