00001 #ifndef Impala_Core_VideoSet_BlackFrameProc_h
00002 #define Impala_Core_VideoSet_BlackFrameProc_h
00003
00004 #include "Core/VideoSet/Reporter.h"
00005 #include "Core/VideoSet/Keyframes.h"
00006 #include "Core/Array/MakeRoi.h"
00007 #include "Core/Array/RGB2Gray.h"
00008 #include "Core/Array/PixSum.h"
00009 #include "Core/Array/RecGauss.h"
00010 #include "Core/Array/Mul.h"
00011 #include "Core/Array/Add.h"
00012 #include "Core/Array/RGB2Intensity.h"
00013 #include "Core/Array/Sqrt.h"
00014 #include "Core/Feature/FeatureTable.h"
00015
00016 namespace Impala
00017 {
00018 namespace Core
00019 {
00020 namespace VideoSet
00021 {
00022
00023
00024 class BlackFrameProc : public Listener
00025 {
00026 public:
00027 typedef Array::Array2dScalarReal64 Array2dScalarReal64;
00028
00029 BlackFrameProc(Reporter* reporter, CmdOptions& options)
00030 {
00031 mReporter = reporter;
00032 mTable = 0;
00033 mStripBorder = options.GetInt("borderWidth");
00034 }
00035
00036 virtual void
00037 HandleNewWalk(VideoSet* vs, String walkType)
00038 {
00039 mWalkType = walkType;
00040 }
00041
00042 virtual void
00043 HandleNewFile(VideoSet* vs, int fileId, Stream::RgbDataSrc* src)
00044 {
00045 mTable = new Feature::FeatureTable(String("avg-int-grad"), 500, 2);
00046 }
00047
00048 void
00049 ComputeGradient(Array2dScalarReal64*& magnitude,
00050
00051 Array2dScalarReal64* im,
00052 Real64 sigma)
00053 {
00054 Real64 precision = 3.0;
00055
00056 Array2dScalarReal64* Lx = 0;
00057 Array2dScalarReal64* Ly = 0;
00058 RecGauss(Lx, im, sigma, sigma, 1, 0, precision);
00059 RecGauss(Ly, im, sigma, sigma, 0, 1, precision);
00060
00061
00062 Array2dScalarReal64* Lx2 = 0;
00063 Array2dScalarReal64* Ly2 = 0;
00064 Mul(Lx2, Lx, Lx);
00065 Mul(Ly2, Ly, Ly);
00066 Add(magnitude, Lx2, Ly2);
00067 Sqrt(magnitude, magnitude);
00068 delete Lx2;
00069 delete Ly2;
00070
00071
00072
00073 delete Lx;
00074 delete Ly;
00075 }
00076
00077 virtual void
00078 HandleNewFrame(VideoSet* vs, int fileId, Stream::RgbDataSrc* src)
00079 {
00080
00081 Array::Array2dVec3UInt8* im = Array::ArrayCreate<Array::Array2dVec3UInt8>
00082 (src->FrameWidth(), src->FrameHeight(), 0, 0, src->DataPtr(), true);
00083
00084
00085 if (!im) {
00086 ILOG_ERROR("No image: fileId=" << fileId);
00087 return;
00088 }
00089
00090 Quid quid = vs->GetQuidFrame(fileId, src->FrameNr());
00091
00092
00093 Geometry::Rectangle rect(mStripBorder, mStripBorder, im->CW() - mStripBorder - 1, im->CH() - mStripBorder - 1, true);
00094 Array::Array2dVec3UInt8* imNoBorder = MakeRoi(im, rect);
00095 delete im;
00096
00097 Array2dScalarReal64* imIntensity = 0;
00098 RGB2Intensity(imIntensity, imNoBorder);
00099 delete imNoBorder;
00100
00101 Real64 sigma = 2.0;
00102 Array2dScalarReal64* magnitude = 0;
00103
00104 ComputeGradient(magnitude, imIntensity, sigma);
00105
00106
00107 int n = (imIntensity->CW() * imIntensity->CH());
00108 Real64 avgMagnitude = PixSum(magnitude) / n;
00109 Real64 avgIntensity = PixSum(imIntensity) / n;
00110
00111 Vector::VectorTem<Real64> codebookVector = Vector::VectorTem<Real64>(2);
00112 codebookVector.Elem(0) = avgIntensity;
00113 codebookVector.Elem(1) = avgMagnitude;
00114 mTable->Add(quid, codebookVector);
00115
00116 delete magnitude;
00117 delete imIntensity;
00118 }
00119
00120 virtual void
00121 HandleDoneFile(VideoSet* vs, int fileId, Stream::RgbDataSrc* src)
00122 {
00123 String tableName = vs->GetFilePathFeatureData(mWalkType, Feature::FeatureDefinition("avg-int-grad"), fileId, false, -1, true, false);
00124 Write(mTable, tableName, vs->GetDatabase(), true);
00125 delete mTable;
00126 mTable = 0;
00127 ILOG_DEBUG("HandleDoneFile: " << fileId);
00128 }
00129
00130 private:
00131
00132 Reporter* mReporter;
00133 int mStripBorder;
00134 Feature::FeatureTable* mTable;
00135 String mWalkType;
00136
00137 ILOG_VAR_DEC;
00138 };
00139
00140 ILOG_VAR_INIT(BlackFrameProc, Core.VideoSet);
00141
00142 }
00143 }
00144 }
00145
00146 #endif