# Question in cvUndistortPoints\[why we can use iterate to get the undistort pixel\]

**URL:** <https://forum.opencv.org/t/question-in-cvundistortpoints-why-we-can-use-iterate-to-get-the-undistort-pixel/9401>\
**Category:** C++\
**Tags:** calib3d\
**Created:** [July 5, 2022, 9:31am UTC](https://forum.opencv.org/t/question-in-cvundistortpoints-why-we-can-use-iterate-to-get-the-undistort-pixel/9401 "2022-07-05T09:31:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jumby](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@jumby](https://forum.opencv.org/u/jumby)\
**Post date:** [July 5, 2022, 9:31am UTC](https://forum.opencv.org/t/question-in-cvundistortpoints-why-we-can-use-iterate-to-get-the-undistort-pixel/9401/1 "2022-07-05T09:31:58Z")

</div>

![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/f/f3392f1ec3ed19565eb0905e00d37069c3b9bda0.png)  
as the pic says,left is the distort pixel,right is the undistort pixel,r = root of (x_x + y_y),k is radial coefficients,p is tangential distortion coefficients.  
if we know right,we can get the corresponding left without iterating.  
if we know left,it is hard to get right,we can get it through iterating.but i wonder why we can get it by iterate(as far as i know,it must be convergent),so anyone can explain it in math theory/formula?

source code bellow

```
    // compensate distortion iteratively
    for( int j = 0; j < iters; j++ )//iters = 5
    {
        double r2 = x*x + y*y;
        double icdist = (1 + ((k[7]*r2 + k[6])*r2 + k[5])*r2)/(1 + ((k[4]*r2 + k[1])*r2 + k[0])*r2);
        double deltaX = 2*k[2]*x*y + k[3]*(r2 + 2*x*x)+ k[8]*r2+k[9]*r2*r2;
        double deltaY = k[2]*(r2 + 2*y*y) + 2*k[3]*x*y+ k[10]*r2+k[11]*r2*r2;
        x = (x0 - deltaX)*icdist;
        y = (y0 - deltaY)*icdist;
    }

```

---

<div class="post-metadata">

**Author:** ![Steve\_in\_Denver](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Steve\_in\_Denver](https://forum.opencv.org/u/Steve_in_Denver)\
**Post date:** [July 9, 2022, 9:46pm UTC](https://forum.opencv.org/t/question-in-cvundistortpoints-why-we-can-use-iterate-to-get-the-undistort-pixel/9401/2 "2022-07-09T21:46:36Z")

</div>

This question / answer might help:

> <https://stackoverflow.com/questions/39671107/lens-distortion-model-vs-correction-model>

The distortion function is modeled as a Taylor series. I think you could just swap the order of the distorted / undistorted coordinates and compute an inverse function, too. Without an inverse function, though, you have to come up with some other way to undistort points, and an iterative approach works. **A word of warning: 5 iterations is not enough if your lens has significant distortion. Newer versions of OpenCV accept a TermCriteria parameter which allows you to specify max iterations. When I care about accuracy (which is pretty much always) I set the maximum iterations quite high. I use 40. For my lens it makes a huge difference compared to 5**

---

<div class="post-metadata">

**Author:** ![jumby](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@jumby](https://forum.opencv.org/u/jumby)\
**Post date:** [July 19, 2022, 7:04am UTC](https://forum.opencv.org/t/question-in-cvundistortpoints-why-we-can-use-iterate-to-get-the-undistort-pixel/9401/3 "2022-07-19T07:04:47Z")

</div>

thanks . i have seen the link you post.  
but i still wonder how we can ensure it is convergent for we use iterate to solve it

---

<div class="post-metadata">

**Author:** ![Steve\_in\_Denver](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Steve\_in\_Denver](https://forum.opencv.org/u/Steve_in_Denver)\
**Post date:** [July 19, 2022, 5:09pm UTC](https://forum.opencv.org/t/question-in-cvundistortpoints-why-we-can-use-iterate-to-get-the-undistort-pixel/9401/4 "2022-07-19T17:09:47Z")

</div>

I’m not sure it is guaranteed to converge in all cases, but if you are working in a well-behaved region of the distortion function, it will converge. My understanding of how the distortion function works is that it it maps undistorted radius (distance from the image center to a given point) to a distorted radius - this tells you how much closer / further from the center the distorted point is.

The function can’t be correctly inverted by simply taking the reciprocal of the rational expression, but you can get a reasonable estimate by doing this. You then fine-tune the estimate by iterating, making smaller and smaller adjustments at each iteration. I think this works in most cases because your first estimate is a reasonable starting point for the search.

If you are looking for a proof that it will converge (or under which conditions it will/won’t converge), I don’t have anything for you. If you do find something, please share it - I would be interested to learn more.

---

<div class="post-metadata">

**Author:** ![jumby](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@jumby](https://forum.opencv.org/u/jumby)\
**Post date:** [July 20, 2022, 1:38am UTC](https://forum.opencv.org/t/question-in-cvundistortpoints-why-we-can-use-iterate-to-get-the-undistort-pixel/9401/5 "2022-07-20T01:38:04Z")

</div>

sure,i am very interested in it .as soon as I get it ,I will post here for share.

much thanks for you:)
