# MRI image segmentation

Magnetic Resonance Imaging (MRI) is a medical image technique used to sense the irregularities in human bodies. Segmentation technique for Magnetic Resonance Imaging (MRI) of the brain is one of the method used by radiographer to detect any abnormality happened specifically for brain.

In digital image processing, segmentation refers to the process of splitting observe image data to a serial of non-overlapping important homogeneous region. Clustering algorithm is one of the process in segmentation.
Clustering in pattern recognition is the process of partitioning a set of pattern vectors in to subsets called clusters.

There are various image segmentation techniques based on clustering. One example is the K-means clustering.

### Image Segmentation

Let's try the Hierarchial clustering with an MRI image of the brain.
The healthy data set consists of a matrix of intensity values.

To use hierarchical clustering we first need to convert the healthy matrix to a vector. And then we need to compute the distance matrix.

R gives us an error that seems to tell us that our vector is huge, and R cannot allocate enough memory.

Let's see the structure of the healthy vector.

The healthy vector has 365636 elements. Let's call this number n. For R to calculate the pairwise distances, it would need to calculate n*(n-1)/2 and store them in the distance matrix.
This number is 6.6844659 × 1010. So we cannot use hierarchical clustering.

Now let's try use the k-means clustering algorithm, that aims at partitioning the data into k clusters, in a way that each data point belongs to the cluster whose mean is the nearest to it.

### Analyze the clusters

To output the segmented image we first need to convert the vector healthy clusters to a matrix.
We will use the dimension function, that takes as an input the healthyClusters vector. We turn it into a matrix using the combine function, the number of rows, and the number of columns that we want.

### Now we will use the healthy brain vector to analyze a brain with a tumor

The tumor.csv file corresponds to an MRI brain image of a patient with oligodendroglioma, a tumor that commonly occurs in the front lobe of the brain. Since brain biopsy is the only definite diagnosis of this tumor, MRI guidance is key in determining its location and geometry.

Now, we will apply the k-means clustering results that we found using the healthy brain image on the tumor vector. To do this we use the flexclust package.
The flexclust package contains the object class KCCA, which stands for K-Centroids Cluster Analysis. We need to convert the information from the clustering algorithm to an object of the class KCCA.

The tumor is the abnormal substance here that is highlighted in red that was not present in the healthy MRI image.

# Singular Value Decomposition and Image Processing

The singular value decomposition (SVD) is a factorization of a real or complex matrix. It has many useful applications in signal processing and statistics.

### Singular Value Decomposition

SVD is the factorization of a $$m \times n$$ matrix $$Y$$ into three matrices as:

$$\mathbf{Y = UDV^\top}$$

With:

• $$U$$ is an $$m\times n$$ orthogonal matrix
• $$V$$ is an $$n\times n$$ orthogonal matrix
• $$D$$ is an $$n\times n$$ diagonal matrix

In R The result of svd(X) is actually a list of three components named d, u and v, such that Y = U %*% D %*% t(V).

#### Example

• we can reconstruct Y

### Image processing

• Load the image and convert it to a greyscale:

• Apply SVD to get U, V, and D
• Plot the magnitude of the singular values

Not that, the total of the first n singular values divided by the sum of all the singular values is the percentage of "information" that those singular values contain. If we want to keep 90% of the information, we just need to compute sums of singular values until we reach 90% of the sum, and discard the rest of the singular values.

### Image Compression with the SVD

Here we continue to show how the SVD can be used for image compression (as we have seen above).

• Original image

• Singluar Value k = 1

• Singluar Value k = 5

• Singluar Value k = 20

• Singluar Value k = 50

• Singluar Value k = 100

• Analysis

With only 10% of the real data we are able to create a very good approximation of the real data.

# Image Processing and Spatial linear transformations

We can think of an image as a function, f, from $$\pmb R^2 \rightarrow R$$ (or a 2D signal):

• f (x,y) gives the intensity at position (x,y)

Realistically, we expect the image only to be defined over a rectangle, with a finite range:
f: [a,b]x[c,d] -> [0,1]

A color image is just three functions pasted together. We can write this as a “vector-valued” function:

$$\pmb {f(x,y)} = \bigg[ \begin{array} {c} & r(x,y) \cr \ & g(x,y) \cr \ & b(x,y) \end{array} \bigg]$$

• Computing Transformations

If you have a transformation matrix you can evaluate the transformation that would be performed by multiplying the transformation matrix by the original array of points.

### Examples of Transformations in 2D Graphics

In 2D graphics Linear transformations can be represented by 2x2 matrices. Most common transformations such as rotation, scaling, shearing, and reflection are linear transformations and can be represented in the 2x2 matrix. Other affine transformations can be represented in a 3x3 matrix.

#### Rotation

For rotation by an angle θ clockwise about the origin, the functional form is $$x' = xcosθ + ysinθ$$
and $$y' = − xsinθ + ycosθ$$. Written in matrix form, this becomes:
$$\begin{bmatrix} x' \cr \ y' \end{bmatrix} = \begin{bmatrix} \cos \theta & \sin\theta \cr \ -\sin \theta & \cos \theta \end{bmatrix} \begin{bmatrix} x \cr \ y \end{bmatrix}$$

#### Scaling

For scaling we have $$x' = s_x \cdot x$$ and $$y' = s_y \cdot y$$. The matrix form is:
$$\begin{bmatrix} x' \cr \ y' \end{bmatrix} = \begin{bmatrix} s_x & 0 \cr \ 0 & s_y \end{bmatrix} \begin{bmatrix} x \cr \ y \end{bmatrix}$$

#### Shearing

For shear mapping (visually similar to slanting), there are two possibilities.
For a shear parallel to the x axis has $$x' = x + ky$$ and $$y' = y$$ ; the shear matrix, applied to column vectors, is:
$$\begin{bmatrix} x' \cr \ y' \end{bmatrix} = \begin{bmatrix} 1 & k \cr \ 0 & 1 \end{bmatrix} \begin{bmatrix} x \cr \ y \end{bmatrix}$$

A shear parallel to the y axis has $$x' = x$$ and $$y' = y + kx$$ , which has matrix form:
$$\begin{bmatrix} x' \cr \ y' \end{bmatrix} = \begin{bmatrix} 1 & 0 \cr \ k & 1 \end{bmatrix} \begin{bmatrix} x \cr \ y \end{bmatrix}$$

### Image Processing

The package EBImage is an R package which provides general purpose functionality for the reading, writing, processing and analysis of images.

#### Image Properties

Images are stored as multi-dimensional arrays containing the pixel intensities. All EBImage functions are also able to work with matrices and arrays.

• Adjusting Brightness

• Adjusting Contrast

• Gamma Correction

• Cropping Image

### Spatial Transformation

Spatial image transformations are done with the functions resize, rotate, translate and the functions flip and flop to reflect images.

Next we show the functions flip, flop, rotate and translate:

#### All spatial transforms except flip and flop are based on the general affine transformation.

Linear transformations using the function affine:

• Horizontal flip

$${m} = \left[ \begin{array}{cc} \ -1 & 0 \cr \ \ 0 & 1 \end{array} \right]$$

$$$$Result = image * m$$$$

• Horizontal shear $${m} = \left[ \begin{array}{cc} 1, 1/2 \cr \ 0, 1 \end{array} \right]$$

• Rotation by π/6 $${m} = \left[ \begin{array}{cc} cos(pi/6), -sin(pi/6) \cr \ sin(pi/6), cos(pi/6) \end{array} \right]$$

• Squeeze mapping with r=3/2 $${m} = \left[ \begin{array}{cc} 3/2, 0 \cr \ 0, 2/3 \end{array} \right]$$

• Scaling by a factor of 3/2 $${m} = \left[ \begin{array}{cc} 3/2, 0 \cr \ 0, 3/2 \end{array} \right]$$

• Scaling horizontally by a factor of 1/2 $${m} = \left[ \begin{array}{cc} 1/2, 0 \cr \ 0, 1 \end{array} \right]$$

# 3D plot of a Klein Bottle

The Klein bottle was discovered in 1882 by Felix Klein and since then has joined the gallery of popular mathematical shapes.
The Klein bottle is a one-sided closed surface named after Klein. We note however this is not a continuous surface in 3-space as the surface cannot go through itself without a discontinuity.

Here is the result from the code above:

# Visualizing World Development Indicators

### Fetching Data from World Bank

The Package WDI (world development indicators) is used to fetch data from WDI.
We can search the data as follow:

### Visualizing GDP per capita

Below we import the data on GDP per capita from years 1980 to 2013 from some countries and we show a simple plot of it: GDP per capita (constant 2000 US\$).

# Map Visualization in R

Here I tried to produce some map visualization in R.

• First using data from GADM database of Global Administrative Areas.
• Second using the package RWorldMap,
• Third using the package ggmap that allows visualizations of spatial data on maps retrieved from Google Maps, OpenStreetMap, etc., and
• Fourth using the package RgoogleMaps allows you to plot data points on any kind of map you can imagine (terrain, satellite, hybrid).

# 1. Data from GADM database

## Plot the map

We can use the variable NAME_1 to plot the map:

And next we created another variable called rainfall in the data.frame, we store random values in that variable:

# 2. RWorldMap

Rworldmap is a package for visualising global data, referenced by country. It provides maps as spatial polygons.

By Changing the xlim and ylim arguments of the plot function we can limit the display to just Europe.

## Geocoding

The geocode function from the ggmap package finds the coordinates of a location using Google Maps. Thus, finding the coordinates of the Extreme points of Europe can be done by:

So we can display only the europe as follow, by modifying the xlim and ylim arguments:

# 3. ggmap

• Fetching a Map

• Plotting on a Map

You can plot any [x,y, +/- z] information you’d like on top of a ggmap, so long as x and y correspond to longitudes and latitudes within the bounds of the map you have fetched. To plot on top of the map you must first make your map a variable and add a geom layer to it.

## 4. RgoogleMaps

'RgoogleMaps' allows you to plot data points on any kind of map you can imagine (terrain, satellite, hybrid).