Perspective transform revisited - Printable Version +- Gimp-Forum.net (https://www.gimp-forum.net) +-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP) +--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP) +--- Thread: Perspective transform revisited (/Thread-Perspective-transform-revisited) |
Perspective transform revisited - Ottia Tuota - 10-28-2020 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_plugins/Gimp_master.html edit: http://kmarkku.arkku.net/Perspective_transform_files/Gimp_master.html (url changed - rich) 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:
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 [attachment=5107] 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) [attachment=5108] 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:
[attachment=5109] That's that. I attach here also the xcf file in case you wish to look at it more closely: [attachment=5110] 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. RE: Perspective transform revisited - Krikor - 10-28-2020 Great! It worked easy, fast and great result. Thank you! RE: Perspective transform revisited - Ottia Tuota - 10-28-2020 (10-28-2020, 07:04 PM)Krikor Wrote: Great! Good to hear! RE: Perspective transform revisited - denzjos - 10-29-2020 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 ? RE: Perspective transform revisited - Ottia Tuota - 10-29-2020 (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-distorted-round?pid=20460#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. RE: Perspective transform revisited - Ottia Tuota - 10-30-2020 (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-distorted-round?pid=20508#pid20508 RE: Perspective transform revisited - Ottia Tuota - 11-03-2020 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_transform_files/Gimp_master.html The changes are:
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? RE: Perspective transform revisited - Blighty - 11-03-2020 Thank you. Works great. Very easy to use. RE: Perspective transform revisited - Krikor - 11-03-2020 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 Thx! RE: Perspective transform revisited - Ottia Tuota - 11-03-2020 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...! |