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