focus.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2008, Willow Garage, Inc.
00005 *  All rights reserved.
00006 *
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 *
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the Willow Garage nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 
00035 #include <ros/ros.h>
00036 #include <sensor_msgs/Image.h>
00037 #include <diagnostic_updater/publisher.h>
00038 #include <stdio.h>
00039 #include <limits.h>
00040 
00041 #define BASESIZE 1000000
00042 int oldwidth = 0;
00043 int oldheight = 0;
00044 uint8_t base[BASESIZE];
00045 
00046 #define NUMSKIP 400
00047 
00048 FILE *gnuplotfile;
00049 
00050 inline void insert(int *max, int val)
00051 {
00052   if (val < max[0])
00053     return;
00054 
00055   int i = 1;
00056   while (val > max[i] && i < NUMSKIP)
00057   {
00058     max[i - 1] = max[i];
00059     i++;
00060   }
00061 
00062   max[i - 1] = val;
00063 }
00064 
00065 void callback(const sensor_msgs::ImageConstPtr& msg)
00066 {
00067   int width = msg->width;
00068   int height = msg->height;
00069 
00070   int max[2][NUMSKIP];
00071   int maxv[NUMSKIP];
00072   bzero(max, sizeof(max));
00073   bzero(maxv, sizeof(maxv));
00074 
00075   if (oldwidth != width || oldheight != height)
00076   {
00077     oldwidth = width;
00078     oldheight = height;
00079     for (int i = 0; i < width * height; i++)
00080       base[i] = 0;
00081     fprintf(stderr, "Resetting base. Show a black screen!\n");
00082   }
00083 
00084   /*assert(width * height < BASESIZE);
00085 
00086   for (int i = 0; i < width * height; i++)
00087     if (base[i] > msg->uint8_data.data[i])
00088       base[i] = msg->uint8_data.data[i];
00089   */
00090   for (int x = 1; x < width - 1; x++)
00091   {
00092     for (int y = 0; y < height - 1; y++)
00093     {                
00094       int a = (x + y) & 1;
00095 
00096       //if (a == 0)
00097       //  continue;
00098         
00099       int i = y * width + x;
00100       int i1 = i + width - 1;
00101       int i2 = i1 + 2;
00102       int p = msg->data[i] - base[i];
00103       int p1 = msg->data[i1] - base[i1];
00104       int p2 = msg->data[i2] - base[i2];
00105 
00106       int d1 = abs(p - p1);
00107       int d2 = abs(p - p2);
00108 
00109       insert(max[a], d1);
00110       insert(max[a], d2);
00111 //        insert(maxv, p);
00112     }
00113   }
00114 
00115 //  for (int i = 0; i < NUMSKIP; i++)
00116 //    printf("%i ", max[i]);
00117 //  printf(": %i %i %i %i\n", width, height, *max, *maxv);
00118   printf("%i %i\n", max[0][0], max[1][0]);
00119   fflush(stdout);
00120 }
00121 
00122 int startx, starty, endx, endy;
00123 
00124 int bound(int x, int min, int max)
00125 {
00126   if (x < min)
00127     return min;
00128   if (x > max)
00129     return max;
00130   return x;
00131 }
00132 
00133 void callback2(const sensor_msgs::ImageConstPtr& msg)
00134 {
00135   int width = msg->width;
00136   int height = msg->height;
00137   int sx = bound(startx, 0, width - 3);
00138   int sy = bound(starty, 0, height - 3);
00139   int ex = bound(endx, sx + 3, width);
00140   int ey = bound(endy, sy + 3, height);
00141 
00142 //  printf("%i %i %i %i %i %i %i %i\n", startx, sx, starty, sy, endx, ex, endy, ey);
00143 
00144   if (oldwidth != width || oldheight != height)
00145   {
00146     oldwidth = width;
00147     oldheight = height;
00148     for (int i = 0; i < width * height; i++)
00149       base[i] = 0;
00150     fprintf(stderr, "Resetting base. Show a black screen!\n");
00151   }
00152 
00153   /*assert(width * height < BASESIZE);
00154 
00155   for (int i = 0; i < width * height; i++)
00156     if (base[i] > msg->uint8_data.data[i])
00157       base[i] = msg->uint8_data.data[i];
00158   */
00159   fprintf(gnuplotfile, "set yrange [-1:255]\n");
00160   fprintf(gnuplotfile, "set terminal x11\n");
00161   fprintf(gnuplotfile, "plot \"-\" using 0:1 with lines\n");
00162   for (int y = sy + 1; y < ey - 1; y++)
00163   {
00164     int max = 0;
00165 
00166     for (int x = sx + 1; x < ex - 1; x++)
00167     {                
00168       int a = (x + y) & 1;
00169 
00170       if (a == 0)
00171         continue;
00172         
00173       int i = y * width + x;
00174       int i1 = i + width - 1;
00175       int i2 = i1 + 2;
00176       int p = msg->data[i] - base[i];
00177       int p1 = msg->data[i1] - base[i1];
00178       int p2 = msg->data[i2] - base[i2];
00179 
00180       int d1 = abs(p - p1);
00181       int d2 = abs(p - p2);
00182 
00183       if (d1 > max) max = d1;
00184       if (d2 > max) max = d2;
00185     }
00186     fprintf(gnuplotfile, "%i\n", max);
00187   }
00188   fprintf(gnuplotfile, "e\n\n");
00189   fflush(gnuplotfile);
00190 }
00191 
00192 int main(int argc, char **argv)
00193 {
00194   gnuplotfile = popen("gnuplot", "w");
00195 
00196   if (!gnuplotfile)
00197   {
00198     perror("popen call failed");
00199     return -1;
00200   }
00201   
00202   ros::init(argc, argv, "forearm_focus");
00203   ros::NodeHandle nh;
00204   ros::NodeHandle pnh = ros::NodeHandle("~");
00205 
00206   pnh.param("startx", startx, -1);
00207   pnh.param("starty", starty, -1);
00208   pnh.param("endx", endx, INT_MAX);
00209   pnh.param("endy", endy, INT_MAX);
00210 
00211   ros::Subscriber sub = nh.subscribe("image", 1, callback2);
00212   ros::spin();
00213   return 0;
00214 }


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