Gimp-Forum.net
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)

Pages: 1 2 3 4


RE: Perspective transform revisited - Ottia Tuota - 11-16-2020

(11-16-2020, 06:11 PM)akovia Wrote: Really nice plugin.

Just using it for straightening perspective DVD cover images like Amazon and other outlets will use sometimes, and it worked perfectly.

Thanks!

Great! It is always a relief to hear such outcomes!


RE: Perspective transform revisited - Ottia Tuota - 11-30-2020

I made another plugin, and with it I think this package is finished. The plugins are at

http://kmarkku.arkku.net/Perspective_transform_files/Gimp_master.html

When you put the file perspective_transform.py in your Gimp's plug-ins folder, you find at Filters > Distorts > Perspective transform four plugins:

  1. Perspective transform - 1+2+3 points to circle (center and 2+3 points)
  2. Perspective transform - 4 points to 4 points
  3. Perspective transform - 4 points to circle (clock face)
  4. Perspective transform - 4 points to rectangle corners

The first is the new plugin and I explain it in this post. The other three are the old plugins; I changed only wordings but they work just as before.

The new plugin "Perspective transform - 1+2+3 points to circle (center and 2+3 points)" is meant to correct a distorted circle. In that sense it does the same thing as the third of the plugins, but the inputs are different. This one may be easier to use (no hours).

I made the following test figure. On the left we have a regular figure. I distorted it with Gimp's Perspective Transform tool to get what is on the right. That is the test figure. Now I want to correct the distortion back.

[attachment=5273]

You must imagine now a distorted circle (an ellipse) around the figure though I didn't draw it there. That is the distorted circle we should now corrected to a true circle.

The plugin takes as input one path with three strokes:

  1. One stroke has 1 anchor. The anchor is supposed to be the point which should become, after correction, the center of the circle.
  2. Another stroke has 2 anchors. They must be points on the distorted circle. Also, they determine the position to which the final figure should be rotated (see below).
  3. The third stroke has 3 anchors. They are just some three other points on the distorted circle.
It does not matter in which order the 3 strokes are in the path.

In the following picture, on the left we see the path I used. The 1-anchor path is the little circle in the middle. The other two strokes you recognize certainly: one has 2 anchors, and one has 3 anchors.

[attachment=5274]

With this path and otherwise with default values, the plugin produced the figure on the right. Rather good, isn't it?

I attach the .xcf file of the distorted figure and the path in case you want to look at it more closely.

[attachment=5275]

The plugin offers a method to set to which position the corrected circle will be rotated. The plugin uses the direction of the 2-anchors stroke for this, and you have three alternatives to choose from:
  • Make it vertical (default)
  • Make it horizontal
  • Keep its direction as it is
You notice that in the example figure I put the 2-anchors stroke to the two petals with blue dots. When I ran the plugin with the default value, the line segment between the blue dots was turned vertical.

But in general, the final figure may turn out to be rotated by 180 degrees from what you wanted. The plugin has an option to correct that. You can also define extra rotation in degrees.

You may be wondering: Why the strokes with 2 and 3 anchors? The 2-anchors stroke has the function that its direction is used to determine the rotation. But why the 3-anchors stroke? Why 5 (=2+3) points on the distorted circle? And why the center point? I cannot resist explaining a little about the ideas behind the plugin, since this all is not quite obvious.

<skip if not interested...>
The plugin is designed to correct a distorted circle to a true circle. The distorted circle is assumed to be what we get when we take a photo of a circle in an angle. In the photo the circle is distorted; it becomes an ellipse, approximately (but that depends on the lens and is not always true). We just assume here that it is an ellipse. To be precise, what the plugin does is that it corrects an ellipse to a circle, and in addition it corrects the distortion also globally in the plane, not only the curve (the ellipse).

An ellipse is determined completely from its 5 points. That is the reason for inputting 5 points on the distorted circle: they enable the plugin to compute everything of the ellipse: center, axes, and whatever. And correcting the ellipse to a circle is then easy.

But if we used only the 5 points and corrected the ellipse (the curve) to a circle, then everything else on the plane would still come out uncorrected. For example, the point that the correction should take to the center of the circle, would generally be still offset from the center. But a remarkable fact is that if we also know which point should become the center of the circle, then we can correct the distortion globally (up to rotation and perhaps reflection)!

Namely, it can be shown that, given an ellipse and one point inside it, there is a projective transformation that maps the ellipse onto a given circle, at the same time sending that specified point to the center of the circle. Moreover, such transformation is unique up to a rotation and possibly a reflection! Therefore, any distortion of a circle, caused by a projective (perspective) mapping (like taking a photo), can be corrected in a unique way using the scheme in the plugin (five points, center, direction, and keeping ccw as ccw). When we know that, writing such plugin means only  constructing that correcting transformation.

I don't give any details about the construction but you may wish to look at the code and the comments therein. This part is the most interesting in the whole process, and I am still amazed at the way it did come through.
</skip...>

That's that. If you have any problems with the plugin, please just ask.


RE: Perspective transform revisited - Krikor - 12-01-2020

This other plugin is welcome! I didn't find it as intuitive as the previous ones, but maybe I haven't understood it as I should yet.
In the image made available in .xcf I could try the correction even using paths other than the one provided and the result was practically the same; pretty satisfactory.
However, I particularly prefer the previous plugins, in this case the 4-point to 4-point generated an image closer to the original with better control over the desired center of the corrected image.
[attachment=5277]
Thanks for keeping perfecting this plugin!


RE: Perspective transform revisited - Ottia Tuota - 12-01-2020

(12-01-2020, 12:45 PM)Krikor Wrote: This other plugin is welcome! I didn't find it as intuitive as the previous ones, but maybe I haven't understood it as I should yet.
In the image made available in .xcf I could try the correction even using paths other than the one provided and the result was practically the same; pretty satisfactory.
However, I particularly prefer the previous plugins, in this case the 4-point to 4-point generated an image closer to the original with better control over the desired center of the corrected image.

Thanks for keeping perfecting this plugin!

Yes, 4 to 4 is good if you know where to put the 4 target points.

In this particular figure it is practically guessing where to put the center, so the result is not perhaps so good. But in some other picture the center point may be easy to set.

Thanks for trying the plugins!


RE: Perspective transform revisited - denzjos - 12-03-2020

Nice job Ottia Tuota. Just used it on another clock and the result is OK. Tried it on a flange drawing and the result is NOK. Any suggestions? Thanks in advance.

Clock OK
[attachment=5286]

Flange NOK
[attachment=5287]


RE: Perspective transform revisited - Ottia Tuota - 12-03-2020

(12-03-2020, 02:17 PM)denzjos Wrote: Nice job Ottia Tuota. Just used it on another clock and the result is OK. Tried it on a flange drawing and the result is NOK. Any suggestions? Thanks in advance.

Clock OK


Flange NOK

Thanks for trying the plugin. I changed the path slightly and got a quite satisfactory result. See the attached file:

[attachment=5288]

The strokes with 2 and 3 anchors: I moved the anchors a little to get them on the inner ellipse as closely as possible.

The stroke with 1 anchor: I changed the diagonals slightly at the top since it seemed to me that the top guide was a little too high. I changed the center point accordingly. (By the way, you had a neat trick to find the center!)

I didn't look into it any further, so I don't know which changes were the most significant. But it seems that the process is very sensitive on the exact placing of the anchors. But that is no fault of the plugin. Rather, it is a feature of the projective transformation (or of the transformation, whatever it is, that is done by that Gimp's inner procedure).


RE: Perspective transform revisited - Ottia Tuota - 12-03-2020

(12-03-2020, 03:28 PM)Ottia Tuota Wrote: ...
(By the way, you had a neat trick to find the center!)
...

But on a second thought, that trick gives only the center of the ellipse (= the distorted circle). What the plugin needs, is the would-be center, meaning the point which should be sent to the center of the corrected circle. In this picture it seems to be the same thing! That is a matter of luck, and probably is due to the method with which the picture was produced to begin with. I guess that it was made from some original picture (containing a true circle) by an affine transformation (scale? shear? ...)? So, the trick does not work generally.


RE: Perspective transform revisited - denzjos - 12-03-2020

Well, I discovered that the flange is a drawing of an ellips.The clock is a photograph or a rendered drawing that have a perspective distortion.The drawing of the flange is 3D drawing in isometric projection. The ellips (green) is not distorted but it is just a angled view. That is why it is easy to find the center-point. To find the center lines you must draw a circle (yellow - here divided in 12, not required) with this point as a center point. The 4 points where the circle is intersecting the ellips are the vertices of a rectangle (red). Lines from the center-point of the circle through the the middle of the sides of the rectangle are the center lines (red and one is discovered by a cyan line) of the ellips. I just tested the plugin to see if it is working to restore the front view of the flange with the holes and the cavity.  

 [attachment=5289]


RE: Perspective transform revisited - Ottia Tuota - 12-03-2020

(12-03-2020, 05:42 PM)denzjos Wrote: Well, I discovered that the flange is a drawing of an ellips.The clock is a photograph or a rendered drawing that have a perspective distortion.The drawing of the flange is 3D drawing in isometric projection. The ellips (green) is not distorted but it is just a angled view. That is why it is easy to find the center-point. To find the center lines you must draw a circle (yellow - here divided in 12, not required) with this point as a center point. The 4 points where the circle is intersecting the ellips are the vertices of a rectangle (red). Lines from the center-point of the circle through the the middle of the sides of the rectangle are the center lines (red and one is discovered by a cyan line) of the ellips. I just tested the plugin to see if it is working to restore the front view of the flange with the holes and the cavity.  

 

Ok. I am not familiar with isometric projections. But I suppose what in such projection happens to a circle is:

  1. the circle is mapped onto an ellipse;
  2. the center of the circle goes to the center of that ellipse.
The latter condition is crucial in what you tried, since you used the center of the ellipse as the would-be center. Then the plugin can indeed restore the original figure and correct the distortion. And I guess that when I placed the anchors a little more carefully, the satisfactory result showed that that indeed happens.

But if we have a general perspective mapping (a projective transformation), then condition 1 still holds but condition 2 is no longer true! That is the case in your clock example. If you look at the distorted clock and the 1-anchor stroke you have put there, that anchor is NOT at the center of the ellipse. Not even if it is the point where the perspective mapping sends the center of the original circle.

Using that point as the would-be center, as you did, is the right thing to do. That point is precisely the point that should be sent to the center of the corrected circle. (After all, we are trying to cancel the effect of the perspective mapping.)

The plugin needs to be told what point should be sent to the center, and knowing that point may not always be so easy.


RE: Perspective transform revisited - denzjos - 12-04-2020

Ottia Tuota, thanks for your explanation concerning this subject.