00001 #include "Basis/CmdOptions.h"
00002 #include "Core/Array/Statistics.h"
00003 #include "Core/Array/MakeFromData.h"
00004 #include "Core/Array/RGB2Intensity.h"
00005 #include "Core/Array/GetRgbPixels.h"
00006 #include "Core/Array/MulVal.h"
00007 #include "Core/Array/MaximumVal.h"
00008 #include "Core/Array/Dilation.h"
00009 #include "Core/Array/GreaterThan.h"
00010 #include "Core/Array/RecGauss.h"
00011 #include "Core/Array/ColorSpace.h"
00012 #include "Core/Array/Rgb2Ooo.h"
00013 #include "Core/Array/ProjectRange.h"
00014 #include "Core/Matrix/MatSet.h"
00015 #include "Core/Stream/RgbDataSrcFactory.h"
00016 #include "Visualization/AppControlSrc.h"
00017 #include "Visualization/Window.h"
00018
00019
00020 #include "OglGui/OglLib.cpp"
00021 #include "Link/ImpalaLib.cpp"
00022
00023 namespace Impala
00024 {
00025 namespace Application
00026 {
00027
00028
00029 using namespace Visualization;
00030 using namespace Core::Array;
00031 using namespace Core::Stream;
00032
00033 class WindowBackground : public Window, public AppControlSrc
00034 {
00035 public:
00036
00037 WindowBackground(RgbDataSrc* src) :
00038 Window(0, 0, SuggestWndWidth(1, src->FrameWidth()),
00039 SuggestWndHeight(1, src->FrameHeight()) + 25, true),
00040 AppControlSrc(1)
00041 {
00042 CmdOptions& options = CmdOptions::GetInstance();
00043 SetSrc(src);
00044 AppController::Instance().AddControl((AppControlSrc*) this, true);
00045 mViewScale = options.GetDouble("viewScale");
00046 mSigma = options.GetDouble("sigma");
00047
00048 Array2dScalarReal64* g = 0;
00049 g = MakeGaussIIR1d(mSigma, 0, 3.0);
00050 mBorderSize = g->CW();
00051 delete g;
00052
00053 Reset();
00054
00055 AppController::Instance().MainLoop();
00056 }
00057
00058 void
00059 Reset()
00060 {
00061 mMinimumThres = 10.0;
00062 mCountdown = 25;
00063 mStat.Reset();
00064 }
00065
00066
00067
00068 void
00069 HandleCycleSrc()
00070 {
00071 mOglWnd->updateScene = 1;
00072 std::string s = GetFpsString();
00073 if (mStat.IsDoingStat())
00074 s += std::string(" learning...");
00075 SetStatusStr((char*) s.c_str());
00076 }
00077
00078 void
00079 HandleNewFrame()
00080 {
00081 int view = 0;
00082 bool startBackground = ! mStat.IsDoingStat();
00083 if (!startBackground)
00084 UpdateView(view++, GetSrc()->DataPtr(), GetSrc()->FrameWidth(),
00085 GetSrc()->FrameHeight(), mViewScale);
00086
00087 ArraySystem& aSys = ArraySystem::Instance();
00088 aSys.MarkMemoryUsage(false);
00089
00090 Array2dVec3UInt8* srcWrap = ArrayCreate<Array2dVec3UInt8>
00091 (GetSrc()->FrameWidth(), GetSrc()->FrameHeight(), 0, 0,
00092 GetSrc()->DataPtr(), true);
00093 Array2dVec3Real64* srcV3R64 = ArrayCreate<Array2dVec3Real64>
00094 (GetSrc()->FrameWidth(), GetSrc()->FrameHeight(), mBorderSize,
00095 mBorderSize);
00096
00097 mCountdown--;
00098 if (mCountdown == 0)
00099 Set(mBackground, srcWrap);
00100
00101 Array2dScalarReal64* lum = 0;
00102 Array2dScalarReal64* col = 0;
00103 Array2dScalarReal64* tmp = 0;
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 Rgb2Ooo(srcV3R64, srcWrap);
00116 ProjectRange(lum, srcV3R64, 1);
00117 ProjectRange(col, srcV3R64, 2);
00118 ProjectRange(tmp, srcV3R64, 3);
00119 Add(col, col, tmp);
00120
00121 RecGauss(lum, lum, mSigma, mSigma, 0, 0, 3);
00122 RecGauss(col, col, mSigma, mSigma, 0, 0, 3);
00123
00124
00125
00126 Array2dScalarReal64* crit = col;
00127 mStat.DoArray(crit);
00128
00129 Array2dScalarReal64* diff = 0;
00130 Sub(diff, mStat.GetMean(), crit);
00131 Abs(diff, diff);
00132
00133
00134 Array2dScalarUInt8* binIm = 0;
00135 Array2dScalarReal64* thr = 0;
00136 MulVal(thr, mStat.GetStDev(), 3.0);
00137 MaximumVal(thr, thr, mMinimumThres);
00138 GreaterThan(binIm, diff, thr);
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 if (startBackground)
00156 {
00157 Array2dVec3UInt8* theBack = 0;
00158 Mul(theBack, mBackground, binIm);
00159 UpdateView(view++, theBack, "Direct", mViewScale);
00160 delete theBack;
00161 }
00162
00163 delete diff;
00164 delete thr;
00165 delete binIm;
00166
00167 delete lum;
00168 delete col;
00169 delete tmp;
00170 delete srcV3R64;
00171 aSys.CheckMemoryUsageSinceMark(false);
00172 }
00173
00174 virtual void
00175 KeyEvent(OglGui::Window* src, int c, int state)
00176 {
00177 AppControlSrc::KeyEvent(src, c, state);
00178 if (c == 'r')
00179 Reset();
00180 if (c == 't')
00181 {
00182 mMinimumThres += 1;
00183 std::cout << "Minimum threshold at " << mMinimumThres << std::endl;
00184 }
00185 if (c == 'T')
00186 {
00187 mMinimumThres -= 1;
00188 std::cout << "Minimum threshold at " << mMinimumThres << std::endl;
00189 }
00190 }
00191
00192 virtual void
00193 MouseFunc(INT msg, INT but, INT state, INT x, INT y)
00194 {
00195 Window::MouseFunc(msg, but, state, x, y);
00196
00197 if ((msg == oglMouseDown) && (but == oglRightButton))
00198 {
00199 OGLMENU menu = oglSys.MenuCreate();
00200 oglSys.MenuAdd(menu, "fps reset", 0, 2);
00201
00202 int choice = oglSys.MenuPopUp(mOglWnd, menu);
00203
00204 switch (choice)
00205 {
00206 case 2:
00207 FpsReset();
00208 break;
00209 }
00210
00211 oglSys.MenuDestroy(menu);
00212 oglSys.UpdateSceneFlag(mOglWnd, 1);
00213 }
00214 }
00215
00216 private:
00217
00218 double mViewScale;
00219 double mMinimumThres;
00220 double mSigma;
00221 int mBorderSize;
00222 int mCountdown;
00223 Statistics<Array2dScalarReal64> mStat;
00224 Array2dVec3UInt8* mBackground;
00225
00226 };
00227
00228 int
00229 mainBackground(int argc, char* argv[])
00230 {
00231 CmdOptions& options = CmdOptions::GetInstance();
00232 options.Initialise(true, true);
00233 options.SetDefault("sigma", "3.0");
00234 std::string usageStr = "camera|filename\n\n";
00235 if (! options.ParseArgs(argc, argv, usageStr, 1))
00236 return 1;
00237
00238 std::string srcName = options.GetArg(0);
00239 RgbDataSrcFactory& factory = RgbDataSrcFactory::Instance();
00240 RgbDataSrc* src = factory.Construct(srcName, options.GetString("src"));
00241 if (! src->Valid())
00242 {
00243 std::cout << "RgbDataSrc failed" << std::endl;
00244 return 0;
00245 }
00246 std::cout << src->FrameWidth() << " x " << src->FrameHeight() << std::endl;
00247
00248 WindowBackground* oglWnd = new WindowBackground(src);
00249 if (! oglWnd->Valid())
00250 {
00251 std::cout << "WindowBackground failed" << std::endl;
00252 return 0;
00253 }
00254 return 1;
00255 }
00256
00257 }
00258 }
00259
00260 int
00261 main(int argc, char* argv[])
00262 {
00263 return Impala::Application::mainBackground(argc, argv);
00264 }