00001 #ifndef Impala_Visualization_FeatureTableSetRank_h
00002 #define Impala_Visualization_FeatureTableSetRank_h
00003
00004 #include "Core/Vector/Similarity.h"
00005 #include "Core/Vector/Norm1Dist.h"
00006 #include "Core/Feature/FeatureTableSet.h"
00007 #include "Core/Table/SimilarityTableSet.h"
00008 #include "Core/VideoSet/SegmentationDocument.h"
00009 #include "OglGui/StringSelector.h"
00010 #include "OglGui/StringListener.h"
00011 #include "OglGui/TextArea.h"
00012 #include "Visualization/ImageSet.h"
00013 #include "Visualization/AppControlDoc.h"
00014 #include "Visualization/Plot/Plot.h"
00015 #include "Visualization/Plot/Line.h"
00016
00017 namespace Impala
00018 {
00019 namespace Visualization
00020 {
00021
00022
00023 class FeatureTableSetRank :
00024 public Window, public AppControlDoc,
00025 public Core::Database::DataDocumentListener, public ImagesListener,
00026 public OglGui::StringListener
00027 {
00028 public:
00029 typedef Core::Feature::FeatureTableSet FeatureTableSet;
00030 typedef Core::Feature::FeatureTable FeatureTable;
00031 typedef Core::Vector::VectorTem<Real64> VectorReal64;
00032 typedef Core::Table::SimilarityTableSet SimilarityTableSet;
00033 typedef SimilarityTableSet::RankTableType RankTableType;
00034 typedef SimilarityTableSet::SimTableType SimTableType;
00035 typedef Core::VideoSet::SegmentationDocument SegmentationDocument;
00036
00037 FeatureTableSetRank(OglGui::Window* parent, int wndWidth, int wndHeight,
00038 int nrImOnRow, int thumbWidth, int thumbHeight,
00039 double viewScale, SegmentationDocument* segDoc,
00040 FeatureTableSet* featSet, bool showPlot,
00041 int controlId) :
00042 Window(parent, wndWidth, wndHeight, true),
00043 AppControlDoc(controlId, segDoc)
00044 {
00045 mSegDoc = segDoc;
00046 mFeatSet = featSet;
00047 mCurFeat = -1;
00048 mCurCompare = -1;
00049 std::vector<std::string> names;
00050 names.push_back("sim");
00051 int t = 0;
00052 mSimSet = new SimilarityTableSet(names, mFeatSet->GetTable(t)->Size());
00053 Core::Table::Copy(mSimSet->GetQuidTable(), mFeatSet->GetTable(t));
00054 mLineSize1 = -1;
00055 mLineData1 = 0;
00056 mLineSize2 = -1;
00057 mLineData2 = 0;
00058 mLineSize3 = -1;
00059 mLineData3 = 0;
00060 mKeyframes = mSegDoc->GetKeyframes();
00061 mKeyframeSet = mSegDoc->GetImSetKeyframes(true);
00062 AddDocListener(this);
00063
00064 mStringSelector = new OglGui::StringSelector(this, wndWidth, 120);
00065 mStringSelector->SetStringListener(this, 0);
00066 int heightUsed = 120;
00067
00068 mPlot1 = 0;
00069 if (showPlot)
00070 {
00071 mPlot1 = new Visualization::Plot::Plot(this, wndWidth, 200);
00072 heightUsed += 200;
00073 mLine1 = new Visualization::Plot::Line(0);
00074 mPlot1->Add(mLine1);
00075 mPlot1->AutoScale();
00076 mPlot1->SetDrawAxes(true);
00077
00078 mPlot2 = new Visualization::Plot::Plot(this, wndWidth, 200);
00079 heightUsed += 200;
00080 mLine2 = new Visualization::Plot::Line(0);
00081 mLine2->SetColor(0, 1, 0);
00082 mPlot2->Add(mLine2);
00083 mLine3 = new Visualization::Plot::Line(0);
00084 mPlot2->Add(mLine3);
00085 mPlot2->AutoScale();
00086 mPlot2->SetDrawAxes(true);
00087 }
00088
00089 mInfoText = new OglGui::TextArea(this, wndWidth, 20, "info");
00090 mInfoText->SetBorderType(0);
00091 heightUsed += 20;
00092
00093 int nrRow = (wndHeight - heightUsed) / (thumbHeight + 6);
00094 mImagesSet = new ImageSet(this, thumbWidth, thumbHeight, viewScale,
00095 nrImOnRow, nrRow);
00096 mImagesSet->ActivateInfoBox(false);
00097 mImagesSet->SetImagesListener(this, 0);
00098
00099 for (int i=0 ; i<mFeatSet->Size() ; i++)
00100 {
00101 mStringSelector->AddString(mFeatSet->GetFeatureDefinition(i).AsString());
00102 }
00103 }
00104
00105 virtual void
00106 HandleNewFile()
00107 {
00108 mImagesSet->RemoveImages();
00109 mImagesSet->SetFirstVisY(0, true);
00110 mCurCompare = -1;
00111 if (!(mSegDoc->HasCursor() && mKeyframes && mKeyframeSet))
00112 return;
00113 mCurFeat = mStringSelector->GetCurrentStringIdx();
00114 FeatureTable* featTab = mFeatSet->GetTable(mCurFeat);
00115 int curKey = mSegDoc->CurKeyfr();
00116 Quid curQuid = mKeyframes->GetQuidFrame(curKey);
00117 VectorReal64 curKeyVec = featTab->FindFeature(curQuid);
00118 Real64* s = mSimSet->GetSimTable(0)->GetColumn1()->GetData();
00119 Core::Vector::Similarity(s, curKeyVec, featTab->GetColumn2(),
00120 0, 1, Core::Vector::Norm1Dist<Real64>);
00121 int nr = Min(featTab->Size(), 200);
00122 mSimSet->ComputeRanksTopN(nr, false);
00123 UpdateLineSize(nr, curKeyVec.Size());
00124 if (mPlot1)
00125 {
00126 for (int i=0 ; i<curKeyVec.Size() ; i++)
00127 {
00128 mLineData2[i] = curKeyVec.Elem(i);
00129 mLineData3[i] = curKeyVec.Elem(0);
00130 }
00131 }
00132 RankTableType* rankTable = mSimSet->GetRankTable(0);
00133 SimTableType* simTable = mSimSet->GetSimTable(0);
00134 for (int i=0 ; i<nr ; i++)
00135 {
00136 Quid quid = rankTable->Get1(i);
00137 int rank = mSimSet->FindQuid(quid);
00138 int keyfr = mKeyframes->GetFrameId(quid);
00139 if (keyfr == -1)
00140 {
00141 ILOG_ERROR("Unable to find keyframe for " << QuidObj(quid));
00142 }
00143 else
00144 {
00145 mImagesSet->AddImage(keyfr, mKeyframeSet);
00146 }
00147 if (mPlot1)
00148 if (i<mLineSize1)
00149 mLineData1[i] = simTable->Get1(rank);
00150 }
00151 UpdateLinePlot();
00152 }
00153
00154 virtual void
00155 StringSelectionEvent(OglGui::StringSelector* src, int strIndex,
00156 void* listenerData)
00157 {
00158 HandleNewFile();
00159 }
00160
00161 virtual void
00162 ImageSelectionEvent(ImagesWindow* src, int imIndex, void* listenerData)
00163 {
00164 RankTableType* rank = mSimSet->GetRankTable(0);
00165 Quid quid = rank->Get1(imIndex);
00166 mCurCompare = mSimSet->FindQuid(quid);
00167 SimTableType* simTable = mSimSet->GetSimTable(0);
00168 mInfoText->SetText("rank = " + MakeString(imIndex) + ", sim = " +
00169 MakeString(simTable->Get1(mCurCompare)));
00170 if (mPlot1)
00171 {
00172 FeatureTable* featTab = mFeatSet->GetTable(mCurFeat);
00173 VectorReal64 vec = featTab->Get2(mCurCompare);
00174 for (int i=0 ; i<vec.Size() ; i++)
00175 mLineData3[i] = vec.Elem(i);
00176 UpdateLinePlot();
00177 }
00178 }
00179
00180 private:
00181
00182 void
00183 UpdateLineSize(int nrCandidates, int featVecLen)
00184 {
00185 if (!mPlot1)
00186 return;
00187 mLineSize1 = Min(200, nrCandidates);
00188 mLineSize2 = featVecLen;
00189 mLineSize3 = featVecLen;
00190 if (mLineData1)
00191 delete mLineData1;
00192 if (mLineData2)
00193 delete mLineData2;
00194 if (mLineData3)
00195 delete mLineData3;
00196 mLineData1 = new double[mLineSize1];
00197 mLineData2 = new double[mLineSize2];
00198 mLineData3 = new double[mLineSize3];
00199 }
00200
00201 void
00202 UpdateLinePlot()
00203 {
00204 if (!mPlot1)
00205 return;
00206
00207 mLine1->SetData(mLineData1, mLineSize1);
00208 mLine1->SetDomain(0, mLineSize1);
00209 mPlot1->AutoScale();
00210
00211 mLine2->SetData(mLineData2, mLineSize2);
00212 mLine2->SetDomain(0, mLineSize2);
00213 if (mCurCompare == -1)
00214 mLine3->SetData(mLineData3, 1);
00215 else
00216 mLine3->SetData(mLineData3, mLineSize3);
00217 mLine3->SetDomain(0, mLineSize3);
00218 mPlot2->AutoScale();
00219 }
00220
00221 SegmentationDocument* mSegDoc;
00222 FeatureTableSet* mFeatSet;
00223 int mCurFeat;
00224 SimilarityTableSet* mSimSet;
00225 int mCurCompare;
00226 Core::VideoSet::Keyframes* mKeyframes;
00227 Core::ImageSet::ImageSet* mKeyframeSet;
00228
00229 int mLineSize1;
00230 int mLineSize2;
00231 int mLineSize3;
00232 double* mLineData1;
00233 double* mLineData2;
00234 double* mLineData3;
00235 Visualization::Plot::Line* mLine1;
00236 Visualization::Plot::Line* mLine2;
00237 Visualization::Plot::Line* mLine3;
00238 Visualization::Plot::Plot* mPlot1;
00239 Visualization::Plot::Plot* mPlot2;
00240
00241 OglGui::StringSelector* mStringSelector;
00242 OglGui::TextArea* mInfoText;
00243 ImageSet* mImagesSet;
00244
00245 ILOG_VAR_DEC;
00246 };
00247
00248 ILOG_VAR_INIT(FeatureTableSetRank, Visualization);
00249
00250 }
00251 }
00252
00253 #endif