saliencyPath.C
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
00036
00037
00038
00039 #include "GUI/XWindow.H"
00040 #include "Raster/Raster.H"
00041 #include "Util/Assert.H"
00042 #include "Util/Timer.H"
00043 #include "Util/log.H"
00044 #include "Image/Image.H"
00045 #include "Image/Pixels.H"
00046 #include "Image/DrawOps.H"
00047 #include "Image/ColorOps.H"
00048
00049 #include <fstream>
00050 #include <iostream>
00051 #include <math.h>
00052 #include <string>
00053 #include <vector>
00054 #include <cfloat>
00055
00056 #define DISKSIZE 50
00057 #define SALPOINTS 8
00058 #define OUT_SPEED 10
00059 #define OUT_ACCEL 1
00060 #define OUT_IMAGE_SIZE_X 128
00061 #define OUT_IMAGE_SIZE_Y 128
00062
00063 using namespace std;
00064
00065 int main(const int argc, const char **argv)
00066 {
00067
00068 if(argc < 1)
00069 std::cerr << "USAGE: saliencyPath image.pgm\n";
00070
00071 string imageFile = argv[1];
00072 const uint salPoints = SALPOINTS;
00073
00074 Image<float> salImage = Raster::ReadGray(imageFile.c_str());
00075 Image<PixRGB<float> > outImage = luminance(salImage);
00076
00077 std::vector<float> xpos(salPoints,0);
00078 std::vector<float> ypos(salPoints,0);
00079 std::vector<float> sval(salPoints,0.0F);
00080
00081
00082
00083 for(uint i = 0; i < salPoints; i++)
00084 {
00085 for(uint x = 0; x < (uint)salImage.getWidth(); x++)
00086 {
00087 for(uint y = 0; y < (uint)salImage.getHeight(); y++)
00088 {
00089
00090 if(salImage.getVal(x,y) > sval[i])
00091 {
00092 sval[i] = salImage.getVal(x,y);
00093 ypos[i] = y;
00094 xpos[i] = x;
00095 }
00096 }
00097 }
00098
00099 drawDisk(salImage,Point2D<int>((int)xpos[i],(int)ypos[i]),DISKSIZE,0.0F);
00100 }
00101
00102 std::vector<uint> cpos(salPoints,0);
00103 std::vector<bool> used(salPoints,false);
00104 std::vector<uint> best(salPoints,0);
00105
00106 float bestVal = FLT_MAX;
00107 bool end = false;
00108 float dist = 0;
00109
00110
00111
00112
00113
00114
00115 while(!end)
00116 {
00117
00118 cpos[0]++;
00119 for(uint i = 0; i < salPoints-1; i++)
00120 {
00121 if(cpos[i] == 10)
00122 {
00123 cpos[i] = 0;
00124 cpos[i+1]++;
00125 }
00126 }
00127
00128
00129 if(cpos[salPoints-1] == 10)
00130 end = true;
00131
00132
00133 for(uint i = 0; i < salPoints; i++)
00134 {
00135 used[i] = false;
00136 }
00137
00138 dist = 0;
00139 for(uint i = 0; i < salPoints - 1; i++)
00140 {
00141
00142 used[cpos[i]] = true;
00143
00144
00145 if(used[cpos[i+1]] == false)
00146 {
00147 dist += sqrt(pow(ypos[cpos[i]] - ypos[cpos[i+1]],2)
00148 + pow(xpos[cpos[i]] - xpos[cpos[i+1]],2));
00149 }
00150 else
00151 {
00152 dist = FLT_MAX;
00153 break;
00154 }
00155 }
00156
00157
00158 if(dist < bestVal)
00159 {
00160 best = cpos;
00161 bestVal = dist;
00162 }
00163 }
00164
00165 string A = ".results.txt";
00166 string B = ".path";
00167 string outFileName = imageFile + A;
00168 string outImageName = imageFile + B;
00169 ofstream outFile(outFileName.c_str(), ios::out);
00170
00171 outFile << OUT_IMAGE_SIZE_X << " " << OUT_IMAGE_SIZE_Y << "\n";
00172 outFile << salPoints << "\n";
00173
00174 std::cerr << "computing lum image\n";
00175
00176 PixRGB<float> red(255.0F,0.0F,0.0F);
00177 PixRGB<float> blue(0.0F,0.0F,255.0F);
00178
00179
00180 for(uint i = 0; i < salPoints; i++)
00181 {
00182 std::cerr << i << "\n";
00183 outFile << xpos[best[i]] << " " << ypos[best[i]] << " "
00184 << OUT_SPEED << " " << OUT_ACCEL << "\n";
00185 drawCircle(outImage,Point2D<int>((int)xpos[best[i]]
00186 ,(int)ypos[best[i]]),25,red,2);
00187 if(i < salPoints-1)
00188 {
00189 drawArrow(outImage,Point2D<int>((int)xpos[best[i]],(int)ypos[best[i]]),
00190 Point2D<int>((int)xpos[best[i+1]],(int)ypos[best[i+1]]),blue,2);
00191 }
00192 std::cerr << ".\n";
00193 }
00194 Raster::WriteRGB(outImage,outImageName,RASFMT_PNG);
00195 outFile.close();
00196 std::cerr << "DONE!";
00197 }