Page 1 of 2

Help with camera distortion correction.

Posted: 2016-05-18T06:24:41-07:00
by DanielB
Hi all!

This is my first message on the forum.

I'm trying to correct the distortion in a camera Genius WideCam 1050 [1]. Since I need to use this camera with the angular amplitude vertically, to correct the image distortion I used the following procedure.

1) I've rotated the image 90 degrees clockwise:

Code: Select all

$ convert 20160517_154016.jpg -rotate 90 20160517_154016-rotated.jpg
2) I used Hugin to set up multiple control points on the rotated image following the steps in this article [2] in order to obtain the coefficients A, B and C of the Dersch function. The values obtained here were: 0.005, -0.066 and 0.069.

3) To use convert for apply the transformation:

Code: Select all

$ convert 20160517_154016-rotated.jpg -distort barrel "0.005 -0.066 0.069" 20160517_154016-rotated-processed.jpg
The test was done by taking a photo of the interior of a house where there are some corners that are not parallel with the edge of the photo. I'm not sure if this is due to the perspective or it is something that can be improved in the transformation. I would like to attach the photos for you can give me an opinion, but I can not find how to do it in this forum.

I'm using Debian GNU/Linux Jessie with Imagemagick 6.8.9.9-5+deb8u2.

Thanks in advance.

Kind regards,
Daniel

[1] http://www.geniusnet.com/Genius/wSite/c ... ctNode=161
[2] http://hugin.sourceforge.net/tutorials/ ... n/en.shtml

Re: Help with camera distortion correction.

Posted: 2016-05-18T07:31:20-07:00
by Bonzo
I would like to attach the photos for you can give me an opinion, but I can not find how to do it in this forum.
You will need to host the photos elsewhere and provide a link here.

Re: Help with camera distortion correction.

Posted: 2016-05-18T08:05:32-07:00
by DanielB
Hi, Bonzo.

Thanks for your reply.

Below I put the references of the photos mentioned in the previous post:

Rotated image:

Image

Rotated image processed:

Image

Thanks.

Kind regards,
Daniel

Re: Help with camera distortion correction.

Posted: 2016-05-18T09:28:41-07:00
by fmw42
One thought is that you need slightly different coefficients for X than for Y.

Re: Help with camera distortion correction.

Posted: 2016-05-18T10:24:36-07:00
by DanielB
Hi, Fred,

Thanks for your answer.
fmw42 wrote:One thought is that you need slightly different coefficients for X than for Y.
Do you mean to the first two values, I suppose, right? Because ImageMagick documentation uses X and Y as other parameters.

Seeing again the transformed image, I am also noticing on the right side such as a downward slope which does not appear differ from the original image.

Kind regards,
Daniel

Re: Help with camera distortion correction.

Posted: 2016-05-18T10:27:58-07:00
by snibgo
In that photograph, most lines (if extended) would pass through or near the centre of the image. They are radial lines, which are not bent by barrel/pincushion distortion.

Perspective is a different type of distortion, which isn't a lens problem, and doesn't bend straight lines.

A better test comes from having lines parallel to the image edges, near the edges. Brick walls are often used. Find a large wall so the photo has nothing but bricks.

Re: Help with camera distortion correction.

Posted: 2016-05-18T12:58:08-07:00
by fmw42
see http://www.imagemagick.org/Usage/distorts/#barrel in the section for having two sets of coefficients, one for x and one for y.

Re: Help with camera distortion correction.

Posted: 2016-05-18T14:06:39-07:00
by DanielB
Hi, snibgo.
snibgo wrote:In that photograph, most lines (if extended) would pass through or near the centre of the image. They are radial lines, which are not bent by barrel/pincushion distortion.

Perspective is a different type of distortion, which isn't a lens problem, and doesn't bend straight lines.
I am noticing on the right side of the transformed image such as a downward slope which does not appear differ from the original image. I would have to check whether this is because the camera is not correctly leveled, although I have a feeling that it is due to deformation of the lens and coefficients chosen had no effect on this. What do you think?

The edge of the wooden wall, although it looks more right in the transformed image, it is also tilted. And I thought maybe it had to do with the lens distortion. I wonder also what do you think about this.
snibgo wrote: A better test comes from having lines parallel to the image edges, near the edges. Brick walls are often used. Find a large wall so the photo has nothing but bricks.
I'll see if I can get something like that where I can bring my notebook, because it is a USB camera. Do you think with having only horizontal parallel lines would be sufficient to adjust the distortion? In this case, do you suggest using the same procedure with Hugin and control points or think there would be some more efficient method to obtain the Dersch coefficients?


Thanks for your reply.

Kind regards,
Daniel

Re: Help with camera distortion correction.

Posted: 2016-05-18T14:25:19-07:00
by DanielB
Hi, Fred.
fmw42 wrote:see http://www.imagemagick.org/Usage/distorts/#barrel in the section for having two sets of coefficients, one for x and one for y.
Ah, it's the same link mentioned in the previous post. So you were referring to add these two additional values to the three already used coefficients.

Do you know if the values "d" and "e" in Hugin represents these values of X and Y? I'm using Hugin 2014.0.0+dfsg-5 on Debian GNU/Linux Jessie.

Thanks for your reply.

Kind regards,
Daniel

Re: Help with camera distortion correction.

Posted: 2016-05-18T14:29:32-07:00
by snibgo
If you tilt the camera up, in the photo the corners of the walls will remain straight but will tilt inwards. If you take a photo of a tall building from outside, tilting the camera up, the edges will tilt inwards. If you stand on a bridge over a railway and photograph it, the tracks will tilt together in the photo, converging at the horizon. These are all due to perspective. You can correct for this if you want with IM's "-distort perspective".

If real-life straight lines become curved, this is probably caused by barrel/pincushion distortion, which can be corrected with "-distort barrel". The Hugin toolset can give you the ABCD parameters. For best results, you need to photograph something that has both vertical and horizontal straight lines. There should be at least 5 lines in each direction: one near each edge, one near the centre, and one each between the center and edge. More lines is better. You only need three points per line, but 5 or more is better.

If the camera lens doesn't focus or zoom, this barrel/pincushion calibration only needs to be done once.

When processing photos, you should correct barrel/pincushion distortion first, then perspective distortion.

Re: Help with camera distortion correction.

Posted: 2016-05-18T14:38:02-07:00
by snibgo
DanielB wrote:Do you know if the values "d" and "e" in Hugin represents these values of X and Y?
Yes. See http://wiki.panotools.org/Lens_correction_model

Re: Help with camera distortion correction.

Posted: 2016-05-18T14:47:21-07:00
by fmw42
To calibrate your camera, I would suggest photographing a grid face on.

Re: Help with camera distortion correction.

Posted: 2016-05-19T08:09:09-07:00
by DanielB
Hi, snibgo.
snibgo wrote:If you tilt the camera up, in the photo the corners of the walls will remain straight but will tilt inwards. If you take a photo of a tall building from outside, tilting the camera up, the edges will tilt inwards. If you stand on a bridge over a railway and photograph it, the tracks will tilt together in the photo, converging at the horizon. These are all due to perspective. You can correct for this if you want with IM's "-distort perspective".
Very interesting observation! Thanks for sharing! I took again a photo of the same scene, but this time I tried to support the USB camera on a tripod trying to be as level as possible. This tripod has a horizontal level and a level relative to the angular inclination of the camera (forward or backward). The tripod also allows a radial tilt of the camera in the longitudinal direction, so I used it to place it vertically. To ensure the verticality, I used a set square.

Rotated image (-90 from photo took with tripod):

Image

Rotated image processed:

Image

As you can see, here the corner of the wooden wall to the left is much more aligned with the edge of the picture, suggesting that the camera would not be inclined upwards but leveled. Although I keep noticing that the floor appears to be inclined downward, toward the right (I'm not sure if it is bent). What do you think could be causing this?

Moreover, although the roof edge look more straight, it seems inclined upward. Do you think these two effects could be due to a problem of parallax of the camera?

On the other hand, it's interesting what you mention about using "-distort perspective". I was watching ImageMagick documentation, although I'm not sure what is the most efficient way to get the values to use for this.
snibgo wrote: If real-life straight lines become curved, this is probably caused by barrel/pincushion distortion, which can be corrected with "-distort barrel". The Hugin toolset can give you the ABCD parameters.
Hugin allows to obtain the parameters A, B, C, D and E. Thank you for confirming in the other post that the values of D and E corresponds to the center of the distortion. Following the reference mentioned in the original message, I only selected in Hugin the A, B and C parameters for calculation. Do you think I should also mark D and E?

In this case, reviewing the documentation of ImageMagick, I see that it expects the following syntax for "-distortion barrel":

Code: Select all

A   B   C   [ D   [ X , Y ] ] 
The optional X,Y arguments provide an optional 'center' for the radial distortion, otherwise it defaults to the exact center of the image given (regardless of its virtual offset). So in the case of using X and Y, should I use the following syntax with the values obtained from Hugin?

Code: Select all

$ convert input.jpg -distort barrel "A B C 0.0 D E" output.jpg
Because somehow I have to tell to convert that I'm not providing the D parameter.
snibgo wrote: For best results, you need to photograph something that has both vertical and horizontal straight lines. There should be at least 5 lines in each direction: one near each edge, one near the centre, and one each between the center and edge. More lines is better. You only need three points per line, but 5 or more is better.
Thanks for the recommendation! I'll see if I can find something like a brick wall or something similar that gives me a grid of reference.
snibgo wrote: If the camera lens doesn't focus or zoom, this barrel/pincushion calibration only needs to be done once.
I think this USB camera does not allow zoom. Anyway the idea is to use it without zoom. However it has a mechanism of manual focus, when rotating the front in a circular manner. This here you say is interesting, since the intention is to have several of these USB cameras connected to Raspberries Pi. So I would have to ensure the focus is the same for all cameras to ensure the correction of the distortion is equally good in all of them. But the rotating drum does not have any reference mark enabling replicate the focus graduation. Any suggestions?
snibgo wrote: When processing photos, you should correct barrel/pincushion distortion first, then perspective distortion.
Thank you for the advice! I'll keep that in mind. Although before I would see how to get the values for the perspective distortion correction.

Thank you very much for your time and reply. It gave me several things to think about.

Kind regards,
Daniel

Re: Help with camera distortion correction.

Posted: 2016-05-19T08:27:35-07:00
by snibgo
Your camera isn't square-on to the wall. It should be rotated slightly right, which is clockwise looking down on a vertical axis.

You would still have perspective, of course, but it would be what is called "single-point" perspective. Line that are horizontal, moving away from the camera, would converge at the centre of the image.

For barrel, set D = 1 - (A+B+C). Then the corners of the output image will be in the same place as the corners of the input image.

Re: Help with camera distortion correction.

Posted: 2016-05-23T15:32:08-07:00
by DanielB
Hi, snibgo.
snibgo wrote:Your camera isn't square-on to the wall. It should be rotated slightly right, which is clockwise looking down on a vertical axis.

You would still have perspective, of course, but it would be what is called "single-point" perspective. Line that are horizontal, moving away from the camera, would converge at the centre of the image.
I've managed to position the camera a little better. What do you think?

Image

Image

What I have noticed is that if I stand in front of the camera, my feet are stretched a bit as a result of the correction. Perhaps this is because I used for the control points the lines of limits on the tiling, which appear curved.
snibgo wrote: For barrel, set D = 1 - (A+B+C). Then the corners of the output image will be in the same place as the corners of the input image.
For the correction I used A, B and C in Hugin, but not D. Do you think that using D could improve the correction?


Thans for your reply.

Kind regards,
Daniel