Hamish wrote:
> I may be dreaming, but I seem to remember writing a script to do that
> some time ago (maybe March?) to crop the blank edges off the NOAA BSB
> format nautical charts. try searching the archives and wiki.
> 
> see also the image classification modules, r.mask, and "g.region zoom=".


nope, not dreaming. see attached.

you'll have to adjust it to suit your needs, but the method works,
especially if the collars are square to the image bounds.

otherwise open the image in GIMP or whatever, use the magic wand select
on the border, set it to 0:0:0 or 255:255:255, reapply georef with
gdal_translate, reimport with r.in.gdal, use r.null to remove 0:0:0 or
255:255:255.


Hamish



      
#!/bin/sh
############################################################################
#
# MODULE:       gdal_slice.sh
#
# AUTHOR(S):    M. Hamish Bowman
#
# PURPOSE:      Crop away the border around an image.
#		Reads a GDAL format raster, outputs a GeoTiff.
#
# COPYRIGHT:    (c) 2009 M.Hamish Bowman, and the GRASS Developement Team
#
#               This program is free software under the GNU General Public
#               License (>=v2). Read the file COPYING that comes with
#               GPSDrive for details.
#
############################################################################


### modify to suit your needs ###

r.in.gdal in=./BSB_ROOT/12200/12200_1_merc.tif out=noaa_12200

MAP="noaa_12200"

g.region rast="$MAP"

# check that we are dealing with a paletted map
eval `r.info -t "$MAP"`
if [ "$datatype" != "CELL" ] ; then
   echo "ERROR: only categorical maps can be processed"
fi


# find cats in map
CATS=`r.category $MAP`

# find black and white category numbers
unset BLACK WHITE SALMON
for CAT in $CATS ; do
  RULE=`r.what.color in="$MAP" value="$CAT"`
  echo "$RULE"
  COLOR=`echo "$RULE" | cut -f2 -d' '`
  if [ "$COLOR" = "0:0:0" ] ; then
     BLACK="$CAT"
  elif [ "$COLOR" = "255:255:255" ] ; then
     WHITE="$CAT"
  elif [ "$COLOR" = "219:73:150" ] ; then
     SALMON="$CAT"
  fi
done
echo "black is $BLACK, white is $WHITE, salmon is $SALMON"

REMAINING=`echo $CATS | tr ' ' '\n' | grep -vw "$BLACK\|$WHITE\|$SALMON"`

#### setup temporary file
TMP="`g.tempfile pid=$$`"
if [ $? -ne 0 ] || [ -z "$TMP" ] ; then
    g.message -e "Unable to create temporary files"
    #exit 1
fi

echo "$BLACK = NULL" > "$TMP"
echo "$WHITE = NULL" >> "$TMP"
echo "$SALMON = NULL" >> "$TMP"
for CAT in $REMAINING ; do
   echo "$CAT = $CAT" >> "$TMP"
done


# remove stray single dots
r.neighbors in="$MAP" out="$MAP.despeckle" method=mode

# make a reclass map, setting black and white to NULL
r.reclass in="$MAP.despeckle" out="$MAP.filtered" rules="$TMP"

g.region zoom="$MAP.filtered"
r.mapcalc "$MAP.cropped = $MAP"

#r.mapcalc "$MAP.cropped = $MAP.filtered"

## debug method for finding residuals
#r.mapcalc "$MAP.cover = 1"
#g.region rast="$MAP"
#r.mapcalc "$MAP.residual = if($MAP.cover == 1, null(), $MAP)"
#r.stats -c "$MAP.residual"
## single 6,8 dot
#d.zoom to collar which should be empty
#r.univar $MAP
#r.out.xyz $MAP
#echo "symbol basic/star 20 -72.35262972 38.8881031 black blue" | d.graph -m
#echo "symbol basic/star 20 -74.51749113 39.27423606 black aqua" | d.graph -m

r.out.gdal in="$MAP" out="$MAP_decollared.tif" type=Byte #...

_______________________________________________
grass-user mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/grass-user

Reply via email to