Skip to content
Snippets Groups Projects
bsdetectionwidget.cpp 45 KiB
Newer Older
even's avatar
even committed
#include <iostream>
even's avatar
even committed
#include <fstream>
even's avatar
even committed
#include <cstdlib>
#include <ctime>
even's avatar
even committed
#include <cmath>
even's avatar
even committed
#include "bsdetectionwidget.h"

using namespace std;


even's avatar
even committed
const int BSDetectionWidget::BACK_BLACK = 0;
const int BSDetectionWidget::BACK_WHITE = 1;
const int BSDetectionWidget::BACK_IMAGE = 2;
const int BSDetectionWidget::BACK_GRAD = 3;
const int BSDetectionWidget::BACK_GRADX = 4;
const int BSDetectionWidget::BACK_GRADY = 5;

even's avatar
even committed
const int BSDetectionWidget::DEFAULT_PEN_WIDTH = 1;



BSDetectionWidget::BSDetectionWidget (QWidget *parent)
{
  Q_UNUSED (parent);

even's avatar
even committed
  // Sets initial user inputs parameters
even's avatar
even committed
  setFocus ();
  grabKeyboard ();
even's avatar
even committed
  udef = false;
even's avatar
even committed
  nodrag = true;
even's avatar
even committed

even's avatar
even committed
  // Initializes the gradient map and the auxiliary views
even's avatar
even committed
  gMap = NULL;
even's avatar
even committed
  // accuview = NULL;
even's avatar
even committed
  strucview = NULL;
  profileview = NULL;
even's avatar
even committed
  idetview = NULL;
even's avatar
even committed
  cannyview = NULL;
  yorkview = NULL;
even's avatar
even committed

even's avatar
even committed
  // Sets initial user outputs parameters
even's avatar
even committed
  verbose = false;
even's avatar
even committed
  statsOn = false;
even's avatar
even committed
  background = BACK_IMAGE;
even's avatar
even committed
  bsBoundsVisible = false;
even's avatar
even committed
  blevel = 0;
even's avatar
even committed

even's avatar
even committed
  // Sets display parameters
  darkHighlightOn = false;
  arlequinOn = true;
even's avatar
even committed
  selectionColor = Qt::red;
even's avatar
even committed
  bsColor = Qt::blue;
  bsHighColor = Qt::yellow;
even's avatar
even committed
  bsPointsVisible = true;
even's avatar
even committed
  boundColor = Qt::green;
  boundHighColor = Qt::magenta;
even's avatar
even committed

  maxWidth = 768;
  maxHeight = 512;
  xMaxShift = 0;
  yMaxShift = 0;
  xShift = 0;
  yShift = 0;
  zoom = 1;
even's avatar
even committed
}


BSDetectionWidget::~BSDetectionWidget ()
{
even's avatar
even committed
  // if (accuview != NULL) delete accuview;
even's avatar
even committed
  if (strucview != NULL) delete strucview;
even's avatar
even committed
  if (profileview != NULL) delete profileview;
  if (idetview != NULL) delete idetview;
even's avatar
even committed
  if (cannyview != NULL) delete cannyview;
  if (yorkview != NULL) delete yorkview;
even's avatar
even committed
}


QSize BSDetectionWidget::openImage (const QString &fileName, int type)
{
  QSize newSize (0, 0);
  loadedImage.load (fileName);
  width = loadedImage.width ();
  height = loadedImage.height ();
  newSize = loadedImage.size ();
 
  augmentedImage = loadedImage;
  if (gMap != NULL) delete gMap;
  gMap = new VMap (width, height, getBitmap (augmentedImage), type);
  detector.setGradientMap (gMap);
even's avatar
even committed
  buildGradientImage (0);
even's avatar
even committed
 
  update ();
even's avatar
even committed
  if (idetview != NULL) idetview->setImage (&loadedImage, gMap);
even's avatar
even committed
  if (profileview != NULL) profileview->setImage (&loadedImage, gMap);
even's avatar
even committed
  if (strucview != NULL) strucview->setGradientImage (&gradImage);
even's avatar
even committed

even's avatar
even committed
  xMaxShift = (width > maxWidth ? maxWidth - width : 0);
  yMaxShift = (height > maxHeight ? maxHeight - height : 0);
  xShift = xMaxShift / 2;
  if (xShift > 0) xShift = 0;
  yShift = yMaxShift / 2;
  if (yShift > 0) yShift = 0;
  return newSize.boundedTo (QSize (maxWidth, maxHeight));
even's avatar
even committed
}


even's avatar
even committed
void BSDetectionWidget::setDefaults ()
{
  ifstream input ("gradient.txt", ios::in);
  if (input)
  {
    int gtval = gMap->getGradientThreshold ();
    input >> gtval;
    if (gtval >= 0 && gtval < 256)
      gMap->incGradientThreshold (gtval - gMap->getGradientThreshold ());
  }
}


even's avatar
even committed
int **BSDetectionWidget::getBitmap (const QImage &image)
{
  int w = image.width ();
  int h = image.height ();
  
  int **tabImage = new int*[h];
  for (int i = 0; i < h; i++)
  {
    tabImage[i] = new int[w];
    for(int j = 0; j < w; j++)
    {
      QColor c = QColor (image.pixel (j, h - i - 1));
      tabImage[i][j] = c.value ();
    }
  }
  return tabImage;
}


even's avatar
even committed
void BSDetectionWidget::toggleBackground ()
{
  if (background++ == BACK_GRADY) background = BACK_BLACK;
  if (background >= BACK_GRAD) buildGradientImage (background - BACK_GRAD);
}


even's avatar
even committed
bool BSDetectionWidget::saveAugmentedImage (const QString &fileName,
                                            const char *fileFormat)
{
  QImage aImage = augmentedImage;
  return (aImage.save (fileName, fileFormat));
}


void BSDetectionWidget::clearImage ()
{
  augmentedImage.fill (qRgb (255, 255, 255));
  update ();
}


even's avatar
even committed
void BSDetectionWidget::buildGradientImage (int dir)
{
  int w = gMap->getWidth ();
  int h = gMap->getHeight ();
even's avatar
even committed
  double *gn = new double[w * h];
  // double gn[w * h];
even's avatar
even committed
  for (int j = 0; j < h; j++)
    for (int i = 0; i < w; i++)
    {
      if (dir == 2)
        gn[j * w + i] = gMap->getValue(i,j).y ();
      else if (dir == 1)
        gn[j * w + i] = gMap->getValue(i,j).x ();
      else
        gn[j * w + i] = gMap->magn (i, j);
    }
  double min = gn[0];
  double max = gn[0];
  for (int i = 1; i < w * h; i++)
  {
    if (max < gn[i]) max = gn[i];
    if (min > gn[i]) min = gn[i];
  }
  gradImage = QImage (w, h, QImage::Format_RGB32);
  for (int j = 0; j < h; j++)
    for (int i = 0; i < w; i++)
    {
      int val = (int) ((gn[(h - 1 - j) * w + i] - min) * 255 / (max - min));
      gradImage.setPixel (i, j, val + val * 256 + val * 256 * 256);
    }
  // gradImage.save ("hgradient.png");
}


even's avatar
even committed
void BSDetectionWidget::paintEvent (QPaintEvent *)
{
  QPainter painter (this);
even's avatar
even committed
  QImage zoomImage = augmentedImage.scaled (width / zoom, height / zoom);
  painter.drawImage (QPoint (xShift, yShift), zoomImage);
even's avatar
even committed
}


even's avatar
even committed
/*
even's avatar
even committed
void BSDetectionWidget::closeAccuAnalyzer ()
{
  if (accuview != NULL)
  {
    accuview->close ();
    delete accuview;
    accuview = NULL;
  }
}
even's avatar
even committed
*/
even's avatar
even committed


void BSDetectionWidget::closePixelAnalyzer ()
{
  if (strucview != NULL)
  {
    strucview->close ();
    delete strucview;
    strucview = NULL;
  }
}


void BSDetectionWidget::closeProfileAnalyzer ()
{
  if (profileview != NULL)
  {
    profileview->close ();
    delete profileview;
    profileview = NULL;
  }
}


even's avatar
even committed
void BSDetectionWidget::closeIdetAnalyzer ()
{
  if (idetview != NULL)
  {
    idetview->close ();
    delete idetview;
    idetview = NULL;
  }
}


even's avatar
even committed
/*
even's avatar
even committed
void BSDetectionWidget::switchAccuAnalyzer ()
{
  if (accuview != NULL)
  {
    accuview->close ();
    delete accuview;
    accuview = NULL;
  }
  else
  {
    accuview = new BSAccumulatorView (&detector);
    accuview->show ();
  }
}
even's avatar
even committed
*/
even's avatar
even committed


void BSDetectionWidget::switchPixelAnalyzer ()
{
  if (strucview != NULL)
  {
    strucview->close ();
    delete strucview;
    strucview = NULL;
  }
  else
  {
    strucview = new BSStructureView (&loadedImage, &detector);
even's avatar
even committed
    strucview->setGradientImage (&gradImage);
even's avatar
even committed
    strucview->show ();
  }
}


void BSDetectionWidget::switchProfileAnalyzer ()
{
  if (profileview != NULL)
  {
    profileview->close ();
    delete profileview;
    profileview = NULL;
  }
  else
  {
    profileview = new BSProfileView ();
    profileview->setImage (&loadedImage, gMap);
    if (! p1.equals (p2)) profileview->buildScans (p1, p2);
    profileview->show ();
  }
}


even's avatar
even committed
void BSDetectionWidget::switchIdetAnalyzer ()
{
  if (idetview != NULL)
  {
    idetview->close ();
    delete idetview;
    idetview = NULL;
  }
  else
  {
    idetview = new BSIdetView (&detector);
    idetview->setImage (&loadedImage, gMap);
even's avatar
even committed
    idetview->update ();
even's avatar
even committed
    idetview->show ();
  }
}


void BSDetectionWidget::switchHighlightColors ()
{
  darkHighlightOn = ! darkHighlightOn;
  if (darkHighlightOn)
  {
    bsHighColor = Qt::black;
    boundHighColor = Qt::blue;
  }
  else
  {
    bsHighColor = Qt::yellow;
    boundHighColor = Qt::magenta;
  }
}


void BSDetectionWidget::switchArlequin ()
{
  arlequinOn = ! arlequinOn;
}


even's avatar
even committed
void BSDetectionWidget::mousePressEvent (QMouseEvent *event)
{
even's avatar
even committed
  oldp1.set (p1);
  oldp2.set (p2);
even's avatar
even committed
  oldudef = udef;
even's avatar
even committed
  int ex = zoom * (event->pos().x () - xShift);
  int ey = zoom * (event->pos().y () - yShift);
  p1 = Pt2i (ex, height - 1 - ey);
even's avatar
even committed
  if (p1.manhattan (p2) < 10) p1.set (oldp1);
  else if (p1.manhattan (oldp1) < 10) p1.set (p2);
even's avatar
even committed
  udef = true;
even's avatar
even committed
}


void BSDetectionWidget::mouseReleaseEvent (QMouseEvent *event)
{
even's avatar
even committed
  int ex = zoom * (event->pos().x () - xShift);
  int ey = zoom * (event->pos().y () - yShift);
  p2 = Pt2i (ex, height - 1 - ey);
even's avatar
even committed
  if (p1.equals (p2))
  {
    p1.set (oldp1);
    p2.set (oldp2);
even's avatar
even committed
    udef = oldudef;
even's avatar
even committed
  }
  else
  {
    cerr << "p1 defined: " << p1.x () << " " << p1.y () << endl;
    cerr << "p2 defined: " << p2.x () << " " << p2.y () << endl;
    detector.resetMaxDetections ();
even's avatar
even committed
    extract ();
  }
even's avatar
even committed
}


void BSDetectionWidget::mouseMoveEvent (QMouseEvent *event)
{
even's avatar
even committed
  int ex = zoom * (event->pos().x () - xShift);
  int ey = zoom * (event->pos().y () - yShift);
  p2 = Pt2i (ex, height - 1 - ey);
even's avatar
even committed
  if (verbose) cerr << "(" << p1.x () << ", " << p1.y () << ") ("
                    << p2.x () << ", " << p2.y () << ")" << endl;
  if (p1.manhattan (p2) > 5
      && (width > p2.x() && height > p2.y()
          && p2.x() > 0 && p2.y() > 0))
  {
even's avatar
even committed
    nodrag = false;
    extract ();
    nodrag = true;
even's avatar
even committed
    detector.setMaxTrials (0);
even's avatar
even committed
  }
}


void BSDetectionWidget::keyPressEvent (QKeyEvent *event)
{
even's avatar
even committed
  int count = 0;
even's avatar
even committed
  if (isActiveWindow ()) switch (event->key ())
  {
even's avatar
even committed
    case Qt::Key_A :
      // Registers the last extracted blurred segment
even's avatar
even committed
      count = saveExtractedSegment ();
      cout << count << " new segment(s) -> "
           << extractedSegments.size () << " segments registered" << endl;
even's avatar
even committed
      break;

even's avatar
even committed
    case Qt::Key_B :
      if (event->modifiers () & Qt::ControlModifier)
even's avatar
even committed
      {
even's avatar
even committed
        // Toggles background image
        toggleBackground ();
        if (p1.equals (p2)) displayBackground ();
even's avatar
even committed
        else displayDetectionResult ();
even's avatar
even committed
      }
even's avatar
even committed
      else
      {
        // Tunes the gradient resolution for gradient local max filtering
        detector.incGradientResolution (
          (event->modifiers () & Qt::ShiftModifier ? -1 : 1));
        cout << "Gradient resolution = "
             << detector.getGradientResolution () << endl;
        extract ();
      }
even's avatar
even committed
      break;

    case Qt::Key_C :
      // Clears the registered blurred segments
even's avatar
even committed
      cout << "Withdraws registered segments" << endl;
even's avatar
even committed
      clearSavedSegments ();
      break;

    case Qt::Key_D :
      if (event->modifiers () & Qt::ControlModifier)
even's avatar
even committed
      {
even's avatar
even committed
        // Switches density test at initial step
        detector.switchDensityTest ();
        cout << "Density test : "
even's avatar
even committed
             << (detector.isDensityTestOn () ? "on" : "off") << endl;
even's avatar
even committed
        extract ();
even's avatar
even committed
      }
even's avatar
even committed
      break;

even's avatar
even committed
    case Qt::Key_E :
      // Handles directed edge or stroke detection
      if (event->modifiers () & Qt::ControlModifier)
        detector.switchEdgeDirectionConstraint ();
      else detector.invertEdgeDirection ();
      switch (detector.edgeDirectionConstraint ())
even's avatar
even committed
      {
even's avatar
even committed
        case 0 :
even's avatar
even committed
          cout << "Line detection mode set" << endl;
even's avatar
even committed
          break;
        case 1 :
even's avatar
even committed
          cout << "Main edge detection mode set" << endl;
even's avatar
even committed
          break;
        case -1 :
even's avatar
even committed
          cout << "Opposite edge detection mode set" << endl;
even's avatar
even committed
          break;
even's avatar
even committed
      }
even's avatar
even committed
      extract ();
      break;

    case Qt::Key_F :
      if (event->modifiers () & Qt::ControlModifier)
even's avatar
even committed
        // Switches initial detection filtering
        detector.switchFiltering (BSDetector::STEP_INITIAL);
        cout << "Pre-filtering "
             << (detector.isFiltering (BSDetector::STEP_INITIAL) ? "on" : "off")
             << endl;
        extract ();
even's avatar
even committed
      break;

even's avatar
even committed
    case Qt::Key_G :
even's avatar
even committed
      if (event->modifiers () & Qt::ControlModifier)
      {
        // Switches length test at final step
        detector.switchFinalLengthTest ();
        cout << "Final length test : "
             << (detector.isFinalLengthTestOn () ? "on" : "off") << endl;
even's avatar
even committed
        extract ();
even's avatar
even committed
      }
      else
      {
        // Tunes the gradient threshold for maximal value detection
even's avatar
even committed
        detector.incSensitivity (
even's avatar
even committed
          (event->modifiers () & Qt::ShiftModifier ? -1 : 1));
even's avatar
even committed
        cout << "Sensitivity = " << detector.getSensitivity () << endl;
even's avatar
even committed
        extract ();
      }
even's avatar
even committed
      break;

    case Qt::Key_H :
      if (event->modifiers () & Qt::ControlModifier)
even's avatar
even committed
      {
even's avatar
even committed
        // Switches final detection filtering
        detector.switchFiltering (BSDetector::STEP_FINAL);
        cout << "Final filtering "
             << (detector.isFiltering (BSDetector::STEP_FINAL) ? "on" : "off")
             << endl;
        extract ();
even's avatar
even committed
      }
even's avatar
even committed
      break;

even's avatar
even committed
    case Qt::Key_J :
      if (event->modifiers () & Qt::ControlModifier)
      {
even's avatar
even committed
        // Switches the proximity constraint for fast tracks
        detector.switchFastTrackProximityConstraint ();
        cout << "Proximity constraint on fast tracks "
             << (detector.fastTrackProximityConstraintOn () ? "on" : "off")
even's avatar
even committed
             << endl;
        extract ();
      }
even's avatar
even committed
      else if (detector.fastTrackProximityConstraintOn ())
even's avatar
even committed
      {
even's avatar
even committed
        // Tunes the proximity threshold for fast tracks
        detector.incFastTrackProximityThreshold (
even's avatar
even committed
                    (event->modifiers () & Qt::ShiftModifier) == 0);
even's avatar
even committed
        cout << "Proximity threshold for fast tracks = "
             << detector.getFastTrackProximityThreshold () << endl;
even's avatar
even committed
        extract ();
      }
      break;

even's avatar
even committed
    case Qt::Key_K :
      if (event->modifiers () & Qt::ControlModifier)
      {
        // Switches the final step connectivity constraint
        detector.switchConnectivityConstraint ();
even's avatar
even committed
        cout << "Fragmentation test "
even's avatar
even committed
             << (detector.isConnectivityConstraintOn () ? "on" : "off")
even's avatar
even committed
             << endl;
        extract ();
      }
even's avatar
even committed
      else
      {
        // Tunes the minimal size of connected components
        detector.incConnectedComponentMinSize (
                    (event->modifiers () & Qt::ShiftModifier) == 0);
even's avatar
even committed
        cout << "Fragments minimal size = "
even's avatar
even committed
             << detector.getConnectedComponentMinSize () << endl;
        extract ();
      }
even's avatar
even committed
      break;

even's avatar
even committed
    case Qt::Key_L :
even's avatar
even committed
      if (event->modifiers () & Qt::ControlModifier)
      {
        // Switches density test at final step
        detector.switchFinalDensityTest ();
        cout << "Final density test : "
             << (detector.isFinalDensityTestOn () ? "on" : "off") << endl;
even's avatar
even committed
        extract ();
even's avatar
even committed
      }
      else
      {
        // Tunes the output blurred segment minimal size
        detector.setBSminSize (detector.getBSminSize () +
          (event->modifiers () & Qt::ShiftModifier ? -1 : 1));
even's avatar
even committed
        cout << "Detected blurred segments min size = "
             << detector.getBSminSize () << endl;
even's avatar
even committed
        extract ();
      }
even's avatar
even committed
      break;

even's avatar
even committed
    case Qt::Key_M :
even's avatar
even committed
      if (event->modifiers () & Qt::ControlModifier)
      {
even's avatar
even committed
        // Switches the multi-detection
        detector.switchMultiSelection ();
        cout << "Multi-selection "
             << (detector.isMultiSelection () ? "on" : "off") << endl;
even's avatar
even committed
      }
      else
      {
even's avatar
even committed
        // Runs an automatic detection
even's avatar
even committed
        udef = false;
        detector.resetMaxDetections ();
even's avatar
even committed
        cout << "Detects all segments" << endl;
even's avatar
even committed
        extract ();
even's avatar
even committed
      }
even's avatar
even committed
      break;

even's avatar
even committed
    case Qt::Key_N :
even's avatar
even committed
      if (event->modifiers () & Qt::ControlModifier)
      {
even's avatar
even committed
        // Switches the initial detection extension limitation
        detector.switchInitialBounding ();
        cout << "Initial step max extension = "
             << detector.initialDetectionMaxExtent () << endl;
even's avatar
even committed
        extract ();
even's avatar
even committed
      }
      else
      {
even's avatar
even committed
        // Highlights the next segment in a multi-detection
        vector<BlurredSegment *> bsl = detector.getBlurredSegments ();
        if (! bsl.empty ())
even's avatar
even committed
        {
          detector.incMaxDetections (event->modifiers () & Qt::ShiftModifier);
          cout << "Selection of segment "
               << detector.getMaxDetections () << endl;
even's avatar
even committed
          extract ();
even's avatar
even committed
        }
      }
      break;

    case Qt::Key_O :
      if (event->modifiers () & Qt::ControlModifier)
      {
        // Switches the scan directionality
        detector.switchOrthoScans ();
        cout << (detector.orthoScansOn () ?
                 "Orthographic scans" : "Directional scans") << endl;
even's avatar
even committed
        extract ();
even's avatar
even committed
      }
      else
      {
        // Outputs the detected segment
even's avatar
even committed
        cout << "Outputs detection result" << endl;
even's avatar
even committed
        writeDetectionResult ();
even's avatar
even committed
      }
      break;

even's avatar
even committed
    case Qt::Key_P :
      if (event->modifiers () & Qt::ControlModifier)
      {
even's avatar
even committed
        // Switchues the preliminary detection
even's avatar
even committed
        detector.switchPreliminary ();
even's avatar
even committed
        cout << "Initial detection duplication "
even's avatar
even committed
             << (detector.isPreliminary () ? "on" : "off") << endl;
        extract ();
      }
      else
      {
        // Captures main window
even's avatar
even committed
        cout << "Saves main window in capture.png" << endl;
even's avatar
even committed
        augmentedImage.save ("capture.png");
      }
even's avatar
even committed
      break;

even's avatar
even committed
    case Qt::Key_Q :
      if (event->modifiers () & Qt::ControlModifier)
      {
        // Switches the scan flexibility
        detector.switchDynamicScans ();
        cout << (detector.dynamicScansOn () ?
                 "Dynamic scans" : "Static scans") << endl;
        extract ();
      }
      else
      {
        // Displays registered blurred segments
even's avatar
even committed
        cout << "Displays all registered segments" << endl;
even's avatar
even committed
        displaySavedSegments ();
      }
even's avatar
even committed
      break;

even's avatar
even committed
    case Qt::Key_R :
even's avatar
even committed
      if (event->modifiers () & Qt::ControlModifier)
      {
        // Toggles the occupancy mask dilation type
        gMap->toggleMaskDilation ();
        cout << "Occupancy mask dilation size : "
             << gMap->getMaskDilation () << endl;
even's avatar
even committed
        extract ();
even's avatar
even committed
      }
      else
      {
        // Tunes the automatic detection grid resolution
        detector.setAutoGridResolution (detector.getAutoGridResolution () +
          (event->modifiers () & Qt::ShiftModifier ? -1 : 1));
even's avatar
even committed
        cout << "Automatic detection grid resolution = "
even's avatar
even committed
             << detector.getAutoGridResolution () << " pixels" << endl;
      }
even's avatar
even committed
      break;

    case Qt::Key_S :
even's avatar
even committed
      if (event->modifiers () & Qt::ControlModifier)
      {
even's avatar
even committed
        // Switches the interruption handling
        detector.switchAutoRestart ();
        cout << "Segment continuation after = "
             << detector.getRestartOnLack () << " pixels" << endl;
even's avatar
even committed
        extract ();
even's avatar
even committed
        // Tunes the pixel lack tolerence value
        detector.setPixelLackTolerence (detector.getPixelLackTolerence () +
          (event->modifiers () & Qt::ShiftModifier ? -1 : 1));
even's avatar
even committed
        cout << "Tolerence to detection lacks = "
             << detector.getPixelLackTolerence () << " pixels" << endl;
even's avatar
even committed
        extract ();
even's avatar
even committed
      break;

even's avatar
even committed
    case Qt::Key_T :
      if (event->modifiers () & Qt::ControlModifier)
      {
        // Switches the progressive thinning
        detector.toggleThinning ();
        if (detector.isThinningOn () && detector.isThickenningOn ())
even's avatar
even committed
          detector.toggleThickenning ();
even's avatar
even committed
        cout << "Thinning "
             << (detector.isThinningOn () ? "on" : "off") << endl;
even's avatar
even committed
        extract ();
even's avatar
even committed
      }
even's avatar
even committed
      break;

even's avatar
even committed
    case Qt::Key_U :
even's avatar
even committed
      if (event->modifiers () & Qt::ControlModifier)
even's avatar
even committed
      {
        // Switches the display of the detected blurred segment bounds
        bsBoundsVisible = ! bsBoundsVisible;
even's avatar
even committed
        cout << "Enclosing segments "
even's avatar
even committed
             << (bsBoundsVisible ? "visible" : "hidden") << endl;
        extract ();
even's avatar
even committed
      }
      else
      {
        // Replays last extraction
        cerr << "p1 update: " << p1.x () << " " << p1.y () << endl;
        cerr << "p2 update: " << p2.x () << " " << p2.y () << endl;
        extract ();
      }
even's avatar
even committed
      break;

    case Qt::Key_V :
      if (event->modifiers () & Qt::ControlModifier)
even's avatar
even committed
        // Switches the detection result edition
        switchVerbose ();
even's avatar
even committed
      break;

even's avatar
even committed
    case Qt::Key_W :
even's avatar
even committed
      if (event->modifiers () & Qt::ControlModifier)
even's avatar
even committed
      {
even's avatar
even committed
        // Switches the scan centering on the detected segment
        detector.switchScanRecentering ();
        cout << "Fine tracking centered on " << (detector.isScanRecentering () ?
                "detected segment" : "initial scan") << endl;
even's avatar
even committed
        extract ();
      }
      else
      {
even's avatar
even committed
        // Tunes the assigned max width margin for fine tracks
        detector.setFastTracksMaxMargin (detector.fastTracksMaxMargin () +
even's avatar
even committed
          (event->modifiers () & Qt::ShiftModifier ? -1 : 1));
even's avatar
even committed
        cout << "Fast tracks max width margin = "
             << detector.fastTracksMaxMargin () << endl;
even's avatar
even committed
        extract ();
even's avatar
even committed
      }
even's avatar
even committed
      break;

even's avatar
even committed
    case Qt::Key_X :
      if (event->modifiers () & Qt::ControlModifier)
even's avatar
even committed
      {
even's avatar
even committed
        // Switches the setting of the assigned width on the detected segment
        detector.switchScanFitting ();
        cout << "Fine tracking fitted to " << (detector.isScanFitting () ?
                "detected segment width" : "assigned width") << endl;
even's avatar
even committed
        extract ();
      }
      else
      {
        // Tunes the assigned max width for fast tracks
        detector.setFineTracksMaxWidth (detector.fineTracksMaxWidth () +
          (event->modifiers () & Qt::ShiftModifier ? -1 : 1));
even's avatar
even committed
        cout << "Initial assigned width = "
even's avatar
even committed
             << detector.fineTracksMaxWidth () << endl;
even's avatar
even committed
        extract ();
even's avatar
even committed
      }
      break;

even's avatar
even committed
    case Qt::Key_Y :
      if (event->modifiers () & Qt::ControlModifier)
      {
        // Switches the display of the detected blurred segment pixels
        bsPointsVisible = ! bsPointsVisible;
even's avatar
even committed
        cout << "Blurred segment "
even's avatar
even committed
             << (bsPointsVisible ? "visible" : "hidden") << endl;
        displayDetectionResult ();
even's avatar
even committed
      }
even's avatar
even committed
      else
      {
        // Tunes the background image black level
        incBlackLevel ((event->modifiers () & Qt::ShiftModifier) ? -1 : 1);
        cout << "Background black level = " << getBlackLevel () << endl;
even's avatar
even committed
        displayDetectionResult ();
even's avatar
even committed
      }
even's avatar
even committed
      break;

even's avatar
even committed
    case Qt::Key_Z :
      if (event->modifiers () & Qt::ControlModifier)
      {
        // Switches the thickenning control
        detector.toggleThickenning ();
        if (detector.isThickenningOn () && detector.isThinningOn ())
even's avatar
even committed
          detector.toggleThinning ();
even's avatar
even committed
        cout << "Assigned width control "
even's avatar
even committed
             << (detector.isThickenningOn () ? "on" : "off") << endl;
even's avatar
even committed
        extract ();
even's avatar
even committed
      }
      else
      {
        // Tunes the thickenning limit
        detector.incThickenningLimit (
even's avatar
even committed
          (event->modifiers () & Qt::ShiftModifier) ? -1 : 1);
even's avatar
even committed
        cout << "Thickenning limit = " << detector.getThickenningLimit ()
             << " pixels" << endl;
even's avatar
even committed
        extract ();
even's avatar
even committed
      }
      break;

    case Qt::Key_Exclam :
      switchHighlightColors ();
even's avatar
even committed
      cout << "Highlight colors "
           << (isHighlightColorsOn () ? "on" : "off") << endl;
      displayDetectionResult ();
      break;

    case Qt::Key_Equal :
      switchArlequin ();
even's avatar
even committed
      cout << "Random coloring " << (isArlequinOn () ? "on" : "off") << endl;
      displayDetectionResult ();
      break;

even's avatar
even committed
    case Qt::Key_Asterisk :
      switchStats ();
even's avatar
even committed
      cout << "Stats display " << (isStatsOn () ? "on" : "off") << endl;
even's avatar
even committed
      displayDetectionResult ();
      break;

even's avatar
even committed
    case Qt::Key_Dollar :
      writeTest ();
even's avatar
even committed
      cout << "Selection stroke saved" << endl;
      break;

    case Qt::Key_Percent :
      if (cannyview == NULL)
        cannyview = new BSCannyView (&loadedImage, &detector);
      cannyview->show ();
      break;

even's avatar
even committed
    case Qt::Key_Semicolon :
      if (yorkview == NULL)
        yorkview = new BSYorkView (&loadedImage, &detector);
      yorkview->show ();
even's avatar
even committed
      break;

even's avatar
even committed
    case Qt::Key_Plus :
      if (zoom > 1)
      {
        zoom /= 2;
        xShift = xShift * 2 - maxWidth / 2;
        yShift = yShift * 2 - maxHeight / 2;
even's avatar
even committed
        cout << "Zoom : " << zoom << endl;
even's avatar
even committed
        displayDetectionResult ();
      }
      break;

    case Qt::Key_Minus :
      if (width / zoom > maxWidth || height / zoom > maxHeight)
      {
        zoom *= 2;
        xShift = xShift / 2 + maxWidth / 4;
        if (xShift > 0) xShift = 0;
        if ((maxWidth - xShift) * zoom > width)
          xShift = maxWidth - width / zoom;
        yShift = yShift / 2 + maxHeight / 4;
        if (yShift > 0) yShift = 0;
        if ((maxHeight - yShift) * zoom > height)
          yShift = maxHeight - height / zoom;
even's avatar
even committed
        cout << "Zoom : " << zoom << endl;
even's avatar
even committed
        displayDetectionResult ();
      }
      break;

    case Qt::Key_Left :
      xShift += 50;
      if (xShift > 0) xShift = 0;
even's avatar
even committed
      cout << "X-shift : " << xShift << endl;
even's avatar
even committed
      displayDetectionResult ();
      break;

    case Qt::Key_Right :
      xShift -= 50;
      if ((maxWidth - xShift) * zoom > width)
        xShift = maxWidth - width / zoom;
even's avatar
even committed
      cout << "X-shift : " << xShift << endl;
even's avatar
even committed
      displayDetectionResult ();
      break;

    case Qt::Key_Up :
      yShift += 50;
      if (yShift > 0) yShift = 0;
even's avatar
even committed
      cout << "Y-shift : " << yShift << endl;
even's avatar
even committed
      displayDetectionResult ();
      break;

    case Qt::Key_Down :
      yShift -= 50;
      if ((maxHeight - yShift) * zoom > height)
        yShift = maxHeight - height / zoom;
even's avatar
even committed
      cout << "Y-shift : " << yShift << endl;
even's avatar
even committed
      displayDetectionResult ();
      break;

even's avatar
even committed
    case Qt::Key_1 :
      switchPixelAnalyzer ();
      break;

    case Qt::Key_2 :
even's avatar
even committed
      // switchAccuAnalyzer ();
even's avatar
even committed
      break;

    case Qt::Key_3 :
      switchProfileAnalyzer ();
      break;

even's avatar
even committed
    case Qt::Key_4 :
      switchIdetAnalyzer ();
      break;
even's avatar
even committed

    case Qt::Key_5 :
      // Switches the crosswise segment detection
      detector.switchTrackCrosswise ();
      extract ();
      cout << "Crosswise segment detection "
           << (detector.trackCrosswiseOn () ? "on" : "off") << endl;
      break;
even's avatar
even committed
    case Qt::Key_6 :
      detector.switchDetector ();
      cout << (detector.oldDetectorOn () ?
even's avatar
even committed
               "Old detector set" : "New detector set") << endl;
even's avatar
even committed
      extract ();
      break;

even's avatar
even committed
    case Qt::Key_7 :
      storeUserInput ();
      break;

even's avatar
even committed
    case Qt::Key_8 :
      alternateTest ();
      break;

    case Qt::Key_9 :
      performanceTest ();
      break;

    case Qt::Key_0 :
      localTest ();
      break;
  }
  else if (strucview != NULL && strucview->isActiveWindow ())
  {
even's avatar
even committed
    if (strucview->processKeyEvent (event)) extract ();
even's avatar
even committed
  }
even's avatar
even committed
  /* else if (accuview != NULL && accuview->isActiveWindow ())
even's avatar
even committed
  {
even's avatar
even committed
    if (accuview->processKeyEvent (event)) extract ();
even's avatar
even committed
  } */
even's avatar
even committed
  else if (profileview != NULL && profileview->isActiveWindow ())
  {
even's avatar
even committed
    if (profileview->processKeyEvent (event)) extract ();
even's avatar
even committed
  }
even's avatar
even committed
  else if (idetview != NULL && idetview->isActiveWindow ())
  {
    if (idetview->processKeyEvent (event)) extract ();
  }
even's avatar
even committed
  else if (yorkview != NULL && yorkview->isActiveWindow ())
  {
    if (yorkview->processKeyEvent (event)) extract ();
  }
  else if (cannyview != NULL && cannyview->isActiveWindow ())
  {
    if (cannyview->processKeyEvent (event)) extract ();
  }
even's avatar
even committed
}


even's avatar
even committed
void BSDetectionWidget::drawPoints (QPainter &painter,
                                    vector<Pt2i> pts, QColor color)
even's avatar
even committed
{
even's avatar
even committed
  vector<Pt2i>::iterator iter = pts.begin ();
  while (iter != pts.end ())
even's avatar
even committed
  {
even's avatar
even committed
    Pt2i p = *iter++;
even's avatar
even committed
    painter.setPen (QPen (color, DEFAULT_PEN_WIDTH, Qt::SolidLine,
                          Qt::RoundCap, Qt::RoundJoin));
    if (p.x() < width && p.y() < height && p.x() >= 0 && p.y() >= 0)
      painter.drawPoint (QPoint (p.x(), height - 1 - p.y()));  // dec 1
even's avatar
even committed
  }
even's avatar
even committed
}