00001 #ifndef Impala_Core_Feature_PointDescriptorTable_h
00002 #define Impala_Core_Feature_PointDescriptorTable_h
00003
00004 #include "Core/Array/Arrays.h"
00005 #include "Core/Vector/VectorSet.h"
00006 #include "Core/Feature/FeatureDefinition.h"
00007 #include "Core/Feature/PointDescriptorIO.h"
00008 #include "Core/Matrix/MatFunc.h"
00009 #include "Core/Table/TableTem.h"
00010 #include "Util/IOBufferFile.h"
00011
00012 namespace Impala
00013 {
00014 namespace Core
00015 {
00016 namespace Feature
00017 {
00018
00019
00020 typedef Table::TableTem<Column::ColumnTem<Real64>,
00021 Column::ColumnTem<Real64>,
00022 Column::ColumnTem<Real64>,
00023 Column::ColumnTem<Real64>,
00024 Column::ColumnTem<Real64>,
00025 Vector::VectorSet<Array::Array2dScalarReal64> >
00026 PointDescriptorTableBaseType;
00027
00028
00031 class PointDescriptorTable : public PointDescriptorTableBaseType
00032 {
00033 public:
00034 typedef Column::ColumnTem<Real64> ColumnReal64;
00035 typedef Vector::VectorSet<Array::Array2dScalarReal64> ColumnVectorSet;
00036 typedef Vector::VectorTem<Real64> VectorReal64;
00037
00038 PointDescriptorTable(FeatureDefinition def=FeatureDefinition("")) :
00039 PointDescriptorTableBaseType(ColumnReal64(0), ColumnReal64(0),
00040 ColumnReal64(0), ColumnReal64(0), ColumnReal64(0),
00041 ColumnVectorSet(true, 0, 0))
00042 {
00043 mFeatureDef = def;
00044 SetColName(1, "x");
00045 SetColName(2, "y");
00046 SetColName(3, "scale");
00047 SetColName(4, "orientation");
00048 SetColName(5, "cornerness");
00049 SetColName(6, mFeatureDef.GetName());
00050 }
00051
00052 PointDescriptorTable(FeatureDefinition def, int tableSize, int vecLen) :
00053 PointDescriptorTableBaseType(ColumnReal64(tableSize),
00054 ColumnReal64(tableSize),
00055 ColumnReal64(tableSize),
00056 ColumnReal64(tableSize),
00057 ColumnReal64(tableSize),
00058 ColumnVectorSet(true, vecLen, tableSize))
00059 {
00060 mFeatureDef = def;
00061 SetColName(1, "x");
00062 SetColName(2, "y");
00063 SetColName(3, "scale");
00064 SetColName(4, "orientation");
00065 SetColName(5, "cornerness");
00066 SetColName(6, mFeatureDef.GetName());
00067 }
00068
00069 inline Real64
00070 GetX(int index) const
00071 {
00072 return Get1(index);
00073 }
00074
00075 inline Real64
00076 GetY(int index) const
00077 {
00078 return Get2(index);
00079 }
00080
00081 inline Real64
00082 GetScale(int index) const
00083 {
00084 return Get3(index);
00085 }
00086
00087 inline Real64
00088 GetOrientation(int index) const
00089 {
00090 return Get4(index);
00091 }
00092
00093 void
00094 SetDescriptor(int index, VectorReal64& v)
00095 {
00096 GetColumn6()->GetVector(index, true) = v;
00097 }
00098
00099 Real64*
00100 GetDescriptorData(int index)
00101 {
00102 return GetColumn6()->GetVectorData(index);
00103 }
00104
00105 Matrix::Mat*
00106 GetDescriptorMatrix()
00107 {
00108 return GetColumn6()->GetStorage();
00109 }
00110
00111 static PointDescriptorTable*
00112 ImportFromBuffer(FeatureDefinition def, Util::IOBuffer* buffer, int mStripBorder)
00113 {
00114 using namespace Impala::Core::Matrix;
00115 Mat* points = 0;
00116 Mat* descriptors = 0;
00117
00118 String dataType = ReadBINDESC1FromBuffer(points, descriptors, buffer, true, true);
00119 if(dataType.substr(0, 6) != "CIRCLE")
00120 {
00121 ILOG_WARN("Data type of file read is not CIRCLE, but '" << dataType << "'");
00122 }
00123 ILOG_DEBUG("Loaded file: rows=" << MatNrRow(points) << "; cols=" << MatNrCol(points));
00124 PointDescriptorTable* res = new PointDescriptorTable(def);
00125 for(int i = 0; i < MatNrRow(points); i++)
00126 {
00127
00128
00129 res->Add(*MatE(points, i, 0) - 1 - mStripBorder,
00130 *MatE(points, i, 1) - 1 - mStripBorder,
00131 *MatE(points, i, 2), *MatE(points, i, 3),
00132 *MatE(points, i, 4));
00133 }
00134
00135 res->ReplaceAllDescriptors(descriptors);
00136 delete points;
00137
00138 return res;
00139 }
00140
00141 static PointDescriptorTable*
00142 ImportFromFile(FeatureDefinition def, String filename, int mStripBorder)
00143 {
00144
00145 Util::IOBuffer* buffer = new Util::IOBufferFile(filename, true, false);
00146 PointDescriptorTable* res = ImportFromBuffer(def, buffer, mStripBorder);
00147 delete buffer;
00148 return res;
00149 }
00150
00151 void
00152 ExportToFile(String filename, int mStripBorder, String mOutputFormat)
00153 {
00154 using namespace Impala::Core::Matrix;
00155
00156 Util::IOBuffer* buffer = new Util::IOBufferFile(filename, false, false);
00157 Mat* points = MatCreate<Mat>(Size(), 5);
00158 for(int i = 0; i < Size(); i++)
00159 {
00160
00161
00162 *MatE(points, i, 0) = Get1(i) + 1 + mStripBorder;
00163 *MatE(points, i, 1) = Get2(i) + 1 + mStripBorder;
00164 *MatE(points, i, 2) = Get3(i);
00165 *MatE(points, i, 3) = Get4(i);
00166 *MatE(points, i, 4) = Get5(i);
00167 }
00168 Mat* descriptorWrapper = MatCreate<Mat>(Size(),
00169 MatNrCol(GetColumn6()->GetStorage()),
00170 MatE(GetColumn6()->GetStorage(), 0, 0), true);
00171 if((mOutputFormat == "bindesc1") || (mOutputFormat == "binary"))
00172 {
00173 WriteBINDESC1ToBuffer(buffer, points, descriptorWrapper, "CIRCLE ");
00174 }
00175 else
00176 {
00177 WriteKOEN1ToBuffer(buffer, points, descriptorWrapper, "CIRCLE");
00178 }
00179 delete descriptorWrapper;
00180 delete points;
00181 delete buffer;
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 FeatureDefinition
00193 GetFeatureDefinition()
00194 {
00195 return mFeatureDef;
00196 }
00197
00198 void
00199 SetFeatureDefinition(FeatureDefinition def)
00200 {
00201 mFeatureDef = def;
00202 }
00203
00204 int
00205 GetDescriptorLength()
00206 {
00207 return GetColumn6()->GetVectorLength(0);
00208 }
00209
00210 void
00211 SetDescriptorLength(int descriptorLength)
00212 {
00213 int storageSize = GetColumn6()->GetStorage()->CH();
00214 Array::Array2dScalarReal64* descriptors = new
00215 Array::Array2dScalarReal64(descriptorLength, storageSize, 0, 0);
00216 GetColumn6()->SetStorage(descriptors);
00217 }
00218
00219 void
00220 ReplaceAllDescriptors(Array::Array2dScalarReal64* descriptors)
00221 {
00222
00223 GetColumn6()->SetStorage(descriptors);
00224 }
00225
00226 ILOG_VAR_DEC;
00227
00228 private:
00229
00230 FeatureDefinition mFeatureDef;
00231
00232 };
00233
00234 ILOG_VAR_INIT(PointDescriptorTable, Impala.Core.Feature);
00235
00236 }
00237 }
00238 }
00239
00240 #endif