tuw-robotics / tuw_marker_detection

28 stars 28 forks source link

ROS-Image-Camera Info synchronization problem #9

Open cagrihakkoymaz opened 3 years ago

cagrihakkoymaz commented 3 years ago

I try to create one node for divide one picture to two ROI's and publish for two different TUW checkerboard node. Everything fine but when i subscribe image and camera info same time with TUW checkerboard node, I get an error like below.Even there is a same number of message ,they are not sync. I will be appreciate any suggestion.

My Code:

!/usr/bin/env python

"""OpenCV feature detectors with ros CompressedImage Topics in python.

This example subscribes to a ros topic containing sensor_msgs CompressedImage. It converts the CompressedImage into a numpy.ndarray, then detects and marks features in that image. It finally displays and publishes the new image - again as CompressedImage topic. """ author = 'Simon Haller ' version= '0.1' license = 'BSD'

Python libs

import sys, time import message_filters

numpy and scipy

import numpy as np from scipy.ndimage import filters

OpenCV

import cv2

Ros libraries

import roslib import rospy

Ros Messages

from sensor_msgs.msg import Image,CameraInfo

We do not use cv_bridge it does not support CompressedImage in python

from cv_bridge import CvBridge, CvBridgeError

VERBOSE=False bridge = CvBridge() def cropped(image):

#1st ROI

y1_1_ratio = 0.1
x1_1_ratio = 0.1
y2_1_ratio= 0.6
x2_1_ratio = 0.6

#2nd ROI
y1_2_ratio = 0.5
x1_2_ratio = 0.5
y2_2_ratio = 0.9
x2_2_ratio = 0.8

#open the converted image
height, width,channel= image.shape

print(type(x),type(h),type(w),type(y))

y1_1 = int(height*y1_1_ratio)
x1_1 = int(width*x1_1_ratio)
y2_1 = int(height*(y2_1_ratio))
x2_1 = int(width*(x2_1_ratio))

y1_2 = int(height*y1_2_ratio)
x1_2 = int(width*x1_2_ratio)
y2_2 = int(height*(y2_2_ratio))
x2_2 = int(width*(x2_2_ratio))

print(y1_1,x1_1,y2_1,x2_1)    
print(y1_2,x1_2,y2_2,x2_2)    

crop_image = np.zeros_like(image)
crop_image2 = np.zeros_like(image)

#perform image cropping
crop_image[x1_1:x2_1, y1_1:y2_1] = image[x1_1:x2_1, y1_1:y2_1]
crop_image2[x1_2:x2_2, y1_2:y2_2] =image[x1_2:x2_2, y1_2:y2_2]

return crop_image,crop_image2

class image_feature:

def __init__(self):

    '''Initialize ros publisher, ros subscriber'''
    # topic where we publish
    self.image_pub = rospy.Publisher("/output/image_raw/ROI_1",
        Image,queue_size = 2)
    self.image_pub2 = rospy.Publisher("/output/image_raw/ROI_2",
        Image,queue_size = 2)

    self.info_pub= rospy.Publisher("/output/image_raw/camera_info", CameraInfo, queue_size=1)

    # self.bridge = CvBridge()

    # subscribed Topic
    self.image_sub = rospy.Subscriber("/pylon_camera_node/image_raw", Image, self.callback,  queue_size = 1)

    self.info_sub = rospy.Subscriber("/pylon_camera_node/camera_info", CameraInfo, self.callback2, queue_size = 1)

    #self.image_sub = message_filters.Subscriber("/pylon_camera_node/image_raw", Image,  queue_size = 1)
    # self.info_sub = message_filters.Subscriber("/pylon_camera_node/camera_info", CameraInfo, queue_size = 1)
    # ts = message_filters.TimeSynchronizer([self.image_sub, self.info_sub], 10)
    # ts.registerCallback(self.callback)

    if VERBOSE :
        print("subscribed to /camera/image/compressed")

def callback2(self, camera_info):
    msg = camera_info
    print("denem")
    print(msg)

    self.info_pub.publish(msg)

def callback(self, ros_data):
    '''Callback function of subscribed topic. 
    Here images get converted and features detected'''
    if VERBOSE :
        print('received image of type: "%s"' %ros_data.encoding)

    #### direct conversion to CV2 ####
    #np_arr = np.fromstring(ros_data.data, np.uint8)
    #image_np = cv2.imdecode(np_arr, cv2.CV_LOAD_IMAGE_COLOR)
    #image_np = cv2.imdecode(np_arr, cv2.IMREAD_COLOR) # OpenCV >= 3.0:
    cv_image = bridge.imgmsg_to_cv2(ros_data, desired_encoding='rgb8')

    #### Feature detectors using CV2 #### 
    # "","Grid","Pyramid" + 
    # "FAST","GFTT","HARRIS","MSER","ORB","SIFT","STAR","SURF"
    #method = "GridFAST"
    #feat_det = cv2.FeatureDetector_create(method)
    time1 = time.time()

    # convert np image to grayscale
    #featPoints = feat_det.detect(
        #cv2.cvtColor(image_np, cv2.COLOR_BGR2GRAY))

    time2 = time.time()
    if VERBOSE :
        print ('%s detector found: %s points in: %s sec.'%(method,
            len(featPoints),time2-time1))

    # for featpoint in featPoints:
    #     x,y = featpoint.pt
    #     cv2.circle(image_np,(int(x),int(y)), 3, (0,0,255), -1)

    crpd,crpd2=cropped(cv_image)

    image_message = bridge.cv2_to_imgmsg(crpd, encoding="rgb8")
    image_message2 = bridge.cv2_to_imgmsg(crpd2, encoding="rgb8")

    # cv2.imshow('cv_img', crpd)
    # cv2.waitKey(2)

    #### Create CompressedIamge ####
    msg = image_message 
    msg2=image_message2 
    # msg3 = camera_info

    # self.pub_info.publish(msg3)
    self.image_pub.publish(msg)
    self.image_pub2.publish(msg2)

def main(args): '''Initializes and cleanup ros node''' ic = image_feature() rospy.init_node('ROI_NODE', anonymous=True) rate = rospy.Rate(20)

try:
    rate.sleep()

    rospy.spin()
except KeyboardInterrupt:
    print("Shutting down ROS Image feature detector module")
cv2.destroyAllWindows()

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

Error: [ WARN] [1615357424.053186321]: [image_transport] Topics '/output/image_raw/ROI_1' and '/output/image_raw/camera_info' do not appear to be synchronized. In the last 10s: Image messages received: 50 CameraInfo messages received: 50 Synchronized pairs: 0 [ WARN] [1615357434.053175138]: [image_transport] Topics '/output/image_raw/ROI_1' and '/output/image_raw/camera_info' do not appear to be synchronized. In the last 10s: Image messages received: 50 CameraInfo messages received: 50 Synchronized pairs: 0