visualize_descs.py
Go to the documentation of this file.
00001 import roslib; roslib.load_manifest('normal_descriptor_node')
00002 import sys
00003 import rospy
00004 from sensor_msgs.msg import PointCloud2
00005 from normal_descriptor_node.msg import ndesc, ndesc_pc
00006 
00007 import numpy as np
00008 from scipy.misc import toimage
00009 import pylab
00010 import struct
00011 
00012 def get_pos_rgb_gray(data):
00013     resolution = (data.height, data.width)
00014     # 3D position for each pixel
00015     img = np.fromstring(data.data, np.float32)
00016 
00017     x = img[0::8].reshape(resolution)
00018     y = img[1::8].reshape(resolution)
00019     z = img[2::8].reshape(resolution) 
00020     rgb = img[4::8]
00021     r = np.zeros(data.height*data.width)
00022     g = np.zeros(data.height*data.width)
00023     b = np.zeros(data.height*data.width)
00024     for i in range(rgb.shape[0]):
00025         int_rgb = struct.unpack('i', struct.pack('f', rgb[i]))[0]
00026         r[i] = (int_rgb & 0xff0000) >> 16
00027         g[i] = (int_rgb & 0xff00) >> 8
00028         b[i] = (int_rgb & 0xff)
00029     r = np.flipud(r.reshape(resolution))
00030     g = np.flipud(g.reshape(resolution))
00031     b = np.flipud(b.reshape(resolution))
00032     x    = np.flipud(x)
00033     y    = np.flipud(y)
00034     z    = np.flipud(z)
00035     gray = (0.30*r+0.59*g+0.11*b).astype('uint8')
00036     return (x, y, z, r, g, b, toimage(np.array([r,g,b]).astype('uint8')))#gray)
00037 
00038 
00039 
00040 class node_visualize_descs:
00041     
00042     def __init__(self):
00043         print "init node_visualize_descs"
00044         self.pcl = None
00045         #self.stamp_pcl = None
00046         self.desc_centroid = None
00047         #self.stamp_desc_centroid = None
00048         self.desc_selected = None
00049         #self.stamp_desc_selected = None
00050         #assuming static descriptor parameters!!
00051         self.nori=4
00052         self.nspa=4
00053         self.pixw=20
00054 
00055 
00056     def listener(self):
00057         pylab.ion()
00058         rospy.init_node('node_visualize_descs', anonymous=True)
00059         # GET SUBSCRIBERS!
00060         rospy.Subscriber("visualize_descs/rgb_pcl", PointCloud2, self.callback_get_pcl)
00061         rospy.Subscriber("visualize_descs/ndesc_center", ndesc, self.callback_center)
00062         rospy.Subscriber("visualize_descs/ndesc_desc", ndesc, self.callback_desc)
00063         rospy.spin()
00064         
00065     def callback_get_pcl(self, data):
00066         (x, y, z, r, g, b, gray) = get_pos_rgb_gray(data)
00067         self.pcl = (x, y, z, r, g, b, gray)
00068         #self.stamp_pcl = data.header.stamp
00069         #if not ( (self.stamp_pcl == self.stamp_desc_selected) and (self.stamp_pcl == self.stamp_desc_centroid) ):
00070         if self.desc_centroid==None or self.desc_selected==None:
00071             #print self.stamp_pcl, self.stamp_desc_selected, self.stamp_desc_centroid
00072             return
00073         self.callback()
00074 
00075     def callback_center(self, data):
00076         #get descriptor
00077         self.desc_centroid = data
00078         #self.stamp_desc_centroid = data.header.stamp
00079         #if not ( (self.stamp_pcl == self.stamp_desc_selected) and (self.stamp_pcl == self.stamp_desc_centroid) ):
00080         if self.pcl==None or self.desc_selected==None:
00081             return
00082         self.callback()
00083 
00084 
00085     def callback_desc(self, data):
00086         # get descriptor
00087         self.desc_selected=data
00088         #self.stamp_desc_selected = data.header.stamp        
00089         #if not ( (self.stamp_pcl == self.stamp_desc_selected) and (self.stamp_pcl == self.stamp_desc_centroid) ):
00090         if self.pcl==None or self.desc_centroid==None:
00091             #print self.stamp_pcl, self.stamp_desc_selected, self.stamp_desc_centroid
00092             return
00093         self.callback()
00094 
00095 
00096     def callback(self):
00097         print "visualizing"
00098         pylab.figure(1)
00099         pylab.cla()
00100         pylab.imshow(self.pcl[6])
00101         pylab.hold('on')
00102         self.visualize_box_in_image(self.desc_selected.u, self.desc_selected.v)
00103         pylab.draw()
00104         pylab.draw()
00105 
00106         pylab.figure(2)
00107         self.visualize_one_desc(np.array(self.desc_selected.descriptor))
00108         pylab.suptitle("Desc selected")
00109         pylab.draw()
00110         pylab.draw()
00111 
00112         pylab.figure(3)
00113         self.visualize_one_desc(np.array(self.desc_centroid.descriptor))
00114         pylab.suptitle("Desc centroid")
00115         pylab.draw()
00116         pylab.draw()
00117         self.desc_centroid=None
00118         self.desc_selected=None
00119         self.pcl=None
00120     #-----------------------------------
00121 
00122     def visualize_one_desc(self, npdescs):
00123         nori = self.nori
00124         nspa = self.nspa
00125         pylab.cla()
00126         offset=0
00127         vmin=np.min(npdescs)
00128         vmax=np.max(npdescs)
00129         for ix in range(1,nspa*nspa+1):
00130             pylab.subplot(nspa,nspa,ix)
00131             pylab.imshow(npdescs[offset:offset+nori*nori].reshape(nori,nori), interpolation='nearest',vmin=vmin, vmax=vmax)
00132             pylab.yticks(np.arange(-0.5,4.5,1),[str(np.round(a,2)) for a in np.arange(-np.pi,np.pi+np.pi/2.,2*np.pi/4.)])
00133             pylab.xticks(np.arange(-0.5,4.5,1),[str(np.round(a,2)) for a in np.arange(np.pi/2.,np.pi+np.pi/4.,np.pi/8.)])    
00134         #pylab.bar(np.arange(0,nori*nori),npdescs[i][offset:offset+nori*nori],width=0.2)
00135             print zip(np.arange(0,nori*nori),npdescs[offset:offset+nori*nori])
00136             offset+=nori*nori
00137     #-----------------------------
00138 
00139     def visualize_box_in_image(self, u, v):
00140         nspa = self.nspa
00141         pixw = self.pixw
00142         pylab.plot(np.array([[u-pixw,u-pixw,u-pixw,u+pixw],[u+pixw,u-pixw,u+pixw,u+pixw] ]), np.array([[v-pixw,v-pixw,v+pixw,v-pixw],[v-pixw,v+pixw,v+pixw,v+pixw]]),'b')
00143         for ix in range(1,nspa):
00144             pylab.plot(np.array([u-pixw, u+pixw]).T,np.array([(v-pixw) + ix*(2*pixw+1)/float(nspa),(v-pixw) + ix*(2*pixw+1)/float(nspa)]).T,'g')
00145             pylab.plot(np.array([(u-pixw) + ix*(2*pixw+1)/float(nspa),(u-pixw) + ix*(2*pixw+1)/float(nspa)]).T,np.array([v-pixw, v+pixw]).T,'g')
00146         pylab.plot(u,v,'*r')
00147     #-----------------------------
00148 
00149 
00150 #########################################################################################
00151 #########################################################################################
00152 #########################################################################################
00153 #########################################################################################
00154 
00155 if __name__ == '__main__':
00156 
00157     pylab.ion()
00158     A = node_visualize_descs()
00159     A.listener()
00160 


normal_descriptor_node
Author(s): Arnau Ramisa
autogenerated on Fri Dec 6 2013 20:19:55