00001 /*!@file AppPsycho/score-classic-searcharray.C */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Rob Peters <rjpeters at usc dot edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/AppPsycho/score-classic-searcharray.C $ 00035 // $Id: score-classic-searcharray.C 9490 2008-03-18 17:44:42Z rjpeters $ 00036 // 00037 00038 #ifndef APPPSYCHO_SCORE_CLASSIC_SEARCHARRAY_C_DEFINED 00039 #define APPPSYCHO_SCORE_CLASSIC_SEARCHARRAY_C_DEFINED 00040 00041 #include "Image/CutPaste.H" 00042 #include "Image/Image.H" 00043 #include "Image/MathOps.H" 00044 #include "Image/Range.H" 00045 #include "Image/ShapeOps.H" 00046 #include "Raster/GenericFrame.H" 00047 #include "Raster/PfmParser.H" 00048 #include "Raster/Raster.H" 00049 #include "Util/StringConversions.H" 00050 #include "Util/StringUtil.H" 00051 #include "Util/log.H" 00052 00053 #include "GUI/XWinManaged.H" 00054 #include "Image/Normalize.H" 00055 #include "Image/DrawOps.H" 00056 00057 #include <cstdio> 00058 #include <fstream> 00059 #include <string> 00060 #include <vector> 00061 00062 static int fixnum(const double x, const std::vector<double>& vals) 00063 { 00064 int fixnum = 1; 00065 for (size_t i = 0; i < vals.size(); ++i) 00066 if (vals[i] >= x) ++fixnum; 00067 return fixnum; 00068 } 00069 00070 int main(int argc, char** argv) 00071 { 00072 if (argc != 6) 00073 { 00074 fprintf(stderr, "usage: %s salmap dims keyfile scorefile stimimage\n", argv[0]); 00075 return -1; 00076 } 00077 00078 const std::string salmapfile = argv[1]; 00079 const Dims rescaledims = fromStr<Dims>(argv[2]); 00080 const std::string keyfile = argv[3]; 00081 const std::string scorefile = argv[4]; 00082 const Image<byte> stimimage = Raster::ReadGray(argv[5]); 00083 00084 // Image<PixRGB<byte> > stimmarkup(stimimage); 00085 00086 const Image<float> salmap0 = PfmParser(salmapfile).getFrame().asFloat(); 00087 const Image<float> salmap = rescaleBilinear(salmap0, rescaledims); 00088 00089 // Image<PixRGB<byte> > salmarkup(normalizeFloat(salmap, FLOAT_NORM_0_255));; 00090 00091 std::vector<double> bgmeanvals; 00092 std::vector<double> bgmaxvals; 00093 std::vector<double> fgmeanvals; 00094 std::vector<double> fgmaxvals; 00095 00096 std::ifstream ifs(keyfile.c_str()); 00097 if (!ifs.is_open()) 00098 LFATAL("couldn't open '%s' for reading", keyfile.c_str()); 00099 00100 std::string line; 00101 while (std::getline(ifs, line)) 00102 { 00103 if (line[0] == '%') 00104 continue; 00105 00106 std::vector<std::string> toks; 00107 split(line, " ", std::back_inserter(toks)); 00108 if (toks.size() != 5) 00109 LFATAL("malformed line: '%s'", line.c_str()); 00110 00111 const int x0 = fromStr<int>(toks[1]); 00112 const int y0 = fromStr<int>(toks[2]); 00113 const int x1 = fromStr<int>(toks[3]); 00114 const int y1 = fromStr<int>(toks[4]); 00115 00116 // const Rectangle r = Rectangle::tlbrO(y0 + int(0.25*(y1-y0)), 00117 // x0 + int(0.25*(x1-x0)), 00118 // y0 + int(0.75*(y1-y0)), 00119 // x0 + int(0.75*(x1-x0))) 00120 // .getOverlap(salmap.getBounds()); 00121 const Rectangle r = Rectangle::tlbrO(y0, x0, y1, x1) 00122 .getOverlap(salmap.getBounds()); 00123 00124 const Image<float> patch = crop(salmap, r, false); 00125 00126 const float patchmean = mean(patch); 00127 const Range<float> patchrange = rangeOf(patch); 00128 00129 if (fromStr<int>(toks[0]) == 0) 00130 { 00131 bgmeanvals.push_back(patchmean); 00132 bgmaxvals.push_back(patchrange.max()); 00133 00134 // drawRectSquareCorners(stimmarkup, r, PixRGB<byte>(0,0,255), 2); 00135 // drawRectSquareCorners(salmarkup, r, PixRGB<byte>(0,0,255), 2); 00136 } 00137 else if (fromStr<int>(toks[0]) == 1) 00138 { 00139 fgmeanvals.push_back(patchmean); 00140 fgmaxvals.push_back(patchrange.max()); 00141 00142 // drawRectSquareCorners(stimmarkup, r, PixRGB<byte>(255,0,0), 2); 00143 // drawRectSquareCorners(salmarkup, r, PixRGB<byte>(255,0,0), 2); 00144 } 00145 else 00146 LFATAL("invalid fg value '%s'", toks[0].c_str()); 00147 } 00148 00149 ifs.close(); 00150 00151 const Image<double> bgmean(&bgmeanvals[0], bgmeanvals.size(), 1); 00152 const Image<double> bgmax(&bgmaxvals[0], bgmaxvals.size(), 1); 00153 const Image<double> fgmean(&fgmeanvals[0], fgmeanvals.size(), 1); 00154 const Image<double> fgmax(&fgmaxvals[0], fgmaxvals.size(), 1); 00155 00156 const double mean_bgmean = mean(bgmean); 00157 const double mean_bgmax = mean(bgmax); 00158 const double std_bgmean = stdev(bgmean); 00159 const double std_bgmax = stdev(bgmax); 00160 const double mean_fgmean = mean(fgmean); 00161 const double mean_fgmax = mean(fgmax); 00162 00163 FILE* fwarn = fopen("warnings.txt", "a"); 00164 if (fwarn == 0) 00165 LFATAL("couldn't open 'warnings.txt' for appending"); 00166 bool bad = false; 00167 #define CHECKNAN(x) if (isnan(x)) { bad = true; fprintf(fwarn, "nan: %s (args = %s %s %s %s)\n", #x, argv[1], argv[2], argv[3], argv[4]); } 00168 CHECKNAN(mean_bgmean); 00169 CHECKNAN(mean_bgmax); 00170 CHECKNAN(std_bgmean); 00171 CHECKNAN(std_bgmax); 00172 CHECKNAN(mean_fgmean); 00173 CHECKNAN(mean_fgmax); 00174 fclose(fwarn); 00175 00176 if (bad) exit(0); 00177 00178 double mean_nitems = 0.0; 00179 double mean_fgmean_zscore = 0.0; 00180 double mean_fgmax_zscore = 0.0; 00181 double mean_fgmean_over_mean_bgmean = 0.0; 00182 double mean_fgmax_over_mean_bgmax = 0.0; 00183 double mean_fgmean_minus_mean_bgmean = 0.0; 00184 double mean_fgmax_minus_mean_bgmax = 0.0; 00185 double mean_fgmean_fixnum = 0.0; 00186 double mean_fgmax_fixnum = 0.0; 00187 int n = 0; 00188 00189 std::ifstream scorefs(scorefile.c_str()); 00190 if (scorefs.is_open()) 00191 { 00192 while (std::getline(scorefs, line)) 00193 { 00194 std::vector<std::string> toks; 00195 split(line, " ", std::back_inserter(toks)); 00196 if (toks.size() != 9) 00197 LFATAL("malformed line in score file: '%s'", line.c_str()); 00198 00199 ++n; 00200 mean_nitems += fromStr<double>(toks[0]); 00201 mean_fgmean_zscore += fromStr<double>(toks[1]); 00202 mean_fgmax_zscore += fromStr<double>(toks[2]); 00203 mean_fgmean_over_mean_bgmean += fromStr<double>(toks[3]); 00204 mean_fgmax_over_mean_bgmax += fromStr<double>(toks[4]); 00205 mean_fgmean_minus_mean_bgmean += fromStr<double>(toks[5]); 00206 mean_fgmax_minus_mean_bgmax += fromStr<double>(toks[6]); 00207 mean_fgmean_fixnum += fromStr<double>(toks[7]); 00208 mean_fgmax_fixnum += fromStr<double>(toks[8]); 00209 } 00210 scorefs.close(); 00211 } 00212 00213 FILE* f = fopen(scorefile.c_str(), "a"); 00214 if (f == 0) 00215 LFATAL("couldn't open '%s' for appending", scorefile.c_str()); 00216 // mean(fgmean_zscore) mean(fgmax_zscore) mean(fgmean)/mean(bgmean) mean(fgmax)/mean(bgmax) mean(fgmean)-mean(bgmean) mean(fgmax)-mean(bgmax) 00217 fprintf(f, "%12d %12g %12g %12g %12g %12g %12g %12d %12d\n", 00218 int(bgmeanvals.size()+fgmeanvals.size()), 00219 mean((fgmean - mean_bgmean) / std_bgmean), 00220 mean((fgmax - mean_bgmax) / std_bgmax), 00221 mean_fgmean / mean_bgmean, 00222 mean_fgmax / mean_bgmax, 00223 mean_fgmean - mean_bgmean, 00224 mean_fgmax - mean_bgmax, 00225 fixnum(mean_fgmean, bgmeanvals), 00226 fixnum(mean_fgmax, bgmaxvals)); 00227 fprintf(stderr, "current: %12d %12g %12g %12g %12g %12g %12g %12d %12d\n", 00228 int(bgmeanvals.size()+fgmeanvals.size()), 00229 mean((fgmean - mean_bgmean) / std_bgmean), 00230 mean((fgmax - mean_bgmax) / std_bgmax), 00231 mean_fgmean / mean_bgmean, 00232 mean_fgmax / mean_bgmax, 00233 mean_fgmean - mean_bgmean, 00234 mean_fgmax - mean_bgmax, 00235 fixnum(mean_fgmean, bgmeanvals), 00236 fixnum(mean_fgmax, bgmaxvals)); 00237 fclose(f); 00238 00239 mean_nitems += int(bgmeanvals.size()+fgmeanvals.size()); 00240 mean_fgmean_zscore += mean((fgmean - mean_bgmean) / std_bgmean); 00241 mean_fgmax_zscore += mean((fgmax - mean_bgmax) / std_bgmax); 00242 mean_fgmean_over_mean_bgmean += mean_fgmean / mean_bgmean; 00243 mean_fgmax_over_mean_bgmax += mean_fgmax / mean_bgmax; 00244 mean_fgmean_minus_mean_bgmean += mean_fgmean - mean_bgmean; 00245 mean_fgmax_minus_mean_bgmax += mean_fgmax - mean_bgmax; 00246 mean_fgmean_fixnum += fixnum(mean_fgmean, bgmeanvals); 00247 mean_fgmax_fixnum += fixnum(mean_fgmax, bgmaxvals); 00248 00249 ++n; 00250 00251 mean_nitems /= n; 00252 mean_fgmean_zscore /= n; 00253 mean_fgmax_zscore /= n; 00254 mean_fgmean_over_mean_bgmean /= n; 00255 mean_fgmax_over_mean_bgmax /= n; 00256 mean_fgmean_minus_mean_bgmean /= n; 00257 mean_fgmax_minus_mean_bgmax /= n; 00258 mean_fgmean_fixnum /= n; 00259 mean_fgmax_fixnum /= n; 00260 00261 FILE* fsum = fopen(("sum-"+scorefile).c_str(), "w"); 00262 if (fsum == 0) 00263 LFATAL("couldn't open 'sum-%s' for writing", scorefile.c_str()); 00264 fprintf(fsum, "%-70s %3d %12g %12g %12g %12g %12g %12g %12g %12g %12g\n", 00265 ("sum-"+scorefile).c_str(), 00266 n, 00267 mean_nitems, 00268 mean_fgmean_zscore, 00269 mean_fgmax_zscore, 00270 mean_fgmean_over_mean_bgmean, 00271 mean_fgmax_over_mean_bgmax, 00272 mean_fgmean_minus_mean_bgmean, 00273 mean_fgmax_minus_mean_bgmax, 00274 mean_fgmean_fixnum, 00275 mean_fgmax_fixnum); 00276 fprintf(stderr, "overall: %3d %12g %12g %12g %12g %12g %12g %12g %12g %12g\n", 00277 n, 00278 mean_nitems, 00279 mean_fgmean_zscore, 00280 mean_fgmax_zscore, 00281 mean_fgmean_over_mean_bgmean, 00282 mean_fgmax_over_mean_bgmax, 00283 mean_fgmean_minus_mean_bgmean, 00284 mean_fgmax_minus_mean_bgmax, 00285 mean_fgmean_fixnum, 00286 mean_fgmax_fixnum); 00287 fclose(fsum); 00288 00289 // XWinManaged xwin(concatX(stimmarkup, salmarkup)); 00290 // while (!xwin.pressedCloseButton()) 00291 // usleep(10000); 00292 } 00293 00294 // ###################################################################### 00295 /* So things look consistent in everyone's emacs... */ 00296 /* Local Variables: */ 00297 /* mode: c++ */ 00298 /* indent-tabs-mode: nil */ 00299 /* End: */ 00300 00301 #endif // APPPSYCHO_SCORE_CLASSIC_SEARCHARRAY_C_DEFINED