00001
00002
00003
00004 #ifndef Impala_Application_SDash_IdentificationPane_h
00005 #define Impala_Application_SDash_IdentificationPane_h
00006
00007 #include "Visualization/RgbOglImage.h"
00008 #include "Visualization/Video.h"
00009
00010 #include "OglGui/GroupBox.h"
00011 #include "OglGui/ViewStrip.h"
00012 #include "OglGui/Button.h"
00013 #include "OglGui/DirectionButton.h"
00014 #include "OglGui/ImagesListener.h"
00015 #include "OglGui/InfoViewer.h"
00016
00017 #include "Application/sdash/RoiInfo.h"
00018 #include "Application/sdash/RegionsOfInterestInfo.h"
00019 #include "Application/sdash/RegionsOfInterestCorrelation.h"
00020 #include "Application/sdash/RoiLoader.h"
00021 #include "Application/sdash/GuiEventListener.h"
00022
00023 namespace Impala {
00024 namespace Application {
00025 namespace SDash {
00026
00027 class IdentificationPane :
00028 public OglGui::GroupBox,
00029 public OglGui::ButtonListener,
00030 public OglGui::ImagesListener,
00031 public OglGui::WindowListener
00032 {
00033
00034 public:
00035
00036 RegionsOfInterestInfo* mRoiInfo;
00037 RegionsOfInterestCorrelation* mRoiCorrelations;
00038
00039 IdentificationPane(
00040 const std::vector<Visualization::Video>& videos,
00041 const std::string& roiPath,
00042 Window* parent, int x, int y, int w, int h, strconst label,
00043 int borderType=BEV_ETCHED
00044 )
00045 : OglGui::GroupBox(parent, x, y, w, h, label, borderType)
00046 , mVideos(videos)
00047 , mRoiLoader(RoiLoader(roiPath))
00048 {
00049 mSimilarLoadingId = 0;
00050 StaticText* sText;
00051 sText = new StaticText(this, 4, h-22, w-8, 20, "Similars");
00052 sText->SetAlign(oglLeftAlign, oglCenterAlign);
00053
00054 Button* infoBtn = new Button(this, w/2-22, h-23, 44, 20, "info",
00055 BEV_ETCHED,true);
00056 infoBtn->SetButtonListener(this,0);
00057
00058 mClearSimilars = new Button(this, w-50, h-23, 46, 20, "clear",
00059 BEV_ETCHED,true);
00060 mClearSimilars->SetButtonListener(this,1);
00061
00062 OGLWND* oglWnd;
00063 mSimilarsViewStrip = new ViewStrip(this,4,h/2,w-8,h/2-24);
00064 mSimilarsViewStrip->AspectRatio(0.5f);
00065 mSimilarsViewStrip->SetMargin(6);
00066 mSimilarsViewStrip->Spacing(6);
00067 mSimilarsViewStrip->SetViewTags((FlexViewTags) & ~movableTag);
00068 mSimilarsViewStrip->LayoutViews();
00069 oglWnd = mSimilarsViewStrip->GetOGLWND();
00070 oglSys.AllowPicking(oglWnd, 1,1);
00071 oglSys.AllowDeleteView(oglWnd, 1);
00072 oglSys.SetAllMouseMotion(oglWnd,1);
00073
00074 mSimilarsViewStrip->SetImagesListener(this);
00075 mSimilarsViewStrip->SetWindowListener(this, 1);
00076
00077 sText = new StaticText(this, 4, h/2-20, w/2, 20, "Positives");
00078 sText->SetAlign(oglLeftAlign, oglCenterAlign);
00079 mPositivesViewStrip = new ViewStrip(this,4,4,w-8,h/2-24);
00080 mPositivesViewStrip->AspectRatio(0.5);
00081 mPositivesViewStrip->SetMargin(6);
00082 mPositivesViewStrip->Spacing(6);
00083 mPositivesViewStrip->LayoutViews();
00084 oglWnd = mPositivesViewStrip->GetOGLWND();
00085 oglSys.AllowPicking(oglWnd, 1,1);
00086 oglSys.SetAllMouseMotion(oglWnd,1);
00087 mPositivesViewStrip->ScaleTo(this);
00088 mPositivesViewStrip->SetImagesListener(this);
00089 mPositivesViewStrip->SetWindowListener(this, 2);
00090
00091 mClearPositives = new Button(this, w-50, h/2-20, 46, 20, "clear",
00092 BEV_ETCHED,true);
00093 mClearPositives->SetButtonListener(this,2);
00094
00095 mSelectedToPositives =
00096 new DirectionButton(this,w/2-10,h/2-20,20,20,3,BEV_ETCHED);
00097 mSelectedToPositives->SetDirectionFeedback(true);
00098 mSelectedToPositives->SetButtonListener(this,3);
00099
00100 mViewInfoText = new StaticText(this,100,h-22,200,20,"");
00101 mViewInfoText->SetForeground(0xff808080);
00102
00103
00104 ScaleChildren();
00105 }
00106
00107 virtual ~IdentificationPane()
00108 {
00109 }
00110
00111 void SetGuiEventListener(GuiEventListener* listener)
00112 {
00113 mGuiEventListener = listener;
00114 }
00115
00116 ViewStrip* Similars()
00117 {
00118 return mSimilarsViewStrip;
00119 }
00120 ViewStrip* Positives()
00121 {
00122 return mPositivesViewStrip;
00123 }
00124
00125 virtual void ButtonSelectionEvent(Button *src, void* userData)
00126 {
00127 if (userData==0)
00128 new InfoViewer(this, W()/2-300,H()/2-150,600,300,
00129 "Info on Similar and Positives Image Strips",
00130 "ViewStripsInfo.txt");
00131 if (userData==(void*)1)
00132 ClearSimilars();
00133 if (userData==(void*)2)
00134 ClearPositives();
00135 if (userData==(void*)3)
00136 SelectedToPositives();
00137 }
00138
00139 void OglViewSetRoiInfo(OGLVIEW* oglView, RoiInfo* roiInfo)
00140 {
00141 oglView->UserData1 = (void*) roiInfo->videoId;
00142 oglView->UserData2 = (void*) roiInfo->frameNr;
00143 oglView->UserData3 = (void*) roiInfo->roiNr;
00144 }
00145
00146 RoiInfo OglViewGetRoiInfo(OGLVIEW* oglView)
00147 {
00148 RoiInfo roiInfo;
00149 roiInfo.videoId = (int) oglView->UserData1;
00150 roiInfo.frameNr = (int) oglView->UserData2;
00151 roiInfo.roiNr = (int) oglView->UserData3;
00152 return roiInfo;
00153 }
00154
00155 virtual void DisplayFunc()
00156 {
00157 char buf[10];
00158 int cnt = 0;
00159 for ( ; mSimilarLoadingId < mRoisOfSimilars.size(); mSimilarLoadingId++)
00160 {
00161 int subjectVideoId = mRoisOfSimilars[0].videoId;
00162 RoiInfo& similRoi = mRoisOfSimilars[mSimilarLoadingId];
00163 OGLIMAGE* image = mRoiLoader.Load(similRoi.videoId, similRoi.frameNr, similRoi.roiNr);
00164 View* view = mSimilarsViewStrip->AddImage(image);
00165 OGLVIEW* oglView = view->GetOGLVIEW2D();
00166 sprintf(buf, "#%d", similRoi.videoId+1);
00167 viewSys.SetStr(oglView, OGL_TEXT, buf);
00168 ULONG col = (similRoi.videoId != subjectVideoId) ? 0xffffff00 : oglGREEN;
00169 viewSys.SetColor(oglView, OGL_TEXT,col);
00170 oglSys.ReleaseOglImage(image);
00171 OglViewSetRoiInfo(view->GetOGLVIEW2D(), &similRoi);
00172 if (++cnt > 4)
00173 {
00174 mSimilarLoadingId++;
00175 break;
00176 }
00177 }
00178 if (mSimilarLoadingId == mRoisOfSimilars.size())
00179 {
00180 mSimilarLoadingId = 0;
00181 mRoisOfSimilars.clear();
00182 }
00183 GroupBox::DisplayFunc();
00184 }
00185
00186 void SearchSimilars(RoiInfo subjRoi, int maxFrameNr)
00187 {
00188 int absRoiNr = mRoiInfo->GetAbsRoiNr(subjRoi.videoId, subjRoi.frameNr, subjRoi.roiNr);
00189 const unsigned short* similarsP = mRoiCorrelations->GetAllCorrelated(absRoiNr);
00190
00191 ClearSimilars();
00192
00193 for (int i = 0; i < CORRELATED_PER_ROI; i++)
00194 {
00195 RoiInfo similRoi = mRoiInfo->GetRoiInfo(*similarsP);
00196
00197 if (similRoi.videoId >= 0 && similRoi.videoId < mVideos.size())
00198 {
00199 if (similRoi.frameNr <= maxFrameNr)
00200 {
00201 if (!mVideos[similRoi.videoId].IsMovementFrame(similRoi.frameNr))
00202 {
00203 mRoisOfSimilars.push_back(similRoi);
00204 }
00205
00206
00207
00208
00209 }
00210 }
00211
00212 similarsP++;
00213 }
00214 }
00215
00216 void SelectedToPositives()
00217 {
00218 int i = 0;
00219 while(OGLVIEW* oglView = mSimilarsViewStrip->FindIndexView(i))
00220 {
00221 if (viewSys.HasTags(oglView, selectedTag))
00222 AddPositive(oglView);
00223 i++;
00224 }
00225 }
00226
00227 virtual void
00228 WindowKeyboardEvent(Window *src, int c, int state, void* userData)
00229 {
00230 if (userData==(void*)1)
00231 {
00232 if (c==oglDELETE)
00233 mSimilarsViewStrip->LayoutViews();
00234 if (c=='s')
00235 SelectedToPositives();
00236 }
00237
00238 if (userData==(void*)2 && c==oglDELETE)
00239 {
00240 int i = mPositivesViewStrip->Size();
00241 if (i==0)
00242 return;
00243 while (OGLVIEW* oglView=mPositivesViewStrip->FindIndexView(i-1))
00244 {
00245 if (viewSys.HasTags(oglView, selectedTag))
00246 {
00247 mRoisOfPositives.erase(mRoisOfPositives.begin()+i-1);
00248 viewSys.DeleteView(oglView);
00249 }
00250 i--;
00251 }
00252 mPositivesViewStrip->LayoutViews();
00253 }
00254 }
00255
00256 virtual void WindowInitDisplayEvent(Window *src, void* userData)
00257 {
00258 OGLWND* oglWnd;
00259 bool hasObjects;
00260 int state;
00261 if (userData == (void*) 1)
00262 {
00263 oglWnd = mSimilarsViewStrip->GetOGLWND();
00264 hasObjects = oglWnd->objectList != 0;
00265 state = mClearSimilars->GetState();
00266 mClearSimilars->SetState(hasObjects ? (state?state:1) : 0);
00267
00268 int n = viewSys.TagTagged(oglWnd,selectedTag,1,selectedTag,1);
00269 state = mSelectedToPositives->GetState();
00270 mSelectedToPositives->SetState(n ? (state?state:1) : 0);
00271 }
00272 else if (userData == (void*) 2)
00273 {
00274 oglWnd = mPositivesViewStrip->GetOGLWND();
00275 hasObjects = oglWnd->objectList != 0;
00276 state = mClearPositives->GetState();
00277 mClearPositives->SetState(hasObjects ? (state?state:1) : 0);
00278 }
00279 }
00280
00281 virtual void
00282 WindowMouseEvent(Window *src, int msg, int but, int state,
00283 int x, int y, void* userData )
00284 {
00285 if (msg == oglMouseLeave)
00286 mViewInfoText->SetText("");
00287 if (msg == oglMouseMove && !state)
00288 {
00289 OGLVIEW* oglView = viewSys.FindView(src->GetOGLWND(), x, y);
00290 mViewInfoText->SetText("");
00291 if (oglView)
00292 {
00293 int videoId = (int)(long long) oglView->UserData1;
00294 int frameNr = (int)(long long) oglView->UserData2;
00295
00296 int secs = 14*3600 + 14*60 + 40 + frameNr * mVideos[videoId].GetTimePerFrame();
00297 int h, m, s;
00298 char buf[100];
00299
00300 OglGui::AlertTimeLine::GetTimeUnits(secs,h,m,s);
00301 sprintf(buf, "Cam: %d Time: %02d:%02d:%02d", videoId+1, h, m, s);
00302 mViewInfoText->SetText(buf);
00303 }
00304 }
00305 }
00306
00307 virtual void
00308 ImageSelectionEvent(Window* src, int imIndex, int msg, int btn, int state, void* userData)
00309 {
00310 ViewStrip* vsP = (ViewStrip*) src;
00311 OGLVIEW* oglView = vsP->FindIndexView(imIndex);
00312 RoiInfo roiInfo = OglViewGetRoiInfo(oglView);
00313
00314 if (btn==oglRightButton || msg == oglMouseDblClick)
00315 {
00316 if (vsP == mSimilarsViewStrip)
00317 AddPositive(oglView);
00318 }
00319 else if (!(state&oglControl))
00320
00321 mGuiEventListener->GuiEvent(this, (void*) &roiInfo);
00322 }
00323
00324
00325
00326
00327
00328
00329
00330
00331 std::vector<OGLIMAGE*> GetPositives(bool doClear = false)
00332 {
00333 LIST* obj;
00334 OGLVIEW* oglView;
00335 OGLWND* oglWnd = mPositivesViewStrip->GetOGLWND();
00336 std::vector<OGLIMAGE*> result;
00337 ForAllElements(obj, oglWnd->objectList)
00338 {
00339 oglView = (OGLVIEW*) obj->info;
00340
00341
00342
00343 OGLIMAGE* im = viewSys.GetImage(oglView);
00344 result.push_back(im);
00345 }
00346
00347 if (doClear)
00348 ClearPositives();
00349 return result;
00350 }
00351
00352 void ClearSimilars()
00353 {
00354 viewSys.DeleteViews(mSimilarsViewStrip->GetOGLWND());
00355 mSimilarsViewStrip->LayoutViews();
00356 mSimilarsViewStrip->GetScrollBar()->SetNewPos(0);
00357 mRoisOfSimilars.clear();
00358 mSimilarLoadingId = 0;
00359 }
00360
00361 void ClearPositives()
00362 {
00363 mRoisOfPositives.clear();
00364
00365 viewSys.DeleteViews(mPositivesViewStrip->GetOGLWND());
00366 mPositivesViewStrip->LayoutViews();
00367 }
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382 void SuggestTrack(std::vector<int>& videoIds, std::vector<int>& frameNrs)
00383 {
00384 if (!mRoisOfPositives.empty())
00385 {
00386 int prevVideoId = -1;
00387
00388 for (int r = mRoisOfPositives.size() - 1; r >= 0 ; r--)
00389 if (mRoisOfPositives[r].videoId != prevVideoId)
00390 {
00391 videoIds.insert(videoIds.begin(), mRoisOfPositives[r].videoId);
00392 frameNrs.insert(frameNrs.begin(), mRoisOfPositives[r].frameNr);
00393 prevVideoId = mRoisOfPositives[r].videoId;
00394 }
00395 }
00396 }
00397
00398
00399
00400
00401
00402
00403
00404 private:
00405
00406 ViewStrip* mSimilarsViewStrip;
00407 ViewStrip* mPositivesViewStrip;
00408 RoiInfo mSubjectRoiInfo;
00409 RoiLoader mRoiLoader;
00410
00411 GuiEventListener* mGuiEventListener;
00412 const std::vector<Visualization::Video>& mVideos;
00413 std::vector<RoiInfo> mRoisOfPositives;
00414 std::vector<RoiInfo> mRoisOfSimilars;
00415 int mSimilarLoadingId;
00416
00417 StaticText* mViewInfoText;
00418 Button* mClearSimilars;
00419 Button* mClearPositives;
00420 DirectionButton* mSelectedToPositives;
00421
00422
00423 void AddPositive(OGLVIEW* oglView)
00424 {
00425
00426
00427 RoiInfo newRoi = OglViewGetRoiInfo(oglView);
00428 float frameTimeNewRoi = newRoi.frameNr * mVideos[newRoi.videoId].GetTimePerFrame();
00429 std::vector<RoiInfo>::iterator iter = mRoisOfPositives.begin();
00430 int insertAt = 0;
00431 while (iter != mRoisOfPositives.end())
00432 {
00433 RoiInfo roi = *iter;
00434 float frameTimeRoi = roi.frameNr * mVideos[roi.videoId].GetTimePerFrame();
00435
00436 if (frameTimeRoi > frameTimeNewRoi)
00437 break;
00438 else if (roi.frameNr == newRoi.frameNr)
00439 if (roi.videoId == newRoi.videoId && roi.roiNr == newRoi.roiNr)
00440 return;
00441 iter++;
00442 insertAt++;
00443 }
00444 mRoisOfPositives.insert(iter, newRoi);
00445
00446
00447 std::vector<OGLIMAGE*> currentPositives = GetPositives();
00448
00449 viewSys.DeleteViews(mPositivesViewStrip->GetOGLWND());
00450 mPositivesViewStrip->LayoutViews();
00451 View* view;
00452 for (int i = 0; i <= currentPositives.size(); i++)
00453 {
00454 if (i < insertAt)
00455 view = mPositivesViewStrip->AddImage(currentPositives[i]);
00456 else if (i == insertAt)
00457 view = mPositivesViewStrip->AddImage(oglView->im);
00458 else
00459 view = mPositivesViewStrip->AddImage(currentPositives[i-1]);
00460
00461
00462
00463
00464
00465 RoiInfo roiInfo = mRoisOfPositives[i];
00466
00467 OglViewSetRoiInfo(view->GetOGLVIEW2D(), &roiInfo);
00468 }
00469
00470
00471 for (int i=0; i<currentPositives.size(); i++)
00472 oglSys.ReleaseOglImage(currentPositives[i]);
00473
00474 }
00475
00476 };
00477
00478 }
00479 }
00480 }
00481
00482 #endif