Page 1 of 1

Transform question.

Posted: 2012-03-22T13:19:03-07:00
by VanGog
Hello, I found hardships using photoshop to do this kind of transformation/desortion of big map scheme, so want to ask if would be any solution coming from ImageMagick. I need to transform whole map.

I have created a big aerial photo of my country and I want to overlap it with schemes of maps, which contain marks of airports. Problem is that for some reason these maps do not match exactly to the aerial photos taken from Google. For this task I take as a demo one of these maps and I made there some drawings that shows the measure of distortion needed:

First image:
http://www.uploadup.com/di-XRS1.jpg
The green line in area which is OK. At this place no transform is needed. This area must stay untouched by the transform effect.
The red line shows rough direction (estimated direction) of the desortion. The begin shows the place that needs to be placed for some distance.

More detail here:
http://www.uploadup.com/di-OP27.jpg
The big orange arrow shows the area pointing to digit 0, as I wanted to say that in this strip of the area 0 "pressure" should be applied.
The next arrows have different meaning. The little green arrows shows the direction of pressure and the big orange numbers shows "how much" the place would be shifted with the transform effect.

I also added green lines, which signify levels of movement. By the bottom green line is just a little shift (0,25). When you move to next green line, the distance of the airports by this line are more inaccurate, but not so much yet, so here number 0,3. Third green line has bigger distance, 0,5. Close to to top green line there is about 1.0. These are just number which I tried to illustrate how big is the shift. No units to the numbers.

My problem is, that Photoshop (which I have) cannot do such transform, because you can specify central point, and then you can resize whole image, or just its width or just its height. The image above which I made, shows the differences between airports drawn on one map, and where they should be shifted during transform effect. I don't know how this differences was created, but probably author of the map used different source if map then me. Has IM some solution that could help in this situation?

Re: Transform question.

Posted: 2012-03-22T15:08:26-07:00
by fmw42
If you have a set of control points between the two images, you can use -distort shepards to warp the two images. Note however, that this scheme using inverse distance squared distortion and may not look as good as you want. Also you probably want to add fixed points at the corners of the image to hold those down. I have used such a scheme to do morphing animation. See my script, shapemorph2 at the link below. Also see http://www.imagemagick.org/Usage/distorts/#shepards

Re: Transform question.

Posted: 2012-03-22T15:56:50-07:00
by VanGog
On the page here:
http://www.imagemagick.org/Usage/distor ... rol_points
"For example here we move the 'nose' of our koala image"
Not the nose is moved, but whole koala. So I think this is first problem. Is it possible to determine how the area should be wide (like a circle area)? I don't know how wide is the area that the pixel are affected.

I would need to determine fixed points not just for corners, but also for the green line.

* * *

What do you mean by "between the two images"? I suppose I work with one image for now. I take the scheme to transform.

Do I need to add hundreds of points and tell exactly where I want to move every point? This looks like problem. Is there any function to simplify it, so I would define just few points to determine direction. Because I would like to move all points in some area, for example right down from the green line, but they should be moved with the logic: "The more far the point is from the green line, the more it will be moved."
I imagine something like I would specify the green line fixed points and then to specify the area in the lower right corner to be affected, then to specify how much the pixels should be moved or just specify the function.

Re: Transform question.

Posted: 2012-03-22T16:49:47-07:00
by fmw42
I don't know how wide is the area that the pixel are affected.
Using -distort shepards, the influence of any pixel by the control points is inversely proportional to the square of the distance between the pixel and the control point. So nearby pixels to any control point will be adjusted but more distance pixels will not be affected very much.

Re: Transform question.

Posted: 2012-03-22T16:51:51-07:00
by fmw42
On the page here:
http://www.imagemagick.org/Usage/distor ... rol_points
"For example here we move the 'nose' of our koala image"
Not the nose is moved, but whole koala. So I think this is first problem. Is it possible to determine how the area should be wide (like a circle area)? I don't know how wide is the area that the pixel are affected.
That example uses an affine transformation which will affect every pixel in the image -- basically it is, scale, rotate, translate (and shear).

I was referring to -distort shepards and not -distort Affine.

Re: Transform question.

Posted: 2012-03-23T01:27:37-07:00
by anthony
VanGog wrote:On the page here:
http://www.imagemagick.org/Usage/distor ... rol_points
"For example here we move the 'nose' of our koala image"
Not the nose is moved, but whole koala. So I think this is first problem.
The nose was moved! and that is the point. A single point distortion is generally just a translation when there is no other 'limits' placed on the distortion. Affine, Perspective, and Bilinear distortions also all do the same thing! Shepard's too.

The first shepards example is a two point move (equal and oppisite), creating a stretched reagion between, and a lesser compression on the outside. Later the 'nose' move is repeated with the corners pinned so the whole image can no longer move freely.

Note ALL areas away from fixed pins, will move. Far away from the pins this movement 'flattens out' into an average translation of all the 'pins' (fixed or otherwise). It is just the way the distortion works.
Is it possible to determine how the area should be wide (like a circle area)? I don't know how wide is the area that the pixel are affected.
The distance between pins basically determines the 'local' area of the distortion. There is no fixed radius, or other limits, though their has been a discussion at one point (I think) about adding one of the mathematical limiting factors to the shepards an optional experts option. However lack of interest (feedback) has not give us incentive to add it to the formula.

Right now I am very busy, and can not add a radial limit component, but the source is open, and you are welcome to add it. In Im Sources, it is the file "magick/distort.c", around lines 2680, actual distortion, destination coord to source lookup point, is only about 20 lines or so. The distortion itself is based on a X and Y displacement generated using shepards interpolation. See Sparse-Color Shepards, which uses the same method for R,G,B color channels...
http://www.imagemagick.org/Usage/canvas/#shepards

Or the 'FX' equivelent of that (for just a few points) in... More Complex DIY gradients...
http://www.imagemagick.org/Usage/canvas ... nt_complex

This last actually clearly shows the 'areas of influence' the Shepard's Interpoolation Method generates for its distortions.
that area also provides likes to wikipedia references to the method, and to papers with 'radial limit' additions.


Their is another set of distortion that is all purely localized by radius.
Interactive Image Warping (circluar warps) (EWA)
http://www.gson.org/thesis/warping-thesis.pdf
The 'Swirl' (localized rotation) and 'Implode' (localized scaling) image warping operations are based on this
http://www.imagemagick.org/Usage/warping/#circular
However the 'localized translation' is not implemented.
I have been thinking about how to add these to distort, though you end up with a list of 'localised' distortions, that does not translate well into the 'list of numbers' argument handling that distort currently uses.

Re: Transform question.

Posted: 2012-03-23T02:55:23-07:00
by VanGog
fmw42 wrote:
I don't know how wide is the area that the pixel are affected.
inversely proportional to the square of the distance
This terminology is too hard to me to understand. I dont klnow what it is inversely proportional. And square of the distance, what is it. You dont need to answer.

Re: Transform question.

Posted: 2012-03-23T02:56:01-07:00
by VanGog
fmw42 wrote:
On the page here:
http://www.imagemagick.org/Usage/distor ... rol_points
"For example here we move the 'nose' of our koala image"
Not the nose is moved, but whole koala. So I think this is first problem. Is it possible to determine how the area should be wide (like a circle area)? I don't know how wide is the area that the pixel are affected.
That example uses an affine transformation which will affect every pixel in the image -- basically it is, scale, rotate, translate (and shear).

I was referring to -distort shepards and not -distort Affine.
OK, I checked that because I looked for Control points explanation.

So does it mean, that I must create hundreds of control points? What is it "nearby" how is it far or close.

Re: Transform question.

Posted: 2012-03-23T03:46:28-07:00
by VanGog
anthony wrote:
VanGog wrote: Later the 'nose' move is repeated with the corners pinned so the whole image can no longer move freely.
How do I recognize the "pins" in the code? There are just positions in the code.

So is it possible to add the pins into the green line?

Do you know the warp in photothop? It looks like this:
http://www.photoshopessentials.com/imag ... p-grid.jpg
They have simple grid, which parts you can move, but it is bad, that you cannot add more lines to it and that you cannot add any fixed point (or I dont know how to do it :-( ). Dont you think that would be good if IM would have similar function to work with a grid like this? The coala is very little image, but I have image much more bigger (2821x1702). Now imagine how many points I would need to specify if I would use the Sheppards method. It takes some time to read every x,y pozition in PS info palete and to write it. Now what if you would have 30x30 control points in the image?

I think it would be more simple to work with it, if user could define his grid, how he wants to slit the image. So you could define e.g. 12x12 or 30x30 square areas in the image. Then you could define some areas that you dont want to work with (fixed areas) like 12,3 12,4 12,5 12,6 13,3 13,4 12,5 13,6. So 12 is row number 12 and 3 4 5 6 are columns. Then I could move my square by simple 14,3 1024,1100 14,5 ... or so. But it would be more simple if I could just to determine the direction and "power" or (how far to move it).

I know you do hard work. I admire your abilities of mathematics and abilities to program and think these things. I am not C programmer so this is not something I could help with. But I think that in simplicity is a power :-) And it is very good that you are still open to work and continue developing and improving IM. In some points it's better then PS.

Re: Transform question.

Posted: 2012-03-23T10:17:12-07:00
by fmw42
I dont klnow what it is inversely proportional. And square of the distance,
Inverse squared distance = 1/(distance*distance)

see
http://csep10.phys.utk.edu/astr162/lect ... nsity.html
http://www.ems-i.com/smshelp/Data_Modul ... ighted.htm

Re: Transform question.

Posted: 2012-03-23T13:48:45-07:00
by VanGog
I'm not good in mathematics. But it sounds to me like near to geometry, which I am a little more familiar with.

Re: Transform question.

Posted: 2012-03-23T19:22:35-07:00
by Werty
VanGog wrote: The coala is very little image, but I have image much more bigger (2821x1702). Now imagine how many points I would need to specify if I would use the Sheppards method. It takes some time to read every x,y pozition in PS info palete and to write it.
You could create a new layer, and then draw on the image with a solid color like green where you want the points, then seperate the layer and extract the coordinates with the IM .txt format.

Could you upload the image you have and what you want overlaid, so we can have a look at it ?

Re: Transform question.

Posted: 2012-03-25T18:37:53-07:00
by anthony
The terminology is not hard.

A control point is the position in th esource image, and the corresponding position you want in the destination image.

In photoshop terms it would be the movements of the 16 points at the grid intersections, and how they should be moved. Though Imagemagick does not actually have a 'grid' distortion at this time, Sheperd's should produce somethign close to it.

A 'pin' is just a control point that does not move. that is this position in the image is NOT to change. Corners and edges of an image are often 'pinned'.

The area a control point modifies is typically about half the distance to the neighbouring control points. Though their is a global 'averaging' effect in areas that handl few control points.

As to how many control points you need. That depends on how much 'control' you want the distortion. Particulalry in areas that are not close to a control point.

Suggestion. start with 5 points. four 'pins' (not moved) in teh corners and one moving point on the main object of interest (nose, hand, pumpkin?, etc)

Then add more points as you need to get the effect you want. Add pins in areas you don't want to move (like edges), and control points that are moved, in areas you do want to change.

Re: Transform question.

Posted: 2012-03-28T02:11:46-07:00
by VanGog
Thanks for all the explanation. I decided to give up and save my time. I have my maps done and now going to do some other work. If you ever will create good grid distortion system I will be glad to hear about it. Note: by the word "good" I mean efficiency of the work.