00001 /*!@file AppDevices/test-HSV.C Test HSV histogramming of framegrabber input */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00005 // 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 <itti@usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/AppDevices/test-HSV.C $ 00035 // $Id: test-HSV.C 8145 2007-03-20 19:48:53Z rjpeters $ 00036 // 00037 00038 #include "Component/ModelManager.H" 00039 #include "Devices/FrameGrabberConfigurator.H" 00040 #include "GUI/XWindow.H" 00041 #include "Image/DrawOps.H" 00042 #include "Image/Image.H" 00043 #include "Image/Pixels.H" 00044 #include "Transport/FrameIstream.H" 00045 #include "Util/Timer.H" 00046 #include "Util/log.H" 00047 00048 #include <cstdio> 00049 #include <cstdlib> 00050 #include <cstring> 00051 00052 #define NAVG 20 00053 00054 /*! This simple executable tests video frame grabbing through the 00055 video4linux driver (see V4Lgrabber.H) or the IEEE1394 (firewire) 00056 grabber (see IEEE1394grabber.H). Selection of the grabber type is 00057 made via the --fg-type=XX command-line option. */ 00058 int main(const int argc, const char **argv) 00059 { 00060 // instantiate a model manager: 00061 ModelManager manager("Frame Grabber Tester"); 00062 00063 // Instantiate our various ModelComponents: 00064 nub::soft_ref<FrameGrabberConfigurator> 00065 gbc(new FrameGrabberConfigurator(manager)); 00066 manager.addSubComponent(gbc); 00067 00068 // Parse command-line: 00069 if (manager.parseCommandLine(argc, argv, "", 0, 0) == false) return(1); 00070 00071 // do post-command-line configs: 00072 nub::soft_ref<FrameIstream> gb = gbc->getFrameGrabber(); 00073 if (gb.isInvalid()) 00074 LFATAL("You need to select a frame grabber type via the " 00075 "--fg-type=XX command-line option for this program " 00076 "to be useful"); 00077 00078 // let's get all our ModelComponent instances started: 00079 manager.start(); 00080 00081 // get ready for main loop: 00082 XWindow win(gb->peekDims(), -1, -1, "test-HSV window"); 00083 XWindow histoH(Dims(425,210), -1, -1, "Histogram Hue"); 00084 XWindow histoS(Dims(425,210), -1, -1, "Histogram Satutation"); 00085 XWindow histoV(Dims(425,210), -1, -1, "Histogram Value"); 00086 00087 int sizeX, sizeY; 00088 int winXleft, winXright, winYtop, winYbottom; 00089 long pixels; 00090 float H,S,V; 00091 //float Hs,Ss,Vs; 00092 float Hmax,Smax,Vmax; 00093 int Hhist[200]; 00094 int Shist[200]; 00095 int Vhist[200]; 00096 00097 PixRGB<float> pix; 00098 Image<PixRGB<byte> > HistoH; 00099 Image<PixRGB<byte> > HistoS; 00100 Image<PixRGB<byte> > HistoV; 00101 while(1) { 00102 HistoH.resize(425,210,true); 00103 HistoS.resize(425,210,true); 00104 HistoV.resize(425,210,true); 00105 00106 for(int i = 0; i < 200 ; i++) 00107 { 00108 Hhist[i] = 0; 00109 Shist[i] = 0; 00110 Vhist[i] = 0; 00111 } 00112 H = 0; S = 0; V = 0; 00113 Hmax = 0; Smax = 0; Vmax = 0; 00114 pixels = 0; 00115 Image< PixRGB<byte> > ima = gb->readRGB(); 00116 sizeX = ima.getWidth(); 00117 sizeY = ima.getHeight(); 00118 winXleft = sizeX/3; 00119 winXright = sizeX - sizeX/3; 00120 winYtop = sizeY/3; 00121 winYbottom = sizeY - sizeY/3; 00122 for(int i = winXleft; i < winXright; i++) 00123 { 00124 for(int j = winYtop; j < winYbottom; j++) 00125 { 00126 float pixH,pixS,pixV; 00127 int doH,doS,doV; 00128 pix = PixRGB<float>(ima.getVal(i,j)); 00129 PixHSV<float>(pix).getHSV(pixH,pixS,pixV); 00130 doH = (int)((pixH/360)*200); 00131 doS = (int)(pixS*200); 00132 doV = (int)((pixV/255)*200); 00133 Hhist[doH]++; 00134 Shist[doS]++; 00135 Vhist[doV]++; 00136 H += pixH; 00137 S += pixS; 00138 V += pixV; 00139 pixels++; 00140 } 00141 } 00142 H = H/pixels; 00143 S = S/pixels; 00144 V = V/pixels; 00145 for(int i = 0; i < 200 ; i++) 00146 { 00147 if(Hhist[i] > Hmax) Hmax = Hhist[i]; 00148 if(Shist[i] > Smax) Smax = Shist[i]; 00149 if(Vhist[i] > Vmax) Vmax = Vhist[i]; 00150 } 00151 for(int i = 0; i < 200 ; i++) 00152 { 00153 drawRect(HistoH, Rectangle::tlbrI(1,((i+1)*2-1),(int)((Hhist[i]/Hmax)*200.0F)+2,((i+1)*2)), 00154 PixRGB<byte>(255,0,0),1); 00155 drawRect(HistoS, Rectangle::tlbrI(1,((i+1)*2-1),(int)((Shist[i]/Smax)*200.0F)+2,((i+1)*2)), 00156 PixRGB<byte>(0,255,0),1); 00157 drawRect(HistoV, Rectangle::tlbrI(1,((i+1)*2-1),(int)((Vhist[i]/Vmax)*200.0F)+2,((i+1)*2)), 00158 PixRGB<byte>(0,0,255),1); 00159 } 00160 drawRect(ima, Rectangle::tlbrI(winYtop,winXleft,winYbottom,winXright), 00161 PixRGB<byte>(255,255,0),1); 00162 LINFO("H %f S %f V %f",H,S,V); 00163 win.drawImage(ima); 00164 histoH.drawImage(HistoH); 00165 histoS.drawImage(HistoS); 00166 histoV.drawImage(HistoV); 00167 } 00168 // stop all our ModelComponents 00169 manager.stop(); 00170 00171 // all done! 00172 return 0; 00173 } 00174 00175 // ###################################################################### 00176 /* So things look consistent in everyone's emacs... */ 00177 /* Local Variables: */ 00178 /* indent-tabs-mode: nil */ 00179 /* End: */