testimg.cpp

Go to the documentation of this file.
00001 /*****************************************************************************
00002  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003 downloading, copying, installing or using the software you agree to this
00004 license. If you do not agree to this license, do not download, install, copy or
00005 use the software.
00006 
00007 Contributors License Agreement
00008 
00009 Copyright© 2007, Akhmed Umyarov. All rights reserved.
00010 
00011 Redistribution and use in source and binary forms, with or without modification,
00012 are permitted provided that the following conditions are met:
00013 - Redistributions of source code must retain the above copyright notice, this
00014 list of conditions and the following disclaimer.
00015 - Redistributions in binary form must reproduce the above copyright notice, this
00016 list of conditions and the following disclaimer in the documentation and/or
00017 other materials provided with the distribution.
00018 - The name of Contributor may not be used to endorse or promote products derived
00019 from this software without specific prior written permission.
00020 
00021 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00022 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00023 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00024 DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
00025 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00027 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
00029 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00030 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 All information provided related to future Intel products and plans is
00032 preliminary and subject to change at any time, without notice.
00033 *****************************************************************************/
00034 
00041 #include "cvconvnet.h"
00042 #include <opencv/highgui.h> 
00043 #include <iostream>
00044 #include <fstream>
00045 #include <sstream>
00046 
00047 using namespace std;
00048 
00060 int main(int argc, char *argv[])
00061 {
00062         if (argc <=2 )
00063         {
00064                 cerr << "Usage: " << endl << "\ttestimg <network.xml> <imagefile(s)>" << endl;
00065                 return 1;
00066         }
00067 
00068         // Create empty net object
00069         CvConvNet net;
00070 
00071         // Source featuremap size
00072         CvSize inputsz = cvSize(32,32);
00073 
00074         // Load mnist.xml file into a std::string called xml
00075         ifstream ifs(argv[1]);
00076         string xml ( (istreambuf_iterator<char> (ifs)) , istreambuf_iterator<char>() );
00077         
00078         // Create network from XML string
00079         if ( !net.fromString(xml) )
00080         {
00081                 cerr << "*** ERROR: Can't load net from XML" << endl << "Check file "<< argv[1] << endl;
00082                 return 1;
00083         }
00084 
00085         // create some GUI
00086         cvNamedWindow("Image", CV_WINDOW_AUTOSIZE); 
00087         cvMoveWindow("Image", inputsz.height, inputsz.width);
00088         CvFont font;
00089         cvInitFont(&font, CV_FONT_HERSHEY_PLAIN, 1.0, 1.0);
00090 
00091         // Grayscale img pointer
00092         IplImage* img;
00093 
00094         // Also create a color image (for display)
00095         IplImage *colorimg = cvCreateImage( inputsz, IPL_DEPTH_8U, 3 );
00096 
00097         // Cycle over input images
00098         for (int i=2; i<argc; i++)
00099         {
00100                 // Load the image
00101                 if ((img = cvLoadImage( argv[i], CV_LOAD_IMAGE_GRAYSCALE )) == NULL)
00102                 {
00103                         cerr << "ERROR: Bad image file: " << argv[i] << endl; 
00104                         break;
00105                 }
00106 
00107                 // Forward propagate the grayscale (8 bit) image and get the value
00108                 ostringstream val;
00109                 val << (int) net.fprop(img);
00110 
00111                 // Make image colorful
00112                 cvCvtColor(img,colorimg,CV_GRAY2RGB);
00113 
00114                 // Draw green text for the recognized number on top of the image
00115                 cvPutText(colorimg, val.str().c_str(), cvPoint(0,inputsz.height/2), &font, CV_RGB(0,255,0));
00116 
00117                 // show the image
00118                 cvShowImage("Image", colorimg );
00119                 cvWaitKey(1000);
00120                 
00121                 cvReleaseImage(&img);
00122         }
00123         // Free buffers
00124         cvReleaseImage(&colorimg);
00125 
00126         return 0;
00127 }

Generated on Fri Aug 3 16:17:27 2007 for ConvNet by  doxygen 1.5.0