bag_stitcher.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 #sample run command for using the kinect as a capture device
00003 #rosrun pano_ros capture_pano.py image:=/kinect/rgb/image_raw camera_info:=/kinect/rgb/camera_info
00004 
00005 import roslib; roslib.load_manifest('pano_py')
00006 import rospy
00007 import sys
00008 import ros
00009 import cv
00010 import tempfile
00011 import rosbag
00012 from std_msgs.msg import String
00013 from cv_bridge import CvBridge, CvBridgeError
00014 import message_filters
00015 from sensor_msgs.msg import Image, CameraInfo
00016 import os
00017 import pano_py as pano
00018 import pano_cv as pcv
00019 
00020 class StitchJob(object):
00021     bag_name = "default.bag"
00022     stitch_file_name = "default.jpg"
00023     camera_info = CameraInfo()
00024     def __init__(self,bag_name,stitch_file_name):
00025         self.bag_name = bag_name
00026         self.stitch_file_name = stitch_file_name
00027         self.bridge = CvBridge()
00028     def stitch(self):
00029         bag = rosbag.Bag(self.bag_name)
00030         saved_camera_info = False
00031         img_n = 0
00032         pano_dir = tempfile.mkdtemp("pano")
00033         image_names = []
00034         
00035         camera = pano.Camera()
00036         blur_detector = pano.BlurDetector()
00037         
00038         for topic, msg, t in bag.read_messages(topics=['camera_info']):
00039             if ('camera_info' in topic):
00040                 print "reading camera info"
00041                 K = pcv.Mat(3,3,pcv.CV_32FC1)
00042                 K.fromarray(msg.K)
00043                 img_size = pcv.Size( msg.width, msg.height)                
00044                 camera.setCameraIntrinsics(K,pcv.Mat(),img_size)             
00045                 break
00046         
00047         options = pano.Options()
00048         options.camera = camera
00049         options.stitch_size = pcv.Size(4000,2000)
00050         options.directory = pano_dir
00051         #options.image_names.assign(image_names)
00052         params = options.fitter_params
00053         params.error_thresh = 6;
00054         params.inliers_thresh = 15;
00055         params.maxiters = 100;
00056         params.nNeeded = 2;
00057         options.stitch_output = self.stitch_file_name
00058         
00059        
00060         stitcher = pano.StitchEngine(options)
00061         
00062         for topic, msg, t in bag.read_messages(topics=['image']):
00063             if ('image' in topic):
00064                 try:
00065                     cv_image = self.bridge.imgmsg_to_cv(msg, "rgb8")
00066                     b_prob =   blur_detector.checkBlur(cv_image)
00067                     print "image blur prob = %f" %b_prob         
00068                     pano_image = pcv.convertCvMat2Mat(cv_image)
00069                     stitcher.addNewImage(pano_image)
00070                     img_n += 1                    
00071                 except CvBridgeError, e:
00072                     print e
00073                                
00074         bag.close()
00075         
00076         blended = cv.CreateMat(2000,4000,cv.CV_8UC3)
00077         
00078         stitcher.stitch(blended,self.stitch_cb)
00079         
00080         cv.NamedWindow("blended",pcv.CV_WINDOW_KEEPRATIO)
00081         cv.ShowImage("blended",blended)
00082         
00083         cv.SaveImage(self.stitch_file_name,blended)
00084         cv.waitKey(0)
00085         
00086        
00087         return True
00088         
00089     def stitch_cb(self,progress):
00090         print "Python progress %d" % progress
00091         return 0
00092     
00093 if __name__ == '__main__':
00094     if len(sys.argv) != 3:
00095         print "usage:\n%s pano.bag stitch.jpg" % sys.argv[0]
00096     else:    
00097         stitch_job = StitchJob(sys.argv[1],sys.argv[2])
00098         success = stitch_job.stitch()
00099       


pano_py
Author(s): Ethan Rublee
autogenerated on Mon Oct 6 2014 08:05:14