# Segmentation: Thresholding and Edge Detection

In this notebook our goal is to estimate the location and radius of spherical markers visible in a Cone-Beam CT volume.

We will use two approaches:

1. Segment the fiducial using a thresholding approach, derive the sphere's radius from the segmentation. This approach is solely based on SimpleITK.
2. Localize the fiducial's edges using the Canny edge detector and then fit a sphere to these edges using a least squares approach. This approach is a combination of SimpleITK and scipy/numpy.

Note that all of the operations, filtering and computations, are natively in 3D. This is the "magic" of ITK and SimpleITK at work.

The practical need for localizing spherical fiducials in CBCT images and additional algorithmic details are described in: Z. Yaniv, "Localizing spherical fiducials in C-arm based cone-beam CT", Med. Phys., Vol. 36(11), pp. 4957-4966.

In [1]:
library(SimpleITK)


Loading required package: rPython



Load the volume and look at the image (visualization requires window-leveling).

In [2]:
spherical_fiducials_image <- ReadImage(fetch_data("spherical_fiducials.mha"))
Show(spherical_fiducials_image, "spheres")


After looking at the image you should have identified two spheres. Now select a Region Of Interest (ROI) around the sphere which you want to analyze.

In [3]:
roi1 = list(c(280,320), c(65,90), c(8, 30))
roi2 = list(c(200,240), c(65,100), c(15, 40))

# Select the ROI
roi = roi1
# Update the R ROI, SimpleITK indexes are zero based, R indexes start at one
r_roi = lapply(roi, function(x) x+1)

# Create the mask image from an R array
amask <- array(0, spherical_fiducials_image$GetSize()) xs <- r_roi[[1]][1]:r_roi[[1]][2] ys <- r_roi[[2]][1]:r_roi[[2]][2] zs <- r_roi[[3]][1]:r_roi[[3]][2] amask[xs, ys, zs] <- mask_value mask <- Cast(as.image(amask), "sitkUInt8") mask$CopyInformation(spherical_fiducials_image)
Show(LabelOverlay(Cast(IntensityWindowing(spherical_fiducials_image, windowMinimum=-32767,
windowMaximum=-29611),
"sitkUInt8"),


## Thresholding based approach¶

Our region of interest is expected to have a bimodal intensity distribution with high intensities belonging to the spherical marker and low ones to the background. We can thus use Otsu's method for threshold selection to segment the sphere and estimate its radius.

In [4]:
# Set pixels that are in [min_intensity,otsu_threshold] to inside_value, values above otsu_threshold are
# set to outside_value. The sphere's have higher intensity values than the background, so they are outside.

inside_value <- 0
outside_value <- 255
number_of_histogram_bins <- 100

labeled_result <- OtsuThreshold(spherical_fiducials_image, mask, inside_value, outside_value,

# Estimate the sphere radius and location from the segmented image using the LabelShapeStatisticsImageFilter.
label_shape_analysis <- LabelShapeStatisticsImageFilter()
label_shape_analysis$SetBackgroundValue(inside_value) label_shape_analysis$Execute(labeled_result)
cat("The sphere's center is: ", label_shape_analysis$GetCentroid(outside_value), "\n") cat("The sphere's radius is: ",label_shape_analysis$GetEquivalentSphericalRadius(outside_value),"mm")

The sphere's center is:  19.86006 -80.34566 9.13347
The sphere's radius is:  3.46282 mm

## Edge detection based approach¶

In this approach we will localize the sphere's edges in 3D using SimpleITK. We then compute a least squares sphere that optimally fits the 3D points using scipy/numpy. The mathematical formulation we use is as follows:

Given $m$ points in $\mathbb{R}^n$, $m>n+1$, we want to fit them to a sphere such that the sum of the squared algebraic distances is minimized. The algebraic distance is: $$\delta_i = \mathbf{p_i}^T\mathbf{p_i} - 2\mathbf{p_i}^T\mathbf{c} + \mathbf{c}^T\mathbf{c}-r^2$$

The optimal sphere parameters are computed as: $$[\mathbf{c^*},r^*] = argmin_{\mathbf{c},r} \Sigma _{i=1}^m \delta _i ^2$$

setting $k=\mathbf{c}^T\mathbf{c}-r^2$ we obtain the following linear equation system ($Ax=b$): $$\left[\begin{array}{cc} -2\mathbf{p_1}^T & 1\\ \vdots & \vdots \\ -2\mathbf{p_m}^T & 1 \end{array} \right] \left[\begin{array}{c} \mathbf{c}\\ k \end{array} \right] = \left[\begin{array}{c} -\mathbf{p_1}^T\mathbf{p_1}\\ \vdots\\ -\mathbf{p_m}^T\mathbf{p_m} \end{array} \right]$$

The solution of this equation system minimizes $\Sigma _{i=1}^m \delta _i ^2 = \|Ax-b\|^2$.

Note that the equation system admits solutions where $k \geq \mathbf{c}^T\mathbf{c}$. That is, we have a solution that does not represent a valid sphere, as $r^2<=0$. This situation can arise in the presence of outliers.

Note that this is not the geometric distance which is what we really want to minimize and that we are assuming that there are no outliers. Both issues were addressed in the original work ("Localizing spherical fiducials in C-arm based cone-beam CT").

In [5]:
# Create a cropped version of the original image.
sub_image = spherical_fiducials_image[r_roi[[1]][1]:r_roi[[1]][2],
r_roi[[2]][1]:r_roi[[2]][2],
r_roi[[3]][1]:r_roi[[3]][2]]

# Edge detection on the sub_image with appropriate thresholds and smoothing.
edges <- CannyEdgeDetection(Cast(sub_image, "sitkFloat32"),
lowerThreshold=0.0,
upperThreshold=200.0,
variance = c(5.0, 5.0, 5.0))

# Get the 3D location of the edge points
edge_indexes <- which(as.array(edges)==1.0, arr.ind=TRUE)
# Always remember to modify indexes when shifting between native R operations and SimpleITK operations
physical_points <- t(apply(edge_indexes - 1, MARGIN=1,
sub_image$TransformIndexToPhysicalPoint))  Visually inspect the results of edge detection, just to make sure. Note that because SimpleITK is working in the physical world (not pixels, but mm) we can easily transfer the edges localized in the cropped image to the original. In [6]: edge_label <- Image(spherical_fiducials_image$GetSize(), "sitkUInt8")
edge_label$CopyInformation(spherical_fiducials_image) e_label <- 255 dev_null <- apply(physical_points, MARGIN=1, function(x, img, label) img$SetPixel(img$TransformPhysicalPointToIndex(x),label), img=edge_label, label=e_label) Show(LabelOverlay(Cast(IntensityWindowing(spherical_fiducials_image, windowMinimum=-32767, windowMaximum=-29611), "sitkUInt8"), edge_label, opacity=0.5))  Setup and solve linear equation system. In [7]: A <- -2 * physical_points A <- cbind(A, 1) b <- -rowSums(physical_points^2) x <- solve(qr(A, LAPACK=TRUE), b) cat("The sphere's center is: ", x[1:3], "\n") cat("The sphere's radius is: ", sqrt(x[1:3] %*% x[1:3] - x[4]), "\n")  The sphere's center is: 19.76043 -80.33228 9.253223 The sphere's radius is: 3.052485  Now, solve using R's linear model fitting. We also weigh the edge points based on the gradient magnitude. In [8]: gradient_magnitude = GradientMagnitude(sub_image) grad_weights = apply(edge_indexes-1, MARGIN=1, gradient_magnitude$GetPixel)

df <- data.frame(Y=rowSums(physical_points^2), x=physical_points[, 1],
y=physical_points[, 2], z=physical_points[, 3])
fit <- lm(Y ~ x + y + z, data=df, weights=grad_weights)

center <- coefficients(fit)[c("x", "y", "z")] / 2
cat("The sphere's center is: ", center, "\n")

The sphere's center is:  19.75684 -80.3487 9.257366