[GRASS-SVN] r52167 - grass-addons/grass7/raster/r.ht

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Jun 20 07:35:15 PDT 2012


Author: wenzeslaus
Date: 2012-06-20 07:35:14 -0700 (Wed, 20 Jun 2012)
New Revision: 52167

Modified:
   grass-addons/grass7/raster/r.ht/extract_line.cpp
   grass-addons/grass7/raster/r.ht/extract_line.h
   grass-addons/grass7/raster/r.ht/hough.cpp
   grass-addons/grass7/raster/r.ht/hough.h
   grass-addons/grass7/raster/r.ht/houghtransform.h
   grass-addons/grass7/raster/r.ht/main.cpp
   grass-addons/grass7/raster/r.ht/matrix.h
Log:
hough: ht image output, more options, cleaning up code

Modified: grass-addons/grass7/raster/r.ht/extract_line.cpp
===================================================================
--- grass-addons/grass7/raster/r.ht/extract_line.cpp	2012-06-20 13:43:47 UTC (rev 52166)
+++ grass-addons/grass7/raster/r.ht/extract_line.cpp	2012-06-20 14:35:14 UTC (rev 52167)
@@ -121,7 +121,7 @@
 }
 
 void extract(const Matrix& I,
-             float orient, const int lineGap, const int lineLength,
+             float orient, int gapSize, int maxNumOfGaps, const int lineGap, const int lineLength,
              LineCoordinates lineCoordinates, SegmentList& segments)
 {
     const int rows = I.rows ();
@@ -194,10 +194,10 @@
                 //    reset the gap
                 if ( isData(I, indicesI, indicesJ, cols, rows) )
                 {
-                    if (gap > 5)
+                    if (gap > gapSize)
                     {
                         numOfGaps++;
-                        if (numOfGaps > 5)
+                        if (numOfGaps > maxNumOfGaps)
                         {
                             useLine = false;
                             break;

Modified: grass-addons/grass7/raster/r.ht/extract_line.h
===================================================================
--- grass-addons/grass7/raster/r.ht/extract_line.h	2012-06-20 13:43:47 UTC (rev 52166)
+++ grass-addons/grass7/raster/r.ht/extract_line.h	2012-06-20 14:35:14 UTC (rev 52167)
@@ -11,6 +11,6 @@
 typedef std::vector< Segment > SegmentList;
 typedef std::list< std::pair<int, int> > LineCoordinates;
 
-void extract(const matrix::Matrix& I, const float orient, const int lineGap, const int lineLength, LineCoordinates lineCoordinates, SegmentList& segments);
+void extract(const matrix::Matrix& I, const float orient, int gapSize, int maxNumOfGaps, const int lineGap, const int lineLength, LineCoordinates lineCoordinates, SegmentList& segments);
 
 #endif // EXTRACT_LINE_H

Modified: grass-addons/grass7/raster/r.ht/hough.cpp
===================================================================
--- grass-addons/grass7/raster/r.ht/hough.cpp	2012-06-20 13:43:47 UTC (rev 52166)
+++ grass-addons/grass7/raster/r.ht/hough.cpp	2012-06-20 14:35:14 UTC (rev 52167)
@@ -27,6 +27,13 @@
 using grass::Rast_get_row;
 using grass::Rast_close;
 
+using grass::Rast_window_rows;
+using grass::Rast_window_cols;
+using grass::Rast_allocate_d_buf;
+using grass::Rast_open_fp_new;
+using grass::Rast_put_d_row;
+using grass::Rast_get_cellhd;
+
 using grass::G_gettext;
 using grass::G_fatal_error;
 using grass::G_debug;
@@ -78,6 +85,54 @@
     Rast_close(map_fd);
 }
 
+void create_raster_map(const char *name, struct Cell_head *window, const Matrix& mat)
+{
+    struct Cell_head original_window;
+    DCELL *cell_real;
+    int rows, cols; /* number of rows and columns */
+    long totsize; /* total number of data points */ // FIXME: make clear the size_t usage
+    int realfd;
+
+    /* get the rows and columns in the current window */
+    rows = mat.rows();
+    cols = mat.columns();
+    totsize = rows * cols;
+
+    G_get_set_window(&original_window);
+
+    window->bottom = 0;
+    window->top = rows;
+    window->cols = cols;
+    window->east = cols;
+    window->north = rows;
+    window->ns_res = 1;
+    window->rows = rows;
+    window->south = 0;
+    window->west = 0;
+    window->ew_res = 1;
+    window->tb_res = 1;
+
+    Rast_set_window(window);
+
+    /* allocate the space for one row of cell map data */
+    cell_real = Rast_allocate_d_buf();
+
+    /* open the output cell maps */
+    realfd = Rast_open_fp_new(name);
+
+    for (int i = 0; i < rows; i++) {
+        for (int j = 0; j < cols; j++) {
+            cell_real[j] = mat(i, j);
+        }
+        Rast_put_d_row(realfd, cell_real);
+    }
+
+    Rast_close(realfd);
+    G_free(cell_real);
+
+    Rast_set_window(&original_window);
+}
+
 /**
   \param cellhd raster map header, used for converting rows/cols to easting/northing
   */
@@ -118,7 +173,7 @@
 void extract_line_segments(const Matrix &I,
                            const HoughTransform::Peaks& peaks,
                            const HoughTransform::TracebackMap& houghMap,
-                           int gap,
+                           int gapSize, int maxNumOfGaps, int gap,
                            int minSegmentLength,
                            SegmentList& segments)
 {
@@ -134,7 +189,7 @@
         {
             const HoughTransform::CoordinatesList& lineCoordinates = coordsIt->second;
 
-            extract(I, (theta-90)/180*M_PI, gap, minSegmentLength, lineCoordinates, segments);
+            extract(I, (theta-90)/180*M_PI, gapSize, maxNumOfGaps, gap, minSegmentLength, lineCoordinates, segments);
         }
         else
         {
@@ -143,11 +198,11 @@
     }
 }
 
-void hough_peaks(int maxPeaks, int threshold, int sizeOfNeighbourhood,
-                 int gap, int minSegmentLength,
+void hough_peaks(int maxPeaks, int threshold, double angleWith, int sizeOfNeighbourhood,
+                 int gapSize, int maxNumOfGaps, int gap, int minSegmentLength,
                  const char *name, const char *mapset, size_t nrows, size_t ncols,
-                 const char *anglesMapName, int angleWidth,
-                 const char *result)
+                 const char *anglesMapName,
+                 const char *houghImageName, const char *result)
 {
     Matrix I(nrows, ncols);
     read_raster_map(name, mapset, nrows, ncols, I);
@@ -158,7 +213,7 @@
     {
         Matrix angles(nrows, ncols);
         read_raster_map(anglesMapName, mapset, nrows, ncols, angles);
-        hough.compute(angles, angleWidth);
+        hough.compute(angles, angleWith);
     }
     else
     {
@@ -167,11 +222,18 @@
 
     hough.findPeaks(maxPeaks, threshold, sizeOfNeighbourhood);
 
+    if (houghImageName != NULL)
+    {
+        struct Cell_head window;
+        Rast_get_cellhd(name, "", &window);
+        create_raster_map(houghImageName, &window, hough.getHoughMatrix());
+    }
+
     const HoughTransform::Peaks& peaks = hough.getPeaks();
     const HoughTransform::TracebackMap& houghMap = hough.getHoughMap();
     SegmentList segments;
 
-    extract_line_segments(I, peaks, houghMap, gap, minSegmentLength, segments);
+    extract_line_segments(I, peaks, houghMap, gapSize, maxNumOfGaps, gap, minSegmentLength, segments);
 
     Cell_head cellhd;
     Rast_get_window(&cellhd);

Modified: grass-addons/grass7/raster/r.ht/hough.h
===================================================================
--- grass-addons/grass7/raster/r.ht/hough.h	2012-06-20 13:43:47 UTC (rev 52166)
+++ grass-addons/grass7/raster/r.ht/hough.h	2012-06-20 14:35:14 UTC (rev 52167)
@@ -6,6 +6,6 @@
 #include <grass/raster.h>
 }
 
-void hough_peaks(int maxPeaks, int threshold, int sizeOfNeighbourhood, int gap, int minSegmentLength, const char * name, const char* mapset, size_t nrows, size_t ncols, const char * angleMapName, int angleWidth, const char * result);
+void hough_peaks(int maxPeaks, int threshold, double angleWith, int sizeOfNeighbourhood, int gapSize, int maxNumOfGaps, int gap, int minSegmentLength, const char *name, const char *mapset, size_t nrows, size_t ncols, const char *angleMapName, const char *houghImageName, const char *result);
 
 #endif // HOUGH_H

Modified: grass-addons/grass7/raster/r.ht/houghtransform.h
===================================================================
--- grass-addons/grass7/raster/r.ht/houghtransform.h	2012-06-20 13:43:47 UTC (rev 52166)
+++ grass-addons/grass7/raster/r.ht/houghtransform.h	2012-06-20 14:35:14 UTC (rev 52167)
@@ -66,10 +66,10 @@
 
     /* getters */
 
-    const Matrix & getHoughMatrix() { return mHoughMatrix; }
-    const Matrix & getOrigMatrix() { return mOriginalMatrix; }
+    const Matrix & getHoughMatrix() const { return mHoughMatrix; }
+    const Matrix & getOrigMatrix()  const { return mOriginalMatrix; }
     const Peaks & getPeaks() const { return mPeaks; }
-    const TracebackMap & getHoughMap() { return mHoughMap; }
+    const TracebackMap & getHoughMap() const { return mHoughMap; }
 
 private:
 

Modified: grass-addons/grass7/raster/r.ht/main.cpp
===================================================================
--- grass-addons/grass7/raster/r.ht/main.cpp	2012-06-20 13:43:47 UTC (rev 52166)
+++ grass-addons/grass7/raster/r.ht/main.cpp	2012-06-20 14:35:14 UTC (rev 52167)
@@ -5,7 +5,7 @@
  * AUTHOR(S):    Anna Kratochvilova - kratochanna gmail.com
  *               Vaclav Petras - wenzeslaus gmail.com
  *
- * PURPOSE:      Detects line segments using Hough transform.
+ * PURPOSE:      Edge detection usig Canny algorithm.
  *
  * COPYRIGHT:    (C) 2012 by the GRASS Development Team
  *
@@ -49,9 +49,10 @@
     struct GModule *module;	/* GRASS module for parsing arguments */
 
     /* options */
-    struct Option *input, *output, *anglesOption,
-        *maxLinesOption, *maxGapOption, *minSegmentLengthOption,
-            *angleWidthOption;
+    struct Option *input, *output, *anglesOption, *houghImageNameOption,
+            *angleWidthOption,
+            *minGapOption, *maxNumberOfGapsOption,
+        *maxLinesOption, *maxGapOption, *minSegmentLengthOption;
 
     /* initialize GIS environment */
     G_gisinit(argv[0]);		/* reads grass env, stores program name to G_program_name() */
@@ -75,6 +76,19 @@
     anglesOption->required = NO;
     anglesOption->description = _("Approximate number of line segments.");
 
+    houghImageNameOption = G_define_standard_option(G_OPT_R_OUTPUT);
+    houghImageNameOption->key = "hough_image";
+    houghImageNameOption->required = NO;
+    houghImageNameOption->description = _("Name of output image containing Hough transform");
+
+    angleWidthOption = G_define_option();
+    angleWidthOption->key = "angle_width";
+    angleWidthOption->type = TYPE_INTEGER;
+    angleWidthOption->required = NO;
+    angleWidthOption->multiple = NO;
+    angleWidthOption->description = _("Width of circle sector (only when you provide angle map).");
+    angleWidthOption->answer = const_cast<char *>("5");
+
     // this option will become max peaks number to find in HT
     maxLinesOption = G_define_option();
     maxLinesOption->key = "lines_number";
@@ -90,6 +104,22 @@
                                     );
     maxLinesOption->answer = const_cast<char *>("20");
 
+    minGapOption = G_define_option();
+    minGapOption->key = "gap_size";
+    minGapOption->type = TYPE_INTEGER;
+    minGapOption->required = NO;
+    minGapOption->multiple = NO;
+    minGapOption->description = _("Minimal cell count considered as a gap");
+    minGapOption->answer = const_cast<char *>("5");
+
+    maxNumberOfGapsOption = G_define_option();
+    maxNumberOfGapsOption->key = "max_gap_count";
+    maxNumberOfGapsOption->type = TYPE_INTEGER;
+    maxNumberOfGapsOption->required = NO;
+    maxNumberOfGapsOption->multiple = NO;
+    maxNumberOfGapsOption->description = _("Maximal number of gaps in line segment");
+    maxNumberOfGapsOption->answer = const_cast<char *>("5");
+
     maxGapOption = G_define_option();
     maxGapOption->key = "max_gap";
     maxGapOption->type = TYPE_INTEGER;
@@ -106,14 +136,6 @@
     minSegmentLengthOption->description = _("Minimal length of line segment");
     minSegmentLengthOption->answer = const_cast<char *>("50");
 
-    angleWidthOption = G_define_option();
-    angleWidthOption->key = "angle_width";
-    angleWidthOption->type = TYPE_INTEGER;
-    angleWidthOption->required = NO;
-    angleWidthOption->multiple = NO;
-    angleWidthOption->description = _("Width of circle sector (only when you provide angle map).");
-    angleWidthOption->answer = const_cast<char *>("5");
-
     /* options and flags parser */
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
@@ -124,12 +146,13 @@
 
     int maxPeaks = atoi(maxLinesOption->answer);
     int threshold = 10;
+    int angleWidth = atoi(angleWidthOption->answer);
+    int gapSize = atoi(minGapOption->answer);
     int gap = atoi(maxGapOption->answer);
+    int maxNumOfGaps = atoi(maxNumberOfGapsOption->answer);
     int minSegmentLength = atoi(minSegmentLengthOption->answer);
     int sizeOfNeighbourhood = 1;
 
-    int angleWidth = atoi(angleWidthOption->answer);
-
     /* returns NULL if the map was not found in any mapset,
      * mapset name otherwise */
     mapset = (char *)G_find_raster2(name, "");
@@ -155,7 +178,7 @@
 
     /* **** */
 
-    hough_peaks(maxPeaks, threshold, sizeOfNeighbourhood, gap, minSegmentLength, name, mapset, nrows, ncols, anglesOption->answer, angleWidth, result);
+    hough_peaks(maxPeaks, threshold, angleWidth, sizeOfNeighbourhood, gapSize, maxNumOfGaps, gap, minSegmentLength, name, mapset, nrows, ncols, anglesOption->answer, houghImageNameOption->answer, result);
 
     /* **** */
 

Modified: grass-addons/grass7/raster/r.ht/matrix.h
===================================================================
--- grass-addons/grass7/raster/r.ht/matrix.h	2012-06-20 13:43:47 UTC (rev 52166)
+++ grass-addons/grass7/raster/r.ht/matrix.h	2012-06-20 14:35:14 UTC (rev 52167)
@@ -14,41 +14,45 @@
 {
 public:
     Matrix(size_t r, size_t c, double val = 0)
+        : mRows(r), mCols(c)
     {
         resize(r, c, val);
     }
-    Matrix() {}
+    Matrix() : mRows(0), mCols(0) {}
 
     void resize(size_t r, size_t c, double val = 0)
     {
+        mRows = r;
+        mCols = c;
         std::vector<double> row;
         row.resize(c, val);
         mat.resize(r, row);
     }
 
+    /**
+
+      if (r > rows() || c > columns()) error;
+      */
     double& operator ()(size_t r, size_t c)
     {
-        if (r > rows() || c > columns())
-        {
-            // FIXME: fatal error
-        }
         return mat[r][c];
     }
+    /**
+
+      if (r > rows() || c > columns()) error;
+      */
     const double& operator ()(size_t r, size_t c) const
     {
         return mat[r][c];
     }
-    size_t rows() const { return mat.size(); }
-    size_t columns() const
-    {
-        if (!mat.empty())
-            return mat[0].size();
-        return 0;
-    }
+    size_t rows() const { return mRows; }
+    size_t columns() const { return mCols; }
 
     std::vector<double> row_max(std::vector<size_t>& colIndexes) const
     {
         std::vector<double> ret;
+        ret.reserve(rows());
+        colIndexes.reserve(rows());
         for (size_t i = 0; i < rows(); ++i)
         {
             std::vector<double>::const_iterator maxe = std::max_element(mat[i].begin(), mat[i].end());
@@ -63,6 +67,8 @@
 
 private:
     std::vector< std::vector<double> > mat;
+    size_t mRows;
+    size_t mCols;
 };
 
 class ColumnVector
@@ -91,42 +97,6 @@
     std::vector<double> vec;
 };
 
-class RowVector
-{
-public:
-    RowVector(Matrix mat)
-    {
-        for (size_t i = 0; i < mat.columns(); ++i)
-        {
-            vec.push_back(mat(0, i));
-        }
-    }
-    RowVector() {}
-
-    double& operator ()(size_t i)
-    {
-        return vec[i];
-    }
-    const double& operator ()(size_t i) const
-    {
-        return vec[i];
-    }
-    size_t length() { return vec.size(); }
-
-    RowVector operator -(double d)
-    {
-        RowVector ret(*this);
-        for (size_t i = 0; i < ret.length(); ++i)
-        {
-            ret(i) = ret(i) - d;
-        }
-        return ret;
-    }
-
-private:
-    std::vector<double> vec;
-};
-
 class Range
 {
 public:



More information about the grass-commit mailing list