ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

As PeteBlackerThe3rd suggested you're publishing the image in your __init__ method right after the publisher is created. Here is an example class that works for your case: import rospy from sensor_msgs.msg import Image import cv2 from cv_bridge import CvBridge, CvBridgeError import time

class ImagePublisher(object):

  def __init__(self):
    self.node_rate = 1

    self.image_pub = rospy.Publisher("image_topic", Image)
    cv_image = cv2.imread('/Pictures/image.png', 0)
    self.bridge = CvBridge()

    try:
      self.image_message = self.bridge.cv2_to_imgmsg(cv_image, "passthrough")
    except CvBridgeError as e:
      print(e)

    time.sleep(5)
    self.image_pub.publish(self.image_message)

  def doSmth(self):
    rospy.loginfo('its working!')
    # self.image_pub.publish(self.image_message)

  def run(self):
    loop = rospy.Rate(self.node_rate)
    while not rospy.is_shutdown():
      self.doSmth()
      loop.sleep()

If you want to publish the image only once then use the time.sleep() in your __init__ . Otherwise if you want to publish on some rate then uncomment the publish line in doSmth().

As PeteBlackerThe3rd suggested you're publishing the image in your __init__ method right after the publisher is created. Here is an example class that works for your case: case:

import rospy
from sensor_msgs.msg import Image
import cv2
from cv_bridge import CvBridge, CvBridgeError
import time

time


class ImagePublisher(object):

  def __init__(self):
    self.node_rate = 1

    self.image_pub = rospy.Publisher("image_topic", Image)
    cv_image = cv2.imread('/Pictures/image.png', 0)
cv2.imread(
        '/home/pavel/agv_dev/ros/src/playground/read_image/resources/pavel.png', 1)
    self.bridge = CvBridge()

    try:
      self.image_message = self.bridge.cv2_to_imgmsg(cv_image, "passthrough")
    except CvBridgeError as e:
      print(e)

    time.sleep(5)
    self.image_pub.publish(self.image_message)

  def doSmth(self):
    rospy.loginfo('its working!')
    # self.image_pub.publish(self.image_message)

  def run(self):
    loop = rospy.Rate(self.node_rate)
    while not rospy.is_shutdown():
      self.doSmth()
      loop.sleep()

If you want to publish the image only once then use the time.sleep() in your __init__ . Otherwise if you want to publish on some rate then uncomment the publish line in doSmth().

As PeteBlackerThe3rd suggested you're publishing the image in your __init__ method right after the publisher is created. Here is an example class that works for your case:

import rospy
from sensor_msgs.msg import Image
import cv2
from cv_bridge import CvBridge, CvBridgeError
import time


class ImagePublisher(object):

  def __init__(self):
    self.node_rate = 1

    self.image_pub = rospy.Publisher("image_topic", Image)
    cv_image = cv2.imread(
        '/home/pavel/agv_dev/ros/src/playground/read_image/resources/pavel.png', 1)
cv2.imread('/Pictures/image.png',0)
    self.bridge = CvBridge()

    try:
      self.image_message = self.bridge.cv2_to_imgmsg(cv_image, "passthrough")
    except CvBridgeError as e:
      print(e)

    time.sleep(5)
    self.image_pub.publish(self.image_message)

  def doSmth(self):
    rospy.loginfo('its working!')
    # self.image_pub.publish(self.image_message)

  def run(self):
    loop = rospy.Rate(self.node_rate)
    while not rospy.is_shutdown():
      self.doSmth()
      loop.sleep()

If you want to publish the image only once then use the time.sleep() in your __init__ . Otherwise if you want to publish on some rate then uncomment the publish line in doSmth().

As PeteBlackerThe3rd suggested you're publishing the image in your __init__ method right after the publisher is created. Here is an example class that works for your case:

import rospy
from sensor_msgs.msg import Image
import cv2
from cv_bridge import CvBridge, CvBridgeError
import time


class ImagePublisher(object):

  def __init__(self):
    self.node_rate = 1

    self.image_pub = rospy.Publisher("image_topic", Image)
    cv_image = cv2.imread('/Pictures/image.png',0)
    self.bridge = CvBridge()

    try:
      self.image_message = self.bridge.cv2_to_imgmsg(cv_image, "passthrough")
    except CvBridgeError as e:
      print(e)

    time.sleep(5)
    self.image_pub.publish(self.image_message)

  def doSmth(self):
    rospy.loginfo('its working!')
    # self.image_pub.publish(self.image_message)

  def run(self):
    loop = rospy.Rate(self.node_rate)
    while not rospy.is_shutdown():
      self.doSmth()
      loop.sleep()

If you want to publish the image only once then use the time.sleep() in your __init__ . Otherwise if you want to publish on some rate then uncomment the publish line in doSmth().

doSmth().

EDIT: WIth image_view I am encountering conversion errors due to encoding/color formats. In my case it is trying to convert image from '8UC3' to 'bgr8' so I had to change this line:

self.image_message = self.bridge.cv2_to_imgmsg(cv_image, encoding="bgr8")

Now I can see the image using:

rosrun image_view image_view image:=/image_topic

Anyway the initial problem you had was still related to publishing the image in your __init__ method right after the publisher is create.

As PeteBlackerThe3rd suggested you're publishing the image in your __init__ method right after the publisher is created. Here is an example class that works for your case:

import rospy
from sensor_msgs.msg import Image
import cv2
from cv_bridge import CvBridge, CvBridgeError
import time


class ImagePublisher(object):

  def __init__(self):
    self.node_rate = 1

    self.image_pub = rospy.Publisher("image_topic", Image)
    cv_image = cv2.imread('/Pictures/image.png',0)
    self.bridge = CvBridge()

    try:
      self.image_message = self.bridge.cv2_to_imgmsg(cv_image, "passthrough")
    except CvBridgeError as e:
      print(e)

    time.sleep(5)
    self.image_pub.publish(self.image_message)

  def doSmth(self):
    rospy.loginfo('its working!')
    # self.image_pub.publish(self.image_message)

  def run(self):
    loop = rospy.Rate(self.node_rate)
    while not rospy.is_shutdown():
      self.doSmth()
      loop.sleep()

If you want to publish the image only once then use the time.sleep() in your __init__ . Otherwise if you want to publish on some rate then uncomment the publish line in doSmth().

EDIT: WIth image_view I am encountering conversion errors due to encoding/color formats. In my case it is trying to convert image from '8UC3' to 'bgr8' so I had to change this line:

self.image_message = self.bridge.cv2_to_imgmsg(cv_image, encoding="bgr8")

Now I can see the image using:

rosrun image_view image_view image:=/image_topic

Anyway the initial problem you had was still related to publishing the image in your __init__ method right after the publisher is create.

create.
Also note that if you are publishing it only once as in your example, make sure to run image_view before you run your node.