Robotics StackExchange | Archived questions

Converting sensor_msgs/Image unit8[] data to sumable type for computing average brightness

!/usr/bin/env python

from sensor_msgs.msg import Image
import rospy

average =0.0
def ImageCallback(image):
    global average 
    suma=0.0
    average=0.0
    #print(type(image.data[200]),"--------",image.data[200])
    for i in range(len(image.data)):
        print(type(image.data[i]),"--------",image.data[i])
    #average = suma/len(image.data)






if __name__=='__main__':
    while not rospy.is_shutdown():
        try:
            rospy.init_node('wezel',anonymous=True)
            image_subscriber = rospy.Subscriber('/camera/rgb/image_raw',Image,ImageCallback)
            print ("Average brightness is {}".format(average))
            rospy.spin()
        except:
            pass

Above code gives me as na output 'randomly': (type 'str', '--------', 'p') (type 'str', '--------', '8') (type 'str', '--------', '\xb2')

Am i using data from sensor_msg.msg/Image properly? How to convert this array to numbers, to compute average brightness f.e.?

When im using comand

rostopic echo /camera/rgb/image_raw

im getting numbers as an output.

Asked by Marseiliais on 2019-09-02 16:38:16 UTC

Comments

Answers

I would recommend you take a look at here, use cv_bridge to read image as rgb, then you can play around with pixel values.

Asked by Choco93 on 2019-09-04 06:54:19 UTC

Comments