From 49863bbf0e4924672ef7f48d3725a2048853699a Mon Sep 17 00:00:00 2001
From: even <philippe.even@loria.fr>
Date: Sat, 29 Dec 2018 21:25:50 +0100
Subject: [PATCH] stats output

---
 Code/Seg/BSTools/bsdetectionwidget.cpp | 148 ++++++++++++++++++++++---
 Code/Seg/BSTools/bsdetectionwidget.h   |   7 +-
 2 files changed, 135 insertions(+), 20 deletions(-)

diff --git a/Code/Seg/BSTools/bsdetectionwidget.cpp b/Code/Seg/BSTools/bsdetectionwidget.cpp
index d8c432d..231311e 100755
--- a/Code/Seg/BSTools/bsdetectionwidget.cpp
+++ b/Code/Seg/BSTools/bsdetectionwidget.cpp
@@ -37,7 +37,6 @@ BSDetectionWidget::BSDetectionWidget (QWidget *parent)
   idetview = NULL;
 
   // Sets initial user outputs parameters
-  alternate = 0;
   verbose = false;
   stats = false;
   background = BACK_IMAGE;
@@ -331,7 +330,6 @@ void BSDetectionWidget::mouseReleaseEvent (QMouseEvent *event)
   }
   else
   {
-    alternate = 0;
     cerr << "p1 defined: " << p1.x () << " " << p1.y () << endl;
     cerr << "p2 defined: " << p2.x () << " " << p2.y () << endl;
     detector.resetMaxDetections ();
@@ -782,6 +780,10 @@ void BSDetectionWidget::keyPressEvent (QKeyEvent *event)
       displayDetectionResult ();
       break;
 
+    case Qt::Key_Dollar :
+      writeTest ();
+      break;
+
     case Qt::Key_1 :
       switchPixelAnalyzer ();
       break;
@@ -955,6 +957,16 @@ void BSDetectionWidget::displayBackground ()
 }
 
 
+void BSDetectionWidget::writeTest ()
+{
+  ofstream outf ("test.txt", ios::out);
+  outf << p1.x() << " " << p1.y () << endl;
+  outf << p2.x() << " " << p2.y () << endl;
+  outf.close ();
+  cout << "Selection saved in test.txt" << endl;
+}
+
+
 void BSDetectionWidget::writeDetectionResult ()
 {
   BlurredSegment *bs = detector.getBlurredSegment ();
@@ -1099,6 +1111,8 @@ void BSDetectionWidget::clearSavedSegments ()
 
 void BSDetectionWidget::writeStats ()
 {
+  int longEdgeThreshold = 1600;
+
   vector<BlurredSegment *> bss = detector.getBlurredSegments ();
   if (bss.empty ())
   {
@@ -1123,15 +1137,18 @@ void BSDetectionWidget::writeStats ()
   {
     int nbdssnull = 0;
     double ltotal = 0.;
+    int lcount = 0;
     double wtotal = 0.;
     vector<BlurredSegment *>::iterator it = bss.begin ();
     while (it != bss.end ())
     {
       Pt2i ptb = (*it)->getLastRight ();
       Pt2i ptf = (*it)->getLastLeft ();
-      double length = sqrt ((ptb.x () - ptf.x ()) * (ptb.x () - ptf.x ())
-                            + (ptb.y () - ptf.y ()) * (ptb.y () - ptf.y ()));
+      int length2 = (ptb.x () - ptf.x ()) * (ptb.x () - ptf.x ())
+                    + (ptb.y () - ptf.y ()) * (ptb.y () - ptf.y ());
+      double length = sqrt (length2);
       ltotal += length;
+      if (length2 > longEdgeThreshold) lcount ++;
       DigitalStraightSegment *dss = (*it)->getSegment ();
       if (dss != NULL)
       {
@@ -1144,9 +1161,10 @@ void BSDetectionWidget::writeStats ()
 
     cout << bss.size () << " blurred segments detected on "
          << detector.countOfTrials () << " trials " << endl;
+    cout << lcount << " long (>40) blurred segments detected" << endl;
     cout << "Mean length : " << ltotal / bss.size () << endl;
     cout << "Mean width (per unit length) : " << wtotal / ltotal << endl;
-    cout << nbdssnull << " DSS null" << endl;
+    if (nbdssnull != 0) cout << nbdssnull << " DSS null" << endl;
   }
 }
 
@@ -1223,22 +1241,116 @@ void BSDetectionWidget::extract ()
 
 void BSDetectionWidget::alternateTest ()
 {
-  if (p1.equals (p2))
-  {
-    cout << "Stroke undefined" << endl;
-    return;
-  }
-
-  if (++alternate == 3) alternate = 0;
-  if (alternate == 0)
-  {
-  }
-  else if (alternate == 1)
+  int longEdgeThreshold = 1600;
+  int nbruns = 100;
+  double diff1 = 0.;
+  int nbdssnull = 0;
+  double ltotal = 0.;
+  double lltotal = 0.;
+  int lcount = 0;
+  double wtotal = 0.;
+  double lwtotal = 0.;
+  vector<BlurredSegment *> bss;
+  vector<BlurredSegment *>::iterator it;
+
+  ofstream outf ("perf.txt", ios::out);
+  outf << nbruns << endl;
+  outf << width << endl;
+  outf << height << endl;
+
+  if (detector.oldDetectorOn ()) detector.switchDetector ();
+  cout << "Performance test on "
+       << (detector.oldDetectorOn () ? "old" : "new") << " detector" << endl;
+  clock_t start = clock ();
+  for (int i = 0; i < nbruns; i++) detector.detectAll ();
+  diff1 = (clock () - start) / (double) CLOCKS_PER_SEC;
+  outf << diff1 << endl;
+
+  detector.detectAll ();
+  bss = detector.getBlurredSegments ();
+  it = bss.begin ();
+  while (it != bss.end ())
   {
+    Pt2i ptb = (*it)->getLastRight ();
+    Pt2i ptf = (*it)->getLastLeft ();
+    int length2 = (ptb.x () - ptf.x ()) * (ptb.x () - ptf.x ())
+                  + (ptb.y () - ptf.y ()) * (ptb.y () - ptf.y ());
+    double length = sqrt (length2);
+    ltotal += length;
+    if (length2 > longEdgeThreshold)
+    {
+      lcount ++;
+      lltotal += length;
+    }
+    DigitalStraightSegment *dss = (*it)->getSegment ();
+    if (dss != NULL)
+    {
+      double width = dss->width () / (double) (dss->period ());
+      wtotal += width * length;
+      if (length2 > longEdgeThreshold) lwtotal += width * length;
+    }
+    else nbdssnull ++;
+    it ++;
   }
-  else if (alternate == 2)
+  outf << detector.countOfTrials () << endl;
+  outf << bss.size () << endl;
+  outf << lcount << endl;;
+  outf << ltotal / bss.size () << endl;
+  outf << wtotal / ltotal << endl;
+  outf << lwtotal / lltotal << endl;
+  if (nbdssnull != 0) cout << nbdssnull << " DSS null" << endl;
+
+  diff1 = 0.;
+  nbdssnull = 0;
+  ltotal = 0.;
+  lltotal = 0.;
+  lcount = 0;
+  wtotal = 0.;
+  lwtotal = 0.;
+  detector.switchDetector ();
+  cout << "Performance test on "
+       << (detector.oldDetectorOn () ? "old" : "new") << " detector" << endl;
+  start = clock ();
+  for (int i = 0; i < nbruns; i++) detector.detectAll ();
+  diff1 = (clock () - start) / (double) CLOCKS_PER_SEC;
+  outf << diff1 << endl;
+
+  detector.detectAll ();
+  bss = detector.getBlurredSegments ();
+  it = bss.begin ();
+  while (it != bss.end ())
   {
+    Pt2i ptb = (*it)->getLastRight ();
+    Pt2i ptf = (*it)->getLastLeft ();
+    int length2 = (ptb.x () - ptf.x ()) * (ptb.x () - ptf.x ())
+                  + (ptb.y () - ptf.y ()) * (ptb.y () - ptf.y ());
+    double length = sqrt (length2);
+    ltotal += length;
+    if (length2 > longEdgeThreshold)
+    {
+      lcount ++;
+      lltotal += length;
+    }
+    DigitalStraightSegment *dss = (*it)->getSegment ();
+    if (dss != NULL)
+    {
+      double width = dss->width () / (double) (dss->period ());
+      wtotal += width * length;
+      if (length2 > longEdgeThreshold) lwtotal += width * length;
+    }
+    else nbdssnull ++;
+    it ++;
   }
+  outf << detector.countOfTrials () << endl;
+  outf << bss.size () << endl;
+  outf << lcount << endl;
+  outf << ltotal / bss.size () << endl;
+  outf << wtotal / ltotal << endl;
+  outf << lwtotal / lltotal << endl;
+  if (nbdssnull != 0) cout << nbdssnull << " DSS null" << endl;
+
+  outf.close ();
+  cout << "Selection saved in perf.txt" << endl;
 } 
 
 
@@ -1331,7 +1443,7 @@ QSize BSDetectionWidget::setRandomImage (int type)
   vector<BlurredSegment *> rbs[nbsegs];
    
   bool dispEach = false;
-  int nbruns = 100;
+  int nbruns = 1000;
   int nbIniPts[nbruns];
   int oldTrials[nbruns];
   int newTrials[nbruns];
diff --git a/Code/Seg/BSTools/bsdetectionwidget.h b/Code/Seg/BSTools/bsdetectionwidget.h
index 5aced00..114b709 100755
--- a/Code/Seg/BSTools/bsdetectionwidget.h
+++ b/Code/Seg/BSTools/bsdetectionwidget.h
@@ -222,8 +222,6 @@ private:
   bool udef;
   /** Saved user definition flag. */
   bool oldudef;
-  /** Activation of alternate comparative tests (F8). */
-  int alternate;
 
   /** Kind of highlight colors. */
   bool darkHighlightOn;
@@ -360,6 +358,11 @@ private:
    */
   void writeStats ();
 
+  /**
+   * \brief Writes the selection stroke in test.txt.
+   */
+  void writeTest ();
+
   /**
    * \brief Writes the result of the last detection in a file.
    */
-- 
GitLab