[GRASS-SVN] r51980 - grass-addons/grass7/imagery/i.segment

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Jun 5 21:24:26 PDT 2012


Author: momsen
Date: 2012-06-05 21:24:26 -0700 (Tue, 05 Jun 2012)
New Revision: 51980

Added:
   grass-addons/grass7/imagery/i.segment/create_isegs.c
Modified:
   grass-addons/grass7/imagery/i.segment/iseg.h
   grass-addons/grass7/imagery/i.segment/main.c
   grass-addons/grass7/imagery/i.segment/parse_args.c
   grass-addons/grass7/imagery/i.segment/write_output.c
Log:
ran indent.  I/O portion of module runs, but doesn't save output raster.

Added: grass-addons/grass7/imagery/i.segment/create_isegs.c
===================================================================
--- grass-addons/grass7/imagery/i.segment/create_isegs.c	                        (rev 0)
+++ grass-addons/grass7/imagery/i.segment/create_isegs.c	2012-06-06 04:24:26 UTC (rev 51980)
@@ -0,0 +1,44 @@
+/* PURPOSE:      Develop the image segments */
+
+/* Currently only region growing is implemented */
+
+#include <stdlib.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+#include <grass/imagery.h>
+#include <grass/segment.h>	/* segmentation library */
+#include "iseg.h"
+
+int create_isegs(struct files *files, struct functions *functions)
+{
+    int row, col, nrows, ncols, *segval;
+    double *bandsval;
+
+    /* **************write fake data to test I/O portion of module */
+
+    /*picked this up in the file reading as well, just do it again? */
+    nrows = Rast_window_rows();
+    ncols = Rast_window_cols();
+
+    /* initialize data structure */
+    segval = (int *)G_malloc(2 * sizeof(int));
+    /* TODO why didn't this work?  error: request fof member 'nbands' in something not a structure or union */
+    /* bandsval = (double *) G_malloc(files.nbands * sizeof(double)); */
+    bandsval = (double *)G_malloc(1 * sizeof(double));
+
+    G_verbose_message("writing fake data");
+    for (row = 0; row < nrows; row++) {
+	G_percent(row, nrows, 1);	/*this didn't get displayed in the output??? Does it get erased when done? */
+	for (col = 0; col < ncols; col++) {
+	    //segval[0] = bandsval[0]; /*segment number */  /* just copying the map for testing. */
+	    segval[0] = 42;
+	    segval[1] = 1;	/*processing flag */
+	    segment_put(&files->out_seg, (void *)segval, row, col);
+	}
+    }
+
+    /* TODO: free memory */
+
+    return 0;
+}

Modified: grass-addons/grass7/imagery/i.segment/iseg.h
===================================================================
--- grass-addons/grass7/imagery/i.segment/iseg.h	2012-06-05 20:28:52 UTC (rev 51979)
+++ grass-addons/grass7/imagery/i.segment/iseg.h	2012-06-06 04:24:26 UTC (rev 51980)
@@ -1,3 +1,4 @@
+
 /****************************************************************************
  *
  * MODULE:       i.segment
@@ -16,27 +17,27 @@
 
 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?
+    /* int *band_fd, out_fd;   Do I need these, or is the SEGMENT enough? */
+    int nbands;
+    SEGMENT bands_seg, out_seg;
 
-	// 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?
-	
-	char *out_name; /* name of output raster map */
-	RASTER_MAP_TYPE data_type; /* assuming input and output are the same right now */
+    // 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?
+
+    char *out_name;		/* name of output raster map */
+    RASTER_MAP_TYPE data_type;	/* assuming input and output are the same right now */
 };
 
 
@@ -47,13 +48,13 @@
 struct functions
 {
 
-//based on options, such as diagonal neighbors, etc:
-//	find_neighbor  - point to euclidean or manhattan or ... neighbor function
-//	calc_simularity
-		
-	/* not really a function, but carry these along here to have one less variable to pass? */
-	float threshold;
+    //based on options, such as diagonal neighbors, etc:
+    //      find_neighbor  - point to euclidean or manhattan or ... neighbor function
+    //      calc_simularity
 
+    /* not really a function, but carry these along here to have one less variable to pass? */
+    float threshold;
+
 };
 
 /* parse_args.c */
@@ -63,3 +64,6 @@
 /* write_output.c */
 /* also currently closes files */
 int write_output(struct files *);
+
+/* create_isegs.c */
+int create_isegs(struct files *, struct functions *);

Modified: grass-addons/grass7/imagery/i.segment/main.c
===================================================================
--- grass-addons/grass7/imagery/i.segment/main.c	2012-06-05 20:28:52 UTC (rev 51979)
+++ grass-addons/grass7/imagery/i.segment/main.c	2012-06-06 04:24:26 UTC (rev 51980)
@@ -1,3 +1,4 @@
+
 /****************************************************************************
  *
  * MODULE:       i.segment
@@ -19,31 +20,33 @@
 
 #include <stdlib.h>
 #include <grass/gis.h>
-#include <grass/glocale.h> /* message translation */
+#include <grass/glocale.h>	/* message translation */
 #include "iseg.h"
 
 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();
-	G_add_keyword(_("imagery"));
-	G_add_keyword(_("segmentation"));
-	module->description = _("Segments an image.");
+    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_debug(1, "Starting parse_args...");
-	parse_args(argc, argv, &files, &functions);
-	/* Write Segmentation Function Next Week !!! */
-    
+    G_gisinit(argv[0]);
+
+    module = G_define_module();
+    G_add_keyword(_("imagery"));
+    G_add_keyword(_("segmentation"));
+    module->description = _("Segments an image.");
+
+    parse_args(argc, argv, &files, &functions);
+    /* Write Segmentation Function Next Week !!! */
+
+    G_debug(1, "Main: starting create_isegs()");
+    create_isegs(&files, &functions);
+
     G_debug(1, "starting write_output...");
-	G_verbose_message("Main: starting write_output...");
+    G_verbose_message("Main: starting write_output...");
     write_output(&files);
-    
-	G_done_msg("Number of segments created: ");
-	
-	exit(EXIT_SUCCESS);
+
+    G_done_msg("Number of segments created: ");
+
+    exit(EXIT_SUCCESS);
 }

Modified: grass-addons/grass7/imagery/i.segment/parse_args.c
===================================================================
--- grass-addons/grass7/imagery/i.segment/parse_args.c	2012-06-05 20:28:52 UTC (rev 51979)
+++ grass-addons/grass7/imagery/i.segment/parse_args.c	2012-06-06 04:24:26 UTC (rev 51980)
@@ -18,21 +18,21 @@
     struct Option *group, *seeds, *output, *method, *threshold;	/* Establish an Option pointer for each option */
     struct Flag *diagonal;	/* Establish a Flag pointer for each option */
 
-	/* for the opening files portion */
+    /* for the opening files portion */
     struct Ref Ref;		/* group reference list */
     int *in_fd;
     RASTER_MAP_TYPE data_type;
     int n, row, col, nrows, ncols, srows, scols, inlen, outlen, nseg;
-    DCELL **inbuf;  /* buffer array, to store lines from each of the imagery group rasters */
-	double *inval;  /* array, to collect data from one column of inbuf to be put into segmentation file */
-    char *in_file, *out_file;  /* original functions required const char, new segment_open() does not */
+    DCELL **inbuf;		/* buffer array, to store lines from each of the imagery group rasters */
+    double *inval;		/* array, to collect data from one column of inbuf to be put into segmentation file */
+    char *in_file, *out_file;	/* original functions required const char, new segment_open() does not */
 
-	/*debugging */
-	SEGMENT testseg;
+    /*debugging */
+    SEGMENT testseg;
 
     group = G_define_standard_option(G_OPT_I_GROUP);
 
-	/* deleted subgroup line, but still appears in input form */
+    /* deleted subgroup line, but still appears in input form */
 
     /* OK to require the user to create a group?  Otherwise later add an either/or option to give just a single raster map... */
 
@@ -78,28 +78,27 @@
     diagonal = G_define_flag();
     diagonal->key = 'd';
     diagonal->description =
-	_
-	("Use 8 neighbors (3x3 neighborhood) instead of the default 4 neighbors for each pixel.");
+	_("Use 8 neighbors (3x3 neighborhood) instead of the default 4 neighbors for each pixel.");
 
 
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
     G_debug(1, "For the option <%s> you chose: <%s>",
-    group->description, group->answer);
-    
+	    group->description, group->answer);
+
     G_debug(1, "For the option <%s> you chose: <%s>",
-    seeds->description, seeds->answer);
-    
+	    seeds->description, seeds->answer);
+
     G_debug(1, "For the option <%s> you chose: <%s>",
-    output->description, output->answer);
-    
+	    output->description, output->answer);
+
     G_debug(1, "For the option <%s> you chose: <%s>",
-    method->description, method->answer);
-    
+	    method->description, method->answer);
+
     G_debug(1, "For the option <%s> you chose: <%s>",
-    threshold->description, threshold->answer);
-    
+	    threshold->description, threshold->answer);
+
     G_debug(1, "The value of the diagonal flag is: %d", diagonal->answer);
 
     /* Validation */
@@ -135,26 +134,23 @@
     /* TODO, this is from i.smap/openfiles.c  lines 17-23 checked if subgroup had maps, does API handles the checks? */
 
     if (!I_get_group_ref(group->answer, &Ref))
-	G_fatal_error(_
-		      ("Unable to read REF file for group <%s>"),
+	G_fatal_error(_("Unable to read REF file for group <%s>"),
 		      group->answer);
 
     if (Ref.nfiles <= 0)
-	G_fatal_error(_
-		      ("Group <%s> contains no raster maps"),
-		      group->answer);
+	G_fatal_error(_("Group <%s> contains no raster maps"), group->answer);
 
     /* Read Imagery Group */
 
     in_fd = G_malloc(Ref.nfiles * sizeof(int));
-	inbuf = (DCELL **) G_malloc(Ref.nfiles * sizeof(DCELL *));
-	inval = (double *) G_malloc(Ref.nfiles * sizeof(double));
+    inbuf = (DCELL **) G_malloc(Ref.nfiles * sizeof(DCELL *));
+    inval = (double *)G_malloc(Ref.nfiles * sizeof(double));
 
 
     G_verbose_message("Opening input rasters...");
     for (n = 0; n < Ref.nfiles; n++) {
-		inbuf[n] = Rast_allocate_d_buf();
-		in_fd[n] = Rast_open_old(Ref.file[n].name, Ref.file[n].mapset);
+	inbuf[n] = Rast_allocate_d_buf();
+	in_fd[n] = Rast_open_old(Ref.file[n].name, Ref.file[n].mapset);
     }
 
 
@@ -170,11 +166,11 @@
     files->data_type = data_type;
 
 
-	/* size of each element to be stored */
-	
-	inlen = sizeof(double) * Ref.nfiles;
-	outlen = sizeof(int) * 2;  /* change in write_output.c if this value changes TODO: better to save this in the files data structure?*/
+    /* size of each element to be stored */
 
+    inlen = sizeof(double) * Ref.nfiles;
+    outlen = sizeof(int) * 2;	/* change in write_output.c if this value changes TODO: better to save this in the files data structure? */
+
     nrows = Rast_window_rows();
     ncols = Rast_window_cols();
 
@@ -195,45 +191,49 @@
     in_file = G_tempfile();
     out_file = G_tempfile();
 
-	G_debug(1, "Image size:  %d rows, %d cols", nrows, ncols);
-	G_debug(1, "Segmented to tiles with size:  %d rows, %d cols", srows, scols);
-	G_debug(1, "File names, in: %s, out: %s", in_file, out_file);
-	G_debug(1, "Data element size, in: %d , out: %d ", inlen, outlen);
-	G_debug(1, "number of segments to have in memory: %d", nseg);
-	G_debug(1, "hi");
-		
-	/* size: reading all input bands as DCELL  TODO: could consider checking input to see if it is all FCELL or CELL, could reduce memory requirements.*/
+    G_debug(1, "Image size:  %d rows, %d cols", nrows, ncols);
+    G_debug(1, "Segmented to tiles with size:  %d rows, %d cols", srows,
+	    scols);
+    G_debug(1, "File names, in: %s, out: %s", in_file, out_file);
+    G_debug(1, "Data element size, in: %d , out: %d ", inlen, outlen);
+    G_debug(1, "number of segments to have in memory: %d", nseg);
 
-	if (segment_open(&files->bands_seg, in_file, nrows, ncols, srows, scols, inlen, nseg ) != 1)
-		G_fatal_error("Unable to create input temporary files");
-	
-	/* programmer's manual had G_tempfile() directly in call.  Do I need to know the temp file name to delete it? */
-	
-	G_debug(1, "finished segment_open(...bands_seg...)");
-	
-	/* TODO: signed integer gives a 2 billion segment limit, depending on how the initialization is done, this means 2 billion max input pixels. */
-	if (segment_open(&files->out_seg, out_file, nrows, ncols, srows, scols, outlen, nseg ) != 1)
-		G_fatal_error("Unable to create output temporary files");
-	
-/*
-int segment_open(SEGMENT *SEG, char *fname, off_t nrows, off_t ncols, int srows, int scols, int len, int nseg), open a new segment structure.
+    /* size: reading all input bands as DCELL  TODO: could consider checking input to see if it is all FCELL or CELL, could reduce memory requirements. */
 
-A new file with full path name fname will be created and formatted. The original nonsegmented data matrix consists of nrows and ncols. The segments consist of srows by scols. The data items have length len bytes. The number of segments to be retained in memory is given by nseg. This routine calls segment_format() and segment_init(), see below. If segment_open() is used, the routines segment_format() and segment_init() must not be used.
-*/
+    if (segment_open
+	(&files->bands_seg, in_file, nrows, ncols, srows, scols, inlen,
+	 nseg) != 1)
+	G_fatal_error("Unable to create input temporary files");
 
+    /* programmer's manual had G_tempfile() directly in call.  Do I need to know the temp file name to delete it? */
+
+    G_debug(1, "finished segment_open(...bands_seg...)");
+
+    /* TODO: signed integer gives a 2 billion segment limit, depending on how the initialization is done, this means 2 billion max input pixels. */
+    if (segment_open
+	(&files->out_seg, out_file, nrows, ncols, srows, scols, outlen,
+	 nseg) != 1)
+	G_fatal_error("Unable to create output temporary files");
+
+    /*
+       int segment_open(SEGMENT *SEG, char *fname, off_t nrows, off_t ncols, int srows, int scols, int len, int nseg), open a new segment structure.
+
+       A new file with full path name fname will be created and formatted. The original nonsegmented data matrix consists of nrows and ncols. The segments consist of srows by scols. The data items have length len bytes. The number of segments to be retained in memory is given by nseg. This routine calls segment_format() and segment_init(), see below. If segment_open() is used, the routines segment_format() and segment_init() must not be used.
+     */
+
     /* load input bands to segment structure */
     G_verbose_message("Reading input rasters into temporary data files...");
 
     for (row = 0; row < nrows; row++) {
-		for (n = 0; n < Ref.nfiles; n++) {
-			Rast_get_d_row(in_fd[n], inbuf[n], row);
-		}
-		for (col = 0; col < ncols; col++) {
-			for (n = 0; n < Ref.nfiles; n++){
-				inval[n] = inbuf[n][col];
-			}
-		   segment_put(&files->bands_seg, (void *)inval, row, col);
-		}
+	for (n = 0; n < Ref.nfiles; n++) {
+	    Rast_get_d_row(in_fd[n], inbuf[n], row);
+	}
+	for (col = 0; col < ncols; col++) {
+	    for (n = 0; n < Ref.nfiles; n++) {
+		inval[n] = inbuf[n][col];
+	    }
+	    segment_put(&files->bands_seg, (void *)inval, row, col);
+	}
     }
 
     /* Don't need to do anything else for the output segmentation file?  It is initialized to zeros and will be written to later... */

Modified: grass-addons/grass7/imagery/i.segment/write_output.c
===================================================================
--- grass-addons/grass7/imagery/i.segment/write_output.c	2012-06-05 20:28:52 UTC (rev 51979)
+++ grass-addons/grass7/imagery/i.segment/write_output.c	2012-06-06 04:24:26 UTC (rev 51980)
@@ -4,58 +4,60 @@
 #include <stdlib.h>
 #include <grass/gis.h>
 #include <grass/glocale.h>
-#include <grass/segment.h> /* segmentation library */
+#include <grass/imagery.h>
+#include <grass/segment.h>	/* segmentation library */
 #include "iseg.h"
 
 int write_output(struct files *files)
 {
-	int out_fd, row, col, nrows, ncols;
-	CELL *outbuf;
-	int *outval;
-	
-	outbuf = Rast_allocate_c_buf();
-	outval = (int *) G_malloc(2 * sizeof(int));
+    int out_fd, row, col, nrows, ncols;
+    CELL *outbuf;
+    int *outval;
 
-	/*picked this up in the file reading as well, just do it again? */
+    outbuf = Rast_allocate_c_buf();	/*hold one row of data to put into raster */
+    outval = (int *)G_malloc(2 * sizeof(int));	/* hold one "cell" from the segmentation file */
+
+    /*picked this up in the file reading as well, just do it again? */
     nrows = Rast_window_rows();
     ncols = Rast_window_cols();
 
-	/* Todo: return codes are 1 for these, need to check and react to errors? programmer's manual didn't include it... */
-	
-	segment_flush(&files->out_seg); /* force all data to disk */
-	
-	G_debug(1, "preparing output raster");
-	/* open output raster map */
-	out_fd = Rast_open_new(files->out_name, files->data_type); /* I assume even if it already exists, this will overwrite it... */
-	
-	G_debug(1, "start data transfer from segmentation file to raster");
-	/* transfer data from segmentation file to raster*/
-	/* output segmentation file, each element includes the segment ID then the processing flag.  So just need the first part of it. */
-	/* Hmm, does this mean I can't use a buffer, since the length isn't simply integer or float?! */
+    /* Todo: return codes are 1 for these, need to check and react to errors? programmer's manual didn't include it... */
 
+    segment_flush(&files->out_seg);	/* force all data to disk */
+
+    G_debug(1, "preparing output raster");
+    /* open output raster map */
+    out_fd = Rast_open_new(files->out_name, files->data_type);	/* I assume even if it already exists, this will overwrite it... */
+
+    G_debug(1, "start data transfer from segmentation file to raster");
+    /* transfer data from segmentation file to raster */
+    /* output segmentation file, each element includes the segment ID then the processing flag.  So just need the first part of it. */
+    /* Hmm, does this mean I can't use a buffer, since the length isn't simply integer or float?! */
+
     for (row = 0; row < nrows; row++) {
-		G_percent(row, nrows, 1);
-		for (col = 0; col < ncols; col++) {
-			G_debug(1, "got here");
-			segment_get(&files->out_seg, &outval, row, col); /* segment_get (&seg, &value, row, col); */
-			G_debug(1, "segment_get() worked");
-			G_debug(1, "outval[0] = %i", outval[0]); /*This didn't work - something is wrong with outval reference */
-			outbuf[col] = outval[0]; /*just want segment assignment, not the other processing flag(s) */
-			}
-		G_debug(1, "just before Rast_put_row()");
-		Rast_put_row(out_fd, outbuf, CELL_TYPE);
+	G_percent(row, nrows, 1);
+	for (col = 0; col < ncols; col++) {
+	    //~ G_debug(1, "got here");
+	    /* didn't work?                 segment_get(&files->out_seg, &outval, row, col); */
+	    segment_get(&files->out_seg, (void *)outval, row, col);	/* segment_get (&seg, &value, row, col); */
+	    //~ G_debug(1, "segment_get() worked");
+	    //~ G_debug(1, "outval[0] = %i", outval[0]); /*This didn't work - something is wrong with outval reference */
+	    outbuf[col] = outval[0];	/*just want segment assignment, not the other processing flag(s) */
 	}
+	//~ G_debug(1, "just before Rast_put_row()");
+	Rast_put_row(out_fd, outbuf, CELL_TYPE);
+    }
 
-	
-	/* close segmentation files and output raster */
-	G_debug(1, "closing files");
-	segment_release (&files->bands_seg);
-	segment_release (&files->out_seg);
-	close (out_fd);
 
-/* TODO Note: The Segment Library does not know the name of the segment file. It does not attempt to remove the file. If the file is only temporary, the programmer should remove the file after closing it. */
+    /* close segmentation files and output raster */
+    G_debug(1, "closing files");
+    segment_release(&files->bands_seg);
+    segment_release(&files->out_seg);
+    close(out_fd);
 
-/* anything else left to clean up? */
+    /* TODO Note: The Segment Library does not know the name of the segment file. It does not attempt to remove the file. If the file is only temporary, the programmer should remove the file after closing it. */
 
-	return 0;
+    /* anything else left to clean up? */
+
+    return 0;
 }



More information about the grass-commit mailing list