# Difficulties implementing Equations

I am trying to write this equations in TeXnic Center, but so far, I am not very successful. The equations I want to write are here: http://www.mathworks.com/help/vision/ref/vision.opticalflow-class.html - Horn&Schunk method.

The Horn-Schunk method, by assuming that the optical flow is smooth over the entire image, computes an estimate of the velocity field, that minimizes this equation:
\begin{flushleft}

$E = \int \!\!\! \int (I_xu + I_yv + I_t)^2 dxdy + \alpha \int \!\!\! \int (\frac{\partial \textit{u}}{\partial x}^2 + \frac{\partial \textit{u}}{\partial y}^2 + \frac{\partial \textit{v}}{\partial x}^2 + \frac{\partial \textit{v}}{\partial y}^2)dxdy$
\end{flushleft}

where \alpha is the smoothness term of the velocity field,
$\frac{\partial \textit{u}}{\partial x}$
and
$\frac{\partial \textit{v}}{\partial x}$
are the spatial derivatives of the optical velocity component \textit{u}. The \alpha regularization parameter controls the strength of the smoothness constraint and is usually selected heuristically. The Horn-Schunck method minimizes the previous equation to obtain the velocity field, [u v], for each pixel in the image, which is given by the following equations:

$\stackrel u{k+1}{x,y} = \stackrel u{-k}{x,y} - \frac{I_x[I_x\stackrel u{-k}{x,y} + I_y\stackrel v{-k}{x,y} + I_t]}{\alpha^2 + I_x^2 + I_y^2$

$\stackrel v{k+1}{x,y} = \stackrel v{-k}{x,y} - \frac{I_y[I_x\stackrel u{-k}{x,y} + I_y\stackrel v{-k}{x,y} + I_t]}{\alpha^2 + I_x^2 + I_y^2$


I am newbie in LateX, and when I compile, it doesn't puts out the expected result. Also, when I write \alpha, afterwards, the text gets formatted in a different way (see picture attached). Can someone help me?

-
please always post complete document that allows people to reproduce the problem. Don't ignore TeX error messages! You will have had an error message from \alpha saying it is a math mode command, and as you can see from the output everything after that is in math Use $\alpha$ –  David Carlisle Jul 13 at 16:12
ok @DavidCarlisle thanks. Could you help me with the equations? :/ –  user2205242 Jul 13 at 16:15
Welcome to TeX.SX. –  Christian Hupfer Jul 13 at 16:17
well fix all the easy errors as reported by TeX eg you don't have $ around \alpha but then you have incorrect $ around variables that are already in math mode \int ($I_ should be \int (I_ with no $. But if you want someone to try your code make it start with \documentclass and end \end{document} –  David Carlisle Jul 13 at 16:18
That is why you should always post a complete document that paragraph is indented because of some earlier code you hav enot shown. Perhaps it is a list environment or quote or abstract that is not finished. Impossible to say. If TeX generates an error message the final pdf form should just be used as a rough debugging aid, TeX just inserts things to allow it to carry on, not to make sensible output. If you have a list environment that starts but is not finished tehn there will be an error message about that. –  David Carlisle Jul 13 at 16:50
show 6 more comments

You're quite close but you are over working things.

First, inside a displayed equations you do no need to enclose mathematics within dollar signs, so you can drop then $...$.

Secondly, your \stackrel's are unnecessary and completely confused me: subscripts and superscripts are done with _ and ^, respectively.

Here is a cleaned up working version of your code:

\documentclass{amsart}

\usepackage{amsmath}

\begin{document}

\begin{equation*}
E = \iint (I_xu + I_yv + I_t)^2\,dx\,dy + \alpha \iint
(\frac{\partial u}{\partial x}^2 + \frac{\partial
u}{\partial y}^2 + \frac{\partial v}{\partial x}^2 +
\frac{\partial v}{\partial y}^2)\,dx\,dy
\end{equation*}

where $\alpha$ is the smoothness term of the velocity field,
$\frac{\partial u}{\partial x}$
and
$\frac{\partial v}{\partial x}$
are the spatial derivatives of the optical velocity component $u$. The $\alpha$ regularization parameter controls the strength of the smoothness constraint and is usually selected heuristically. The Horn-Schunck method minimizes the previous equation to obtain the velocity field, $[u v]$, for each pixel in the image, which is given by the following equations:

$u^{k+1}_{x,y} = u^{-k}_{x,y} - \frac{I_x[I_xu^{-k}_{x,y} + I_yv^{-k}_{x,y} + I_t]}{\alpha^2 + I_x^2 + I_y^2}$

$v^{k+1}_{x,y} = v^{-k}_{x,y} - \frac{I_y[I_xu^{-k}_{x,y} + I_yv^{-k}_{x,y} + I_t]}{\alpha^2 + I_x^2 + I_y^2}$

\end{document}


-
great! Thanks so much @Andrew! God Bless all of you! –  user2205242 Jul 13 at 16:37
Andrew, can you explain me one thing? Look my initial picture. The paragraph in the beginning of the page is 20mm dislocated to the right and at the bottom, the text starts a little bit before. why? :/ –  user2205242 Jul 13 at 16:41
@user2205242 Can yu say exactly which paragraph you mean? If it is the about... then it is because this starts a new paragaph.If you delete the empty line this will fix itself. –  Andrew Jul 13 at 16:49
You need to fix one more thing: The terms to the immediate right of the equal signs of the final two equations represent spatial averages. Thus thus need to be written as \bar{u}^{k}_{x,y} and \bar{v}^{k}_{x,y} rather than as u^{-k}_{x,y} and v^{-k}_{x,y}. –  Mico Jul 13 at 17:18
a couple more things ... (1) don't leave a blank line before a display -- it fouls up the vertical spacing. (2) multiple display lines should be grouped in a multi-line environment; here i'd suggest align* from amsmath. this suppresses unwanted page breaks as well as keeping the adjacent formulas grouped more sensibly. (gather is better if you don't want alignment.) –  barbara beeton Jul 13 at 18:23
show 1 more comment

I've taken a crack at cleaning up your code.

Some dos and donts:

• Don't use $...$ inside display-math mode to denote math objects; all objects in display-math mode are assumed to be math objects.

• Similarly, don't use \textit{...} to denote math items: If already in math mode, you need do nothing extra; if not, use $u$. (Some, but not all, fonts distinguish between math italics and text italics; when in math mode, use math italics.)

• The flushleft environment encasing a display-math environment achieves nothing; leave it off.

• Take care to resize various parentheses when they enclose "large" objects, using either \left and \right or, better still, optimally chosen sizing directives such as \big and \bigg.

• To write a "bar" (overline) across a variable x, write either \bar{x} or \overline{x} (if you want a heftier bar).

• Use ^ ("caret") to initiate superscript matter and _ ("underline") to initiate subscript matter.

• Don't leave extra blank lines in matter that clearly is meant to be a single logical paragraph; all-blank lines initiate a paragraph break, with the first word of the new paragraph right-indented (in the amount of \parindent).

• When you have groups of equations (such as the last two ones in your example), try using an environment such as align or align*. Please consult the user guide of the amsmath package to find out more about these environments.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
The Horn-Schunk method, by assuming that the optical flow is smooth over the entire image, computes an estimate of the velocity field, that minimizes this equation:
$E = \int \!\!\! \int \bigl(I_xu + I_yv + I_t\bigr)^2 dxdy + \alpha \int \!\!\! \int \biggl(\frac{\partial u}{\partial x}^2 + \frac{\partial u}{\partial y}^2 + \frac{\partial v}{\partial x}^2 + \frac{\partial v}{\partial y}^2\biggr)dxdy$
where $\alpha$ is the smoothness term of the velocity field and
$\frac{\partial u}{\partial x}$ and
$\frac{\partial v}{\partial x}$
are the spatial derivatives of the optical velocity component~$u$. The $\alpha$ regularization parameter controls the strength of the smoothness constraint and is usually selected heuristically. The Horn-Schunck method minimizes the previous equation to obtain the velocity field, $[u\ v]$, for each pixel in the image, which is given by the following equations:
\begin{align*}
u^{k+1}_{x,y} &= \bar{u}^{k}_{x,y} -
\frac{I_x[I_x \bar{u}^{k}_{x,y} + I_y\bar{v}^{k}_{x,y}
+ I_t]}{\alpha^2 + I_x^2 + I_y^2}\\
v^{k+1}_{x,y} &= \bar{v}^{k}_{x,y} -
\frac{I_y[I_x\bar{u}^{k}_{x,y} + I_y\bar{v}^{k}_{x,y}
+ I_t]}{\alpha^2 + I_x^2 + I_y^2}
\end{align*}
\end{document}

-
thanks so much! Nevertheless, the last 2 equations are not in the way that I want! Please, mathworks.com/help/vision/ref/vision.opticalflow-class.html see the Horn Schunk method, the last two equations :/ –  user2205242 Jul 13 at 16:33
@user2205242 - Thanks for this pointer; it looks like what you need are superscript terms. Give me a few minutes to update my answer. –  Mico Jul 13 at 16:35
someone already posted the correct answer! Thanks so much everyone! –  user2205242 Jul 13 at 16:37
@ Mico probably also worth mentioning that empty lines in mid-paragraph need to be avoided (I see that you've removed them :) )... It seems @user2205242 should read some introduction to LaTeX and mathmode, ctan.org/pkg/voss-mathmode maybe? –  cgnieder Jul 13 at 16:39
@Mico, can you explain me one thing? Look my initial picture. The paragraph in the beginning of the page is 20mm dislocated to the right and at the bottom, the text starts a little bit before. why? :/ –  user2205242 Jul 13 at 16:41
show 5 more comments

Here is an attempt to get something sensible. Note that you have a command for double integrals, that is \iint. The amsmath package (loaded by mathtools) defined a number of multiline equations environment, of which I use align*. The nccmath package is here to have medium-sized fractions in in-line formulae, rather than text-style, which is too small in my opinion.

As Mico explained the meaning of the \stackrel commands, I solved using the mathabx package that has a widebar command, that's better-looking in my opinion than plain \bar:

     \documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{mathtools, nccmath, mathabx}%

\begin{document}

The Horn-Schunk method, by assuming that the optical flow is smooth over the entire image, computes an estimate of the velocity field, that minimizes this equation:
$E = \iint \bigl(I_x u + I_y v + I_t\bigr)^2 dxdy + \alpha \iint\left(\frac{\partial \textit{u}}{\partial x}^2 + \frac{\partial \textit{u}}{\partial y}^2 + \frac{\partial \textit{v}}{\partial x}^2 + \frac{\partial \textit{v}}{\partial y}^2\right)dxdy,$
where $\alpha$ is the smoothness term of the velocity field, $\mfrac{\partial \textit{u}}{\partial x}$ and $\mfrac{\partial \textit{v}}{\partial x}$ are the spatial derivatives of the optical velocity component \textit{u}. The $\alpha$ regularization parameter controls the strength of the smoothness constraint and is usually selected heuristically. The Horn-Schunck method minimizes the previous equation to obtain the velocity field, $[u, v]$, for each pixel in the image, which is given by the following equations:
\begin{align*}
\widebar u^ {k+1}_{x,y} & = \widebar u^{k}_{x,y} - \frac{I_x[I_x \widebar u^{k}_{x,y} + I_y \widebar v^{k}_{x,y} + I_t]}{\alpha^2 + I_x^2 + I_y^2} \\
\widebar v^{k+1}_{x,y} & = \widebar v^{k}_{x,y} - \frac{I_y[I_x \widebar u^{k}_{x,y} + I_y \widebar v^{k}_{x,y} + I_t]}{\alpha^2 + I_x^2 + I_y^2}
\end{align*}

\end{document}


-
I see you were also baffled by the \stackrel-related material. Have a look at my (updated) answer: What's needed is a combination of \bar (to denote spatial averages) and ^{k}_{x,y} to denote superscript and subscript matters... –  Mico Jul 13 at 16:52
@Mico: Thanks for the tip. It allowed to give a more complete answer, using widebar from mathabx. –  Bernard Jul 13 at 17:50
@Bernard -- but please remember that mathabx changes almost all symbols. (you can see the difference in the shapes of the integrals and the \partials.) see Importing a Single Symbol From a Different Font for a method of getting around that problem. –  barbara beeton Jul 13 at 18:35
I now that, but couldn't manage to make \widebar work without loading mathabx. Maybe I didn't try enough… Actually I personally use Hendrik Vogt's solution (tex.stackexchange.com/questions/171907/…) but didn't dare propose it to someone who says he's a beginner. –  Bernard Jul 13 at 18:44