Go to the documentation of this file.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 #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
00085
00086
00087
00088
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
00097
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
00112 }
00113 }
00114
00115
00116
00117
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
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
00154
00155
00156
00157
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 }