I have ported to the new C++ api the code from this answer. However, while I understand the most of the code I cannot get the idea behind the calc_shift() function and how the pixels shift is extracted. If someone could provide me an explanation I would be really grateful. The function has as follows:
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);
}
}
and 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;
I would like to understand the above code because I want also to use it for the pincushion distortion case where the k is < 0. If I use it as it is now the code falls into an infinite loop with a k<0 given value.