def _solve_lr(vlines, w, l, opt_options=OPTIMIZATION_OPTIONS, opt_method=OPTIMIZATION_METHOD, limit=0.3):
""" Solve for the left and right edge displacement.
This routine estimates the amount to move the upper left and right cornders of the image
in a horizontal direction in order to make the given lines parallel and vertical.
:param vlines: Lines that we want to map to vertical lines.
:param w: The width of the image
:param l: The height of the image
:param opt_options: Optimization options passed into `minimize`
:param opt_method: The optimization method.
:param limit: A limit on the amount of displacement -- beyond this and we will assume failure.
:return: (dl, dr), the horizontal displacement of the left and right corners.
"""
if len(vlines) == 0:
return 0, 0
a = np.append(vlines[:, 0, :], np.ones((len(vlines), 1)), axis=1)
b = np.append(vlines[:, 1, :], np.ones((len(vlines), 1)), axis=1)
def objective(x):
dl, dr = x
Hv = np.linalg.inv(H_v(dl, dr, w, l))
return np.sum(np.abs(Hv[0, :].dot(a.T) / Hv[2, :].dot(a.T) - Hv[0, :].dot(b.T) / Hv[2, :].dot(b.T)))
res = minimize(objective, (0., 0.),
options=opt_options,
method=opt_method)
dl, dr = res.x
# Give up if the solution is not plausible (this indicates that the 'vlines' are too noisy
if abs(dl) > limit * w:
dl = 0
if abs(dr) > limit * w:
dr = 0
return dl, dr
评论列表
文章目录