00001 /*!@file AppPsycho/saliencyPath.C finds a saliency path in an image 00002 */ 00003 00004 // //////////////////////////////////////////////////////////////////// // 00005 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00006 // University of Southern California (USC) and the iLab at USC. // 00007 // See http://iLab.usc.edu for information about this project. // 00008 // //////////////////////////////////////////////////////////////////// // 00009 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00010 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00011 // in Visual Environments, and Applications'' by Christof Koch and // 00012 // Laurent Itti, California Institute of Technology, 2001 (patent // 00013 // pending; application number 09/912,225 filed July 23, 2001; see // 00014 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00015 // //////////////////////////////////////////////////////////////////// // 00016 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00017 // // 00018 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00019 // redistribute it and/or modify it under the terms of the GNU General // 00020 // Public License as published by the Free Software Foundation; either // 00021 // version 2 of the License, or (at your option) any later version. // 00022 // // 00023 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00024 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00025 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00026 // PURPOSE. See the GNU General Public License for more details. // 00027 // // 00028 // You should have received a copy of the GNU General Public License // 00029 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00030 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00031 // Boston, MA 02111-1307 USA. // 00032 // //////////////////////////////////////////////////////////////////// // 00033 // 00034 // Primary maintainer for this file: T. Nathan Mundhenk <mundhenk@usc.edu> 00035 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/AppPsycho/saliencyPath.C $ 00036 // $Id: saliencyPath.C 9412 2008-03-10 23:10:15Z farhan $ 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 // find the n most salient points 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 // if this point is better, then store it 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 // for each salient location, disk it so we don't count it twice 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 /* By setting salPoints as a constant at compile time, if we use 00111 loop unrolling, this portion of the code will be optimized for 00112 indexing into the vectors. 00113 */ 00114 00115 while(!end) 00116 { 00117 // increment over all permutations like an odometer 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 // when the last permutation "flips" then end 00129 if(cpos[salPoints-1] == 10) 00130 end = true; 00131 00132 // to keep points unique, don't use the same point twice 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 // record this point as being used 00142 used[cpos[i]] = true; 00143 // compute distance over current permutation only if 00144 // it is unique 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 // if the distance is superior, then store it 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 //outFile << salImage.getWidth() << " " << salImage.getHeight() << "\n"; 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 //output best path to file and an output image 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 }