3

我已将此答案中的代码移植到新的 C++ api 。但是,虽然我了解大部分代码,但我无法理解 calc_shift() 函数背后的想法以及如何提取像素移位。如果有人能给我一个解释,我将不胜感激。功能如下:

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);
    }

}

上述函数的调用方式如下所示:

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;

我想理解上面的代码,因为我还想将它用于 k < 0 的枕形失真情况。如果我现在使用它,代码将陷入 k<0 给定值的无限循环。

4

0 回答 0