11-30-2020, 03:36 PM
I made another plugin, and with it I think this package is finished. The plugins are at
http://kmarkku.arkku.net/Perspective_tra...aster.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:
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.
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:
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.
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.
example.xcf (Size: 188.12 KB / Downloads: 244)
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:
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.
http://kmarkku.arkku.net/Perspective_tra...aster.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:
- Perspective transform - 1+2+3 points to circle (center and 2+3 points)
- Perspective transform - 4 points to 4 points
- Perspective transform - 4 points to circle (clock face)
- Perspective transform - 4 points to rectangle corners
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.
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:
- One stroke has 1 anchor. The anchor is supposed to be the point which should become, after correction, the center of the circle.
- 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).
- The third stroke has 3 anchors. They are just some three other points on the distorted circle.
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.
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.
example.xcf (Size: 188.12 KB / Downloads: 244)
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
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.