[GRASS-SVN] r51920 - grass-addons/grass7/imagery/i.segment
svn_grass at osgeo.org
svn_grass at osgeo.org
Fri Jun 1 09:41:31 PDT 2012
Author: momsen
Date: 2012-06-01 09:41:31 -0700 (Fri, 01 Jun 2012)
New Revision: 51920
Modified:
grass-addons/grass7/imagery/i.segment/get_input.c
grass-addons/grass7/imagery/i.segment/iseg.h
grass-addons/grass7/imagery/i.segment/main.c
Log:
untested, first pass framework using segmentation library and groups
Modified: grass-addons/grass7/imagery/i.segment/get_input.c
===================================================================
--- grass-addons/grass7/imagery/i.segment/get_input.c 2012-06-01 15:28:53 UTC (rev 51919)
+++ grass-addons/grass7/imagery/i.segment/get_input.c 2012-06-01 16:41:31 UTC (rev 51920)
@@ -16,18 +16,29 @@
#include <stdlib.h>
#include <grass/gis.h>
#include <grass/glocale.h>
-#include "segment.h"
+#include <grass/imagery.h>
-int get_input(int argc, char *argv[])
+#include <grass/segment.h> /* segmentation library */
+#include "iseg.h"
+
+int get_input(int argc, char *argv[], struct files *files, struct functions *functions)
{
//reference: http://grass.osgeo.org/programming7/gislib.html#Command_Line_Parsing
- input = G_define_standard_option(G_OPT_R_INPUT); /* Request a pointer to memory for each option */
- input->key = "input"; /* TODO: update to allow groups. Maybe this needs a group/subgroup variables, or we can check if the input is group/subgroup/raster */
- input->type = TYPE_STRING;
- input->required = YES;
- input->description = _("Raster map to be segmented.");
+ struct Option *group, *subgroup, *seeds, *output, *method, *threshold; //*input, /* Establish an Option pointer for each option */
+ struct Flag *diagonal; /* Establish a Flag pointer for each option */
+
+ group = G_define_standard_option(G_OPT_I_GROUP);
+ subgroup = G_define_standard_option(G_OPT_I_SUBGROUP);
+
+ //OK to require the user to create a group? Otherwise later add an either/or option to give just a single raster map...
+ //~ input = G_define_standard_option(G_OPT_R_INPUT); /* Request a pointer to memory for each option */
+ //~ input->key = "input"; /* TODO: update to allow groups. Maybe this needs a group/subgroup variables, or we can check if the input is group/subgroup/raster */
+ //~ input->type = TYPE_STRING;
+ //~ input->required = YES;
+ //~ input->description = _("Raster map to be segmented.");
+
//~ seeds = G_define_standard_option(G_OPT_V_INPUT);
//~ seeds->key = "seeds";
//~ seeds->type = TYPE_STRING;
@@ -45,10 +56,12 @@
seeds->description = _("Optional raster map with starting seeds.");
output = G_define_standard_option(G_OPT_R_OUTPUT);
- output->key = "output";
- output->type = TYPE_STRING;
- output->required = YES;
- output->description = _("Name of output raster map.");
+//seems API handles this part...
+ //~ output->key = "output";
+ //~ output->type = TYPE_STRING;
+ //~ output->required = YES;
+ //~ output->description = _("Name of output raster map.");
+
//TODO: when put in a new raster map, error message:
//~ Command 'd.rast map=testing at samples' failed
//~ Details: Raster map <testing at samples> not found
@@ -86,8 +99,141 @@
//~ Unable to fetch interface description for command 'i.segment'.
//~ Details: D1/1: testing debug!
+ //~ G_debug(1, "For the option <%s> you chose: <%s>",
+ //~ input->description, input->answer);
+ //~
+ //~ G_debug(1, "For the option <%s> you chose: <%s>",
+ //~ seeds->description, seeds->answer);
+ //~
+ //~ G_debug(1, "For the option <%s> you chose: <%s>",
+ //~ output->description, output->answer);
+ //~
+ //~ G_debug(1, "For the option <%s> you chose: <%s>",
+ //~ method->description, method->answer);
+ //~
+ //~ G_debug(1, "For the option <%s> you chose: <%s>",
+ //~ threshold->description, threshold->answer);
+ //~
+ //~ G_debug(1, "The value of the diagonal flag is: %d", diagonal->answer);
+
+
if (G_parser(argc, argv))
exit (EXIT_FAILURE);
+
+// Open Files (file segmentation) //
+
+ struct Ref Ref; /* subgroup reference list */
+ int *in_fd, out_fd, *seg_in_fd, seg_out_fd;
+ RASTER_MAP_TYPE data_type;
+ int n, row, col, nrows, ncols, srows, scols, seg_in_mem;
+ void *inbuf;
+ int buf[NCOLS]; /* for copying into segmentation file */
+
+ const char *in_file, out_file;
+
+ // ****** open the input rasters ******* //
+
+ // i.smap/openfiles.c lines 17-23 checked if subgroup had maps, does API handles the checks?
+ // can no subgroup be entered, just a group?
+
+ if (!I_get_subgroup_ref(group->answer,subgroup->answer, &Ref))
+ G_fatal_error(_("Unable to read REF file for subgroup <%s> in group <%s>"),
+ subgroup->answer, group->answer);
+
+ if (Ref.nfiles <= 0)
+ G_fatal_error(_("Subgroup <%s> in group <%s> contains no raster maps"),
+ subgroup->answer, group->answer);
+
+ // open input group maps for reading
+ for(n = 0; n < Ref.nfiles; n++)
+ {
+ in_fd[n] = Rast_open_old(Ref.file[n].name, Ref.file[n].mapset); // in_fd = Rast_open_old(input->answer, "");
+ }
+
+
+
+ // ********** find out file segmentation size ************ //
+
+ //loop again... need to get largest option
+ //for today assume all are the same
+ //data_type = Rast_get_map_type(in_fd[n]);
+ data_type = Rast_get_map_type(in_fd[0]);
+
+ nrows = Rast_window_rows();
+ ncols = Rast_window_cols();
+
+ //TODO: i.cost and i.watershed take different approaches...
+ //hardcode for now
+ srows = nrows / 8;
+ scols = ncols / 8;
+
+ //TODO: make calculations for this
+ seg_in_mem = 4;
+
+// G_debug(1, " %d rows, %d cols", nrows, ncols);
+
+
+ // ******** create temporary segmentation failes **********//
+
+ /* Initalize access to database and create temporary files */
+ for(n = 0; n < Ref.nfiles; n++)
+ {
+ in_file[n] = G_tempfile();
+ }
+
+ out_file = G_tempfile();
+
+ /* Format segmented files */
+ for(n = 0; n < Ref.nfiles; n++)
+ {
+ seg_in_fd[n] = creat(in_file[n], 0666);
+ if (segment_format(seg_in_fd[n], nrows, ncols, srows, scols, sizeof(data_type)) != 1) //TODO: this data_type should be from each map
+ G_fatal_error("can not create temporary file");
+ close(seg_in_fd[n]);
+ }
+ seg_out_fd = creat(out_file, 0666);
+ if (segment_format(seg_out_fd, nrows, ncols, srows, scols, sizeof(data_type)) != 1)
+ G_fatal_error("can not create temporary file");
+ close(seg_out_fd);
+
+
+ /* Open and initialize all segment files */
+ for(n = 0; n < Ref.nfiles; n++)
+ {
+ seg_in_fd[n] = open(in_file[n], 2); //TODO: second parameter here is different in many places...
+ if (segment_init(&files->bands_seg[n], seg_in_fd[n], seg_in_mem) != 1)
+ G_fatal_error("can not initialize temporary file");
+ }
+ seg_out_fd = open(out_file, 2);
+ if (segment_init(&files->out_seg, seg_out_fd, seg_in_mem) != 1)
+ G_fatal_error("can not initialize temporary file");
+
+ /* convert flat files to segmented files */
+ inbuf = Rast_allocate_buf(data_type);
+ /* allocate memory for buf? */
+
+ for (n = 0; n < Ref.nfiles; n++)
+ {
+ for (row = 0; row < nrows; row++)
+ {
+ Rast_get_row(in_fd[n], inbuf, row, data_type);
+ for (col = 0; col < ncols; col++)
+ {
+ /*fill buf */
+ // G_incr_void_ptr
+ }
+ segment_put_row (&files->bands_seg[n], buf, row);
+ }
+ }
+
+ /* repeat for output */
+
+ /* close stuff!!! in_fd, seg_in_fd, etc, etc */
+
+ /*
+ from r.cost
+ unlink(in_file); /* remove submatrix files */
+
return 0;
}
Modified: grass-addons/grass7/imagery/i.segment/iseg.h
===================================================================
--- grass-addons/grass7/imagery/i.segment/iseg.h 2012-06-01 15:28:53 UTC (rev 51919)
+++ grass-addons/grass7/imagery/i.segment/iseg.h 2012-06-01 16:41:31 UTC (rev 51920)
@@ -2,7 +2,7 @@
*
* MODULE: i.segment
* AUTHOR(S): Eric Momsen <eric.momsen at gmail com>
- * PURPOSE: global variable and function listing
+ * PURPOSE: structure definition and function listing
* COPYRIGHT: (C) 2012 by Eric Momsen, and the GRASS Development Team
*
* This program is free software under the GNU General Public
@@ -11,10 +11,45 @@
*
*****************************************************************************/
-extern struct GModule *module;
-extern struct Option *input, *seeds, *output, *method, *threshold;
-extern struct Flag *diagonal;
-extern int in_fd, out_fd;
+struct files
+{
+ /* int *band_fd, out_fd; /* Do I need these, or is the SEGMENT enough? */
+ int nbands;
+ SEGMENT *bands_seg, out_seg;
+
+ // naming using "bands" to follow i.smap but it doesn't need to be just landsat bands
+
+ // If we want to implement the option to load directly to memory
+ // instead of declaring "SEGMENT" could we declare as void
+ // then assign either a SEGMENT or a matrix during the get_input procedure?
+ // i.smap has this: struct Categories output_labels;
+ // I don't think I'll be using categories...
+
+ // i.smap has: char *isdata;
+ // not sure yet if I'll need that
+
+ //will need to add something here for the vector contsraints and for seeds
+ // store those directly in RAM instead of SEGMENT library?
+};
+
+
+// I think if I use function pointers, I can set up one time in the input
+// what similarity function, etc, will be used later in the processing
+// and make it easier to add additional variations later.
+
+struct functions
+{
+
+//based on options, such as diagonal neighbors, etc:
+// find_neighbor
+// calc_simularity
+
+ //not really a function, but input to the functions
+ double threshold;
+};
+
/* get_input.c */
-int get_input(int, char *[]);
+/* gets input from user, validates, opens files, and sets up functions */
+int get_input(int, char *[], struct files *, struct functions *);
+
Modified: grass-addons/grass7/imagery/i.segment/main.c
===================================================================
--- grass-addons/grass7/imagery/i.segment/main.c 2012-06-01 15:28:53 UTC (rev 51919)
+++ grass-addons/grass7/imagery/i.segment/main.c 2012-06-01 16:41:31 UTC (rev 51920)
@@ -8,7 +8,13 @@
* This program is free software under the GNU General Public
* License (>=v2). Read the COPYING file that comes with GRASS
* for details.
+ *
*
+ * NOTE: the word "segment" is already used by the Segmentation
+ * Library for the data files/tiling, so iseg (image segmentation)
+ * will be used to refer to the image segmentation.
+ *
+ *
*****************************************************************************/
// #include <grass/config.h>
@@ -19,7 +25,7 @@
//#include <grass/imagery.h>
#include <grass/glocale.h> //defines _() what exactly is that doing...(something about local language translation?) anything else in glocale.h that I should be aware of...
#include <grass/raster.h>
-#include "segment.h"
+#include "iseg.h"
//~ (for my reference, order for headers), adding them only as needed...
//~ 1. Core system headers (stdio.h, ctype.h, ...)
@@ -27,19 +33,12 @@
//~ 3. Headers for core systems of the package being compiled (grass/gis.h, grass/glocale.h, ...)
//~ 4. Headers for the specific library/program being compiled (geodesic.h, ...)
-struct GModule *module;
-struct Option *input, *seeds, *output, *method, *threshold; /* Establish an Option pointer for each option */
-struct Flag *diagonal; /* Establish a Flag pointer for each option */
-int in_fd, out_fd;
-
-RASTER_MAP_TYPE data_type;
-int row, col, nrows, ncols;
-void *inbuf;
-
-// ? Any reasons not to use "modular global" variables for this application?
-
int main(int argc, char *argv[])
{
+ struct files files; /* input and output file descriptors, data structure, buffers */
+ struct functions functions; /* function pointers and parameters for the calculations */
+ struct GModule *module;
+
G_gisinit(argv[0]);
module = G_define_module();
@@ -47,47 +46,13 @@
G_add_keyword(_("segmentation"));
module->description = _("Segments an image.");
- get_input(argc, argv);
+ get_input(argc, argv, files, functions);
- G_debug(1, "For the option <%s> you chose: <%s>",
- input->description, input->answer);
-
- G_debug(1, "For the option <%s> you chose: <%s>",
- seeds->description, seeds->answer);
-
- G_debug(1, "For the option <%s> you chose: <%s>",
- output->description, output->answer);
-
- G_debug(1, "For the option <%s> you chose: <%s>",
- method->description, method->answer);
-
- G_debug(1, "For the option <%s> you chose: <%s>",
- threshold->description, threshold->answer);
-
- G_debug(1, "The value of the diagonal flag is: %d", diagonal->answer);
-
-
-
- in_fd = Rast_open_old(input->answer, "");
- data_type = Rast_get_map_type(in_fd); /*TODO: when add group/subgroup, need to check if all input are same data_type*/
- out_fd = Rast_open_new(output->answer, data_type);
- inbuf = Rast_allocate_buf(data_type);
-
- nrows = Rast_window_rows();
- ncols = Rast_window_cols();
-
- for (row = 0; row < nrows; row++) {
- Rast_get_row(in_fd, inbuf, row, data_type);
- //~ for (col = 0; col < ncols; col++) {
- //~
- //~ }
-
- Rast_put_row(out_fd, inbuf, data_type);
- }
-
+//need to update this part still:
G_free(inbuf);
Rast_close(in_fd);
Rast_close(out_fd);
+//
G_done_msg("Number of segments created: ");
More information about the grass-commit
mailing list