The biopixR package includes an image of microbeads as an example to demonstrate its analytical and processing abilities for biological imagery. This sample image displays the packageâ€™s features, enabling users to experiment with image analysis and manipulation within the contexts of biotechnology and life sciences. Researchers and practitioners can utilize this illustration to comprehend the application of biopixR to their individual imaging requirements, whether pertaining to cell biology, microscopy, or any other biological imaging applications.

```
library(biopixR)
plot(beads)
```

As the biopixR package primarily uses the image processing packages imager and magick, the examples are stored in the specific imager class: cimg.

`class(beads)`

`[1] "cimg" "imager_array" "numeric" `

The objective of this task is to extract important information from
an image consisting of microbeads. As a preliminary step, it is
essential to distinguish between individual microbeads and acquire their
corresponding coordinates or positions. The `objectDetection`

function uses edge detection and labeling techniques to gather elaborate
information about the microbeads, allowing for the identification and
differentiation of individual particles according to their unique edges.
This procedure assists in deriving precise coordinates for every
microbead in the image, which sets the groundwork for subsequent
analysis and characterization of the microbeads within the biopixR
package.

```
res_objectDetection <-
objectDetection(beads, method = 'edge', alpha = 1, sigma = 0)
```

This function generates a list of objects. Letâ€™s examine the specific outcomes and explore methods for visualizing them, starting with the center coordinates of the microbeads:

```
plot(beads)
with(
res_objectDetection$centers,
points(
res_objectDetection$centers$mx,
res_objectDetection$centers$my,
col = factor(res_objectDetection$centers$value),
pch = 19
)
)
```

Upon closer examination, it is evident that each individual microbead is identified accurately by a singular point at its center, and their distinctiveness is conveyed through varying colors, aligning with our intended objective. However, the identification of clotted microbeads, referred to as doublets or multiplets, deviates from the expected pattern. Notably, not every visually distinguishable microbead is marked with a distinct color. The behavior observed, where doublets are identified as a single entity, is due to their edges disappearing along the contact surface. The same principle applies to multiplets, where the consecutive edges of clotted beads cause them to be treated as a unified, larger object.

Letâ€™s examine the next output from `objectDetection`

. This
function captures the coordinates of labeled regions, providing precise
details about the position of each microbead. By leveraging another
function within the package, `changePixelColor`

, we can
selectively color-specific coordinates in a cimg. Thus, we can apply
this function to tint all the extracted coordinates in the microbead
image and assess whether the outcome aligns with our expectations.

```
changePixelColor(
beads,
res_objectDetection$coordinates,
color = factor(res_objectDetection$coordinates$value),
visualize = TRUE
)
```

The visual depiction shows that all relevant coordinates were
successfully retrieved, with each variant (single microbeads, doublets,
and multiplets) colored accordingly. As previously stated, these clotted
microbeads should be excluded from further consideration. The difference
in size serves as a critical factor for efficient sorting and subsequent
analysis. Therefore, the selected parameter for addressing these
microbeads will be their size. The next section will provide a detailed
explanation of the `sizeFilter`

application process.

Before delving into the available filter functions in the package,
let us first examine the internal visualization feature of the
`objectDetection`

function. The edges identified by the
`edgeDetection`

function are visually emphasized with color,
simplifying the adjustment of the threshold parameter (alpha) in the
`objectDetection`

function. In addition, the identified
centers are represented as green circles. This visualization is
particularly useful in determining the smoothing factor (sigma).
Sometimes, smoothing is necessary to improve the recognition of complete
objects and prevent the marking of fragmented edges.

`res_objectDetection$marked_objects |> plot()`

Nonetheless, a crucial differentiation occurs in obtaining the
highlighted microbeads as a cimg, which opens up possibilities for the
creation of an interactive tool using tcltk. This step facilitates the
development of an interactive interface, empowering users to dynamically
explore the adjustment of various variables and observe the
corresponding shifts in detected microbeads. The interactive interface
is presented through the `interactive_objectDetection`

function within the biopixR package.

As previously stated, it is crucial to remove doublets and multiplets
before performing the analysis. This objective will be addressed in this
section using the `sizeFilter`

. The filter is applied to the
image using previously obtained coordinates and centers, with specified
lower and upper limits. If more objects are identified, automated limit
calculation becomes available based on the interquartile range (IQR) of
the size distribution. To simplify limit selection in cases of
insufficient detected objects, the function will issue a warning and
generate a size distribution.

```
#res_sizeFilter <- sizeFilter(
# centers = res_objectDetection$centers,
# coordinates = res_objectDetection$coordinates,
# lowerlimit = "auto",
# upperlimit = "auto"
#)
```

As shown by the size distribution, there two larger objects (doublet - size: >150 px; multiplet - size: >400 px). Therefore, the limits will be set accordingly. In some cases, it can be difficult to achieve continuous edges around multiplets, which can lead to the detection of multiple small objects that correspond to the edges of the multiplet. To address this issue, it is possible to set a lower limit to exclude results that may be affected by this phenomenon.

```
res_sizeFilter <- sizeFilter(
centers = res_objectDetection$centers,
coordinates = res_objectDetection$coordinates,
lowerlimit = 0,
upperlimit = 150
)
```

**visualization sizeFilter**:

```
changePixelColor(
beads,
res_sizeFilter$coordinates,
color = "darkgreen",
visualize = TRUE
)
```

The goal of excluding clotted microbeads from the analysis has been achieved successfully. As shown in the image above, the resulting data set now only includes individual microbeads.

When microbeads are in close proximity, they can induce fluorescence
in each other. This phenomenon can lead to misleading signals and
contribute to false positives during analysis. To prevent distorted
results, the `proximityFilter`

is used in subsequent steps.
This function inspects each gathered center and surveys a defined radius
for positive pixels. If another positive pixel from a different object
is detected within this range, both are discarded because of their
proximity. The radius can be selected manually or determined
automatically. In the automatic calculation, the size of the remaining
microbeads is determined in the first step. The radius is then
calculated using the following formula, assuming a circular object:

\[ \text{radius} = \sqrt{\frac{A}{\pi}} \]

The function specifies that the scanned area from the center of the microbead is twice the radius, ensuring that the minimum distance to another microbead is half a microbead (only if radius = â€˜autoâ€™). Note that the coordinates obtained from the objectDetection function should be used as they are not filtered and therefore include all coordinates. This ensures the accurate exclusion of microbeads that are in close proximity to doublets or multiplets.

```
res_proximityFilter <-
proximityFilter(
centers = res_sizeFilter$centers,
coordinates = res_objectDetection$coordinates,
radius = "auto"
)
```

**visualization proximityFilter**:

```
changePixelColor(
beads,
res_proximityFilter$coordinates,
color = "darkgreen",
visualize = TRUE
)
text(
res_proximityFilter$centers$mx,
res_proximityFilter$centers$my,
res_proximityFilter$centers$value,
col = "grey"
)
```

The chapterâ€™s objective has been accomplished, as demonstrated by the
most recent plot. The `sizeFilter`

successfully eliminated
the doublet and multiplet from the dataset. Furthermore, microbeads that
lacked at least half the size of a microbead between them were removed
with the aid of the `proximityFilter`

. To demonstrate this
result, the `changePixelColor`

function was once again
utilized, coloring every remaining pixel. Consequently, the microbeads
that remain are highlighted in dark green, indicating their successful
passage through the filtering process.

To conclude this chapter, we need to extract meaningful information
from the filtered data set. One of the most fundamental results to be
displayed after applying a filter is undoubtedly the number of remaining
and discarded objects. As the size of the objects has already been
calculated in both algorithms, this information should also be included
in the display. Moreover, the intensity of the signal is a crucial
parameter for both microbeads and any fluorescent image. Finally, it may
be of interest to calculate the area density, which represents the
percentage of detected pixels (microbeads) relative to the pixel area of
the entire image. To extract this information, the
`resultAnalytics`

function from the biopixR package is
utilized. This function requires the data frame of the remaining
coordinates, the individual size, and the original image as inputs.
Starting from version 0.2.2, the `resultAnalytics`

function
can utilize parallel processing with foreach and doParallel, reducing
the required time by approximately two-thirds.

```
result <-
resultAnalytics(
img = beads,
coordinates = res_proximityFilter$coordinates,
unfiltered = res_objectDetection$coordinates
)
result$detailed
```

```
objectnumber size intensity sd_intensity x y
1 3 98 0.593 0.183 9.23 38.0
2 4 96 0.628 0.183 53.27 39.6
3 5 97 0.591 0.177 108.66 43.9
4 6 423 0.682 0.170 98.16 64.6
5 7 84 0.606 0.177 35.39 97.6
6 8 100 0.531 0.167 58.69 101.2
```

While itâ€™s possible to showcase a detailed version of results featuring individual microbeads with their cluster number, size, intensity, and coordinates, this presentation method can become quite overwhelming, especially when dealing with larger images containing numerous objects. Consequently, the image results are summarized in a single row, emphasizing the key parameters described earlier.

`result$summary`

```
number_of_objects mean_size sd_size mean_intensity sd_intensity
1 6 150 134 0.632 0.181
estimated_rejected coverage
1 3 0.0556
```

A final note regarding the estimated number of rejected objects: As previously mentioned, the detection of multiplets may have limitations. When only the edges are identified, the number of positive pixels is reduced, resulting in an inaccurate estimation of the discarded microbeads. This calculation is obtained by dividing the number of detected pixels by the average size while subtracting the count of centers that have successfully undergone the filter process.

The results generated by the `objectDetection`

function
can be quickly displayed using the `resultAnalytics`

function. Therefore, letâ€™s first examine the unfiltered results
available from the image.

```
result_proximityFilter <-
resultAnalytics(
img = beads,
coordinates = res_objectDetection$coordinates
)
result_proximityFilter$detailed
```

```
objectnumber size intensity sd_intensity x y
1 1 93 0.577 0.177 63.97 8.68
2 2 96 0.630 0.189 69.01 20.24
3 3 98 0.593 0.183 9.23 37.97
4 4 96 0.628 0.183 53.27 39.61
5 5 97 0.591 0.177 108.66 43.86
6 6 423 0.682 0.170 98.16 64.61
7 7 84 0.606 0.177 35.39 97.61
8 8 100 0.531 0.167 58.69 101.17
9 9 190 0.637 0.173 22.72 121.02
10 10 94 0.567 0.169 39.50 125.00
```

To increase versatility, the filter functions can be used
individually, without depending on each other. The following section
examines the results of applying each filter separately. We will begin
with the `sizeFilter`

. Once again, the output from the
`objectDetection`

function is used as input.

```
ind_sizeFilter <- sizeFilter(
centers = res_objectDetection$centers,
coordinates = res_objectDetection$coordinates,
lowerlimit = 50,
upperlimit = 150
)
changePixelColor(
beads,
ind_sizeFilter$coordinates,
color = "darkgreen",
visualize = TRUE
)
text(
ind_sizeFilter$centers$mx,
ind_sizeFilter$centers$my,
ind_sizeFilter$centers$value,
col = "grey"
)
```

```
result_sizeFilter <-
resultAnalytics(
img = beads,
coordinates = ind_sizeFilter$coordinates,
unfiltered = res_objectDetection$coordinates
)
result_sizeFilter$detailed
```

```
objectnumber size intensity sd_intensity x y
1 1 93 0.577 0.177 63.97 8.68
2 2 96 0.630 0.189 69.01 20.24
3 3 98 0.593 0.183 9.23 37.97
4 4 96 0.628 0.183 53.27 39.61
5 5 97 0.591 0.177 108.66 43.86
6 7 84 0.606 0.177 35.39 97.61
7 8 100 0.531 0.167 58.69 101.17
8 10 94 0.567 0.169 39.50 125.00
```

As demonstrated in the previous section, the `sizeFilter`

function successfully removes multiplets and doublets. The resulting
output can then be used directly in the `resultAnalytics`

function to extract the most important information. The following
section will present the individual use of the
`proximityFilter`

. The input remains the same as before.

```
ind_proximityFilter <-
proximityFilter(
centers = res_objectDetection$centers,
coordinates = res_objectDetection$coordinates,
radius = "auto"
)
changePixelColor(
beads,
ind_proximityFilter$coordinates,
color = "darkgreen",
visualize = TRUE
)
text(
ind_proximityFilter$centers$mx,
ind_proximityFilter$centers$my,
ind_proximityFilter$centers$value,
col = "grey"
)
```

As expected, the `proximityFilter`

excluded microbeads
that were close to each other. In this situation, a doublet is in close
proximity to a single microbead, so both the doublet and its neighboring
microbead are rejected by the filter.

```
result_proximityFilter <-
resultAnalytics(
img = beads,
coordinates = ind_proximityFilter$coordinates,
unfiltered = res_objectDetection$coordinates
)
result_proximityFilter$detailed
```

```
objectnumber size intensity sd_intensity x y
1 3 98 0.593 0.183 9.23 38.0
2 4 96 0.628 0.183 53.27 39.6
3 5 97 0.591 0.177 108.66 43.9
4 6 423 0.682 0.170 98.16 64.6
5 7 84 0.606 0.177 35.39 97.6
6 8 100 0.531 0.167 58.69 101.2
```

Another important aspect to consider in bioimaging is the relative distance between individual objects. By examining the spatial relationships between the identified objects, researchers can gain valuable insights into their organization and distribution within the image. This information is critical for understanding biological phenomena such as spatial patterns, cell clustering, or the arrangement of microstructures. In the following sections, we present a method that explores the Euclidean distance between objects to enable users to investigate and quantify spatial relationships within their bioimages.

The relative distance between multiple objects typically refers to
the distances between each pair of objects in relation to each other.
The method to calculate relative distance depends on the dimensionality
of your objects (e.g., 1D, 2D, 3D) and the specific metric you want to
use (e.g., Euclidean distance, Manhattan distance). Here, Iâ€™ll provide a
general approach for 2D objects using the Euclidean distance. Assuming
you have a set of points in a 2D space (e.g., x, y coordinates), you can
calculate the Euclidean distance between each pair of points. The
formula for Euclidean distance between two points ((x_{1},
y_{1})) and ((x_{2}, y_{2})) is:

\[ \text{Distance} = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2} \]

```
euclidean_distance <- function(point1, point2) {
sqrt((point2$mx - point1$mx)^2 + (point2$my - point1$my)^2)
}
```

Now, letâ€™s apply this function on the extracted centers. The centers serve as the initial reference points for measuring the distances between objects. Initially, we create an empty matrix to store the results. Next, we develop a nested for loop to systematically compare each center with every other center, except for comparing a center to itself, resulting in a missing value. This calculation results in a distance matrix.

```
num_points <- nrow(res_proximityFilter$centers)
relative_distances <-
matrix(NA, nrow = num_points, ncol = num_points)
for (i in 1:num_points) {
for (j in 1:num_points) {
if (i == j) {
next
} else {
relative_distances[i, j] <-
euclidean_distance(
res_proximityFilter$centers[i, ],
res_proximityFilter$centers[j, ]
)
}
}
}
as.matrix(relative_distances)
```

```
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] NA 44.1 99.6 92.8 65.1 80.3
[2,] 44.1 NA 55.6 51.4 60.7 61.8
[3,] 99.6 55.6 NA 23.3 90.9 76.0
[4,] 92.8 51.4 23.3 NA 70.9 53.8
[5,] 65.1 60.7 90.9 70.9 NA 23.6
[6,] 80.3 61.8 76.0 53.8 23.6 NA
```

After removing missing values, the average relative distance for each center is calculated, which provides individual insight into its distance from the other microbeads. The resulting values are then added as a new column to the previously presented detailed results, providing further information about the distinct properties of each microbead.

```
for (a in 1:num_points) {
result$detailed$relative_distance[a] <-
mean(na.omit(relative_distances[a, ]))
}
result$detailed
```

```
objectnumber size intensity sd_intensity x y relative_distance
1 3 98 0.593 0.183 9.23 38.0 76.4
2 4 96 0.628 0.183 53.27 39.6 54.7
3 5 97 0.591 0.177 108.66 43.9 69.1
4 6 423 0.682 0.170 98.16 64.6 58.4
5 7 84 0.606 0.177 35.39 97.6 62.2
6 8 100 0.531 0.167 58.69 101.2 59.1
```

As mentioned previously, the detailed table results for a larger image would unavoidably become overly extensive. The same issue is applicable to the matrix. Therefore, to overcome this issue, instead of relying solely on the detailed table results, the mean relative distance is calculated and included in the summary table.

```
result$summary$mean_distance <-
mean(result$detailed$relative_distance)
result$summary
```

```
number_of_objects mean_size sd_size mean_intensity sd_intensity
1 6 150 134 0.632 0.181
estimated_rejected coverage mean_distance
1 3 0.0556 63.3
```

*will be part of the resultAnalytics function as of
version 0.2.0*

To further illustrate the packageâ€™s capabilities, the following section presents a case study mainly focused on addressing discontinuous edges in image analysis. The study showcases the integration of crucial data from two images to determine the quantities of droplets and microbeads. Additionally, the analysis aims to investigate the frequency of events in which a single microbead joins a droplet, as opposed to situations in which multiple microbeads are present in a single droplet. The study employs an algorithm that focuses on filling gaps along discontinuous edges. This is achieved through a combination of detecting line ends and interpolating pixels. By using this comprehensive method, the study provides valuable perspectives on the distribution between droplets and microbeads in the provided image. These findings demonstrate the flexibility of the package to handle complex image analysis scenarios.

The following images serve as test subjects for the upcoming study. The first image displays a brightfield view showing droplets, some of which contain microbeads. The second image on the left displays the fluorescent channel, exhibiting only the microbeads.

```
plot(droplets)
plot(droplet_beads)
```

In typical fashion for image analysis, this study starts by applying
a threshold to the bright-field image. Subsequently, the resulting image
uncovers a distinct challenge: the edges of individual partitions are
not continuous. In order to differentiate individual partitions and
evaluate whether they contain microbeads, it is crucial to bridge these
gaps. Fortunately, the package offers a specialized function,
`fillLineGaps`

, to address this issue in image analysis. This
algorithm identifies line endpoints and connects them to the nearest
neighboring edge that is not their own. Additionally, the
`objectDetection`

function can eliminate specific objects,
such as microbeads. This step is crucial for avoiding the unwanted
outcome of line ends becoming connected to microbeads. The code chunks
below, derived from the `fillLineGaps`

function, visually
demonstrating the elimination of microbeads and the detection of line
ends by highlighting them with the `changePixelColor`

function.

```
# preprocessing: threshold, negate and mirroring
thresh <- threshold(droplets, "13%")
thresh_cimg <- as.cimg(thresh)
thresh_magick <- cimg2magick(thresh_cimg)
neg_thresh <- image_negate(thresh_magick)
neg_thresh_cimg <- magick2cimg(neg_thresh)
neg_thresh_m <- mirror(neg_thresh_cimg, axis = "x")
# first remove microbeads from droplet image (important for the linking of
# discontinuous edges, as otherwise they may connect with the microbeads)
# removes objects to prevent reconnecting with labeled regions that
# are not lines/edges
beads_to_del <- droplet_beads
bead_coords <-
objectDetection(beads_to_del, alpha = 1, sigma = 0.1)
# transform binary image to array to modify individual values
thresh_array <- as.array(neg_thresh_m)
for (i in seq_len(nrow(bead_coords$coordinates))) {
thresh_array[
bead_coords$coordinates[i, 1],
bead_coords$coordinates[i, 2], 1, 1
] <- 0
}
# removed microbeads from droplets and retransformation to cimg
thresh_clean_cimg <- as.cimg(thresh_array)
# displaying problem of discontinous edges
plot(thresh_cimg)
# displaying removed microbeads
plot(thresh_clean_cimg)
```