How to erase shoreline but preserve admin land borders?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
yarr
Posts: 12
Joined: 2016-09-05T02:20:25-07:00
Authentication code: 1151

How to erase shoreline but preserve admin land borders?

Post by yarr »

Hello

I need to separate and remove shoreline from the administrative boundaries mask.
The best result I achieved with these two commands:

Code: Select all

convert admin_borders.png \( oceanmask.png -blur 0x5 -level 99,100% \) \
	-compose In -composite admin_borders_choked.png

convert oceanmask.png -background Navy -alpha shape admin_borders_choked.png \
	-compose Screen -composite admin_borders_no_shoreline.png
but they are not sufficient when applied to developed shoreline.

Target images are much larger (7680 width) and lines are 3px width.

Can you please help me improve this procedure?

yarr
ImageMagick 6.9.2-0 Q16 x64 2015-08-15
Last edited by yarr on 2017-06-23T13:10:38-07:00, edited 1 time in total.
yarr

ImageMagick 7.0.6-0 Q16 x64 2017-06-11
Windows 7 Professional 64 bit, i7-4790 CPU 3.60 GHz, RAM 32 GB
Cartographic environment: Perl, GDAL, Global Mapper
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to erase shoreline but preserve admin land borders?

Post by fmw42 »

I am not sure I understand what you are trying to do and which lines you want to remove. Can you explain better or mark on your images what you want to remove. Do you only want the white lines in admin_borders_choked.png to show on admin_borders_no_shoreline.png or do you want the shoreline (dark borders) to show in white on the admin_borders_no_shoreline.png. Or do you want both white and black lines to show as white on your admin_borders_no_shoreline.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to erase shoreline but preserve admin land borders?

Post by fmw42 »

You do not say what platform/OS you are using and your syntax seems mixed between Window and Unix.

Try this:

Window:

Code: Select all

convert admin_borders.png ( oceanmask.png -blur 0x5 -level 99,100% ) ^
-compose In -composite -solarize 50% -level 0x50% -negate admin_borders_choked2.png

Code: Select all

convert oceanmask.png -background Navy -alpha shape admin_borders_choked2.png ^
-compose Screen -composite admin_borders_no_shoreline2.png
Unix:

Code: Select all

convert admin_borders.png \( oceanmask.png -blur 0x5 -level 99,100% \) \
-compose In -composite -solarize 50% -level 0x50% -negate admin_borders_choked2.png

Code: Select all

convert oceanmask.png -background Navy -alpha shape admin_borders_choked2.png \
-compose Screen -composite admin_borders_no_shoreline2.png
yarr
Posts: 12
Joined: 2016-09-05T02:20:25-07:00
Authentication code: 1151

Re: How to erase shoreline but preserve admin land borders?

Post by yarr »

Thank you, Fred.

Generally I use Windows but its scripting environment is too lazy so I often use Cygwin console too.
Sorry for this mixed syntax - I know the difference between systems.

I would like only to preserve white borders between countries.
Shoreline should be removed but it should be done as thoroughly as possible.

admin_borders_no_shoreline.png file is only for demonstration purpose.

Target file is: admin_borders_choked.png - white transparent lines without traces of shoreline.

Source files comes from vector shapefiles so I can render them much larger if it can help simplifying procedure.

yarr
yarr

ImageMagick 7.0.6-0 Q16 x64 2017-06-11
Windows 7 Professional 64 bit, i7-4790 CPU 3.60 GHz, RAM 32 GB
Cartographic environment: Perl, GDAL, Global Mapper
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to erase shoreline but preserve admin land borders?

Post by fmw42 »

One observation -- your maps are not properly registered such that in many places they do not overlap. That is, features move when alternating the images. This makes it harder.
yarr
Posts: 12
Joined: 2016-09-05T02:20:25-07:00
Authentication code: 1151

Re: How to erase shoreline but preserve admin land borders?

Post by yarr »

They are both rendered from the same cartographic shape with same compression/palette settings.
I don't know what how to prepare them better.
Maybe it would be easier to cut borders before conversion to grayscale PNG?
(Originals are rendered as black & white images.)
Yes, I know this is not trivial task - that's why I asked you for help.

Forgive my English please.
yarr

ImageMagick 7.0.6-0 Q16 x64 2017-06-11
Windows 7 Professional 64 bit, i7-4790 CPU 3.60 GHz, RAM 32 GB
Cartographic environment: Perl, GDAL, Global Mapper
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to erase shoreline but preserve admin land borders?

Post by fmw42 »

Try the following using connected components labeling to remove small blobs. I first remove the transparency. The second example, I add the transparency back again. See http://magick.imagemagick.org/script/co ... onents.php

Input:
Image



Here is what it looks like without the alpha channel (see -alpha off -threshold 0 below):
Image


Code: Select all

convert admin_borders_choked.png -alpha off -threshold 0 \
-define connected-components:area-threshold=75 \
-define connected-components:mean-color=true \
-connected-components 8 \
admin_borders_clean.png
Image

or

Code: Select all

convert admin_borders_choked.png -alpha off -threshold 0 \
-define connected-components:area-threshold=75 \
-define connected-components:mean-color=true \
-connected-components 8 \
-transparent black admin_borders_clean_trans.png
Image
yarr
Posts: 12
Joined: 2016-09-05T02:20:25-07:00
Authentication code: 1151

Re: How to erase shoreline but preserve admin land borders?

Post by yarr »

Fred, both commands produce black image in my environment.

Image: admin_borders_clean.png

Code: Select all

  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: DirectClass
  Geometry: 1536x864+0+0
  Units: Undefined
  Type: Grayscale
  Base type: Grayscale
  Endianess: Undefined
  Colorspace: Gray
  Depth: 16-bit
  Channel depth:
    gray: 16-bit
  Channel statistics:
    Pixels: 1327104
    Gray:
      min: 0 (0)
      max: 21 (0.000320439)
      mean: 0.0524578 (8.00455e-007)
      standard deviation: 0.997415 (1.52196e-005)
      kurtosis: 370.802
      skewness: 19.2773
      entropy: 0.0171581
  Colors: 4
  Histogram:
   1322924: (    0,    0,    0) #000000000000 gray(0)
       641: (    2,    2,    2) #000200020002 gray(0.0030518%)
      2992: (   19,   19,   19) #001300130013 gray(0.0289921%)
       547: (   21,   21,   21) #001500150015 gray(0.0320439%)
  Rendering intent: Undefined
  Gamma: 0.45455
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Background color: gray(255)
  Border color: gray(223)
  Matte color: gray(189)
  Transparent color: gray(0)
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 1536x864+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: Zip
  Orientation: Undefined
  Properties:
    date:create: 2017-06-23T20:53:28+02:00
    date:modify: 2017-06-23T20:59:07+02:00
    png:bKGD: chunk was found (see Background color, above)
    png:cHRM: chunk was found (see Chromaticity, above)
    png:gAMA: gamma=0.45455 (See Gamma, above)
    png:IHDR.bit-depth-orig: 16
    png:IHDR.bit_depth: 16
    png:IHDR.color-type-orig: 0
    png:IHDR.color_type: 0 (Grayscale)
    png:IHDR.interlace_method: 0 (Not interlaced)
    png:IHDR.width,height: 1536, 864
    png:text: 2 tEXt/zTXt/iTXt chunks were found
    png:tIME: 2017-06-23T20:59:07Z
  Artifacts:
    filename: admin_borders_clean.png
    verbose: true
  Tainted: False
  Filesize: 4.38KB
  Number pixels: 1.327M
  Pixels per second: 102.1MB
  User time: 0.016u
  Elapsed time: 0:01.013
Image: admin_borders_clean_trans.png

Code: Select all

  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: DirectClass
  Geometry: 1536x864+0+0
  Units: Undefined
  Type: GrayscaleAlpha
  Base type: Grayscale
  Endianess: Undefined
  Colorspace: Gray
  Depth: 16-bit
  Channel depth:
    gray: 16-bit
    alpha: 1-bit
  Channel statistics:
    Pixels: 1327104
    Gray:
      min: 0 (0)
      max: 21 (0.000320439)
      mean: 0.0524578 (8.00455e-007)
      standard deviation: 0.997415 (1.52196e-005)
      kurtosis: 370.802
      skewness: 19.2773
      entropy: 0.0171581
    Alpha:
      min: 0 (0)
      max: 65535 (1)
      mean: 206.417 (0.00314972)
      standard deviation: 3672.18 (0.0560339)
      kurtosis: 312.492
      skewness: -17.7339
      entropy: 0.0153564
  Alpha: graya(0,0)   #0000000000000000
  Colors: 4
  Histogram:
   1322924: (    0,    0,    0,    0) #0000000000000000 graya(0,0)
       641: (    2,    2,    2,65535) #000200020002FFFF graya(0.0030518%,1)
      2992: (   19,   19,   19,65535) #001300130013FFFF graya(0.0289921%,1)
       547: (   21,   21,   21,65535) #001500150015FFFF graya(0.0320439%,1)
  Rendering intent: Undefined
  Gamma: 0.45455
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Background color: graya(255,1)
  Border color: graya(223,1)
  Matte color: graya(189,1)
  Transparent color: graya(0,0)
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 1536x864+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: Zip
  Orientation: Undefined
  Properties:
    date:create: 2017-06-23T20:53:28+02:00
    date:modify: 2017-06-23T20:59:08+02:00
    png:bKGD: chunk was found (see Background color, above)
    png:cHRM: chunk was found (see Chromaticity, above)
    png:gAMA: gamma=0.45455 (See Gamma, above)
    png:IHDR.bit-depth-orig: 16
    png:IHDR.bit_depth: 16
    png:IHDR.color-type-orig: 0
    png:IHDR.color_type: 0 (Grayscale)
    png:IHDR.interlace_method: 0 (Not interlaced)
    png:IHDR.width,height: 1536, 864
    png:text: 2 tEXt/zTXt/iTXt chunks were found
    png:tIME: 2017-06-23T20:59:07Z
    png:tRNS: chunk was found
  Artifacts:
    filename: admin_borders_clean_trans.png
    verbose: true
  Tainted: False
  Filesize: 4.4KB
  Number pixels: 1.327M
  Pixels per second: 88.48MB
  User time: 0.016u
  Elapsed time: 0:01.014

Code: Select all


Version: ImageMagick 6.9.2-0 Q16 x64 2015-08-15 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC
Delegates (built-in): bzlib cairo freetype jng jp2 jpeg lcms lqr openexr pangocairo png ps rsvg tiff webp xml zlib
yarr

ImageMagick 7.0.6-0 Q16 x64 2017-06-11
Windows 7 Professional 64 bit, i7-4790 CPU 3.60 GHz, RAM 32 GB
Cartographic environment: Perl, GDAL, Global Mapper
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to erase shoreline but preserve admin land borders?

Post by Bonzo »

Fred, both commands produce black image in my environment.
What environment, what exact code are you running? Instead of using some mish-mash why not settle on one method or the other and get the code working first?
yarr
Posts: 12
Joined: 2016-09-05T02:20:25-07:00
Authentication code: 1151

Re: How to erase shoreline but preserve admin land borders?

Post by yarr »

Code: Select all

convert admin_borders.png \
	\( oceanmask.png -blur 0x5 -level 99,100% \) -compose In -composite \
	admin_borders_choked.png

# first command
convert admin_borders_choked.png -alpha off -threshold 0 \
	-define connected-components:area-threshold=75 \
	-define connected-components:mean-color=true \
	-connected-components 8 \
	admin_borders_clean.png

# second command
convert admin_borders_choked.png -alpha off -threshold 0 \
	-define connected-components:area-threshold=75 \
	-define connected-components:mean-color=true \
	-connected-components 8 -transparent black \
	admin_borders_clean_trans.png
Last edited by yarr on 2017-06-23T13:14:55-07:00, edited 1 time in total.
yarr

ImageMagick 7.0.6-0 Q16 x64 2017-06-11
Windows 7 Professional 64 bit, i7-4790 CPU 3.60 GHz, RAM 32 GB
Cartographic environment: Perl, GDAL, Global Mapper
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to erase shoreline but preserve admin land borders?

Post by fmw42 »

Is there a question here? My commands are Unix syntax. Be sure that when you run them you remove any blank spaces after the ending \, if you just copied and pasted. Your versions of ImageMagick is about 70 versions old. You might consider upgrading. Were there any error messages?
yarr
Posts: 12
Joined: 2016-09-05T02:20:25-07:00
Authentication code: 1151

Re: How to erase shoreline but preserve admin land borders?

Post by yarr »

None of this commands worked.
All right, I will upgrade IM and tell you if it helps.
Last edited by yarr on 2017-06-23T13:23:28-07:00, edited 1 time in total.
yarr

ImageMagick 7.0.6-0 Q16 x64 2017-06-11
Windows 7 Professional 64 bit, i7-4790 CPU 3.60 GHz, RAM 32 GB
Cartographic environment: Perl, GDAL, Global Mapper
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to erase shoreline but preserve admin land borders?

Post by fmw42 »

I just reran your commands and it works fine for me.

Code: Select all

convert admin_borders.png \
\( oceanmask.png -blur 0x5 -level 99,100% \) -compose In -composite \
admin_borders_choked.png

convert admin_borders_choked.png -alpha off -threshold 0 \
-define connected-components:area-threshold=75 \
-define connected-components:mean-color=true \
-connected-components 8 \
admin_borders_clean.png

convert admin_borders_choked.png -alpha off -threshold 0 \
-define connected-components:area-threshold=75 \
-define connected-components:mean-color=true \
-connected-components 8 -transparent black \
admin_borders_clean_trans.png
Perhaps there is a bug in your version of ImageMagick. Can you upgrade? What is your version of libpng? See

convert -list format

and look at the PNG line. Mine is

PNG* PNG rw- Portable Network Graphics (libpng 1.6.29)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to erase shoreline but preserve admin land borders?

Post by fmw42 »

Did you use Cygwin? What is your ImageMagick version in Cygwin?
yarr
Posts: 12
Joined: 2016-09-05T02:20:25-07:00
Authentication code: 1151

Re: How to erase shoreline but preserve admin land borders?

Post by yarr »

PNG* rw- Portable Network Graphics (libpng 1.6.17)

I use Windows but I tried Cygwin also.
No matter what I use - commands don't work.
I will upgrade IM. Maybe it will help.

Thanks.
yarr

ImageMagick 7.0.6-0 Q16 x64 2017-06-11
Windows 7 Professional 64 bit, i7-4790 CPU 3.60 GHz, RAM 32 GB
Cartographic environment: Perl, GDAL, Global Mapper
Post Reply