convert uint8 data in sensor_msgs/Image to be integer

asked 2021-06-21 11:40:07 -0600

jenanaputra gravatar image

Hello, Iam using ros melodic and ubuntu 18.04. I tried to subscribed sensor_msgs/Image and wanted to try to convert the uint8 data in it to int8. I attach my sample code as below :

#!/usr/bin/env python


import rospy
from gb_msgs.msg import List
import numpy as np
from sensor_msgs.msg import LaserScan
import math as mat
from sensor_msgs.msg import Image

rospy.init_node('sonar_data_handler')
scan_pub = rospy.Publisher('/scan', LaserScan, queue_size=1000)
pub_pointed_obs = rospy.Publisher('/detected_obs', List, queue_size=1)

scan = LaserScan()
buff_points_obs = List()

num_readings = 128
laser_frequency = 20
beamBearings = np.array([ -0.7854, -0.7698, -0.7544, -0.7392, -0.7242, -0.7095, -0.6949, -0.6805, -0.6662, -0.6521, -0.6382, -0.6244, -0.6107, -0.5972, -0.5838, -0.5705, -0.5573, -0.5443, -0.5313, -0.5184, -0.5057, -0.4930, -0.4804, -0.4679, -0.4554, -0.4431, -0.4308, -0.4186, -0.4064, -0.3943, -0.3823, -0.3703, -0.3584, -0.3465, -0.3347, -0.3229, -0.3112, -0.2996, -0.2879, -0.2763, -0.2648, -0.2532, -0.2418, -0.2303, -0.2189, -0.2075, -0.1961, -0.1848, -0.1735, -0.1622, -0.1509, -0.1396, -0.1284, -0.1172, -0.1060, -0.0948, -0.0836, -0.0724, -0.0613, -0.0501, -0.0390, -0.0278, -0.0167, -0.0056, 0.0056, 0.0167, 0.0278, 0.0390, 0.0501, 0.0613, 0.0724, 0.0836, 0.0948, 0.1060, 0.1172, 0.1284, 0.1396, 0.1509, 0.1622, 0.1735, 0.1848, 0.1961, 0.2075, 0.2189, 0.2303, 0.2418, 0.2532, 0.2648, 0.2763, 0.2879, 0.2996, 0.3112, 0.3229, 0.3347, 0.3465, 0.3584, 0.3703, 0.3823, 0.3943, 0.4064, 0.4186, 0.4308, 0.4431, 0.4554, 0.4679, 0.4804, 0.4930, 0.5057, 0.5184, 0.5313, 0.5443, 0.5573, 0.5705, 0.5838, 0.5972, 0.6107, 0.6244, 0.6382, 0.6521, 0.6662, 0.6805, 0.6949, 0.7095, 0.7242, 0.7392, 0.7544, 0.7698, 0.7854])
scan.ranges = []



def callback(msgs):
    global beamBearings
    # if the range max of sonar is default (50 m)
    max_range = 50
    width = msgs.width
    hight = msgs.height
    sonar = np.zeros((hight,width),dtype=np.uint8)
    sonar.tolist()
    # sonar = np.zeros((hight,width),dtype=np.int8)
    buff = np.zeros(width)
    buff_ = buff.tolist()
    buff_points = np.zeros(width)
    buff_points_ = buff_points.tolist()
    sonar = msgs.data
    sonar_ = np.zeros((hight,width), dtype=np.int8 ) 

    count__ = 0 
    # for i in range(hight) :
    #     for j in range(width):
    #         # sonar_[i][j] = np.int8(np.float32(sonar[count__]))
    #         # sonar_[i][j] = np.int8(sonar[count__])
    #         X_ = np.int8(sonar[count__])
    #         count__ = count__ + 1
    X_ = np.int8(sonar)

    # for i in range (0,hight) :
    #     for j in range (0,width) :
    #         if sonar[i][j] == 255 :
    #             range_ = (max_range/hight)*((hight - x ) + 1)
    #             buff_[j] = range_
    #             if range_ <= 15 :
    #                 x_points = mat.cos(beamBearings[j])*range_
    #                 y_points = mat.sin(beamBearings[j])*range_
    #                 buff_points_[j ...
(more)
edit retag flag offensive close merge delete

Comments

The recommended way to consume sensor_msgs/Image messages in a Python node would be to use cv_bridge. Don't do this manually.

See Converting between ROS images and OpenCV images (Python) on the ROS wiki.

gvdhoorn gravatar image gvdhoorn  ( 2021-06-22 02:56:22 -0600 )edit