00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
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)
00057 cv.NamedWindow("Image window", 1)
00058
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
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
00098
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
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)