Random fields & Probability cheat sheet

  • Random variable – the value of a random experiment.
  • Random field – a spatial function that assigns a random variable to each position
  • Distribution function of a random variable (aka Cumulative distribution function, CDF)
    – Consider \(P_f(z)=\mathcal{P} \{ f <z\} \) The equation tells us how likely for the random variable \(f\) to be less than \(z\). \(P_f(z)\) is the distribution function of \(f\). \(\mathcal{P} \) is the probability.
  • Probability of an event happening has the properties:
    • It is a non-negative number
    • The probability of all the events (all possible outcomes) must equal one
    • The probability of a random variable taking a specific real value is zero. Loosely, this is because a real values need an infinite precision to describe them. We can however specify the probability over an infinity small range. Combining those ranges over all possible values give the probability density function.
  • The probability density function is the differential of the CDF
    \(p_f(z)=\frac{ d P_f(z) }{dz} \)
  • The expected or mean value is \(\mu_f=E\{f\}=\int_{-\infty}^{\infty} zp_f(z) dz\)
  • The variance is \(\sigma^2_f=E\{(f-\mu_f)^2\}=\int_{-\infty}^{\infty} (z-\mu_f)^2p_f(z) dz\)
  • If we have many random variables we can define a joint distribution function
    \(P_{f_1,f_2,f_3,\ldots,f_n}(z_1,z_2,z_3 \ldots z_n)=\mathcal{P} \{ f_1 <z_1,f_2 <z_2,f_3 <z_3,\ldots,f_n <z_n \} \)
  • and the joint probability density function
    \(p_{f_1,f_2,f_3,\ldots,f_n}(z_1,z_2,z_3 \ldots z_n)=\frac{P_{f_1,f_2,f_3,\ldots,f_n}(z_1,z_2,z_3 \ldots z_n)}{dz_1 dz_2 dz_3 \ldots dz_n} \)
  • If random variables are independent then
    \(P_{f_1,f_2,f_3,\ldots,f_n}(z_1,z_2,z_3 \ldots z_n)=P_{f1}(z_1)P_{f2}(z_2)P_{f3}(z_3)\ldots P_{fn}(z_n)\)
  • If they are uncorrelated then
    \(E\{f_i f_j\}=E\{f_i \}E\{f_j\} \quad , \forall i,j, i \neq j\)
  • and orthogonal if
    \(E\{f_i f_j\}=0 \)
  • The covariance is \(c_{ij}=E\{(f_i-\mu_{f_i})( f_j-\mu_{f_j})\}\) which equals zero if they are uncorrelated.

Since we are interested in images (in this blog) we shall introduce random fields from this point of view.  If we have a random variable at every point \(\mathbf{r}\) in a 2D space we have a random field. The random field can be written \(f(\mathbf{r},\omega_i)\) where \(\omega_i\) is the outcome. For a fixed \(\omega_i\) the random field gives a grey level image as we scan over all \(\mathbf{r}\). Scanning over all possible \(\omega_i\) gives a series of images.

Mathematical representation of images and optics.

The way we represent an image mathematically can have a big impact on our ability to mathematically manipulate it. Conceptually it would be simplest to represent an image (let’s assume it’s grey-level) as a 2D array. If my image is a 2D array \(\mathbf{X}\) I could implement the effect a linear shift invariant blurring function \(\mathbf{H}\) and produce an output image \(\mathbf{F}\) via the convolution operator:

\(\mathbf{F}=\mathbf{H} \ast \mathbf{X}\)

I could do other things with this notation such as introduce a shift operator to move my image by one pixel

\(\mathbf{F}=\mathbf{\acute {H}} \ast \mathbf{X}\)

where \(\mathbf{\acute {H}}=[0, 0 ,1]\).

The problem is this is all shift invariant, the same blur or shift is applied to all the pixels in an image. What if the amount of blurring and shifting changes from pixel to pixel as it does in a real image due to imperfections in the camera’s lens? I would need a separate \(\mathbf{\acute {H}}=[0, 0 ,1]\) for every pixel. A more convenient way is to drop the 2D convolution and implement our system using matrix multiplications. To do this we lexicography rearrange the 2D image matrix into a 1D vector

\(\left[ \begin{array}{ccc}
a & b & c \\
d & e & f \\
g & h & i \end{array}\right] \longrightarrow \left[ \begin{array}{c} a\\d\\g\\b\\e\\h\\c\\f\\i \end{array} \right]\)

In Matlab this would be implemented with X1d=X(:) and we can transform it back to 2d with knowledge of the original number of rows and columns X=reshape(X1d,rows,cols).

For simplicity sake I shall reduce the number pixels in my image to 3. But what can we do with this? Well let’s look at a matrix multiply operation

\(\left[ \begin{array}{ccc}
a & b & c \\
d & e & f \\
g & h & i \end{array}\right] \left[ \begin{array}{c}x\\y\\z\end{array}\right] = \left[ \begin{array}{c} ax+by+cz\\dx+ey+fz\\gx+hy+iz\end{array}\right]\)

each row in the matrix is like an operator on each pixel. I’ve effectively got a shift variant convolution. For example I could blur the first pixel and leave the rest the same

\(\left[ \begin{array}{ccc}
0.33& 0.33 & 0.33 \\
0 & 1 & 0 \\
0 & 0 & 1 \end{array}\right] \left[ \begin{array}{c}x\\y\\z\end{array}\right] = \left[ \begin{array}{c} 0.33x+0.33y+0.33z\\y\\z\end{array}\right]\)

Note that the 1s go down the diagonal.

I could implement a shift on the second pixel

\(\left[ \begin{array}{ccc}
1& 0 & 0 \\
0 & 0& 1 \\
0 & 0 & 1 \end{array}\right] \left[ \begin{array}{c}x\\y\\z\end{array}\right] = \left[ \begin{array}{c} x\\z\\z\end{array}\right]\)

by changing the values I could implement rotations and warps.

And we can combine several matrices together to define our system. If \(\mathbf{S}\) is a shift matrix and \(\mathbf{B}\) is a blurring matrix with can simply combine the results together


to describe our shift variant optical system.

An additional step we may wish to introduce the effect of sensor pixel size. We can implement this by making our original image have a much higher resolution and them use a decimation filter to reduce this to a low resolution camera image. To this we create a matrix with \(N\) rows, which equals the number of pixels in the decimated image, and \(M\) columns, which equals the number of pixels in the high resolution image.

\(\left[ \begin{array}{ccc}
0.5& 0.5 & 0 & 0 \\
0 & 0 & 0.5 & 0.5  \end{array}\right] \left[ \begin{array}{c}w\\x\\y\\z\end{array}\right] = \left[ \begin{array}{c} 0.5w+0.5x \\0.5y +0.5z\end{array}\right]\)

shows how we can reduce the resolution by 1/2 in one dimension and we can easily extend this to 2D.


It’s worth noting if the blurring is shift invariant (which is a lot easier to deal with) the matrix is block circulant. This means it is of the form

\(\left[ \begin{array}{ccccc}
d(0) & d(M-1) & d(M-2)& \ldots &d(1) \\
d(1) & d(0) & d(M-1)& \ldots &d(2)\\
d(2) & d(1) & d(0)& \ldots &d(3)\\
d(M-1) & d(M-2) & d(M-3)& \ldots &d(0) \end{array}\right]\)

Note, each row is a shifted version of the one above it. The reason, this is important is that it is easy to invert.

Mac Ports Cheat Sheet

It’s worth reading the mac ports guide here, but here is my cut down cheat sheet of commands I actually use.

To install a port, e.g opencv

sudo port install opencv

To update ports definition list

sudo port selfupdate

Once, that’s finished you can update the outdated ports (this might take sometime depending on the number of updates)

sudo port upgrade outdated

or you can get a list of the outdated ports

port outdated

and upgraded them manually

sudo port upgrade opencv

This should also upgrade dependencies.

To get a list of installed ports

port installed

To search for a port, eg, PHP you could try

port search php

This will give you hundreds of port that mention PHP in their description. You can filter this, for example

port search --name --glob 'php*'

will return only the ports that start with php.

port info opencv

returns information about the opencv port (the lastest version, so watch out if your ports are out of date). A list of variants can be obtained with

port variants opencv

A port variant can be installed using the syntax:

sudo port install opencv +java

This installs the variant of opencv with java bindings. To see which variant is installed type:

port installed opencv

this lists the variant installed, plus the inactive versions of the library.

If a port fails to build it might be worth cleaning up first by running

sudo port clean opencv

and trying again.

To find out what and where port has installed files run

port contents opencv

To remove a port you use either of

sudo port uninstall opencv
sudo port uninstall --follow-dependents opencv

The second option removes the installed dependents as long as no other port is using them. If you don’t use the latter there will be a number of ports left on your system that you didn’t manually install. These are known as leaves and you can list or remove them with

port echo leaves
sudo port uninstall leaves

You might need to repeat the process since uninstalling leaves may create new leaves!

Port will also leave the outdated ports on your system. To remove them use

sudo port uninstall inactive


Boost and CMake

CMake is wonderful on Linux, the problem on Windows is it can’t always find the library since the user could have installed them anywhere. To get it to find the boost library with the CMake-GUI add a new variable BOOST_ROOT of type Path and set the parameter to the boost home directory.

CMake uses a set of modules to locate common library and you can specify the root of these directly. See the documentation at https://cmake.org/cmake/help/v3.0/manual/cmake-modules.7.html