00001 #ifndef Impala_Core_Test_TestImage_h
00002 #define Impala_Core_Test_TestImage_h
00003
00004 #include "Core/ImageSet/ImageSet.h"
00005 #include "Core/Test/TestBase.h"
00006 #include "Core/Array/ReadFile.h"
00007 #include "Core/Array/RGB2Intensity.h"
00008 #include "Core/Array/Set.h"
00009 #include "Core/Array/WriteRaw.h"
00010 #include "Core/Array/ArrayListDelete.h"
00011 #include "Core/Array/Pattern/PatBinaryPixOp.h"
00012 #include "Core/Array/Trait/BpoTestEqual.h"
00013 #include "Core/Array/Arrays.h"
00014 #include "Core/Feature/FeatureTable.h"
00015 #include "Core/Vector/Norm1Dist.h"
00016 #include "Basis/FileName.h"
00017
00018 namespace Impala
00019 {
00020 namespace Core
00021 {
00022 namespace Test
00023 {
00024
00025
00026 class TestImage : public TestBase
00027 {
00028 public:
00029 typedef Feature::FeatureTable FeatureTable;
00030
00031 static void
00032 SetImageSet(ImageSet::ImageSet* imSet)
00033 {
00034 mImSet = imSet;
00035 }
00036
00037 protected:
00038
00039 typedef Array::Array2dVec3UInt8 Array2dVec3UInt8;
00040 typedef Array::Array2dVec3Real64 Array2dVec3Real64;
00041 typedef Array::Array2dScalarReal64 Array2dScalarReal64;
00042 typedef Array::Array2dScalarInt32 Array2dScalarInt32;
00043
00044
00045
00046 Array2dVec3UInt8*
00047 OpenSrcArray(String srcName)
00048 {
00049 if (!mImSet)
00050 {
00051 ILOG_ERROR("No image set");
00052 return 0;
00053 }
00054 int id = mImSet->GetFileId(srcName);
00055 if (id == -1)
00056 {
00057 ILOG_ERROR("Couldn't find " << srcName);
00058 return 0;
00059 }
00060 return mImSet->GetImage(id);
00061 }
00062
00063 Array2dVec3Real64*
00064 OpenSrcArrayAsVec3Real64(String srcName)
00065 {
00066 Array2dVec3UInt8* srcData = OpenSrcArray(srcName);
00067 Array2dVec3Real64* realData = 0;
00068 Set(realData, srcData);
00069 delete srcData;
00070 return realData;
00071 }
00072
00073 Array2dScalarReal64*
00074 OpenSrcArrayAsReal(String srcName)
00075 {
00076 Array2dVec3UInt8* srcData = OpenSrcArray(srcName);
00077 Array2dScalarReal64* realData = 0;
00078 Array::RGB2Intensity(realData, srcData);
00079 delete srcData;
00080 return realData;
00081 }
00082
00083 Array2dScalarInt32*
00084 OpenSrcArrayAsInt(String srcName)
00085 {
00086 Array2dScalarReal64* realData = OpenSrcArrayAsReal(srcName);
00087 Array2dScalarInt32* intData = 0;
00088 Array::Set(intData, realData);
00089 delete realData;
00090 return intData;
00091 }
00092
00093
00094
00095 template<class ArrayT>
00096 void
00097 Compare(ArrayT* src, String refName)
00098 {
00099 ArrayT* ref = 0;
00100 Array::ReadRaw(ref, refName, mImSet->GetDatabase());
00101 if (ref == 0)
00102 {
00103 SetFailure("unable to read reference");
00104 return;
00105 }
00106 DoCompare(src, ref);
00107 delete ref;
00108 }
00109
00110 template<class ArrayT>
00111 void
00112 CompareList(std::vector<ArrayT*> srcList, String refName)
00113 {
00114 std::vector<ArrayT*> refList;
00115 Array::ReadRawList(refList, refName, mImSet->GetDatabase());
00116 if (refList.size() == 0)
00117 {
00118 SetFailure("unable to read reference");
00119 return;
00120 }
00121 if (srcList.size() != refList.size())
00122 {
00123 SetFailure("unequal list size");
00124 Array::ArrayListDelete(&refList);
00125 return;
00126 }
00127 for (int i=0 ; i<srcList.size() ; i++)
00128 DoCompare(srcList[i], refList[i]);
00129 Array::ArrayListDelete(&refList);
00130 }
00131
00132 void
00133 Compare(FeatureTable* src, String refName)
00134 {
00135 FeatureTable ref(FileNameTail(refName));
00136 Table::Read(&ref, refName, mImSet->GetDatabase());
00137 DoCompare(src, &ref);
00138 }
00139
00140 static ImageSet::ImageSet* mImSet;
00141
00142 private:
00143
00144 template<class ArrayT>
00145 void
00146 DoCompare(ArrayT* src, ArrayT* ref)
00147 {
00148 std::ostringstream ost;
00149 if (src->CW() != ref->CW())
00150 ost << "width differs: " << src->CW() << " vs "
00151 << ref->CW() << std::ends;
00152 if (src->CH() != ref->CH())
00153 ost << "height differs: " << src->CH() << " vs "
00154 << ref->CH() << std::ends;
00155 Array::Trait::BpoTestEqual<Array2dScalarReal64, ArrayT, ArrayT> bpo(mErrorTolerance);
00156 Array2dScalarReal64* dst = 0;
00157 Array::Pattern::PatBinaryPixOp(dst, src, ref, bpo);
00158 if (bpo.mNrDiff > 0)
00159 ost << "TestEqual found " << bpo.mNrDiff << " differences, in "
00160 << bpo.mNr << " pixels, sum diffs : " << bpo.mSumDiff
00161 << " avg diff : " << bpo.mSumDiff / bpo.mNrDiff
00162 << " avg src1 : " << bpo.mSumSrc1 / bpo.mNr
00163 << " avg src2 : " << bpo.mSumSrc2 / bpo.mNr << std::ends;
00164 String r = ost.str();
00165 if (r.size() > 0)
00166 SetFailure(r);
00167 delete dst;
00168 }
00169
00170 void
00171 DoCompare(FeatureTable* src, FeatureTable* ref)
00172 {
00173 std::ostringstream ost;
00174 if (src->Size() != ref->Size())
00175 ost << "Size differs: " << src->Size() << " vs "
00176 << ref->Size() << std::ends;
00177 if (src->GetFeatureVectorLength() != ref->GetFeatureVectorLength())
00178 ost << "Featurevectorlength differs: "
00179 << src->GetFeatureVectorLength() << " vs "
00180 << ref->GetFeatureVectorLength() << std::ends;
00181 int checkSize = Min(src->Size(), ref->Size());
00182 int nDiff = 0;
00183 for (int i=0 ; i<checkSize ; i++)
00184 {
00185 if (src->Get1(i) != ref->Get1(i))
00186 {
00187 nDiff++;
00188 }
00189 else
00190 {
00191 Real64 d = Vector::Norm1Dist(src->Get2(i), ref->Get2(i));
00192 if (d > mErrorTolerance)
00193 nDiff++;
00194 }
00195 }
00196 if (nDiff > 0)
00197 ost << "Found " << nDiff << " differences on " << checkSize
00198 << " rows" << std::ends;
00199 String r = ost.str();
00200 if (r.size() > 0)
00201 SetFailure(r);
00202 }
00203
00204 static const double mErrorTolerance;
00205
00206 ILOG_VAR_DEC;
00207
00208 };
00209
00210 const double TestImage::mErrorTolerance = 0.00001;
00211 ImageSet::ImageSet* TestImage::mImSet = 0;
00212
00213 ILOG_VAR_INIT(TestImage, Core.Test);
00214
00215 }
00216 }
00217 }
00218
00219 #endif