Skip to content
Snippets Groups Projects
mainIPOL.cpp 5.1 KiB
Newer Older
even's avatar
even committed
#include <QImage>
#include <QString>
#include <QColor>
#include "bsdetector.h"
#include "vmap.h"

Kerautret Bertrand's avatar
Kerautret Bertrand committed
#include <iostream>
#include <fstream>



void usage(std::string str)
{
  std::cout << str << " : something is wrong with the prog parameter (not enough parameters or something wrong)... "<< std::endl;  

}


even's avatar
even committed

int main (int argc, char *argv[])
{
  if (argc < 4)
Kerautret Bertrand's avatar
Kerautret Bertrand committed
  {
    usage(argv[0]);
    exit(EXIT_FAILURE);
  }

  
  // getting parameters:
even's avatar
even committed
  // - input image file
Kerautret Bertrand's avatar
Kerautret Bertrand committed
  string input_filename = argv[1];
even's avatar
even committed
  // - input points file
  string inpoints_filename = "";
  ifstream fin;  
  bool with_points = false;
  double widthBS = atof(argv[3]);
  if (argc == 5)
even's avatar
even committed
  {
    inpoints_filename = argv[4];
even's avatar
even committed
    fin.open (inpoints_filename.c_str(), std::fstream::in);
    with_points = true;
  }
Kerautret Bertrand's avatar
Kerautret Bertrand committed
  // - output file
  string output_filename = argv[2];
  ofstream fout;  
even's avatar
even committed
  fout.open (output_filename.c_str(), std::fstream::out);
Kerautret Bertrand's avatar
Kerautret Bertrand committed
  foutAllPts.open("segmentsPoints.dat",std::fstream::out);
Kerautret Bertrand's avatar
Kerautret Bertrand committed
  
even's avatar
even committed
  // Gradient map extraction (uses qt)
even's avatar
even committed
  QImage image;
Kerautret Bertrand's avatar
Kerautret Bertrand committed
  
  image.load (QString (input_filename.c_str()));
even's avatar
even committed
  int width = image.width ();
  int height = image.height ();
  int **tabImage = new int*[height];
  for (int i = 0; i < height; i++)
  {
    tabImage[i] = new int[width];
    for(int j = 0; j < width; j++)
    {
      QColor c = QColor (image.pixel (j, height - i - 1));
      tabImage[i][j] = c.value ();
    }
  }
  VMap *gMap = new VMap (width, height, tabImage, VMap::TYPE_SOBEL_5X5);

even's avatar
even committed
  // Input points reading (uses qt)
  vector<Pt2i> pts;
  if (with_points)
  {
    int val[4], i = 0;
    bool reading = true;
    if (fin)
    {
      while (reading)
      {
        if (fin.eof())
        {
          reading = false;
        }
        else
        {
          fin >> skipws >> val[i++];
        }
        //if (val[i-1] == -1) reading = false;
        //if (fin.eof()) continue;
        
even's avatar
even committed
        if (reading && i == 4)
        {
          pts.push_back (Pt2i (val[0], val[1]));
          pts.push_back (Pt2i (val[2], val[3]));
          i = 0;
        }
      }
    }
    fin.close();
  }
  fout << "# Line detection generated from " << argv[0] << "with format : X1 Y1 X2 Y2 on each line" << std::endl;
even's avatar
even committed

  // Blurred segment detection
  vector<BlurredSegment *> bss;
even's avatar
even committed
  BSDetector detector;
  detector.setGradientMap (gMap);
  detector.setFineTracksMaxWidth (widthBS);

even's avatar
even committed
  if (with_points)
  {
    vector<Pt2i>::iterator pit = pts.begin ();
    int nbseg = 0;
    while (pit != pts.end ())
    {
      Pt2i pt1 = *pit++;
      Pt2i pt2 = *pit++;
      std::cout << nbseg << " detect (" << pt1.x () << ", " << pt1.y ()
                << ") (" << pt2.x () << ", " << pt2.y () << ")" << std::endl;
      detector.detect (pt1, pt2);
      BlurredSegment *mybs = detector.getBlurredSegment ();
      if (mybs != NULL)
      {
        detector.preserveFormerBlurredSegment ();
        bss.push_back (mybs);
        nbseg ++;
      }
    }
  vector<BlurredSegment *>::const_iterator it = bss.begin ();
    while (it != bss.end ())
    {
      // Affichage du premier point
      vector<Pt2i> points = (*it)->getAllPoints ();
      
      fout <<  points.front().x() << " " << points.front().y() << " "
                <<  points.back().x() << " " << points.back().y() << std::endl;
      
      // Affichage du DSS englobant
      // vector<Pt2i> bnd;
      // DigitalStraightSegment *dss = (*it)->getSegment ();
      // if (dss != NULL)
      // {
         
      //   dss->getBounds (bnd, 0, 0, width, height, false);
      //   // cout << "DSS starts from (" << bnd.front().x()
      //   //    << "," << bnd.front().y() << ")" << endl;
      //   auto b = bnd.begin();
      //   for (auto &x: bnd){
      //   cout << "DSS starts from (" << x.x()
      //        << "," << x.y() << ")" << endl;
          
      //   }
      // }

      it++;
    }
 
even's avatar
even committed
    std::cout << nbseg << " detections" << std::endl;
  }
  else
  {
    detector.setMaxTrials (-1);
    detector.detectAll ();
    bss = detector.getBlurredSegments ();
  }
even's avatar
even committed

even's avatar
even committed
  // Display
even's avatar
even committed
  if (! bss.empty ())
  {
    vector<BlurredSegment *>::const_iterator it = bss.begin ();
    while (it != bss.end ())
    {
      // Affichage du premier point
      vector<Pt2i> points = (*it)->getAllPoints ();
Kerautret Bertrand's avatar
Kerautret Bertrand committed
      
      fout <<  points.front().x() << " " << points.front().y() << " "
                <<  points.back().x() << " " << points.back().y() << std::endl;

      // Export pour l'affichage de tous les points d'un segments
Kerautret Bertrand's avatar
Kerautret Bertrand committed
      for(auto &p : points)
      {
        foutAllPts<< p.x() << " " << p.y() << " "; 
      }
      foutAllPts<< std::endl;

even's avatar
even committed
      // Affichage du DSS englobant
Kerautret Bertrand's avatar
Kerautret Bertrand committed
      // vector<Pt2i> bnd;
      // DigitalStraightSegment *dss = (*it)->getSegment ();
      // if (dss != NULL)
      // {
         
      //   dss->getBounds (bnd, 0, 0, width, height, false);
      //   // cout << "DSS starts from (" << bnd.front().x()
      //   //    << "," << bnd.front().y() << ")" << endl;
      //   auto b = bnd.begin();
      //   for (auto &x: bnd){
      //   cout << "DSS starts from (" << x.x()
      //        << "," << x.y() << ")" << endl;
          
      //   }
      // }
even's avatar
even committed

      it++;
    }
  }
Kerautret Bertrand's avatar
Kerautret Bertrand committed
  fout.close();
even's avatar
even committed
  return (0);
}