Camera Projection, solving for scale I'm having trouble solving the following problem.  Hopefully someone can help!
The following is in homogeneous coordinates.  I am projecting a point into a pinhole camera. $\lambda$ is an unknown scaling factor, all other variables are known.
$\mathbf{x} = \mathtt{P}(\lambda \mathbf{X} + \mathbf{Q})$
P is a 3x4 projection matrix.  I'd like to solve for $\lambda$.  Here's some example data:
x = [-0.4052 -0.0502 1.0000]'
P = [1     0     0     0
     0     1     0     0
     0     0     1     0]
X = [0.3925 0.0465 -0.9313 1.0000]'
Q = [-18.4299, 0, -7.7678, 1.0000]'

And for this data, I know $\lambda$ is about 1430 if I just brute force search. Plugging the numbers and this result into the equation gives a correct solution.  However, surely there must be a direct solution for $\lambda$?  I can't seem to work it out!
Thanks,
John
 A: Notice that $|x-P(\lambda X + Q)|$ is minimized exactly when $|x-P(\lambda X + Q)|^2$ is minimized. We can rewrite this as
$$|x-PQ-\lambda PX|^2 = |x-PQ|^2 - 2\lambda(x-PQ)^TPX + \lambda^2 X^TP^TPX$$
which is simply a quadratic in $\lambda$. To minimize it we set its derivative to zero, giving
$$\lambda X^TP^TPX = (x-PQ)^TPX$$
which we solve for $\lambda$ to give
$$\lambda = \frac{(x-PQ)^TPX}{X^TP^TPX}$$
Edit: for the example you gave, this gives an optimal $\lambda$ of -1.068, in contrast to your value of ~1400. I'm not sure why the discrepancy arises.
A: I think what you actually want is the following:
calling $y = P ( \lambda X + Q)$, you want $\left( \frac{y_1}{y_3}, \frac{y_2}{y_3} \right)$ to be as close to $\left( \frac{x_1}{x_3}, \frac{x_2}{x_3} \right)$ as possible.
If so, then the target function to minimize is $\left| \left( \frac{x_1}{x_3} - \frac{y_1}{y_3} \right)^2 + \left( \frac{x_2}{x_3} - \frac{y_2}{y_3} \right) \right|^2$, which is a function of the form
$$
\frac{\alpha \lambda^2 + 2 \beta \lambda + \gamma}{\left( \lambda X_3 + Q_3 \right)^2}
$$
with $\alpha = (x_3 X_1 - x_1 X_3)^2 + (x_3 X_2 - x_2 X_3)^2$, $\beta = (x_3 X_1 - x_1 X_3) (x_3 Q_1 - x_1 Q_3) + (x_3 X_2 - x_2 X_3) (x_3 Q_2 - x_2 Q_3)$, and $\gamma = (x_3 Q_1 - x_1 Q_3)^2 + (x_3 Q_2 - x_2 Q_3)^2$.
This system has a simple minimum at
$$
\lambda = \frac{\beta Q_3 - \gamma X_3}{\beta X_3 - \alpha Q_3},
$$
which in your case turns out to be close to 1426.3
