Visualize RVIZ image into png files

asked 2020-06-30 04:38:13 -0500

dandan gravatar image

updated 2022-03-04 07:03:33 -0500

lucasw gravatar image

Hello I have some problem to run the example for connecting between ROS and Opencv I want to save the rviz image into the jpg image, but even when I try to run the example code is not work. When I run this example code below, there is nothing happened. Is there anyone know which part has problem in my code? Thank you.

import roslib
import sys
import rospy
import cv2
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError

import time

class image_converter:

    def __init__(self):
        self.bridge = CvBridge()
        self.image_pub = rospy.Publisher("/media/ferdyan/LocalDiskE/Hasil/dataset/Final2/2/ros",Image, queue_size=1)
        self.image_sub = rospy.Subscriber("/media/ferdyan/LocalDiskE/Hasil/dataset/Final2/2/ros2",Image,self.callback)

    def callback(self,data):
        rospy.sleep(1)
        try:
            cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
        except CvBridgeError as e:
            print(e)

        (rows,cols,channels) = cv_image.shape
        if cols > 60 and rows > 60 :
            cv2.circle(cv_image, (50,50), 10, 255)

        cv2.imshow("Image window", cv_image)
        cv2.waitKey(0)

        try:
            self.image_pub.publish(self.bridge.cv2_to_imgmsg(cv_image, "bgr8"))
        except CvBridgeError as e:
            print(e)

def main(args):
    ic = image_converter()
    rospy.init_node('image_converter', anonymous=True)
    time.sleep(1)

    try:
        rospy.spin()
    except KeyboardInterrupt:
        print("Shutting down")
    cv2.destroyAllWindows()

if __name__ == '__main__':
    main(sys.argv)
edit retag flag offensive close merge delete