[GRASS-SVN] r52499 - grass-addons/grass7/raster/r.houghtransform

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Aug 2 03:35:51 PDT 2012


Author: wenzeslaus
Date: 2012-08-02 03:35:50 -0700 (Thu, 02 Aug 2012)
New Revision: 52499

Removed:
   grass-addons/grass7/raster/r.houghtransform/matrix.cpp
Modified:
   grass-addons/grass7/raster/r.houghtransform/Makefile
   grass-addons/grass7/raster/r.houghtransform/hough.cpp
   grass-addons/grass7/raster/r.houghtransform/houghtransform.cpp
   grass-addons/grass7/raster/r.houghtransform/houghtransform.h
   grass-addons/grass7/raster/r.houghtransform/linesegmentsextractor.cpp
   grass-addons/grass7/raster/r.houghtransform/linesegmentsextractor.h
   grass-addons/grass7/raster/r.houghtransform/main.cpp
   grass-addons/grass7/raster/r.houghtransform/matrix.h
   grass-addons/grass7/raster/r.houghtransform/r.houghtransform.html
Log:
r.houghtransform: changing DCELL to CELL using templates and typedefs

Modified: grass-addons/grass7/raster/r.houghtransform/Makefile
===================================================================
--- grass-addons/grass7/raster/r.houghtransform/Makefile	2012-08-01 15:57:31 UTC (rev 52498)
+++ grass-addons/grass7/raster/r.houghtransform/Makefile	2012-08-02 10:35:50 UTC (rev 52499)
@@ -11,7 +11,7 @@
 
 include $(MODULE_TOPDIR)/include/Make/Module.make
 
-EXTRA_CFLAGS = $(VECT_CFLAGS) -Wno-sign-compare -Wall -Wextra -O0
+EXTRA_CFLAGS = $(VECT_CFLAGS) -Wno-sign-compare -Wall -Wextra -O0 -Wconversion
 
 LINK = $(CXX)
 

Modified: grass-addons/grass7/raster/r.houghtransform/hough.cpp
===================================================================
--- grass-addons/grass7/raster/r.houghtransform/hough.cpp	2012-08-01 15:57:31 UTC (rev 52498)
+++ grass-addons/grass7/raster/r.houghtransform/hough.cpp	2012-08-02 10:35:50 UTC (rev 52499)
@@ -22,16 +22,16 @@
 using grass::Vect_new_cats_struct;
 using grass::Vect_new_line_struct;
 
-using grass::Rast_allocate_d_input_buf;
+using grass::Rast_allocate_c_input_buf;
 using grass::Rast_open_old;
 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_allocate_c_buf;
 using grass::Rast_open_fp_new;
-using grass::Rast_put_d_row;
+using grass::Rast_put_c_row;
 using grass::Rast_get_cellhd;
 
 using grass::G_gettext;
@@ -40,13 +40,14 @@
 using grass::G_free;
 
 using grass::Colors;
-using grass::FPRange; // FIXME: DCELL/CELL
+using grass::Range;
 using grass::G_mapset;
 
 /** Loads map into memory.
 
   \param[out] mat map in a matrix (row order), field have to be allocated
   */
+template <typename Matrix>
 void read_raster_map(const char *name, const char *mapset, int nrows,
                             int ncols, Matrix& mat)
 {
@@ -55,11 +56,11 @@
 
     int map_fd;
 
-    DCELL *row_buffer;
+    CELL *row_buffer;
 
-    DCELL cell_value;
+    CELL cell_value;
 
-    row_buffer = Rast_allocate_d_input_buf();
+    row_buffer = Rast_allocate_c_buf();
 
     /* load map */
     map_fd = Rast_open_old(name, mapset);
@@ -74,11 +75,11 @@
     //        G_fatal_error(_("Error getting first raster map type"));
 
     for (r = 0; r < nrows; r++) {
-        Rast_get_row(map_fd, row_buffer, r, DCELL_TYPE);
+        Rast_get_row(map_fd, row_buffer, r, CELL_TYPE);
 
         for (c = 0; c < ncols; c++) {
             cell_value = row_buffer[c];
-            if (!Rast_is_d_null_value(&cell_value))
+            if (!Rast_is_c_null_value(&cell_value))
                 mat(r, c) = cell_value;
             else
                 mat(r, c) = 0.0;
@@ -92,22 +93,23 @@
 void apply_hough_colors_to_map(const char *name)
 {
     struct Colors colors;
-    struct FPRange range;
-    DCELL min, max;
+    struct Range range;
+    CELL min, max;
 
-    Rast_read_fp_range(name, G_mapset(), &range);
-    Rast_get_fp_range_min_max(&range, &min, &max);
+    Rast_read_range(name, G_mapset(), &range);
+    Rast_get_range_min_max(&range, &min, &max);
     Rast_make_grey_scale_colors(&colors, min, max);
     Rast_write_colors(name, G_mapset(), &colors);
 }
 
+template <typename Matrix>
 void create_raster_map(const char *name, struct Cell_head *window, const Matrix& mat)
 {
     struct Cell_head original_window;
-    DCELL *cell_real;
+    CELL *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;
+    int mapfd;
 
     /* get the rows and columns in the current window */
     rows = mat.rows();
@@ -131,19 +133,19 @@
     Rast_set_window(window);
 
     /* allocate the space for one row of cell map data */
-    cell_real = Rast_allocate_d_buf();
+    cell_real = Rast_allocate_c_buf();
 
     /* open the output cell maps */
-    realfd = Rast_open_fp_new(name);
+    mapfd = 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_put_c_row(mapfd, cell_real);
     }
 
-    Rast_close(realfd);
+    Rast_close(mapfd);
     G_free(cell_real);
 
     Rast_set_window(&original_window);
@@ -185,6 +187,7 @@
     Vect_close(&Map);
 }
 
+template <typename Matrix>
 void extract_line_segments(const Matrix &I,
                            const HoughTransform::Peaks& peaks,
                            const HoughTransform::TracebackMap& houghMap,
@@ -220,6 +223,7 @@
                  const char *anglesMapName,
                  const char *houghImageName, const char *result)
 {
+    typedef matrix::Matrix<DCELL> Matrix;
     Matrix I(nrows, ncols);
     read_raster_map(name, mapset, nrows, ncols, I);
 

Modified: grass-addons/grass7/raster/r.houghtransform/houghtransform.cpp
===================================================================
--- grass-addons/grass7/raster/r.houghtransform/houghtransform.cpp	2012-08-01 15:57:31 UTC (rev 52498)
+++ grass-addons/grass7/raster/r.houghtransform/houghtransform.cpp	2012-08-02 10:35:50 UTC (rev 52499)
@@ -29,17 +29,17 @@
     mNumR = mOriginalMatrix.rows();
     mNumC = mOriginalMatrix.columns();
 
-    mThetas = ColumnVector(matrix::Range(-M_PI/2.0, M_PI/2.0, M_PI/180.0).matrix_value ());
+    mThetas = ColumnVector(Range(-M_PI/2.0, M_PI/2.0, M_PI/180.0).matrix_value ());
 
     const float diag_length = std::sqrt(mNumR*mNumR + mNumC*mNumC);
     mNumBins = ceil(diag_length) - 1;
 
     mHoughMatrix = Matrix(mNumBins, mThetas.length(), 0.0);
 
-    c2 = ceil(mNumC/2.);
-    r2 = ceil(mNumR/2.);
+    c_2 = ceil(mNumC/2.);
+    r_2 = ceil(mNumR/2.);
 
-    bins0 = 1 - ceil(mNumBins/2.0);
+    first_bins = 1 - ceil(mNumBins/2.0);
 }
 
 /* functions */
@@ -64,10 +64,10 @@
     for (size_t i = minIndex; i < maxIndex; i++)
     {
         const double theta = mThetas(i);
-        const double cT = cos(theta);
-        const double sT = sin(theta);
-        const int rho = (int) floor(cT*(x - c2) + sT*(y - r2) + 0.5);
-        const int bin = (int) (rho - bins0);
+        const double rho_d = std::cos(theta)*(x - c_2) + std::sin(theta)*(y - r_2);
+        const int rho = floor(rho_d + 0.5);
+        const int bin = rho - first_bins;
+
         if ((bin > 0) && (bin < mNumBins))
         {
             mHoughMatrix(bin, i)++;
@@ -198,7 +198,7 @@
 /** \param list[in, out] will be sorted by y cooridinate
   if angle is in range (45, 135] or by x otherwise
   */
-bool HoughTransform::findEndPoints(CoordinatesList& list, Coordinates &beginLine, Coordinates &endLine, const int angle)
+bool HoughTransform::findEndPoints(CoordinatesList& list, Coordinates &beginLine, Coordinates &endLine, const value_type angle)
 {
     if (angle > 45 && angle <= 135)
     {
@@ -221,7 +221,7 @@
     size_t colIndex;
     std::vector<size_t> colIndexes;
 
-    std::vector<double> rowMax = matrix.row_max(colIndexes);
+    std::vector<value_type> rowMax = matrix.row_max(colIndexes);
 
     int max = 0;
     for (size_t i = 0; i < rowMax.size(); i++)

Modified: grass-addons/grass7/raster/r.houghtransform/houghtransform.h
===================================================================
--- grass-addons/grass7/raster/r.houghtransform/houghtransform.h	2012-08-01 15:57:31 UTC (rev 52498)
+++ grass-addons/grass7/raster/r.houghtransform/houghtransform.h	2012-08-02 10:35:50 UTC (rev 52499)
@@ -14,13 +14,14 @@
 
 #include <stdio.h>
 
-using namespace matrix;
-
 class HoughTransform
 {
 public:
-
     /* types */
+    typedef double value_type;
+    typedef matrix::Matrix<value_type> Matrix;
+    typedef matrix::ColumnVector<double> ColumnVector;
+    typedef matrix::Range<double> Range;
 
     /** This is class to provide compatible syntax with \c std::list. */
     template <typename T>
@@ -52,7 +53,7 @@
 
     struct Peak
     {
-        Peak(Coordinates coordinates, int value, Coordinates begin, Coordinates end)
+        Peak(Coordinates coordinates, value_type value, Coordinates begin, Coordinates end)
             : coordinates(coordinates), value(value), beginLine(begin), endLine(end)
         {}
         Coordinates coordinates;
@@ -94,7 +95,7 @@
     // some of these functions can be changed to non-member
     CoordinatesList neighbourhood(Coordinates &coordinates, const int sizeOfNeighbourhood);
     void removePeakEffect(const CoordinatesList &neighbours, Coordinates &beginLine, Coordinates &endLine);
-    bool findEndPoints(CoordinatesList& list, Coordinates &beginLine, Coordinates &endLine, const int angle);
+    bool findEndPoints(CoordinatesList& list, Coordinates &beginLine, Coordinates &endLine, const value_type angle);
     int findMax(const Matrix& matrix, Coordinates &coordinates);
     void computeHoughForXY(int x, int y, size_t minIndex, size_t maxIndex);
 
@@ -106,15 +107,16 @@
     TracebackMap mHoughMap;
     Peaks mPeaks;
 
+    HoughParametres mParams;
+
+    /* helper variables for computations */
     int mNumR;
     int mNumC;
     ColumnVector mThetas;
     int mNumBins;
-    int c2;
-    int r2;
-    int bins0;
-
-    HoughParametres mParams;
+    int c_2;
+    int r_2;
+    int first_bins;
 };
 
 

Modified: grass-addons/grass7/raster/r.houghtransform/linesegmentsextractor.cpp
===================================================================
--- grass-addons/grass7/raster/r.houghtransform/linesegmentsextractor.cpp	2012-08-01 15:57:31 UTC (rev 52498)
+++ grass-addons/grass7/raster/r.houghtransform/linesegmentsextractor.cpp	2012-08-02 10:35:50 UTC (rev 52499)
@@ -32,6 +32,7 @@
     return true;
 }
 
+template<typename Matrix>
 bool isData(const Matrix &I, const std::vector<int> &y, const std::vector<int> &x, const int cols, const int rows)
 {
     for (size_t k = 0; k < y.size(); k++)
@@ -125,7 +126,7 @@
 /* member functions */
 
 void LineSegmentsExtractor::extract(LineCoordinates lineCoordinates,
-                                    float orient,
+                                    const double orient,
                                     SegmentList& segments)
 {
     const int rows = mImage.rows();

Modified: grass-addons/grass7/raster/r.houghtransform/linesegmentsextractor.h
===================================================================
--- grass-addons/grass7/raster/r.houghtransform/linesegmentsextractor.h	2012-08-01 15:57:31 UTC (rev 52498)
+++ grass-addons/grass7/raster/r.houghtransform/linesegmentsextractor.h	2012-08-02 10:35:50 UTC (rev 52499)
@@ -16,11 +16,14 @@
 class LineSegmentsExtractor
 {
 public:
+    typedef double value_type;
+    typedef matrix::Matrix<value_type> Matrix;
+
     /**
       \param image should exist during existence of LineSegmentsExtractor
       \param lineCoordinates will be copied to internal variable
       */
-    LineSegmentsExtractor(const matrix::Matrix& image,
+    LineSegmentsExtractor(const Matrix& image,
                           const ExtractParametres& parametres)
         : mImage(image),
           gapSize(parametres.gapSize), maxNumOfGaps(parametres.maxNumOfGaps),
@@ -33,10 +36,10 @@
       \param orient orientation of lines
       \param[out] segments extracted segments will be added to this list
       */
-    void extract(LineCoordinates lineCoordinates, const float orient, SegmentList& segments);
+    void extract(LineCoordinates lineCoordinates, const double orient, SegmentList& segments);
 
 private:
-    const matrix::Matrix &mImage;
+    const Matrix &mImage;
     int gapSize;
     int maxNumOfGaps;
     int lineGap;

Modified: grass-addons/grass7/raster/r.houghtransform/main.cpp
===================================================================
--- grass-addons/grass7/raster/r.houghtransform/main.cpp	2012-08-01 15:57:31 UTC (rev 52498)
+++ grass-addons/grass7/raster/r.houghtransform/main.cpp	2012-08-02 10:35:50 UTC (rev 52499)
@@ -5,7 +5,7 @@
  * AUTHOR(S):    Anna Kratochvilova - kratochanna gmail.com
  *               Vaclav Petras - wenzeslaus gmail.com
  *
- * PURPOSE:      Line extraction from edges in raster.
+ * PURPOSE:      Line segment extraction using Hough transformation.
  *
  * COPYRIGHT:    (C) 2012 by the GRASS Development Team
  *
@@ -37,7 +37,7 @@
 int main(int argc, char *argv[])
 {
     struct Cell_head cell_head;	/* it stores region information,
-				   and header information of rasters */
+                                   and header information of rasters */
     char *name;			/* input raster name */
 
     char *mapset;		/* mapset name */
@@ -52,7 +52,7 @@
     struct Option *input, *output, *anglesOption, *houghImageNameOption,
             *angleWidthOption,
             *minGapOption, *maxNumberOfGapsOption,
-        *maxLinesOption, *maxGapOption, *minSegmentLengthOption,
+            *maxLinesOption, *maxGapOption, *minSegmentLengthOption,
             *lineWidthOption;
 
     /* initialize GIS environment */
@@ -62,10 +62,11 @@
     module = G_define_module();
     G_add_keyword(_("raster"));
     G_add_keyword(_("hought"));
-    G_add_keyword(_(""));
+    G_add_keyword(_("ht"));
     module->description =
-	_("Canny edge detector. Region shall be set to input map. "
-	  "Can work only on small images since map is loaded into memory.");
+            _("Perform Hough transformation and extracts line segments from image."
+              " Region shall be set to input map."
+              " Can work only on small images since map is loaded into memory.");
 
     /* Define the different options as defined in gis.h */
     input = G_define_standard_option(G_OPT_R_INPUT);
@@ -147,30 +148,31 @@
 
     /* options and flags parser */
     if (G_parser(argc, argv))
-	exit(EXIT_FAILURE);
+        exit(EXIT_FAILURE);
 
     /* stores options and flags to variables */
     result = output->answer;
     name = input->answer;
 
-    ExtractParametres extractParametres;
+
     HoughParametres houghParametres;
-
     houghParametres.maxPeaksNum = atoi(maxLinesOption->answer);
-    houghParametres.threshold = 10;
+    houghParametres.threshold = 10; // TODO: consider option
     houghParametres.angleWidth = atoi(angleWidthOption->answer);
+    houghParametres.sizeOfNeighbourhood = 1; // TODO: consider option
+
+    ExtractParametres extractParametres;
     extractParametres.gapSize = atoi(minGapOption->answer);
     extractParametres.maxGap = atoi(maxGapOption->answer);
     extractParametres.maxNumOfGaps = atoi(maxNumberOfGapsOption->answer);
     extractParametres.lineLength = atoi(minSegmentLengthOption->answer);
     extractParametres.lineWidth = atoi(lineWidthOption->answer);
-    houghParametres.sizeOfNeighbourhood = 1;
 
     /* returns NULL if the map was not found in any mapset,
      * mapset name otherwise */
     mapset = (char *)G_find_raster2(name, "");
     if (mapset == NULL)
-	G_fatal_error(_("Raster map <%s> not found"), name);
+        G_fatal_error(_("Raster map <%s> not found"), name);
 
     /* determine the inputmap type (CELL/FCELL/DCELL) */
     //data_type = Rast_map_type(name, mapset);

Deleted: grass-addons/grass7/raster/r.houghtransform/matrix.cpp
===================================================================
Modified: grass-addons/grass7/raster/r.houghtransform/matrix.h
===================================================================
--- grass-addons/grass7/raster/r.houghtransform/matrix.h	2012-08-01 15:57:31 UTC (rev 52498)
+++ grass-addons/grass7/raster/r.houghtransform/matrix.h	2012-08-02 10:35:50 UTC (rev 52499)
@@ -10,21 +10,24 @@
 
 namespace matrix {
 
+template <typename T>
 class Matrix
 {
 public:
-    Matrix(size_t r, size_t c, double val = 0)
+    typedef T value_type;
+
+    Matrix(size_t r, size_t c, value_type val = 0)
         : mRows(r), mCols(c)
     {
         resize(r, c, val);
     }
     Matrix() : mRows(0), mCols(0) {}
 
-    void resize(size_t r, size_t c, double val = 0)
+    void resize(size_t r, size_t c, value_type val = 0)
     {
         mRows = r;
         mCols = c;
-        std::vector<double> row;
+        std::vector<value_type> row;
         row.resize(c, val);
         mat.resize(r, row);
     }
@@ -33,7 +36,7 @@
 
       if (r > rows() || c > columns()) error;
       */
-    double& operator ()(size_t r, size_t c)
+    value_type& operator ()(size_t r, size_t c)
     {
         return mat[r][c];
     }
@@ -41,22 +44,22 @@
 
       if (r > rows() || c > columns()) error;
       */
-    const double& operator ()(size_t r, size_t c) const
+    const value_type& operator ()(size_t r, size_t c) const
     {
         return mat[r][c];
     }
     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<value_type> row_max(std::vector<size_t>& colIndexes) const
     {
-        std::vector<double> ret;
+        std::vector<value_type> 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());
-            double max = *maxe;
+            typename std::vector<value_type>::const_iterator maxe = std::max_element(mat[i].begin(), mat[i].end());
+            value_type max = *maxe;
             size_t maxi = maxe - mat[i].begin();
 
             ret.push_back(max);
@@ -66,15 +69,18 @@
     }
 
 private:
-    std::vector< std::vector<double> > mat;
+    std::vector< std::vector<value_type> > mat;
     size_t mRows;
     size_t mCols;
 };
 
+template <typename T>
 class ColumnVector
 {
 public:
-    ColumnVector(Matrix mat)
+    typedef T value_type;
+
+    ColumnVector(Matrix<value_type> mat)
     {
         for (size_t i = 0; i < mat.columns(); ++i)
         {
@@ -83,23 +89,26 @@
     }
     ColumnVector() {}
 
-    double& operator ()(size_t i)
+    value_type& operator ()(size_t i)
     {
         return vec[i];
     }
-    const double& operator ()(size_t i) const
+    const value_type& operator ()(size_t i) const
     {
         return vec[i];
     }
     size_t length() { return vec.size(); }
 
 private:
-    std::vector<double> vec;
+    std::vector<value_type> vec;
 };
 
+template <typename T>
 class Range
 {
 public:
+    typedef T value_type;
+
     Range (double b, double l, double i)
         : rng_base(b), rng_limit(l), rng_inc(i), rng_nelem(nelem_internal())
     { }
@@ -108,9 +117,9 @@
       : rng_base(b), rng_limit(l), rng_inc(1),
         rng_nelem(nelem_internal()) { }
 
-    Matrix matrix_value() const
+    Matrix<value_type> matrix_value() const
     {
-        Matrix cache;
+        Matrix<value_type> cache;
         //if (rng_nelem > 0 && cache.nelem () == 0)
           //{
             cache.resize(1, rng_nelem);

Modified: grass-addons/grass7/raster/r.houghtransform/r.houghtransform.html
===================================================================
--- grass-addons/grass7/raster/r.houghtransform/r.houghtransform.html	2012-08-01 15:57:31 UTC (rev 52498)
+++ grass-addons/grass7/raster/r.houghtransform/r.houghtransform.html	2012-08-02 10:35:50 UTC (rev 52499)
@@ -16,3 +16,4 @@
 Vaclav Petras
 
 <p><i>Last changed: $Date$</i>
+



More information about the grass-commit mailing list