This project could include a data collection component as well as computer programming and data analysis. In that respect, it is a good representation of how computation and programming are used in research.

The goal is to track the motion of particles immersed in a liquid undergoing Brownian motion. The data can be fit to Einstein's statistical model of molecular motion to estimate Avogadro's number.

The experiment involves the following five components:

**Data collection.**Using a USB microscope and diluted milk, we can capture images of milkfat globules undergoing Brownian motion. For writing and testing the code, a set of raw data - a sequence of 200 JPEG images - is provided. Once you have a working analysis chain, you can collect and analyze your own data.**Image segmentation.**Read in an image and use thresholding to separate the image into foreground and background pixels.**Particle identification.**Find maximal blobs of connected foreground pixels using depth-first search.**Particle tracking.**Determine how far each particle moves between observations via a simple nearest neighbor calculation.**Data analysis.**Estimate Avogadro's number by averaging a sequence of displacements and plugging the results into Einstein's equations.

For starters, you will use this supplied data to develop and validate your code. Once you are done, you will have the opportunity to collect your own data on fat globules suspended in dilute milk using a USB microscope. The test data was obtained by William Ryu using fluorescent imaging. The file run0.zip contains a sequence of two hundred 640-by-480 color JPEG images, frame00000.jpg through frame00199.jpg.

Each image shows a two-dimensional cross section of a microscope slide. The beads move in and out of the microscope's field of view (the x- and y-directions). Beads also move in the z-direction, so they can move in and out of the microscope's depth of focus; this results in halos, and it can also result in beads completely disappearing from the image.

The first challenge is to identify the beads amidst the noisy data. Each image is 640-by-480 pixels, and each pixel is represented by a Color object which needs to be converted to a luminance value ranging from 0.0 (black) to 255.0 (white). Whiter pixels correspond to beads (foreground) and blacker pixels to water (background). Break the problem into three pieces:

- (i) read in the picture
- (ii) classify the pixels as foreground or background by using a variable threshold luminance value (180.0 works well for the test data
- (iii) find the disc-shaped clumps of foreground pixels that constitute each bead.

A polystyrene bead is typically represented by a disc-like shape of at least some minimum number, $P$, (typically 25) of connected foreground pixels. A blob or connected component is a maximal set of connected foreground pixels, regardless of its shape or size. Refer to any blob containing at least $P$ pixels as a bead. The center-of-mass of a blob (or bead) is the average of the x- and y-coordinates of its constituent pixels.

Suggested code structure:

Create a Python module called `Blobfinder.py`

that includes a class `Blob()`

with the following methods:

```
__init__ # construct an empty blob
add(i,j) # add a pixel (i, j) to the blob
mass() # return number of pixels added = its mass
distanceTo(blob) # return distance between centers of masses of this blob and b
centerOfMass() # return tuple of (x,y) values for this blob's center of mass
# format center-of-mass coordinates with 4 digits to right
# of decimal point
```

Include in the module the following additional functions:

```
def BlobFinder(picture, tau):
'''find all blobs in the picture using the luminance threshold tau'''
def countBeads(P):
'''return the number of beads with >= P pixels'''
def getBeads(P):
'''return all beads with >= P pixels'''
```

`BlobFinder`

object using a luminance threshold of $\tau$ and print out the mass and center-of-mass of all of the beads with at least $P$ pixels, then finally, it should print out the mass and center-of-mass of all of the blobs (beads with at least 1 pixel).

**Note:** The Counting Stars project is exactly like the image manipulation you need to use for this part of the project. It's a good idea to review that notebook and its code to help you get started.

`BlobFinder`

), and writes to a file (one per line, formatted with 4 decimal places to the right of decimal point) the radial displacement that each bead moves from one frame to the next (assuming it is no more than $\Delta p$). Note that it is not necessary to explicitly track a bead through a sequence of frames—you only need to worry about identifying the same bead in two consecutive frames.

The *self-diffusion constant* $D$ characterizes the stochastic movement of a molecule (bead) through a homogeneous medium (the water molecules) as a result of random thermal energy. The Einstein-Smoluchowski equation states that the random displacement of a bead in one dimension has a Gaussian distribution with mean zero and variance $\sigma^2 = 2 D \Delta t$, where $\Delta t$ is the time interval between position measurements. That is, a molecule's mean displacement is zero and its mean square displacement is proportional to the elapsed time between measurements, with the constant of proportionality $2D$. We estimate $\sigma^2$ by computing the variance of all observed bead displacements in the x and y directions. Let ($\Delta x_1$, $\Delta x_2$), ..., ($\Delta x_n$, $\Delta y_n$) be the $n$ bead displacements, and let $r_1$, ..., $r_n$ denote the radial displacements. Then

For the test data, $\Delta t$ = 0.5 so this is an estimate for $D$ as well. The radial displacements $r_i$ are measured in pixels: to convert to meters, multiply by 0.175$\times$10$^{-6}$ (meters per pixel).

The Stokes-Einstein relation asserts that the self-diffusion constant of a spherical particle immersed in a fluid is given by

$$ D = \frac{k_BT}{6\pi\eta\rho}$$where, for the test data,

$T$ = absolute temperature = 297 degrees Kelvin (room temperature)

$\eta$ = viscosity of water = 9.135$\times$10$^{-4}$ N$\cdot$s/m$^2$ (at room temperature)

$\rho$ = radius of bead = 0.5$\times$10$^{-6}$ meters

and $k_B$ is the Boltzmann constant. All parameters are given in SI units. The Boltzmann constant is a fundamental physical constant that relates the average kinetic energy of a molecule to its temperature. We estimate $k_B$ by measuring all of the parameters in the Stokes-Einstein equation, and solving for $k_B$.

**Output formats.** Use four digits to the right of the decimal place for all of your floating point outputs whether they are in floating point format or exponential format.