この回答のコードを新しい 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 の指定された値で無限ループに陥ります。