00001 #ifndef Impala_Core_VideoSet_HistogramExtractor_h
00002 #define Impala_Core_VideoSet_HistogramExtractor_h
00003
00004 #include "Core/VideoSet/Reporter.h"
00005 #include "Core/Vector/VectorTem.h"
00006 #include "Persistency/FeatureTableRepository.h"
00007
00008 namespace Impala
00009 {
00010 namespace Core
00011 {
00012 namespace VideoSet
00013 {
00014
00015 class HistogramExtractor : public Listener
00016 {
00017
00018 protected:
00019
00020 typedef Vector::VectorTem<Real64> VectorReal64;
00021
00022 public:
00023
00024 HistogramExtractor(Reporter* reporter,
00025 CmdOptions& options, CString featureName, int binCount)
00026 {
00027 mFeatureName = featureName;
00028 mBinCount = binCount;
00029 Feature::FeatureDefinition def(GetFeatureName());
00030 mHistogramTable = new Feature::FeatureTable(def, 1000, GetBinCount());
00031 }
00032
00033 virtual
00034 ~HistogramExtractor()
00035 {
00036 }
00037
00038 String
00039 GetFeatureName() const
00040 {
00041 return mFeatureName;
00042 }
00043
00044 virtual void
00045 HandleNewFrame(VideoSet* vs, int fileId, Stream::RgbDataSrc* src)
00046 {
00047 if (!src)
00048 {
00049 ILOG_ERROR("Not a valid source for fileId " << fileId);
00050 return;
00051 }
00052
00053 Array::Array2dVec3UInt8* im = Array::ArrayCreate<Array::Array2dVec3UInt8>
00054 (src->FrameWidth(), src->FrameHeight(), 0, 0, src->DataPtr(), true);
00055 if (!im)
00056 {
00057 ILOG_ERROR("Couldn't load image for fileId " << fileId);
00058 return;
00059 }
00060
00061 Quid quid = vs->GetQuidFrame(fileId, src->FrameNr());
00062
00063 VectorReal64 histogram = ComputeHistogram(im);
00064
00065 mHistogramTable->Add(quid, histogram);
00066
00067 delete im;
00068 }
00069
00070 virtual void
00071 HandleDoneFile(VideoSet* vs, int fileId, Stream::RgbDataSrc* src)
00072 {
00073 Feature::FeatureDefinition def = mHistogramTable->GetFeatureDefinition();
00074 typedef Persistency::FeatureLocator FeatureLocator;
00075 FeatureLocator loc(vs->GetLocator(), false, false, "Keyframes",
00076 def.AsString(), vs->GetContainerFile(fileId));
00077 Persistency::FeatureTableRepository().Add(loc, mHistogramTable);
00078 mHistogramTable->SetSize(0);
00079 }
00080
00081 protected:
00082
00083 int
00084 GetBinCount() const
00085 {
00086 return mBinCount;
00087 }
00088
00089 virtual VectorReal64
00090 ComputeHistogram(Array::Array2dVec3UInt8* image) = 0;
00091
00092 template <class T>
00093 int
00094 ComputeBin(const T val, const T low, const T high, const int nrBins) const
00095 {
00096 const int bin = (nrBins * (val - low)) / (high - low);
00097 if (bin >= 0 && bin < nrBins)
00098 return bin;
00099
00100 if (val == high)
00101 return nrBins - 1;
00102
00103 ILOG_WARN("Unexpected value: " << val <<
00104 " is not in range [" << low << ", " << high << "]");
00105 return (bin < 0) ? 0 : nrBins-1;
00106 }
00107
00108 private:
00109
00110 String mFeatureName;
00111 int mBinCount;
00112 Feature::FeatureTable* mHistogramTable;
00113
00114 ILOG_VAR_DECL;
00115 };
00116
00117 ILOG_VAR_INIT(HistogramExtractor, Impala.Core.VideoSet);
00118
00119 }
00120 }
00121 }
00122
00123 #endif