focus_prosilica.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Software License Agreement (BSD License)
00004 #
00005 # Copyright (c) 2008, Willow Garage, Inc.
00006 # All rights reserved.
00007 #
00008 # Redistribution and use in source and binary forms, with or without
00009 # modification, are permitted provided that the following conditions
00010 # are met:
00011 #
00012 #  * Redistributions of source code must retain the above copyright
00013 #    notice, this list of conditions and the following disclaimer.
00014 #  * Redistributions in binary form must reproduce the above
00015 #    copyright notice, this list of conditions and the following
00016 #    disclaimer in the documentation and/or other materials provided
00017 #    with the distribution.
00018 #  * Neither the name of the Willow Garage nor the names of its
00019 #    contributors may be used to endorse or promote products derived
00020 #    from this software without specific prior written permission.
00021 #
00022 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 # POSSIBILITY OF SUCH DAMAGE.
00034 
00035 ##\author Blaise Gassend
00036 ##\brief Displays prosilica focus on gnuplot window
00037 
00038 PKG = 'pr2_camera_focus'
00039 import roslib
00040 roslib.load_manifest(PKG)
00041 import sys
00042 import rospy
00043 import cv
00044 from std_msgs.msg import String
00045 from sensor_msgs.msg import Image
00046 from cv_bridge import CvBridge, CvBridgeError
00047 import subprocess
00048 from time import sleep
00049 
00050 class ImageConverter:
00051   def __init__(self):
00052     self.image_sub = rospy.Subscriber("prosilica/image", Image, self.process_image)
00053     self.bridge = CvBridge()
00054     null = open('/dev/null', 'w')
00055     self.gnuplot = subprocess.Popen("gnuplot", 
00056             stdin=subprocess.PIPE) #, stdout=null) 
00057     cv.NamedWindow("Image window", 1)
00058     #cv.NamedWindow("Out window", 1)
00059 
00060   def process_image(self, image):
00061     try:
00062       cv_image = self.bridge.imgmsg_to_cv(image, "mono8")
00063     except CvBridgeError, e:
00064       import traceback
00065       traceback.print_exc()
00066 
00067     (width, height) = cv.GetSize(cv_image)
00068     cv_image = cv.GetSubRect(cv_image, (1, 1, width - 2, height - 2))
00069     (width, height) = cv.GetSize(cv_image)
00070     
00071     blurradius = 30
00072     origimg = cv.CreateMat(height, width, cv.CV_32FC1)
00073     cv.Scale(cv_image, origimg, 1.0/255, 0.0)
00074     
00075     blurimg = cv.CreateMat(height, width, cv.CV_32FC1)
00076     cv.Smooth(origimg, blurimg, cv.CV_BLUR, blurradius, blurradius)
00077     blurimgsqr = cv.CreateMat(height, width, cv.CV_32FC1)
00078     cv.Pow(blurimg, blurimgsqr, 2)
00079 
00080     blursqr = cv.CreateMat(height, width, cv.CV_32FC1)
00081     cv.Pow(origimg, blursqr, 2)
00082     cv.Smooth(blursqr, blursqr, cv.CV_BLUR, blurradius, blurradius)
00083 
00084     cv.Sub(blursqr, blurimgsqr, blursqr)
00085     cv.Pow(blursqr, blursqr, -0.5)
00086     cv.Threshold(blursqr, blursqr, 8, 0, cv.CV_THRESH_TOZERO_INV)
00087 
00088     cv.Sub(origimg, blurimg, blurimg)
00089     cv.Abs(blurimg, blurimg)
00090     cv.Smooth(blurimg, blurimg, cv.CV_BLUR, blurradius, blurradius)
00091     #print >> sys.stderr, cv.Sum(blurimg)
00092     
00093     scalefactor=4
00094     scaledimg = cv.CreateMat(height*scalefactor, width*scalefactor, cv.CV_32FC1)
00095     cv.Resize(origimg, scaledimg, cv.CV_INTER_NN)
00096     cv.ShowImage("Image window", scaledimg)
00097     #cv.Resize(blurimg, scaledimg, cv.CV_INTER_NN)
00098     #cv.ShowImage("Out window", scaledimg)
00099     cv.WaitKey(2)
00100 
00101     print >> self.gnuplot.stdin, 'set terminal x11 0'
00102     print >> self.gnuplot.stdin, 'unset logscale xy'
00103     print >> self.gnuplot.stdin, 'set yrange [0:0.4]'
00104     print >> self.gnuplot.stdin, 'plot "-" with lines'
00105     for i in range(width-1):
00106         ## FIXME Plot combination of all lines.
00107         print >> self.gnuplot.stdin, blurimg[0,i]
00108     print >> self.gnuplot.stdin, 'e'
00109  
00110 def main(args):
00111   rospy.init_node('image_converter')
00112   ic = ImageConverter()
00113   try:
00114     rospy.spin()
00115   except KeyboardInterrupt:
00116     print "Shutting down"
00117   except:
00118     print "Shut down"
00119   cv.DestroyAllWindows()
00120 
00121 if __name__ == '__main__':
00122     main(sys.argv)


pr2_camera_focus
Author(s): Blaise Gassend
autogenerated on Sat Dec 28 2013 17:54:48