<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html><head><meta name="robots" content="noindex"> <meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>ITK: itkTestMain.h Source File</title> <link href="DoxygenStyle.css" rel="stylesheet" type="text/css"> </head><body bgcolor="#ffffff"> <!-- Section customized for INSIGHT : Tue Jul 17 01:02:45 2001 --> <center> <a href="index.html" class="qindex">Main Page</a> <a href="modules.html" class="qindex">Groups</a> <a href="namespaces.html" class="qindex">Namespace List</a> <a href="hierarchy.html" class="qindex">Class Hierarchy</a> <a href="classes.html" class="qindex">Alphabetical List</a> <a href="annotated.html" class="qindex">Compound List</a> <a href="files.html" class="qindex">File List</a> <a href="namespacemembers.html" class="qindex">Namespace Members</a> <a href="functions.html" class="qindex">Compound Members</a> <a href="globals.html" class="qindex">File Members</a> <a href="pages.html" class="qindex">Concepts</a></center> <!-- Generated by Doxygen 1.5.9 --> <div class="navpath"><a class="el" href="dir_02e23d841c198393a6e961e9a0678fff.html">Code</a> » <a class="el" href="dir_ea7fc529ede9a38b0438cf13831ae873.html">Common</a> </div> <div class="contents"> <h1>itkTestMain.h</h1><a href="itkTestMain_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*=========================================================================</span> <a name="l00002"></a>00002 <span class="comment"></span> <a name="l00003"></a>00003 <span class="comment"> Program: Insight Segmentation & Registration Toolkit</span> <a name="l00004"></a>00004 <span class="comment"> Module: $RCSfile: itkTestMain.h,v $</span> <a name="l00005"></a>00005 <span class="comment"> Language: C++</span> <a name="l00006"></a>00006 <span class="comment"> Date: $Date: 2008-11-09 18:18:51 $</span> <a name="l00007"></a>00007 <span class="comment"> Version: $Revision: 1.32 $</span> <a name="l00008"></a>00008 <span class="comment"></span> <a name="l00009"></a>00009 <span class="comment"> Copyright (c) Insight Software Consortium. All rights reserved.</span> <a name="l00010"></a>00010 <span class="comment"> See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.</span> <a name="l00011"></a>00011 <span class="comment"></span> <a name="l00012"></a>00012 <span class="comment"> Portions of this code are covered under the VTK copyright.</span> <a name="l00013"></a>00013 <span class="comment"> See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details.</span> <a name="l00014"></a>00014 <span class="comment"></span> <a name="l00015"></a>00015 <span class="comment"> This software is distributed WITHOUT ANY WARRANTY; without even </span> <a name="l00016"></a>00016 <span class="comment"> the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR </span> <a name="l00017"></a>00017 <span class="comment"> PURPOSE. See the above copyright notices for more information.</span> <a name="l00018"></a>00018 <span class="comment"></span> <a name="l00019"></a>00019 <span class="comment">=========================================================================*/</span> <a name="l00020"></a>00020 <a name="l00021"></a>00021 <span class="comment">// This file is used to create TestDriver executables</span> <a name="l00022"></a>00022 <span class="comment">// These executables are able to register a function pointer to a string name</span> <a name="l00023"></a>00023 <span class="comment">// in a lookup table. By including this file, it creates a main function</span> <a name="l00024"></a>00024 <span class="comment">// that calls RegisterTests() then looks up the function pointer for the test</span> <a name="l00025"></a>00025 <span class="comment">// specified on the command line.</span> <a name="l00026"></a>00026 <span class="preprocessor">#include "<a class="code" href="itkWin32Header_8h.html">itkWin32Header.h</a>"</span> <a name="l00027"></a>00027 <span class="preprocessor">#include <map></span> <a name="l00028"></a>00028 <span class="preprocessor">#include <string></span> <a name="l00029"></a>00029 <span class="preprocessor">#include <iostream></span> <a name="l00030"></a>00030 <span class="preprocessor">#include <fstream></span> <a name="l00031"></a>00031 <span class="preprocessor">#include "itkNumericTraits.h"</span> <a name="l00032"></a>00032 <span class="preprocessor">#include "<a class="code" href="itkMultiThreader_8h.html">itkMultiThreader.h</a>"</span> <a name="l00033"></a>00033 <span class="preprocessor">#include "<a class="code" href="itkImage_8h.html">itkImage.h</a>"</span> <a name="l00034"></a>00034 <span class="preprocessor">#include "<a class="code" href="itkImageFileReader_8h.html">itkImageFileReader.h</a>"</span> <a name="l00035"></a>00035 <span class="preprocessor">#include "<a class="code" href="itkImageFileWriter_8h.html">itkImageFileWriter.h</a>"</span> <a name="l00036"></a>00036 <span class="preprocessor">#include "<a class="code" href="itkImageRegionConstIterator_8h.html">itkImageRegionConstIterator.h</a>"</span> <a name="l00037"></a>00037 <span class="preprocessor">#include "<a class="code" href="itkSubtractImageFilter_8h.html">itkSubtractImageFilter.h</a>"</span> <a name="l00038"></a>00038 <span class="preprocessor">#include "<a class="code" href="itkRescaleIntensityImageFilter_8h.html">itkRescaleIntensityImageFilter.h</a>"</span> <a name="l00039"></a>00039 <span class="preprocessor">#include "<a class="code" href="itkExtractImageFilter_8h.html">itkExtractImageFilter.h</a>"</span> <a name="l00040"></a>00040 <span class="preprocessor">#include "<a class="code" href="itkDifferenceImageFilter_8h.html">itkDifferenceImageFilter.h</a>"</span> <a name="l00041"></a>00041 <span class="preprocessor">#include "<a class="code" href="itkImageRegion_8h.html">itkImageRegion.h</a>"</span> <a name="l00042"></a>00042 <span class="preprocessor">#include "itksys/SystemTools.hxx"</span> <a name="l00043"></a>00043 <a name="l00044"></a><a class="code" href="itkTestMain_8h.html#5ae87fb2158f30c533661f95f07f4fae">00044</a> <span class="preprocessor">#define ITK_TEST_DIMENSION_MAX 6</span> <a name="l00045"></a>00045 <span class="preprocessor"></span> <a name="l00046"></a><a class="code" href="itkTestMain_8h.html#18b51f0919f129b467617d26958408c9">00046</a> <span class="keyword">typedef</span> int (*<a class="code" href="itkTestMain_8h.html#18b51f0919f129b467617d26958408c9">MainFuncPointer</a>)(int , <span class="keywordtype">char</span>* [] ); <a name="l00047"></a><a class="code" href="itkTestMain_8h.html#11204a5feee4ef83b8db406514cef2ff">00047</a> std::map<std::string, MainFuncPointer> <a class="code" href="itkTestMain_8h.html#11204a5feee4ef83b8db406514cef2ff">StringToTestFunctionMap</a>; <a name="l00048"></a>00048 <a name="l00049"></a><a class="code" href="itkTestMain_8h.html#9df66dcc0f3619dc87c8fa8edb356434">00049</a> <span class="preprocessor">#define REGISTER_TEST(test) \</span> <a name="l00050"></a>00050 <span class="preprocessor">extern int test(int, char* [] ); \</span> <a name="l00051"></a>00051 <span class="preprocessor">StringToTestFunctionMap[#test] = test</span> <a name="l00052"></a>00052 <span class="preprocessor"></span> <a name="l00053"></a>00053 <span class="keywordtype">int</span> <a class="code" href="itkTestMain_8h.html#f8dc81600084942e2002871d43189e18">RegressionTestImage</a> (<span class="keyword">const</span> <span class="keywordtype">char</span> *testImageFilename, <a name="l00054"></a>00054 <span class="keyword">const</span> <span class="keywordtype">char</span> *baselineImageFilename, <a name="l00055"></a>00055 <span class="keywordtype">int</span> reportErrors, <a name="l00056"></a>00056 <span class="keywordtype">double</span> intensityTolerance = 2.0, <a name="l00057"></a>00057 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> numberOfPixelsTolerance = 0, <a name="l00058"></a>00058 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> radiusTolerance = 0); <a name="l00059"></a>00059 <a name="l00060"></a>00060 std::map<std::string,int> <a class="code" href="itkTestMain_8h.html#7d92f4ecc3fe3b5ee5e4f44ebf769434">RegressionTestBaselines</a> (<span class="keywordtype">char</span> *); <a name="l00061"></a>00061 <a name="l00062"></a>00062 <span class="keywordtype">void</span> <a class="code" href="itkTestMain_8h.html#d7b8501e8086a10448dba6ac0d0d448c">RegisterTests</a>(); <a name="l00063"></a><a class="code" href="itkTestMain_8h.html#82c7008f0492455d1a90cd5b0982bf38">00063</a> <span class="keywordtype">void</span> <a class="code" href="itkTestMain_8h.html#82c7008f0492455d1a90cd5b0982bf38">PrintAvailableTests</a>() <a name="l00064"></a>00064 { <a name="l00065"></a>00065 std::cout << <span class="stringliteral">"Available tests:\n"</span>; <a name="l00066"></a>00066 std::map<std::string, MainFuncPointer>::iterator j = <a class="code" href="itkTestMain_8h.html#11204a5feee4ef83b8db406514cef2ff">StringToTestFunctionMap</a>.begin(); <a name="l00067"></a>00067 <span class="keywordtype">int</span> i = 0; <a name="l00068"></a>00068 <span class="keywordflow">while</span>(j != <a class="code" href="itkTestMain_8h.html#11204a5feee4ef83b8db406514cef2ff">StringToTestFunctionMap</a>.end()) <a name="l00069"></a>00069 { <a name="l00070"></a>00070 std::cout << i << <span class="stringliteral">". "</span> << j->first << <span class="stringliteral">"\n"</span>; <a name="l00071"></a>00071 ++i; <a name="l00072"></a>00072 ++j; <a name="l00073"></a>00073 } <a name="l00074"></a>00074 } <a name="l00075"></a>00075 <a name="l00076"></a><a class="code" href="itkTestMain_8h.html#dacbe0175a79dff748855d8c9839f82b">00076</a> <span class="keywordtype">int</span> <a class="code" href="itkTestMain_8h.html#dacbe0175a79dff748855d8c9839f82b">main</a>(<span class="keywordtype">int</span> ac, <span class="keywordtype">char</span>* av[] ) <a name="l00077"></a>00077 { <a name="l00078"></a>00078 <span class="keywordtype">double</span> intensityTolerance = 2.0; <a name="l00079"></a>00079 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> numberOfPixelsTolerance = 0; <a name="l00080"></a>00080 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> radiusTolerance = 0; <a name="l00081"></a>00081 <a name="l00082"></a>00082 <span class="keyword">typedef</span> std::pair< char *, char *> ComparePairType; <a name="l00083"></a>00083 std::vector< ComparePairType > compareList; <a name="l00084"></a>00084 <a name="l00085"></a>00085 <a class="code" href="itkTestMain_8h.html#d7b8501e8086a10448dba6ac0d0d448c">RegisterTests</a>(); <a name="l00086"></a>00086 std::string testToRun; <a name="l00087"></a>00087 <span class="keywordflow">if</span>(ac < 2) <a name="l00088"></a>00088 { <a name="l00089"></a>00089 <a class="code" href="itkTestMain_8h.html#82c7008f0492455d1a90cd5b0982bf38">PrintAvailableTests</a>(); <a name="l00090"></a>00090 std::cout << <span class="stringliteral">"To run a test, enter the test number: "</span>; <a name="l00091"></a>00091 <span class="keywordtype">int</span> testNum = 0; <a name="l00092"></a>00092 std::cin >> testNum; <a name="l00093"></a>00093 std::map<std::string, MainFuncPointer>::iterator j = <a class="code" href="itkTestMain_8h.html#11204a5feee4ef83b8db406514cef2ff">StringToTestFunctionMap</a>.begin(); <a name="l00094"></a>00094 <span class="keywordtype">int</span> i = 0; <a name="l00095"></a>00095 <span class="keywordflow">while</span>(j != <a class="code" href="itkTestMain_8h.html#11204a5feee4ef83b8db406514cef2ff">StringToTestFunctionMap</a>.end() && i < testNum) <a name="l00096"></a>00096 { <a name="l00097"></a>00097 ++i; <a name="l00098"></a>00098 ++j; <a name="l00099"></a>00099 } <a name="l00100"></a>00100 <span class="keywordflow">if</span>(j == <a class="code" href="itkTestMain_8h.html#11204a5feee4ef83b8db406514cef2ff">StringToTestFunctionMap</a>.end()) <a name="l00101"></a>00101 { <a name="l00102"></a>00102 std::cerr << testNum << <span class="stringliteral">" is an invalid test number\n"</span>; <a name="l00103"></a>00103 <span class="keywordflow">return</span> -1; <a name="l00104"></a>00104 } <a name="l00105"></a>00105 testToRun = j->first; <a name="l00106"></a>00106 } <a name="l00107"></a>00107 <span class="keywordflow">else</span> <a name="l00108"></a>00108 { <a name="l00109"></a>00109 <span class="keywordflow">while</span>( ac > 0 && testToRun.empty() ) <a name="l00110"></a>00110 { <a name="l00111"></a>00111 <span class="keywordflow">if</span> (strcmp(av[1], <span class="stringliteral">"--with-threads"</span>) == 0) <a name="l00112"></a>00112 { <a name="l00113"></a>00113 <span class="keywordtype">int</span> numThreads = atoi(av[2]); <a name="l00114"></a>00114 <a class="code" href="classitk_1_1MultiThreader.html#de079d8e1c3581dc2ae5353aae5577a0">itk::MultiThreader::SetGlobalDefaultNumberOfThreads</a>(numThreads); <a name="l00115"></a>00115 av += 2; <a name="l00116"></a>00116 ac -= 2; <a name="l00117"></a>00117 } <a name="l00118"></a>00118 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strcmp(av[1], <span class="stringliteral">"--without-threads"</span>) == 0) <a name="l00119"></a>00119 { <a name="l00120"></a>00120 <a class="code" href="classitk_1_1MultiThreader.html#de079d8e1c3581dc2ae5353aae5577a0">itk::MultiThreader::SetGlobalDefaultNumberOfThreads</a>(1); <a name="l00121"></a>00121 av += 1; <a name="l00122"></a>00122 ac -= 1; <a name="l00123"></a>00123 } <a name="l00124"></a>00124 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (ac > 3 && strcmp(av[1], <span class="stringliteral">"--compare"</span>) == 0) <a name="l00125"></a>00125 { <a name="l00126"></a>00126 compareList.push_back( ComparePairType( av[2], av[3] ) ); <a name="l00127"></a>00127 av += 3; <a name="l00128"></a>00128 ac -= 3; <a name="l00129"></a>00129 } <a name="l00130"></a>00130 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (ac > 2 && strcmp(av[1], <span class="stringliteral">"--compareNumberOfPixelsTolerance"</span>) == 0) <a name="l00131"></a>00131 { <a name="l00132"></a>00132 numberOfPixelsTolerance = atoi( av[2] ); <a name="l00133"></a>00133 av += 2; <a name="l00134"></a>00134 ac -= 2; <a name="l00135"></a>00135 } <a name="l00136"></a>00136 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (ac > 2 && strcmp(av[1], <span class="stringliteral">"--compareRadiusTolerance"</span>) == 0) <a name="l00137"></a>00137 { <a name="l00138"></a>00138 radiusTolerance = atoi( av[2] ); <a name="l00139"></a>00139 av += 2; <a name="l00140"></a>00140 ac -= 2; <a name="l00141"></a>00141 } <a name="l00142"></a>00142 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (ac > 2 && strcmp(av[1], <span class="stringliteral">"--compareIntensityTolerance"</span>) == 0) <a name="l00143"></a>00143 { <a name="l00144"></a>00144 intensityTolerance = atof( av[2] ); <a name="l00145"></a>00145 av += 2; <a name="l00146"></a>00146 ac -= 2; <a name="l00147"></a>00147 } <a name="l00148"></a>00148 <span class="keywordflow">else</span> <a name="l00149"></a>00149 { <a name="l00150"></a>00150 testToRun = av[1]; <a name="l00151"></a>00151 } <a name="l00152"></a>00152 } <a name="l00153"></a>00153 } <a name="l00154"></a>00154 std::map<std::string, MainFuncPointer>::iterator j = <a class="code" href="itkTestMain_8h.html#11204a5feee4ef83b8db406514cef2ff">StringToTestFunctionMap</a>.find(testToRun); <a name="l00155"></a>00155 <span class="keywordflow">if</span>(j != <a class="code" href="itkTestMain_8h.html#11204a5feee4ef83b8db406514cef2ff">StringToTestFunctionMap</a>.end()) <a name="l00156"></a>00156 { <a name="l00157"></a>00157 <a class="code" href="itkTestMain_8h.html#18b51f0919f129b467617d26958408c9">MainFuncPointer</a> f = j->second; <a name="l00158"></a>00158 <span class="keywordtype">int</span> result; <a name="l00159"></a>00159 <span class="keywordflow">try</span> <a name="l00160"></a>00160 { <a name="l00161"></a>00161 <span class="comment">// Invoke the test's "main" function.</span> <a name="l00162"></a>00162 result = (*f)(ac-1, av+1); <a name="l00163"></a>00163 <a name="l00164"></a>00164 <span class="comment">// Make a list of possible baselines</span> <a name="l00165"></a>00165 <span class="keywordflow">for</span>( <span class="keywordtype">int</span> i=0; i<static_cast<int>(compareList.size()); i++) <a name="l00166"></a>00166 { <a name="l00167"></a>00167 <span class="keywordtype">char</span> * baselineFilename = compareList[i].first; <a name="l00168"></a>00168 <span class="keywordtype">char</span> * testFilename = compareList[i].second; <a name="l00169"></a>00169 std::map<std::string,int> baselines = <a class="code" href="itkTestMain_8h.html#7d92f4ecc3fe3b5ee5e4f44ebf769434">RegressionTestBaselines</a>(baselineFilename); <a name="l00170"></a>00170 std::map<std::string,int>::iterator baseline = baselines.begin(); <a name="l00171"></a>00171 std::string bestBaseline; <a name="l00172"></a>00172 <span class="keywordtype">int</span> bestBaselineStatus = itk::NumericTraits<int>::max(); <a name="l00173"></a>00173 <span class="keywordflow">while</span> (baseline != baselines.end()) <a name="l00174"></a>00174 { <a name="l00175"></a>00175 baseline->second = <a class="code" href="itkTestMain_8h.html#f8dc81600084942e2002871d43189e18">RegressionTestImage</a>(testFilename, <a name="l00176"></a>00176 (baseline->first).c_str(), <a name="l00177"></a>00177 0, <a name="l00178"></a>00178 intensityTolerance, <a name="l00179"></a>00179 numberOfPixelsTolerance, <a name="l00180"></a>00180 radiusTolerance ); <a name="l00181"></a>00181 <span class="keywordflow">if</span> (baseline->second < bestBaselineStatus) <a name="l00182"></a>00182 { <a name="l00183"></a>00183 bestBaseline = baseline->first; <a name="l00184"></a>00184 bestBaselineStatus = baseline->second; <a name="l00185"></a>00185 } <a name="l00186"></a>00186 <span class="keywordflow">if</span> (baseline->second == 0) <a name="l00187"></a>00187 { <a name="l00188"></a>00188 <span class="keywordflow">break</span>; <a name="l00189"></a>00189 } <a name="l00190"></a>00190 ++baseline; <a name="l00191"></a>00191 } <a name="l00192"></a>00192 <span class="comment">// if the best we can do still has errors, generate the error images</span> <a name="l00193"></a>00193 <span class="keywordflow">if</span> (bestBaselineStatus) <a name="l00194"></a>00194 { <a name="l00195"></a>00195 <a class="code" href="itkTestMain_8h.html#f8dc81600084942e2002871d43189e18">RegressionTestImage</a>(testFilename, <a name="l00196"></a>00196 bestBaseline.c_str(), <a name="l00197"></a>00197 1, <a name="l00198"></a>00198 intensityTolerance, <a name="l00199"></a>00199 numberOfPixelsTolerance, <a name="l00200"></a>00200 radiusTolerance ); <a name="l00201"></a>00201 } <a name="l00202"></a>00202 <a name="l00203"></a>00203 <span class="comment">// output the matching baseline</span> <a name="l00204"></a>00204 std::cout << <span class="stringliteral">"<DartMeasurement name=\"BaselineImageName\" type=\"text/string\">"</span>; <a name="l00205"></a>00205 std::cout << itksys::SystemTools::GetFilenameName(bestBaseline); <a name="l00206"></a>00206 std::cout << <span class="stringliteral">"</DartMeasurement>"</span> << std::endl; <a name="l00207"></a>00207 <a name="l00208"></a>00208 result += bestBaselineStatus; <a name="l00209"></a>00209 } <a name="l00210"></a>00210 } <a name="l00211"></a>00211 <span class="keywordflow">catch</span>(<span class="keyword">const</span> itk::ExceptionObject& e) <a name="l00212"></a>00212 { <a name="l00213"></a>00213 std::cerr << <span class="stringliteral">"ITK test driver caught an ITK exception:\n"</span>; <a name="l00214"></a>00214 e.Print(std::cerr); <a name="l00215"></a>00215 result = -1; <a name="l00216"></a>00216 } <a name="l00217"></a>00217 <span class="keywordflow">catch</span>(<span class="keyword">const</span> std::exception& e) <a name="l00218"></a>00218 { <a name="l00219"></a>00219 std::cerr << <span class="stringliteral">"ITK test driver caught an exception:\n"</span>; <a name="l00220"></a>00220 std::cerr << e.what() << <span class="stringliteral">"\n"</span>; <a name="l00221"></a>00221 result = -1; <a name="l00222"></a>00222 } <a name="l00223"></a>00223 <span class="keywordflow">catch</span>(...) <a name="l00224"></a>00224 { <a name="l00225"></a>00225 std::cerr << <span class="stringliteral">"ITK test driver caught an unknown exception!!!\n"</span>; <a name="l00226"></a>00226 result = -1; <a name="l00227"></a>00227 } <a name="l00228"></a>00228 <span class="keywordflow">return</span> result; <a name="l00229"></a>00229 } <a name="l00230"></a>00230 <a class="code" href="itkTestMain_8h.html#82c7008f0492455d1a90cd5b0982bf38">PrintAvailableTests</a>(); <a name="l00231"></a>00231 std::cerr << <span class="stringliteral">"Failed: "</span> << testToRun << <span class="stringliteral">": No test registered with name "</span> << testToRun << <span class="stringliteral">"\n"</span>; <a name="l00232"></a>00232 <span class="keywordflow">return</span> -1; <a name="l00233"></a>00233 } <a name="l00234"></a>00234 <a name="l00235"></a>00235 <span class="comment">// Regression Testing Code</span> <a name="l00236"></a>00236 <a name="l00237"></a><a class="code" href="itkTestMain_8h.html#f8dc81600084942e2002871d43189e18">00237</a> <span class="keywordtype">int</span> <a class="code" href="itkTestMain_8h.html#f8dc81600084942e2002871d43189e18">RegressionTestImage</a> (<span class="keyword">const</span> <span class="keywordtype">char</span> *testImageFilename, <a name="l00238"></a>00238 <span class="keyword">const</span> <span class="keywordtype">char</span> *baselineImageFilename, <a name="l00239"></a>00239 <span class="keywordtype">int</span> reportErrors, <a name="l00240"></a>00240 <span class="keywordtype">double</span> intensityTolerance, <a name="l00241"></a>00241 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> numberOfPixelsTolerance, <a name="l00242"></a>00242 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> radiusTolerance ) <a name="l00243"></a>00243 { <a name="l00244"></a>00244 <span class="comment">// Use the factory mechanism to read the test and baseline files and convert them to double</span> <a name="l00245"></a>00245 <span class="keyword">typedef</span> <a class="code" href="classitk_1_1Image.html" title="Templated n-dimensional image class.">itk::Image<double,ITK_TEST_DIMENSION_MAX></a> ImageType; <a name="l00246"></a>00246 <span class="keyword">typedef</span> <a class="code" href="classitk_1_1Image.html" title="Templated n-dimensional image class.">itk::Image<unsigned char,ITK_TEST_DIMENSION_MAX></a> OutputType; <a name="l00247"></a>00247 <span class="keyword">typedef</span> <a class="code" href="classitk_1_1Image.html" title="Templated n-dimensional image class.">itk::Image<unsigned char,2></a> DiffOutputType; <a name="l00248"></a>00248 <span class="keyword">typedef</span> <a class="code" href="classitk_1_1ImageFileReader.html" title="Data source that reads image data from a single file.">itk::ImageFileReader<ImageType></a> ReaderType; <a name="l00249"></a>00249 <a name="l00250"></a>00250 <span class="comment">// Read the baseline file</span> <a name="l00251"></a>00251 <a class="code" href="itkFEMMacro_8h.html#539cce1a3282ba59952dedcbf9cdb23f">ReaderType::Pointer</a> baselineReader = <a class="code" href="namespaceHardConnectedComponentImageFilter.html#870262f145e0b45206db74df8053b59c">ReaderType::New</a>(); <a name="l00252"></a>00252 baselineReader->SetFileName(baselineImageFilename); <a name="l00253"></a>00253 <span class="keywordflow">try</span> <a name="l00254"></a>00254 { <a name="l00255"></a>00255 baselineReader->UpdateLargestPossibleRegion(); <a name="l00256"></a>00256 } <a name="l00257"></a>00257 <span class="keywordflow">catch</span> (itk::ExceptionObject& e) <a name="l00258"></a>00258 { <a name="l00259"></a>00259 std::cerr << <span class="stringliteral">"Exception detected while reading "</span> << baselineImageFilename << <span class="stringliteral">" : "</span> << e.GetDescription(); <a name="l00260"></a>00260 <span class="keywordflow">return</span> 1000; <a name="l00261"></a>00261 } <a name="l00262"></a>00262 <a name="l00263"></a>00263 <span class="comment">// Read the file generated by the test</span> <a name="l00264"></a>00264 <a class="code" href="itkFEMMacro_8h.html#539cce1a3282ba59952dedcbf9cdb23f">ReaderType::Pointer</a> testReader = <a class="code" href="namespaceHardConnectedComponentImageFilter.html#870262f145e0b45206db74df8053b59c">ReaderType::New</a>(); <a name="l00265"></a>00265 testReader->SetFileName(testImageFilename); <a name="l00266"></a>00266 <span class="keywordflow">try</span> <a name="l00267"></a>00267 { <a name="l00268"></a>00268 testReader->UpdateLargestPossibleRegion(); <a name="l00269"></a>00269 } <a name="l00270"></a>00270 <span class="keywordflow">catch</span> (itk::ExceptionObject& e) <a name="l00271"></a>00271 { <a name="l00272"></a>00272 std::cerr << <span class="stringliteral">"Exception detected while reading "</span> << testImageFilename << <span class="stringliteral">" : "</span> << e.GetDescription() << std::endl; <a name="l00273"></a>00273 <span class="keywordflow">return</span> 1000; <a name="l00274"></a>00274 } <a name="l00275"></a>00275 <a name="l00276"></a>00276 <span class="comment">// The sizes of the baseline and test image must match</span> <a name="l00277"></a>00277 <a class="code" href="namespaceHardConnectedComponentImageFilter.html#5bf82236e1e50e653de84c8ead448599">ImageType::SizeType</a> baselineSize; <a name="l00278"></a>00278 baselineSize = baselineReader->GetOutput()->GetLargestPossibleRegion().GetSize(); <a name="l00279"></a>00279 <a class="code" href="namespaceHardConnectedComponentImageFilter.html#5bf82236e1e50e653de84c8ead448599">ImageType::SizeType</a> testSize; <a name="l00280"></a>00280 testSize = testReader->GetOutput()->GetLargestPossibleRegion().GetSize(); <a name="l00281"></a>00281 <a name="l00282"></a>00282 <span class="keywordflow">if</span> (baselineSize != testSize) <a name="l00283"></a>00283 { <a name="l00284"></a>00284 std::cerr << <span class="stringliteral">"The size of the Baseline image and Test image do not match!"</span> << std::endl; <a name="l00285"></a>00285 std::cerr << <span class="stringliteral">"Baseline image: "</span> << baselineImageFilename <a name="l00286"></a>00286 << <span class="stringliteral">" has size "</span> << baselineSize << std::endl; <a name="l00287"></a>00287 std::cerr << <span class="stringliteral">"Test image: "</span> << testImageFilename <a name="l00288"></a>00288 << <span class="stringliteral">" has size "</span> << testSize << std::endl; <a name="l00289"></a>00289 <span class="keywordflow">return</span> 1; <a name="l00290"></a>00290 } <a name="l00291"></a>00291 <a name="l00292"></a>00292 <span class="comment">// Now compare the two images</span> <a name="l00293"></a>00293 <span class="keyword">typedef</span> <a class="code" href="classitk_1_1DifferenceImageFilter.html" title="Implements comparison between two images.">itk::DifferenceImageFilter<ImageType,ImageType></a> DiffType; <a name="l00294"></a>00294 <a class="code" href="itkFEMMacro_8h.html#539cce1a3282ba59952dedcbf9cdb23f">DiffType::Pointer</a> diff = <a class="code" href="namespaceHardConnectedComponentImageFilter.html#870262f145e0b45206db74df8053b59c">DiffType::New</a>(); <a name="l00295"></a>00295 diff->SetValidInput(baselineReader->GetOutput()); <a name="l00296"></a>00296 diff->SetTestInput(testReader->GetOutput()); <a name="l00297"></a>00297 diff->SetDifferenceThreshold( intensityTolerance ); <a name="l00298"></a>00298 diff->SetToleranceRadius( radiusTolerance ); <a name="l00299"></a>00299 diff->UpdateLargestPossibleRegion(); <a name="l00300"></a>00300 <a name="l00301"></a>00301 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> status = 0; <a name="l00302"></a>00302 status = diff->GetNumberOfPixelsWithDifferences(); <a name="l00303"></a>00303 <a name="l00304"></a>00304 <span class="comment">// if there are discrepencies, create an diff image</span> <a name="l00305"></a>00305 <span class="keywordflow">if</span> ( (status > numberOfPixelsTolerance) && reportErrors ) <a name="l00306"></a>00306 { <a name="l00307"></a>00307 <span class="keyword">typedef</span> <a class="code" href="classitk_1_1RescaleIntensityImageFilter.html" title="Applies a linear transformation to the intensity levels of the input Image.">itk::RescaleIntensityImageFilter<ImageType,OutputType></a> RescaleType; <a name="l00308"></a>00308 <span class="keyword">typedef</span> <a class="code" href="classitk_1_1ExtractImageFilter.html" title="Decrease the image size by cropping the image to the selected region bounds.">itk::ExtractImageFilter<OutputType,DiffOutputType></a> ExtractType; <a name="l00309"></a>00309 <span class="keyword">typedef</span> <a class="code" href="classitk_1_1ImageFileWriter.html" title="Writes image data to a single file.">itk::ImageFileWriter<DiffOutputType></a> WriterType; <a name="l00310"></a>00310 <span class="keyword">typedef</span> <a class="code" href="classitk_1_1ImageRegion.html" title="An image region represents a structured region of data.">itk::ImageRegion<ITK_TEST_DIMENSION_MAX></a> <a class="code" href="namespaceHardConnectedComponentImageFilter.html#ed8c4a40607d317c666eb96ea265e126">RegionType</a>; <a name="l00311"></a>00311 <a class="code" href="namespaceHardConnectedComponentImageFilter.html#5bf82236e1e50e653de84c8ead448599">OutputType::SizeType</a> size; size.Fill(0); <a name="l00312"></a>00312 <a name="l00313"></a>00313 <a class="code" href="itkFEMMacro_8h.html#539cce1a3282ba59952dedcbf9cdb23f">RescaleType::Pointer</a> rescale = <a class="code" href="namespaceHardConnectedComponentImageFilter.html#870262f145e0b45206db74df8053b59c">RescaleType::New</a>(); <a name="l00314"></a>00314 rescale->SetOutputMinimum(itk::NumericTraits<unsigned char>::NonpositiveMin()); <a name="l00315"></a>00315 rescale->SetOutputMaximum(itk::NumericTraits<unsigned char>::max()); <a name="l00316"></a>00316 rescale->SetInput(diff->GetOutput()); <a name="l00317"></a>00317 rescale->UpdateLargestPossibleRegion(); <a name="l00318"></a>00318 size = rescale->GetOutput()->GetLargestPossibleRegion().GetSize(); <a name="l00319"></a>00319 <a name="l00320"></a>00320 <span class="comment">//Get the center slice of the image, In 3D, the first slice</span> <a name="l00321"></a>00321 <span class="comment">//is often a black slice with little debugging information.</span> <a name="l00322"></a>00322 <a class="code" href="namespaceHardConnectedComponentImageFilter.html#be32dc19e71d40322ad9828b9ab40853">OutputType::IndexType</a> index; index.Fill(0); <a name="l00323"></a>00323 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i = 2; i < <a class="code" href="itkTestMain_8h.html#5ae87fb2158f30c533661f95f07f4fae">ITK_TEST_DIMENSION_MAX</a>; i++) <a name="l00324"></a>00324 { <a name="l00325"></a>00325 index[i]=size[i]/2;<span class="comment">//NOTE: Integer Divide used to get approximately the center slice</span> <a name="l00326"></a>00326 size[i] = 0; <a name="l00327"></a>00327 } <a name="l00328"></a>00328 <a name="l00329"></a>00329 RegionType region; <a name="l00330"></a>00330 region.SetIndex(index); <a name="l00331"></a>00331 <a name="l00332"></a>00332 region.SetSize(size); <a name="l00333"></a>00333 <a name="l00334"></a>00334 <a class="code" href="itkFEMMacro_8h.html#539cce1a3282ba59952dedcbf9cdb23f">ExtractType::Pointer</a> extract = <a class="code" href="namespaceHardConnectedComponentImageFilter.html#870262f145e0b45206db74df8053b59c">ExtractType::New</a>(); <a name="l00335"></a>00335 extract->SetInput(rescale->GetOutput()); <a name="l00336"></a>00336 extract->SetExtractionRegion(region); <a name="l00337"></a>00337 <a name="l00338"></a>00338 <a class="code" href="itkFEMMacro_8h.html#539cce1a3282ba59952dedcbf9cdb23f">WriterType::Pointer</a> writer = <a class="code" href="namespaceHardConnectedComponentImageFilter.html#870262f145e0b45206db74df8053b59c">WriterType::New</a>(); <a name="l00339"></a>00339 writer->SetInput(extract->GetOutput()); <a name="l00340"></a>00340 <a name="l00341"></a>00341 std::cout << <span class="stringliteral">"<DartMeasurement name=\"ImageError\" type=\"numeric/double\">"</span>; <a name="l00342"></a>00342 std::cout << status; <a name="l00343"></a>00343 std::cout << <span class="stringliteral">"</DartMeasurement>"</span> << std::endl;<a class="code" href="classitk_1_1OStringStream.html"></a> <a name="l00344"></a>00344 <a class="code" href="classitk_1_1OStringStream.html"></a> <a name="l00345"></a>00345 <a class="code" href="classitk_1_1OStringStream.html"> ::itk::OStringStream</a> diffName; <a name="l00346"></a>00346 diffName << testImageFilename << <span class="stringliteral">".diff.png"</span>; <a name="l00347"></a>00347 <span class="keywordflow">try</span> <a name="l00348"></a>00348 { <a name="l00349"></a>00349 rescale->SetInput(diff->GetOutput()); <a name="l00350"></a>00350 rescale->Update(); <a name="l00351"></a>00351 } <a name="l00352"></a>00352 <span class="keywordflow">catch</span>(<span class="keyword">const</span> std::exception& e) <a name="l00353"></a>00353 { <a name="l00354"></a>00354 std::cerr << <span class="stringliteral">"Error during rescale of "</span> << diffName.str() << std::endl; <a name="l00355"></a>00355 std::cerr << e.what() << <span class="stringliteral">"\n"</span>; <a name="l00356"></a>00356 } <a name="l00357"></a>00357 <span class="keywordflow">catch</span> (...) <a name="l00358"></a>00358 { <a name="l00359"></a>00359 std::cerr << <span class="stringliteral">"Error during rescale of "</span> << diffName.str() << std::endl; <a name="l00360"></a>00360 } <a name="l00361"></a>00361 writer->SetFileName(diffName.str().c_str()); <a name="l00362"></a>00362 <span class="keywordflow">try</span> <a name="l00363"></a>00363 { <a name="l00364"></a>00364 writer->Update(); <a name="l00365"></a>00365 } <a name="l00366"></a>00366 <span class="keywordflow">catch</span>(<span class="keyword">const</span> std::exception& e) <a name="l00367"></a>00367 { <a name="l00368"></a>00368 std::cerr << <span class="stringliteral">"Error during write of "</span> << diffName.str() << std::endl; <a name="l00369"></a>00369 std::cerr << e.what() << <span class="stringliteral">"\n"</span>; <a name="l00370"></a>00370 } <a name="l00371"></a>00371 <span class="keywordflow">catch</span> (...) <a name="l00372"></a>00372 { <a name="l00373"></a>00373 std::cerr << <span class="stringliteral">"Error during write of "</span> << diffName.str() << std::endl; <a name="l00374"></a>00374 } <a name="l00375"></a>00375 <a name="l00376"></a>00376 std::cout << <span class="stringliteral">"<DartMeasurementFile name=\"DifferenceImage\" type=\"image/png\">"</span>; <a name="l00377"></a>00377 std::cout << diffName.str(); <a name="l00378"></a>00378 std::cout << <span class="stringliteral">"</DartMeasurementFile>"</span> << std::endl;<a class="code" href="classitk_1_1OStringStream.html"></a> <a name="l00379"></a>00379 <a class="code" href="classitk_1_1OStringStream.html"></a> <a name="l00380"></a>00380 <a class="code" href="classitk_1_1OStringStream.html"> ::itk::OStringStream</a> baseName; <a name="l00381"></a>00381 baseName << testImageFilename << <span class="stringliteral">".base.png"</span>; <a name="l00382"></a>00382 <span class="keywordflow">try</span> <a name="l00383"></a>00383 { <a name="l00384"></a>00384 rescale->SetInput(baselineReader->GetOutput()); <a name="l00385"></a>00385 rescale->Update(); <a name="l00386"></a>00386 } <a name="l00387"></a>00387 <span class="keywordflow">catch</span>(<span class="keyword">const</span> std::exception& e) <a name="l00388"></a>00388 { <a name="l00389"></a>00389 std::cerr << <span class="stringliteral">"Error during rescale of "</span> << baseName.str() << std::endl; <a name="l00390"></a>00390 std::cerr << e.what() << <span class="stringliteral">"\n"</span>; <a name="l00391"></a>00391 } <a name="l00392"></a>00392 <span class="keywordflow">catch</span> (...) <a name="l00393"></a>00393 { <a name="l00394"></a>00394 std::cerr << <span class="stringliteral">"Error during rescale of "</span> << baseName.str() << std::endl; <a name="l00395"></a>00395 } <a name="l00396"></a>00396 <span class="keywordflow">try</span> <a name="l00397"></a>00397 { <a name="l00398"></a>00398 writer->SetFileName(baseName.str().c_str()); <a name="l00399"></a>00399 writer->Update(); <a name="l00400"></a>00400 } <a name="l00401"></a>00401 <span class="keywordflow">catch</span>(<span class="keyword">const</span> std::exception& e) <a name="l00402"></a>00402 { <a name="l00403"></a>00403 std::cerr << <span class="stringliteral">"Error during write of "</span> << baseName.str() << std::endl; <a name="l00404"></a>00404 std::cerr << e.what() << <span class="stringliteral">"\n"</span>; <a name="l00405"></a>00405 } <a name="l00406"></a>00406 <span class="keywordflow">catch</span> (...) <a name="l00407"></a>00407 { <a name="l00408"></a>00408 std::cerr << <span class="stringliteral">"Error during write of "</span> << baseName.str() << std::endl; <a name="l00409"></a>00409 } <a name="l00410"></a>00410 <a name="l00411"></a>00411 std::cout << <span class="stringliteral">"<DartMeasurementFile name=\"BaselineImage\" type=\"image/png\">"</span>; <a name="l00412"></a>00412 std::cout << baseName.str(); <a name="l00413"></a>00413 std::cout << <span class="stringliteral">"</DartMeasurementFile>"</span> << std::endl;<a class="code" href="classitk_1_1OStringStream.html"></a> <a name="l00414"></a>00414 <a class="code" href="classitk_1_1OStringStream.html"></a> <a name="l00415"></a>00415 <a class="code" href="classitk_1_1OStringStream.html"> ::itk::OStringStream</a> testName; <a name="l00416"></a>00416 testName << testImageFilename << <span class="stringliteral">".test.png"</span>; <a name="l00417"></a>00417 <span class="keywordflow">try</span> <a name="l00418"></a>00418 { <a name="l00419"></a>00419 rescale->SetInput(testReader->GetOutput()); <a name="l00420"></a>00420 rescale->Update(); <a name="l00421"></a>00421 } <a name="l00422"></a>00422 <span class="keywordflow">catch</span>(<span class="keyword">const</span> std::exception& e) <a name="l00423"></a>00423 { <a name="l00424"></a>00424 std::cerr << <span class="stringliteral">"Error during rescale of "</span> << testName.str() << std::endl; <a name="l00425"></a>00425 std::cerr << e.what() << <span class="stringliteral">"\n"</span>; <a name="l00426"></a>00426 } <a name="l00427"></a>00427 <span class="keywordflow">catch</span> (...) <a name="l00428"></a>00428 { <a name="l00429"></a>00429 std::cerr << <span class="stringliteral">"Error during rescale of "</span> << testName.str() << std::endl; <a name="l00430"></a>00430 } <a name="l00431"></a>00431 <span class="keywordflow">try</span> <a name="l00432"></a>00432 { <a name="l00433"></a>00433 writer->SetFileName(testName.str().c_str()); <a name="l00434"></a>00434 writer->Update(); <a name="l00435"></a>00435 } <a name="l00436"></a>00436 <span class="keywordflow">catch</span>(<span class="keyword">const</span> std::exception& e) <a name="l00437"></a>00437 { <a name="l00438"></a>00438 std::cerr << <span class="stringliteral">"Error during write of "</span> << testName.str() << std::endl; <a name="l00439"></a>00439 std::cerr << e.what() << <span class="stringliteral">"\n"</span>; <a name="l00440"></a>00440 } <a name="l00441"></a>00441 <span class="keywordflow">catch</span> (...) <a name="l00442"></a>00442 { <a name="l00443"></a>00443 std::cerr << <span class="stringliteral">"Error during write of "</span> << testName.str() << std::endl; <a name="l00444"></a>00444 } <a name="l00445"></a>00445 <a name="l00446"></a>00446 std::cout << <span class="stringliteral">"<DartMeasurementFile name=\"TestImage\" type=\"image/png\">"</span>; <a name="l00447"></a>00447 std::cout << testName.str(); <a name="l00448"></a>00448 std::cout << <span class="stringliteral">"</DartMeasurementFile>"</span> << std::endl; <a name="l00449"></a>00449 <a name="l00450"></a>00450 <a name="l00451"></a>00451 } <a name="l00452"></a>00452 <span class="keywordflow">return</span> (status > numberOfPixelsTolerance) ? 1 : 0; <a name="l00453"></a>00453 } <a name="l00454"></a>00454 <a name="l00455"></a>00455 <span class="comment">//</span> <a name="l00456"></a>00456 <span class="comment">// Generate all of the possible baselines</span> <a name="l00457"></a>00457 <span class="comment">// The possible baselines are generated fromn the baselineFilename using the following algorithm:</span> <a name="l00458"></a>00458 <span class="comment">// 1) strip the suffix</span> <a name="l00459"></a>00459 <span class="comment">// 2) append a digit .x</span> <a name="l00460"></a>00460 <span class="comment">// 3) append the original suffix.</span> <a name="l00461"></a>00461 <span class="comment">// It the file exists, increment x and continue</span> <a name="l00462"></a>00462 <span class="comment">//</span> <a name="l00463"></a><a class="code" href="itkTestMain_8h.html#7d92f4ecc3fe3b5ee5e4f44ebf769434">00463</a> std::map<std::string,int> <a class="code" href="itkTestMain_8h.html#7d92f4ecc3fe3b5ee5e4f44ebf769434">RegressionTestBaselines</a> (<span class="keywordtype">char</span> *baselineFilename) <a name="l00464"></a>00464 { <a name="l00465"></a>00465 std::map<std::string,int> baselines; <a name="l00466"></a>00466 baselines[std::string(baselineFilename)] = 0; <a name="l00467"></a>00467 <a name="l00468"></a>00468 std::string originalBaseline(baselineFilename); <a name="l00469"></a>00469 <a name="l00470"></a>00470 <span class="keywordtype">int</span> x = 0; <a name="l00471"></a>00471 std::string::size_type suffixPos = originalBaseline.rfind(<span class="stringliteral">"."</span>); <a name="l00472"></a>00472 std::string suffix; <a name="l00473"></a>00473 <span class="keywordflow">if</span> (suffixPos != std::string::npos) <a name="l00474"></a>00474 { <a name="l00475"></a>00475 suffix = originalBaseline.substr(suffixPos,originalBaseline.length()); <a name="l00476"></a>00476 originalBaseline.erase(suffixPos,originalBaseline.length()); <a name="l00477"></a>00477 } <a name="l00478"></a>00478 <span class="keywordflow">while</span> (++x) <a name="l00479"></a>00479 {<a class="code" href="classitk_1_1OStringStream.html"></a> <a name="l00480"></a>00480 <a class="code" href="classitk_1_1OStringStream.html"> ::itk::OStringStream</a> filename; <a name="l00481"></a>00481 filename << originalBaseline << <span class="stringliteral">"."</span> << x << suffix; <a name="l00482"></a>00482 std::ifstream filestream(filename.str().c_str()); <a name="l00483"></a>00483 <span class="keywordflow">if</span> (!filestream) <a name="l00484"></a>00484 { <a name="l00485"></a>00485 <span class="keywordflow">break</span>; <a name="l00486"></a>00486 } <a name="l00487"></a>00487 baselines[filename.str()] = 0; <a name="l00488"></a>00488 filestream.close(); <a name="l00489"></a>00489 } <a name="l00490"></a>00490 <span class="keywordflow">return</span> baselines; <a name="l00491"></a>00491 } <a name="l00492"></a>00492 <a name="l00493"></a>00493 <span class="comment">// Needed for explicit instantiation</span> <a name="l00494"></a>00494 <span class="preprocessor">#include "itkDifferenceImageFilter.txx"</span> <a name="l00495"></a>00495 </pre></div></div> <hr><address><small> Generated at Fri May 8 00:21:00 2009 for ITK by <a href="http://www.stack.nl/~dimitri/doxygen/index.html"> <img src="http://www.stack.nl/~dimitri/doxygen/doxygen.png" alt="doxygen" align="middle" border=0 width=110 height=53> </a> 1.5.9 written by <a href="mailto:dimitri@stack.nl">Dimitri van Heesch</a>, © 1997-2000</small></address> </body> </html>