Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

understanding the idea behind some code snippet

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;
    if(res3<0){
        return calc_shift(x3,x2,cx,k);
    }else{
        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.

understanding the idea behind some code snippet

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;
    if(res3<0){
        return calc_shift(x3,x2,cx,k);
    }else{
        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.

understanding the idea behind some code snippet

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;
    if(res3<0){
        return calc_shift(x3,x2,cx,k);
    }else{
        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.