[GRASS-SVN] r52440 - grass-addons/grass7/imagery/i.histo.match
svn_grass at osgeo.org
svn_grass at osgeo.org
Tue Jul 24 03:45:46 PDT 2012
Author: lucadelu
Date: 2012-07-24 03:45:35 -0700 (Tue, 24 Jul 2012)
New Revision: 52440
Modified:
grass-addons/grass7/imagery/i.histo.match/i.histo.match.py
Log:
improvements on hamish and micheal suggestions
Modified: grass-addons/grass7/imagery/i.histo.match/i.histo.match.py
===================================================================
--- grass-addons/grass7/imagery/i.histo.match/i.histo.match.py 2012-07-24 03:06:56 UTC (rev 52439)
+++ grass-addons/grass7/imagery/i.histo.match/i.histo.match.py 2012-07-24 10:45:35 UTC (rev 52440)
@@ -19,7 +19,8 @@
#############################################################################
#%module
#% description: Calculate histogram matching of several images.
-#% keywords: raster
+#% keywords: imagery
+#% keywords: histogram matching
#%end
#%option G_OPT_R_INPUTS
#% description: Name of raster maps to analize
@@ -64,10 +65,10 @@
if dbopt.find('$GISDBASE/$LOCATION_NAME/$MAPSET') == 0:
dbopt_split = dbopt.split(os.sep)[-1]
env = grass.gisenv()
- path = os.path.join(env['GISDBASE'],env['LOCATION_NAME'],env['MAPSET'])
- dbpath = os.path.join(path,dbopt_split)
+ path = os.path.join(env['GISDBASE'], env['LOCATION_NAME'], env['MAPSET'])
+ dbpath = os.path.join(path, dbopt_split)
else:
- if os.access(os.path.dirname(dbopt),os.W_OK):
+ if os.access(os.path.dirname(dbopt), os.W_OK):
path = os.path.dirname(dbopt)
dbpath = dbopt
else:
@@ -87,14 +88,15 @@
query_create += "integer, cumulative_histogram integer, cdf real)"
curs.execute(query_create)
# set the region on the raster
+ grass.use_temp_region()
grass.run_command('g.region', rast = i)
# calculate statistics
- stats_out = grass.pipe_command('r.stats', flags='c', input= i, fs=':')
+ stats_out = grass.pipe_command('r.stats', flags='cin', input= i, fs=':')
stats = stats_out.communicate()[0].split('\n')[:-1]
stats_dict = dict( s.split(':', 1) for s in stats)
- cdf = 0
+ cdf = 0
# for each number in the range
- for n in range(0,max_value):
+ for n in range(0, max_value):
# try to insert the values otherwise insert 0
try:
val = int(stats_dict[str(n)])
@@ -111,23 +113,23 @@
# number of pixel is the cdf value
numPixel = cdf
# for each number in the range
- for n in range(0,max_value):
+ for n in range(0, max_value):
# select value for cumulative_histogram for the range number
- select_ch="SELECT cumulative_histogram FROM t%s WHERE (grey_value=%i)" % (
- iname, n)
+ select_ch = "SELECT cumulative_histogram FROM t%s WHERE " % iname
+ select_ch += "(grey_value=%i)" % n
result = curs.execute(select_ch)
val = result.fetchone()[0]
# update cdf with new value
if val != 0 and numPixel != 0:
- update_cdf = round(float(val) / float(numPixel),6)
- update_cdf = "UPDATE t%s SET cdf=%s WHERE (grey_value=%i)" % (
- iname, update_cdf, n)
+ update_cdf = round(float(val) / float(numPixel), 6)
+ update_cdf = "UPDATE t%s SET cdf=%s WHERE (grey_value=%i)" % (n,
+ iname, update_cdf,)
curs.execute(update_cdf)
db.commit()
db.commit()
pixelTot = 0
# for each number in the range
- for n in range(0,max_value):
+ for n in range(0, max_value):
numPixel = 0
# for each image
for i in images:
@@ -149,7 +151,7 @@
curs.execute(query_create)
cHist = 0
# for each number in the range
- for n in range(0,max_value):
+ for n in range(0, max_value):
tot = 0
# for each image
for i in images:
@@ -165,20 +167,22 @@
cHist = cHist + int(average)
# insert new values into average table
if cHist != 0 and pixelTot != 0:
- cdf = round(float(cHist) / float(pixelTot),6)
- insert = "INSERT INTO %s VALUES (%i, %i, %i, %s)" % (table_ave, n, int(average), cHist, cdf)
+ cdf = round(float(cHist) / float(pixelTot), 6)
+ insert = "INSERT INTO %s VALUES (%i, %i, %i, %s)" % (table_ave, n,
+ int(average), cHist, cdf)
curs.execute(insert)
db.commit()
# for each image
for i in images:
iname = i.split('@')[0]
+ grass.use_temp_region()
grass.run_command('g.region', rast = i)
# write average rules file
- outfile = open(os.path.join(path,'%s.reclass' % iname),'w')
+ outfile = open(grass.tempfile(), 'w')
new_grey = 0
- for n in range(0,max_value):
- select_min = "SELECT min(abs(a.cdf - b.cdf)) FROM t%s as a," % iname \
- + " %s as b WHERE (a.grey_value=%i)" % (table_ave, n)
+ for n in range(0, max_value):
+ select_min = "SELECT min(abs(a.cdf - b.cdf)) FROM t%s as a," % iname
+ select_min += " %s as b WHERE (a.grey_value=%i)" % (table_ave, n)
result_min = curs.execute(select_min)
min_abs = result_min.fetchone()[0]
select_cdf = "SELECT cdf FROM t%s WHERE grey_value=%i" % (iname, n)
@@ -201,11 +205,13 @@
result = grass.core.find_file(outname, element = 'cell')
if result['fullname'] and grass.overwrite():
grass.run_command('g.remove', rast=outname)
- grass.run_command('r.reclass', input= i, out = outname, rules = outfile.name)
+ grass.run_command('r.reclass', input= i, out = outname,
+ rules = outfile.name)
elif result['fullname'] and not grass.overwrite():
grass.warning(_("Raster map %s already exists and it will be not overwrite" % i))
else:
- grass.run_command('r.reclass', input= i, out = outname, rules = outfile.name)
+ grass.run_command('r.reclass', input= i, out = outname,
+ rules = outfile.name)
# remove the rules file
grass.try_remove(outfile.name)
db.commit()
More information about the grass-commit
mailing list