Ask Your Question

understanding the idea behind some code snippet

asked 2015-03-17 21:16:55 -0500

theodore gravatar image

updated 2015-03-26 08:53:37 -0500

regarding some code that I ported from the old opencv C api to the new C++ api, here, related to some radial distortion transformation. I am trying to figure out how the following function works.

float calc_shift(float x1,float x2,float cx,float k)
    float thresh = 1;
    float x3 = x1+(x2-x1)*0.5;
    float res1 = x1+((x1-cx)*k*((x1-cx)*(x1-cx)));
    float res3 = x3+((x3-cx)*k*((x3-cx)*(x3-cx)));

    std::cerr<<"x1: "<<x1<<" - "<<res1<<" x3: "<<x3<<" - "<<res3<<std::endl;

    if(res1>-thresh and res1 < thresh)
        return x1;
        return calc_shift(x3,x2,cx,k);
        return calc_shift(x1,x3,cx,k);

the way that the above function is called can be seen below:

int w = src.cols;
int h = src.rows;

xShift = calc_shift(0, Cx - 1, Cx, k);
float newCenterX = w - Cx;
float xShift2 = calc_shift(0, newCenterX - 1, newCenterX, k);

yShift = calc_shift(0, Cy - 1, Cy, k);
float newCenterY = w - Cy;
float yShift2 = calc_shift(0, newCenterY - 1, newCenterY, k);

xScale = (w - xShift - xShift2) / w;
yScale = (h - yShift - yShift2) / h;

where Cx and Cy correspond to the new coordinates of a given point, usually the center of the image, and k some distortion coefficient. I know the outcome, but I want to understand the idea behind regarding the specific values that are used as well.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-03-19 10:59:36 -0500

Tetragramm gravatar image

Well, the calc_shift seems to be a funny way of undoing the r_new = r_old + k*r_old^3 radial distortion. Not a very stable one.

I'm not sure what the xScale/yScale is supposed to be though. It looks to be checking the error caused by distortion in both directions from the center, adding them together and dividing by the width. So if everything's perfect, the scale is 1. I'm not sure why it's calculated the way it is. As the distortion goes up it drops way faster than I would think would be useful. Depends on what you're using it for, I guess.

It may be worth it to you to replace calc_shift with a better solving method, Newton's method should work fine. But if it's working as is, don't bother.

edit flag offensive delete link more


@Tetragramm thanks for the response. However, I do not really understand the code snippet within the calc_shift() function. Do you understand what author exactly is doing, step by step. Thanks ;-).

theodore gravatar imagetheodore ( 2016-03-20 17:19:59 -0500 )edit

So the formula for the distortion is in the answer above. What this function is solving for is where r_old equals r_new. x1 is the outer bound of possible values, x2 is the inner bound, and cx is the zero point for the distortion, where no change happens no matter how much distortion there is.

So it calculates the error for the outer bound and half way between the inner and outer. If they are both past the point, the half way point is the new outer bound, repeat.

If they are on either side of the point, the outer stays the same and the half way point becomes the new inner bound.

Tetragramm gravatar imageTetragramm ( 2016-03-20 18:04:46 -0500 )edit

Question Tools

1 follower


Asked: 2015-03-17 21:16:55 -0500

Seen: 335 times

Last updated: Mar 19 '16