Changeset 58886


Ignore:
Timestamp:
Feb 5, 2014, 8:36:23 AM (11 years ago)
Author:
neteler
Message:

r.segment: ported r.seg GRASS 6 Addon to GRASS 7

Location:
grass-addons/grass7/raster
Files:
1 deleted
4 edited
2 copied

Legend:

Unmodified
Added
Removed
  • grass-addons/grass7/raster/Makefile

    r58619 r58886  
    1919        r.modis \
    2020        r.regression.series \
     21        r.segment \
    2122        r.stream.basins \
    2223        r.stream.channel \
  • grass-addons/grass7/raster/r.segment/Makefile

    r43846 r58886  
    11MODULE_TOPDIR = ../..
    22
    3 PGM = r.seg
     3PGM = r.segment
    44
    5 LIBES = $(SEGLIB) $(GISLIB)
    6 DEPENDENCIES = $(SEGDEP) $(GISDEP)
     5LIBES = $(GISLIB) $(RASTERLIB) $(SEGMENTLIB)
     6DEPENDENCIES = $(GISDEP) $(RASTERDEP) $(SEGMENTDEP)
     7
    78
    89include $(MODULE_TOPDIR)/include/Make/Module.make
    9 include $(MODULE_TOPDIR)/include/Make/Doxygen.make
    1010
    1111default: cmd
    1212
    13 #doxygen:
    14 DOXNAME = r.seg
  • grass-addons/grass7/raster/r.segment/README

    r43846 r58886  
    1 GRASS 6.4+: install with
    2   g.extension r.seg
     1GRASS GIS module for image segmentation and edge detection
     2        Alfonso Vitti <alfonso.vitti [at] ing.unitn.it>
     3        see http://www.ing.unitn.it/~vittia/sw
    34
    4 ---------------------
    5 Old method:
    6 r.seg GRASS GIS module for image segmentation and edge detection
    7         Alfonso Vitti <alfonso.vitti [at] ing.unitn.it>
    8         see www.ing.unitn.it/~vittia/sw
     5GRASS 7: install with
     6  g.extension r.segment
    97
    10 copy the "r.seg" directory in the "raster" directory of the GRASS source code directory
    11 
    12 from within the "r.seg" directory, as normal user run:
    13 make
    14 
    15 from the GRASS main source code directory, as root run:
    16 make install
    17 or, as normal user:
    18 sudo make install
  • grass-addons/grass7/raster/r.segment/main.c

    r43846 r58886  
    11/****************************************************************************
    22 *
    3  * MODULE:       r.seg
     3 * MODULE:       r.segment (former name: r.seg)
    44 *
    55 * AUTHOR:       Alfonso Vitti <alfonso.vitti ing.unitn.it>
    66 *
    7  * PURPOSE:      generates a piece-wise smooth approximation of the input
     7 * PURPOSE:          generates a piece-wise smooth approximation of the input
    88 *               raster map and a raster map of the discontinuities (edges) of
    99 *               the output approximation. The discontinuities of the output
     
    1212 * REFERENCE:    http://www.ing.unitn.it/~vittia/phd/vitti_phd.pdf
    1313 *
    14  * COPYRIGHT:    (C) 2007-2010
     14 * COPYRIGHT:    (C) 2007-2014 by Alfonso Vitti, and the GRASS Development Team
    1515 *
    1616 *               This program is free software under the GNU General Public
     
    2424#include <string.h>
    2525#include <grass/gis.h>
     26#include <grass/raster.h>
    2627#include <grass/config.h>
    2728#include <grass/glocale.h>
     
    4243    int max_iter;               /* max number of numerical iterations */
    4344    int iter;                   /* iteration index */
    44     char *mapset;               /* current mapset */
     45    const char *mapset;         /* current mapset */
    4546    void *g_row;                /* input row buffers */
    4647    void *out_u_row, *out_z_row;        /* output row buffer */
     
    6667
    6768
    68     /* initialize GRASS environment */
    69     G_gisinit(argv[0]);         /*reads GRASS env */
    70 
     69    G_gisinit(argv[0]);
    7170
    7271    /* initialize module */
    7372    module = G_define_module();
    74     module->keywords = _("image segmentation, edge detection, smooth");
     73    G_add_keyword(_("imagery"));
     74    G_add_keyword(_("segmentation"));
     75    G_add_keyword(_("edge detection"));
     76    G_add_keyword(_("smoothing"));
    7577    module->description =
    76         _("Generates a smooth approximation of the input raster and a discontinuity map");
    77 
    78     parm.in_g = G_define_option();
     78        _("Generates a smooth approximation of the input raster and a discontinuity map.");
     79
     80    parm.in_g = G_define_standard_option(G_OPT_R_INPUTS);
    7981    parm.in_g->key = "in_g";
    80     parm.in_g->type = TYPE_STRING;
    81     parm.in_g->required = YES;
    8282    parm.in_g->description = _("Input raster map to segment");
    83     parm.in_g->gisprompt = "old,cell,raster";
    84 
    85     parm.out_u = G_define_option();
     83
     84    parm.out_u = G_define_standard_option(G_OPT_R_OUTPUT);
    8685    parm.out_u->key = "out_u";
    87     parm.out_u->type = TYPE_STRING;
    88     parm.out_u->required = YES;
    8986    parm.out_u->description = _("Output segmented raster map");
    90     parm.out_u->gisprompt = "new,cell,raster";
    91 
    92     parm.out_z = G_define_option();
     87
     88    parm.out_z = G_define_standard_option(G_OPT_R_OUTPUT);
    9389    parm.out_z->key = "out_z";
    94     parm.out_z->type = TYPE_STRING;
    95     parm.out_z->required = YES;
    9690    parm.out_z->description =
    9791        _("Output raster map with detected discontinuities");
    98     parm.out_z->gisprompt = "new,cell,raster";
    9992
    10093    opts.lambda = G_define_option();
     
    10497    opts.lambda->answer = "1.0";
    10598    opts.lambda->description = _("Smoothness coefficient [>0]");
     99    opts.lambda->guisection = _("Settings");
    106100
    107101    opts.alpha = G_define_option();
     
    111105    opts.alpha->answer = "1.0";
    112106    opts.alpha->description = _("Discontinuity coefficient [>0]");
     107    opts.alpha->guisection = _("Settings");
    113108
    114109    opts.max_iter = G_define_option();
     
    119114    opts.max_iter->description =
    120115        _("Maximal number of numerical iterations");
     116    opts.max_iter->guisection = _("Settings");
    121117
    122118    opts.tol = G_define_option();
     
    126122    opts.tol->answer = "0.001";
    127123    opts.tol->description = _("Convergence tolerance [>0]");
     124    opts.tol->guisection = _("Settings");
    128125
    129126    opts.kepsilon = G_define_option();
     
    134131    opts.kepsilon->description =
    135132        _("Discontinuity thickness [>0]");
     133    opts.kepsilon->guisection = _("Settings");
    136134
    137135    opts.beta = G_define_option();
     
    148146     * have to be set independently from the values used in MS
    149147     */
     148    opts.beta->guisection = _("Settings");
    150149
    151150    flag_k = G_define_flag();
    152151    flag_k->key = 'k';
    153152    flag_k->description = _("Activate MSK model (Mumford-Shah with curvature term)");
    154 
     153    flag_k->guisection = _("Settings");
    155154
    156155    /* parameters and flags parser */
     
    171170
    172171    if (((usek = (flag_k->answer) == 0) && (beta != 0.0)))
    173         G_warning(_
    174                   ("Beta is not zero and you have not activated the MSK formulation: \n \
     172        G_warning(_("Beta is not zero and you have not activated the MSK formulation: \n \
    175173                                beta will be ignored and MS (default) will be used."));
    176174
    177175    if (((usek = (flag_k->answer) == 1) && (beta == 0.0)))
    178         G_warning(_
    179                   ("You have activated the MSK formulation, but beta is zero:\n \
     176        G_warning(_("You have activated the MSK formulation, but beta is zero:\n \
    180177                                beta should be greater than zero in MSK."));
    181178
    182179    /* check existence and names of raster maps */
    183     mapset = G_find_cell2(in_g, "");
    184     if (!mapset)
    185         G_fatal_error(_("raster file [%s] not found"), in_g);
     180    mapset = G_find_raster2(in_g, "");
     181    if (mapset == NULL)
     182        G_fatal_error(_("Raster map <%s> not found"), in_g);
     183
     184/* still needed in GRASS 7?
    186185    if (G_legal_filename(out_u) < 0)
    187186        G_fatal_error(_("[%s] is an illegal file name"), out_u);
     
    192191    if (strcmp(out_u, out_z) == 0)
    193192        G_fatal_error(_("Output raster maps have the same name [%s]"), out_u);
     193*/
    194194
    195195
     
    203203
    204204    /* get the window dimention */
    205     nr = G_window_rows();
    206     nc = G_window_cols();
     205    nr = Rast_window_rows();
     206    nc = Rast_window_cols();
    207207    nrc = nr * nc;
    208208
     
    217217
    218218    /* open the input raster map for reading */
    219     if ((g_fd = G_open_cell_old(in_g, mapset)) < 0)
    220         G_fatal_error(_("cannot open raster file [%s]"), in_g);
    221     if (G_get_cellhd(in_g, mapset, &cellhd) < 0)
    222         G_fatal_error(_("cannot read [%s] header"), in_g);
    223 
     219    g_fd = Rast_open_old(in_g, mapset);
     220
     221    Rast_get_cellhd(in_g, mapset, &cellhd);
    224222
    225223    /* allocate the buffer for storing the values of the input raster map */
    226     g_row = G_allocate_raster_buf(dcell_data_type);
     224    g_row = Rast_allocate_buf(dcell_data_type);
    227225
    228226
     
    231229        jnc = j * nc;
    232230
    233         if (G_get_raster_row(g_fd, g_row, j, dcell_data_type) < 0)
    234             G_fatal_error(_("Cannot read from [%s] raster,"), in_g);
     231        Rast_get_row(g_fd, g_row, j, dcell_data_type);
    235232
    236233        for (i = 0; i < nc; i++) {
     
    243240
    244241    /* close the input raster map and free memory */
    245     G_close_cell(g_fd);
     242    Rast_close(g_fd);
    246243    G_free(g_row);
    247244
     
    273270
    274271    /* print the total number of iteration performed */
    275     G_message("\nr.seg iterations: %i\n", iter);
    276     G_message("\n");
     272    G_message("Total number of iterations: %i", iter);
    277273
    278274    /* open the output raster maps for writing */
    279     if ((out_u_fd = G_open_raster_new(out_u, dcell_data_type)) < 0)
    280         G_fatal_error(_("cannot open raster file [%s]"), out_u);
    281     if ((out_z_fd = G_open_raster_new(out_z, dcell_data_type)) < 0)
    282         G_fatal_error(_("cannot open raster file [%s]"), out_z);
     275    out_u_fd = Rast_open_new(out_u, dcell_data_type);
     276    out_z_fd = Rast_open_new(out_z, dcell_data_type);
    283277
    284278
    285279    /* allocate the buffer for storing the values of the output raster maps */
    286     out_u_row = G_allocate_raster_buf(dcell_data_type);
    287     out_z_row = G_allocate_raster_buf(dcell_data_type);
     280    out_u_row = Rast_allocate_buf(dcell_data_type);
     281    out_z_row = Rast_allocate_buf(dcell_data_type);
    288282
    289283
     
    295289            ((DCELL *) out_z_row)[i] = *(z + jnc + i);
    296290        }
    297         if (G_put_raster_row(out_u_fd, out_u_row, dcell_data_type) < 0)
    298             G_fatal_error(_("cannot write [%s] raster"), out_u);
    299         if (G_put_raster_row(out_z_fd, out_z_row, dcell_data_type) < 0)
    300             G_fatal_error(_("cannot write [%s] raster"), out_z);
     291        Rast_put_row(out_u_fd, out_u_row, dcell_data_type);
     292        Rast_put_row(out_z_fd, out_z_row, dcell_data_type);
    301293    }
    302294
    303295
    304296    /* close the input raster map and free memory */
    305     G_close_cell(out_u_fd);
     297    Rast_close(out_u_fd);
    306298    G_free(out_u_row);
    307     G_close_cell(out_z_fd);
     299    Rast_close(out_z_fd);
    308300    G_free(out_z_row);
    309301    G_free(g);
     
    311303    G_free(z);
    312304
    313 
    314     /* write history file */
    315     G_short_history(out_u, "raster", &history);
    316     G_command_history(&history);
    317     sprintf(history.edhist[3], "iterations = %i", iter);
    318     history.edlinecnt = 4;
    319     G_write_history(out_u, &history);
    320     G_write_history(out_z, &history);
    321 
     305    /* write map history (meta data) */
     306    Rast_short_history(out_u, "raster", &history);
     307    Rast_set_history(&history, HIST_DATSRC_1, in_g);
     308    Rast_append_format_history(&history, "iterations = %i", iter);
     309    Rast_command_history(&history);
     310    Rast_write_history(out_u, &history);
     311    Rast_write_history(out_z, &history);
    322312
    323313    /* exit */
  • grass-addons/grass7/raster/r.segment/r.segment.html

    r58885 r58886  
    11<h2>DESCRIPTION</h2>
    22
    3 <em><b>r.seg</b></em> generates a piece-wise smooth approximation of the
     3<em><b>r.segment</b></em> generates a piece-wise smooth approximation of the
    44input raster map and a raster map of the discontinuities of the output
    55approximation. <br>
     
    2626Remove any MASK before the execution of the module. If
    2727a MASK is present, the module stops after just one iteration.
    28 <br><br>
     28<p>
    2929
    3030Replace (<em>r.null</em>) any null data with the map average value (get with <em>r.univar</em>).
    31 <br><br>
     31<p>
    3232
    3333The segmentation depends on the parameters alpha and lambda:
     
    6262values of the parameter lambda. The larger the total number of pixels
    6363of the input raster map the larger the number of iterations will be.
    64 <br><br>
     64<p>
    6565
    66 The data type of the output raster maps is DOUBLE PRECISION.  <br><br>
     66The data type of the output raster maps is DOUBLE PRECISION.<p>
    6767
    6868The module works on one raster map at a time, imagery groups are not
    69 supported.  <br><br>
     69supported.<p>
    7070
    7171To avoid to inappropriately re-sampled the input raster map, the settings
     
    7979
    8080The discontinuity thickness should be changed for test purposes only.
    81 <br><br>
     81<p>
    8282
    8383The actual need to use the MSK model should be very rare, see [3].
     
    9595<div><pre class="code">
    9696# set the region to match the <em>ortho_2001_t792_1m</em> raster map:
    97 g.region rast=ortho_2001_t792_1m
     97g.region rast=ortho_2001_t792_1m -p
    9898
    9999# select a smaller region:
    100 g.region n=221725 s=220225 w=638350 e=639550
     100g.region n=221725 s=220225 w=638350 e=639550 -p
    101101
    102 # run r.seg:
    103 r.seg in_g=ortho_2001_t792_1m@PERMANENT out_u=u_OF out_z=z_OF lambda=10 alpha=200 mxi=250
     102# run r.segment:
     103r.segment in_g=ortho_2001_t792_1m@PERMANENT out_u=u_OF out_z=z_OF lambda=10 alpha=200 mxi=250
    104104
    105105# for a better visualization of the output raster map <em>u_OF</em>, set its color table to:
     
    118118r.colors z_OF color=bgyr
    119119
    120 # run r.seg with different parameter values:
    121 r.seg in_g=ortho_2001_t792_1m@PERMANENT out_u=u1_OF out_z=z1_OF lambda=10 alpha=65 mxi=250
    122 r.seg in_g=ortho_2001_t792_1m@PERMANENT out_u=u2_OF out_z=z2_OF lambda=10 alpha=600 mxi=250
    123 r.seg in_g=ortho_2001_t792_1m@PERMANENT out_u=u3_OF out_z=z3_OF lambda=0.1 alpha=200 mxi=250
    124 r.seg in_g=ortho_2001_t792_1m@PERMANENT out_u=u4_OF out_z=z4_OF lambda=1 alpha=200 mxi=250
     120# run r.segment with different parameter values:
     121r.segment in_g=ortho_2001_t792_1m@PERMANENT out_u=u1_OF out_z=z1_OF lambda=10 alpha=65 mxi=250
     122r.segment in_g=ortho_2001_t792_1m@PERMANENT out_u=u2_OF out_z=z2_OF lambda=10 alpha=600 mxi=250
     123r.segment in_g=ortho_2001_t792_1m@PERMANENT out_u=u3_OF out_z=z3_OF lambda=0.1 alpha=200 mxi=250
     124r.segment in_g=ortho_2001_t792_1m@PERMANENT out_u=u4_OF out_z=z4_OF lambda=1 alpha=200 mxi=250
    125125
    126126# visualize and compare the different results
Note: See TracChangeset for help on using the changeset viewer.