00001 /*!@file Apps/NeovisionII/a---ChipValidatorGui.C Simple app to validate chips */ 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: Laurent Itti 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Apps/BorderWatch/BorderWatchGui.C $ 00035 // $Id: BorderWatchGui.C 13059 2010-03-26 08:14:32Z itti $ 00036 // 00037 00038 #include "QtUtil/Util.H" // for argv2qt() 00039 #include "NeovisionII/ChipValidator/ChipValidatorQt.qt.H" 00040 #include "Image/Image.H" 00041 #include "Image/PixelsTypes.H" 00042 #include "Raster/Raster.H" 00043 #include "Util/sformat.H" 00044 00045 #include <QtGui/QApplication> 00046 00047 #include <sys/types.h> 00048 #include <dirent.h> 00049 #include <sys/stat.h> 00050 00051 bool sortChips(const ChipData& d1, const ChipData& d2) 00052 { 00053 return d1.file < d2.file; 00054 } 00055 00056 //! load all chips from a directory 00057 void loadChips(std::vector<ChipData>& vec, const std::string& path, const bool positive) 00058 { 00059 DIR *dir = opendir(path.c_str()); 00060 if (dir == NULL) PLFATAL("Cannot opendir '%s'", path.c_str()); 00061 00062 struct dirent *entry; 00063 while ( (entry = readdir(dir)) ) { 00064 if (entry->d_name[0] == '.') continue; 00065 ChipData cdata; 00066 std::string fullname = sformat("%s/%s", path.c_str(), entry->d_name); 00067 cdata.image = Raster::ReadRGB(fullname); 00068 cdata.file = entry->d_name; 00069 cdata.positive = positive; 00070 00071 vec.push_back(cdata); 00072 } 00073 // Sort the chips by file name - useful if similar files are similarly named 00074 std::sort(vec.begin(),vec.end(),sortChips); 00075 if (closedir(dir)) PLFATAL("Error closing directory '%s'", path.c_str()); 00076 } 00077 00078 //! Chip Validator gui 00079 /*! Directory structure assumes subdirs as follows: 00080 - tp/ true positives 00081 - tn/ true negatives */ 00082 int main(int argc, const char **argv) 00083 { 00084 LOG_FLAGS &= (~LFATAL_XTRA_NOISY); LOG_FLAGS &= (~LFATAL_PRINTS_ABORT); 00085 00086 if (argc != 3) LFATAL("USAGE: %s <indir> <outdir>", argv[0]); 00087 00088 // make sure output directory does not exist: 00089 mode_t mode = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH; 00090 if (mkdir(argv[2], mode)) PLFATAL("Error creating output directory '%s'", argv[2]); 00091 if (mkdir(sformat("%s/tp", argv[2]).c_str(), mode)) PLFATAL("Error creating output directory '%s/tp/'", argv[2]); 00092 if (mkdir(sformat("%s/tn", argv[2]).c_str(), mode)) PLFATAL("Error creating output directory '%s/tn/'", argv[2]); 00093 00094 // load all the input chips: 00095 std::vector<ChipData> chipvec; 00096 LINFO("Loading true positive chips from '%s/tp/' ...", argv[1]); 00097 loadChips(chipvec, sformat("%s/tp", argv[1]), true); 00098 LINFO("Loading true negative chips from '%s/tn/' ...", argv[1]); 00099 loadChips(chipvec, sformat("%s/tn", argv[1]), false); 00100 LINFO(" ... loaded %"ZU" chips in total", chipvec.size()); 00101 00102 // create a QApplication: 00103 LINFO("Starting GUI..."); 00104 int qtargc = 1; const char* qtargv[1] = { "ChipValidatorGui" }; 00105 QApplication a(qtargc, argv2qt(qtargc, qtargv)); 00106 00107 // and a widget: 00108 const Dims griddims(9, 5); 00109 ChipValidatorQt cqt(&a, chipvec, griddims); 00110 cqt.setWindowTitle("iLab USC -- Chip Validator GUI"); 00111 cqt.show(); 00112 00113 // main loop for QApplication: 00114 const int ret = a.exec(); 00115 00116 // save all the chips: 00117 LINFO("Saving %"ZU" validated chips to '%s'", chipvec.size(), argv[2]); 00118 for (size_t i = 0; i < chipvec.size(); ++i) 00119 Raster::WriteRGB(chipvec[i].image, 00120 sformat("%s/%s/%s", argv[2], chipvec[i].positive ? "tp" : "tn", chipvec[i].file.c_str())); 00121 LINFO("All Done."); 00122 00123 // cleanup and exit: 00124 return ret; 00125 }