change projection style on DePolar distortion?

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?".
Post Reply
chris_blues
Posts: 10
Joined: 2016-06-27T12:56:11-07:00
Authentication code: 1151

change projection style on DePolar distortion?

Post by chris_blues »

Hi!

First my specs:
Debian GNU/Linux (testing) 64 bits
IM CLI usage (Bash)

Code: Select all

$ convert -version
Version: ImageMagick 6.8.9-9 Q16 x86_64 2016-06-01 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules OpenMP
Delegates: bzlib djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png tiff wmf x xml zlib
I'm trying to convert a map of the northpole into a cartesian overlay for the world map. (In case someone is interested, here's the background story) But I can't get the projection right.

Here's the command I used (Bash):

Code: Select all

convert -monitor tmp/arctis_16k_cropped.mpc +distort DePolar '8192,0 8192,8192 -100.325' -resize 32768x8192\! tmp/arctis_kartesian.mpc
Here's the icemap overlay half transparent, the world map is opaque:
Image

As you can see, the upper half of the ice-sheet overlay image should be stretched a little, and the lower half should be compressed a little in a rather smooth way. (I kept the continents in the overlay to see how I am coming into the right numbers. I'm still not perfectly on the right start angle, but pretty close.)

In case you wonder about the start angle, here's an explanatory mask of the data:
Image
The 180° line (edges of the world map) is at about 100.325° from the center. I measured 45px up and 247px in to the center. which translates to 10.325° from horizontal + 90° to IM's zero line. This is a pretty rough estimate, since the real data is a GeoTIFF of 24576x24576 !!! And the documentation doesn't mention the exact angle at all :roll:



Are there any switches or options I can pass to the DePolar distortion? The documentation wasn't very forthcoming on the possible projection types (if there are any).

I hope someone can shed some light on this. I'm really running out of ideas.

Thanks
chris

Credits: all images are derived from NASA ( http://visibleearth.nasa.gov , https://nsidc.org/data/docs/noaa/g10005-masam2 MODIS )
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: change projection style on DePolar distortion?

Post by fmw42 »

Depolar does not know about how images were formed or what projection they are in. All it does is convert to XY cartesian from radius,angle relative to some point as the center. Unfortunately, IM does not have much in the way of proper map projection software. It assumes the image is a sphere projected onto a plane tangent at the circle center (pole). Your input image probably does not conform to that simple a projection. It might not be an orthogonal projection, but more of perspective projection. If you knew the actual projection and the math for that projection, it could be done exactly using -fx, but it would be so slow as to be impractical for your size images. Or perhaps your output is some other map projection and not a true XY cartesian system with orthogonal coordinate axes. Or your output image is off center of the pole and that might cause some distortion so that it does not fit if you are not depolar(ing) from the true pole.

See for example:
http://mathworld.wolfram.com/MapProjection.html
http://demonstrations.wolfram.com/WorldMapProjections/
http://www.wolfram.com/mathematica/new- ... ction.html
http://www.demonstrations.wolfram.com/D ... ojections/
http://www.wolfram.com/mathematica/new- ... alization/
chris_blues
Posts: 10
Joined: 2016-06-27T12:56:11-07:00
Authentication code: 1151

Re: change projection style on DePolar distortion?

Post by chris_blues »

Ok, thanks. I'll try to find out, what kind of projections I'm dealing with, and see, how I can apply that to the result, rather than to the DePolar. Since the polar map clearly is no projection of a sphere (the 15° lines should become closer to the edge).

Cheers
chris
chris_blues
Posts: 10
Joined: 2016-06-27T12:56:11-07:00
Authentication code: 1151

Re: change projection style on DePolar distortion?

Post by chris_blues »

BTW, is there some operator I could use on the depolared ice map overlay? Like some distortion - stretch it on the upper half and compress it on the lower half? Any ideas?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: change projection style on DePolar distortion?

Post by snibgo »

Certainly. My "Polar distortions" page may give you some ideas.

See also my "Straightening horizons", which compresses the upper image vertically, stretching the bottom half. It does this by different amounts across the image, to make a wobbly line become a straight line.

Your problem should be simpler, a constant mathematical expression. But I wouldn't know what that is.
snibgo's IM pages: im.snibgo.com
chris_blues
Posts: 10
Joined: 2016-06-27T12:56:11-07:00
Authentication code: 1151

Re: change projection style on DePolar distortion?

Post by chris_blues »

Thanks for the directions! This looks very promising, though I need to learn some more of the way you "call" IM. I'll read up on that...

Thanks!

Edit: I think I figured it out - you use Windows .bat format. So All the %foo% is just a variable, sometimes declared via set foo. %IM% seems to be the path.

%%[fx:w-1]
This took some thinking actually! I just replaced it with "current size - 1" (600).



Though the example produced this on my machine:
I downloaded your "pd_plan_tex.png" from the example and then with that I went going through the example: (Bash)

Code: Select all

## Absolute displacement map:
convert -monitor pd_plan_Blue_noAlpha_depolar.mpc -sparse-color bilinear 0,0,#000,600,0,#f00,0,600,#0f0,600,600,#ff0 -channel G -function ArcSin 2,0,2,0 +channel pd_plan_dm.mpc

## Depolar:
convert -monitor pd_plan_tex.png -background Blue -virtual-pixel None -distort depolar 0 pd_plan_Blue_noAlpha_depolar.mpc

## To Spherical projection (Polar):
convert -monitor pd_plan_Blue_noAlpha_depolar.mpc pd_plan_dm.mpc -compose Distort -set option:compose:args 100%x100% -composite -distort polar 0 -resize 63.66198% pd_plan.png
Image
chris_blues
Posts: 10
Joined: 2016-06-27T12:56:11-07:00
Authentication code: 1151

Re: change projection style on DePolar distortion?

Post by chris_blues »

This actually worked into the right direction! Still it needs adjustment, but getting closer anyway!
Image

I assume, this has sth to do with the Displacement Map? If I reduce the values from

Code: Select all

convert -monitor
  tmp/arctis_16k_cropped.mpc
    -sparse-color bilinear 0,0,#000,16384,0,#f00,0,16384,#0f0,16384,16384,#ff0
    -channel G
    -function ArcSin 2,0,2,0
    +channel
  tmp/arctis_16k_DisplacementMap.mpc
say 50%, like:

Code: Select all

-sparse-color bilinear 0,0,#000,16384,0,#700,0,16384,#070,16384,16384,#770
or should I go on the ArcSin values?

Code: Select all

ArcSin 1,0,1,0
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: change projection style on DePolar distortion?

Post by snibgo »

chris_blues wrote:... you use Windows .bat format. So All the %foo% is just a variable, sometimes declared via set foo. %IM% seems to be the path.
Yes. In Windows BAT, '%' is a special character so needs to be doubled-up, hence "%%[fx:w-1]". Bash would have "%[fx:w-1]". A direct translation from my BAT to bash is:

Code: Select all

#!/bin/bash

PL_SRC=pd_plan_tex.png

convert \
  -verbose \
  -size 601x601 xc:Black \
  -stroke White -fill None \
  -draw "circle 300,300 300,350" \
  -draw "circle 300,300 300,400" \
  -draw "circle 300,300 300,450" \
  -draw "circle 300,300 300,500" \
  -draw "circle 300,300 300,550" \
  -draw "circle 300,300 300,600" \
  ${PL_SRC}

PL_SC=0,0,#000,\
%[fx:w-1],0,#f00,\
0,%[fx:h-1],#0f0,\
%[fx:w-1],%[fx:h-1],#ff0

echo $PL_SC

convert \
  ${PL_SRC} \
  -sparse-color bilinear "${PL_SC}" \
  -channel G \
  -function ArcSin 2,0,2,0 \
  +channel \
  pd_plan_dm.png

convert \
  ${PL_SRC} \
  -background Blue \
  -virtual-pixel None \
  -distort depolar 0 \
  pd_plan_dm.png \
  -compose Distort \
    -set option:compose:args 100%x100% \
    -composite \
  -distort polar 0 \
  -resize 63.66198% \
  pd_plan.png
I use PL_SC, and split it into lines, for legibility on the web page.

I don't know what is mathematically wrong with your result. If it is just a radial scale factor, then resize vertically just before "-distort polar":

Code: Select all

convert \
  ${PL_SRC} \
  -background Blue \
  -virtual-pixel None \
  -distort depolar 0 \
  pd_plan_dm.png \
  -compose Distort \
    -set option:compose:args 100%x100% \
    -composite \
  -resize 100%x90% \
  -distort polar 0 \
  -resize 63.66198% \
  pd_plan.png
If you can get a scale factor (eg 90%) to work at the bottom, but it is wrong in the middle, then the distortion map would need adjusting.
snibgo's IM pages: im.snibgo.com
chris_blues
Posts: 10
Joined: 2016-06-27T12:56:11-07:00
Authentication code: 1151

Re: change projection style on DePolar distortion?

Post by chris_blues »

snibgo wrote:I don't know what is mathematically wrong with your result.
From the looks of it, the mathematical function is about right, but much too pronounced. So I thought, maybe, as a first guess, just get the strength/amplitude of that curve to 50%, to see if we're getting closer. I'm just not quite sure, where to tweak the paramters.

My first guess would be this ArcSin thing might very possible do the curvature for the depolar function.

On the other hand, I haven't figured out the workings of the displacement map yet.
My guess is, that the 3 parameters of the map with 4 maximums (000,0f0, f00, ff0) is controlling the distortion of the depolar scan-vector. I imagine that much like a radar-screen. You have that line, which goes from the center going in circles. This line now scans the underlying pixels and represent them on a linear map. Now, if we have this displacement map to have the finger on the knob to compress or stretch the resolution of the scan-line. So essentially we never touch the corners (maxima) of the map, since its being scanned circular too(?). Which brings me to the idea, to distort that map beforehand also, so that it's a circle. As I realize that, it occurs to me, that the result of your example is also missing the corners. I need the full circle, the outer edge of the circle represents the equator.
At least, that would be the best I could archieve. I'm beginning to think about settling for a little less accuracy, but at least a result.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: change projection style on DePolar distortion?

Post by snibgo »

The code in my script takes a circular image (true, it is really rectangular, but we ignore everything outside the inscribed circle).

It unrolls that image, with "-distort depolar", making a rectangular image. The entire top row of that image represents the centre of the circular image. The bottom of the rectangle represents the circumference of the circle.

The displacement works on the unrolled, rectangular image. All of the map is used. Each map pixel corresponds to a pixel in the unrolled, rectangular image.

Then the rectangular image is rolled up with "-distort polar" into a circle.

(I can visualise the unrolling and rolling, but it's difficult to describe. I should make an animation.)
snibgo's IM pages: im.snibgo.com
chris_blues
Posts: 10
Joined: 2016-06-27T12:56:11-07:00
Authentication code: 1151

Re: change projection style on DePolar distortion?

Post by chris_blues »

Ah, I see, then it seems to boil down to the ArcSin function in the generation of the map.

BTW, why gets that first image loaded? To get the correct size for the diplacement map?
Post Reply