Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Perspective transform revisited
#1
Here is a new plugin to perform Perspective transform (=projective transformation for a mathematician), based on two 4-anchor paths. To get it, go to

http://kmarkku.arkku.net/Miscellaneous_p...aster.html

move to the bottom row and click the Download button. You get a .zip file. Unzip it and put the sole file it contains (perspective_transform.py) to your user's plug-ins folder and restart Gimp. The plugin will be in Gimp's menu at Filters > Distorts > Perspective transform > ...

The plugin transforms the active layer. It asks for two paths, Source and Target. Both are supposed to be 4-anchor paths. The transformation will send the anchors of the Source onto the anchors of the Target. So, if you have a picture and in it you have some four specific points A,B,C,D that you would like to move to some other points P,Q,R,S, then:
  1. Create the Source path so that it has A,B,C,D as its anchors. Don't make it closed.
  2. Create the Target path so that it has P,Q,R,S as its anchors. Don't make it closed. Ensure that the ordering of P,Q,R,S in the Target path corresponds to the ordering of A,B,C,D in the Source path (excepting possible reversal).
  3. Make sure that the layer you want to transform is the active one.
  4. Open the plugin's GUI.
  5. If you want to retain the original layer as it is, so that the plugin will work on a copy, click the bottom entry (Keep the original layer?) to set it to Yes.
  6. Run the plugin.
  7. If the plugin runs ok and produces some result but it is somehow wrong, repeat but this time click the entry Use the target reversed?  to Yes.
  8. If still not happy, look at the example case below.
  9. If the result is ok, you may wish to do Image > Fit Canvas to Layers.
Some inputs make the job impossible: the transformation would send some parts of the layer to infinity. This problem is inherent in the Perspective (projective) transformation; infinities do occur, and if they happen to lie in the region of the layer, then nothing can be done. If such happens, the plugin raises an error (an Exception). Your layer may be excessively large; crop it to its essential contents and try again. Or change the Source or Target path a little and try again.

The plugin also contains options to draw two special lines connected with infinities, provided that they intersect the image area. I say nothing about those here. You can just ignore them.

Example: I use the picture by Denzjos, which was what lead to this plugin; see https://www.gimp-forum.net/Thread-fix-distorted-round

   

The following picture tries to show the paths Source (red) and Target (blue) that I used. (The clock is dimmed just to show the paths better)

   

The Source path (red) is chosen from the hours 12,3,6,9 on the dial and left unclosed. The Target path (blue) has its anchors at the corners of a square tilted by 45 degrees. I made it as follows:
  1. Make a square selection.
  2. Do Selection to path. Rename the path to Target.
  3. Dismiss the selection.
  4. Rotate the Target by 45 degrees.
  5. Move and scale the Target so that it is close to the Source. (Not necessary.)
  6. Delete one edge from the Target, so that it is open at the same side as the Source.
Then, when I ran the plugin with these Source and Target, the result was somehow wrong. So I tried again but now using the target reversed. The result was reasonably good:

   


That's that. I attach here also the xcf file in case you wish to look at it more closely:


.xcf   clock.xcf (Size: 1.35 MB / Downloads: 21)

The plugin was quite a rushed job. I mostly just copied pieces of code from an unpublished work. If you have problems with the plugin, please be kind enough to tell me. And you will certainly find points that could be made better. My knowledge about Gimp is rather rudimentary, so please make suggestions.
Reply
#2
Great!
It worked easy, fast and great result.
Thank you!
Reply
#3
(10-28-2020, 07:04 PM)Krikor Wrote: Great!
It worked easy, fast and great result.
Thank you!

Good to hear!
Reply
#4
Tested on Windows 10 gimp 2.10.22 and is working fine. Thanks. But, if one have no coordinate system in the picture or no points as reference ?
Reply
#5
(10-29-2020, 05:15 PM)denzjos Wrote: Tested on Windows 10 gimp 2.10.22 and is working fine. Thanks. But, if one have no coordinate system in the picture or no points as reference ?

The idea in the simple plugin was that we have some 4 precise points in the picture and that they should be transformed to certain other 4 precise points. So, we need 8 precise points. If there are no such 8 precise points then the idea does not work, and perhaps it is better to use the regular Perspective Transform tool. There you can transform the picture in real time and play by the eye.

Or are you thinking like Ofnuts (https://www.gimp-forum.net/Thread-fix-di...0#pid20460) that we should be able to do the transformation with less or different information? He asked that if the picture contains 4 points that should lie on a circle but do not because of distortion, and if we have possibly some additional information, then is it possible to find a transformation to put the 4 points on a circle? I guess so, but I have no exact answer (I am no expert in projective transformations).

Such problems can be made. What comes to my mind is 3 points and the center. But even to that I don't know an answer.

Or perhaps you have some other similar problem in mind? Perhaps you could be more specific?


Edit: Those questions of Ofnuts and of my own may have simple answers after all. I must think about it.
Reply
#6
(10-29-2020, 07:59 PM)Ottia Tuota Wrote: Edit: Those questions of Ofnuts and of my own may have simple answers after all. I must think about it.

For the answer I had in mind, see https://www.gimp-forum.net/Thread-fix-di...8#pid20508
Reply
#7
The link in post #1 no longer works. Apparently I can no more edit that post. Here is a new link to the new version 0.2, with a little change plus a new plugin.

http://kmarkku.arkku.net/Perspective_tra...aster.html

The changes are:
  1. Dropped the options to draw the two special lines. I shouldn't have put them in the plugin to begin with. If for some strange reason somebody wants them back, just raise your voice.
  2. Made another plugin in the same package, called "Perspective transform by a path and a rectangle".

How to get the new version: click the link above, scroll down to the bottom, click the Download button, unzip the file you get, and place the file perspective_transform.py in your Gimp's user plug-ins folder. You get now two plugins, and they are located in Gimp's menu at

Filters > Distorts > Perspective transform > ...

The new plugin works on the active layer. I give no pictures but only explain. The plugin takes as input one 4-anchor path ("source path"): Suppose you have in the picture some area A that you would like to be rectangular but it is not due to perspectivity. You make the source path so that it goes round that area A, cyclically from corner to corner, so defining a quadrangle. Then you call the plugin. That's it. The layer is distorted so that area A becomes rectangular with sides horizontal and vertical.

That is the default working of the plugin. You can also tell the plugin more exactly what you want. The plugin uses so-called "target rectangle". It is the rectangle onto which the quadrangle will be mapped. You can let the plugin use its default action; then it computes the target rectangle from the source path with an algorithm of its own. If the result is not satisfactory, you have two ways to specify the target rectangle exactly: by a rectangle selection, or by guides (two horizontal, two vertical).

For instance, you may want area A to become square, or otherwise have some particular aspect ratio. To that end, you can use a selection or guides.

The target rectangle will normally have sides horizontal and vertical. The plugin offers an option to rotate it. The angle is inputted in degrees.

Example: you can do the same correction as in post #1 (the clock picture by denzjos) also as follows: For the source path take the same one which appeared in post #1 (connect the hours 3,6,9,12 in the dial). Make a square selection of proper size. Then call the new plugin by choosing the selection for the target rectangle, and setting the rotation angle to 45.

If the plugin does something odd, or if it does not work at all, please report.

Occasionally the plugin fails with the message that a part of the layer would go to infinity. Then you can try to crop the layer to its essential contents, or you can try to change the source path.

I have the nagging feeling that I am re-inventing the wheel here. There must already be a tool that does the same? How else could it be since this must be a frequently appearing problem. But Gimp's Transform Tools, as far as I understand, work so that one has to play by one's eye. My plugin gives exact result (as soon as the source path is good). So, tell me: is there some older tool or plugin for the same job?
Reply
#8
Thank you.
Works great.
Very easy to use.
Reply
#9
It's nice to have more options.
I played around with a few images, both plugins ran well.

I applied the plugin to displace (move) Popeye's arm in the image below.
https://i.imgur.com/tnMYEo4.gif
[Image: tnMYEo4.gif]
Thx!
Reply
#10
Thanks to you both. Nice animation. I never thought that the plugins could be used that way.

But the real thanks go to whoever it was who wrote to Gimp that excellent internal procedure 'gimp_item_transform_matrix'. It makes it possible to do any projective transformations, and to make such plugins, what is left is to write interfaces (which necessarily involves solving some nice mathematical problems). If only there were something similar for more general transformations, not only projective...!
Reply


Forum Jump: