The goal of IPA is to provide a set of functions for image processing and analysis.
You can install the development version from GitHub with:
install.packages("remotes") # if not installed
remotes::install_github("villegar/IPA")
You should start by loading IPA
on your session.
library(IPA)
rm_background
)This function removes the background from an image, based on a threshold
value (bkg_thr
). This can be found by creating a histogram of the
image.
Note: currently, it only removes dark backgrounds; a future version will have an option for light backgrounds as well.
AB_001_B <- system.file("extdata", "AB_001_B.jp2", package = "IPA")
AB_001_B_img <- imager::load.image(AB_001_B)
plot(AB_001_B_img)
hist(AB_001_B_img, main = "Pixel histogram for AB_001_B")
abline(v = 0.4, col = "red") # Mark bkg_thr
Based on the histogram, we should look for pixel concentration (background), for this example, the background is dark, so the threshold should be close to zero.
rm_background
function with the corresponding background
threshold (bkg_thr = 0.4
for the example image).IPA::rm_background(image_path = AB_001_B, bkg_thr = 0.4)
_wb
(without background,
transparent), and extension
.png
.AB_001_B_wb <- system.file("extdata", "AB_001_B_wb.png", package = "IPA")
AB_001_B_wb_img <- imager::load.image(AB_001_B_wb)
plot(AB_001_B_wb_img)
Note: the function plot
displays transparent pixels as 0 (black)
values, that is why the previous plot shows a black background (to see
the transparent image, look up for the generated *_wb.png
file).
find_area
.alpha <- imager::channel(AB_001_B_wb_img, 4) # the alpha channel is the fourth one
blobs <- IPA::find_area(alpha, start = c(2000, 5750), px_tol = 500)
| x0 | width | y0 | height | | ---: | ----: | ---: | -----: | | 2000 | 3000 | 5750 | 500 | | 2000 | 2500 | 5750 | 1450 |
Next, remove the previously detected objects, this can be done using the
function add_alpha
.
for (a in seq_len(nrow(blobs))) {
area <- as.numeric(blobs[a, ])
AB_001_B_wb_img <- IPA::add_alpha(AB_001_B_wb_img, area = area)
}
plot(AB_001_B_wb_img)
abline(h = 5750, v = 2000, col = "red") # Mark the trimmed area
find_area
)TODO
add_alpha
)TODO
rgb_decomposition
)This function extracts each layer from an image as a matrix, for further processing.
test_data <- data.frame(name = c("R", "G", "B"), values = c(2, 2, 2))
RGB <- c("red", "green", "blue")
png("inst/figures/test_plot.png")
barplot(height = test_data$values, names = test_data$name, col = RGB)
dev.off()
This code generates the following barplot
(inst/figures/test_plot.png
)
Which we want to decompose into 3 images:
Red layer Green layer Blue layerFor this purpose we can use the function rgb_decomposition
, which can
be called as follows
rgb_decomposition(subdirectory,
# optional
extension = "jpg",
RData = TRUE,
recursive = TRUE)
where subdirectory
is the name of a directory where to search for the
images. The other arguments are optional; extension
is the file format
of the images, RData
is a boolean flag to indicate whether or not the
layers should be stored as RData
format or CSV, the latter requires
more disk space. Finally, recursive
is a boolean flag on whether or
not explore the subdirectory
recursively for more images.
rgb_decomposition
function to extract the layers of the
example image previously created:rgb_decomposition("inst/figures/", "png", recursive = FALSE)
After running this, three new files (per image) will be on disk, called
IMAGE-NAME-red.RData
, IMAGE-NAME-green.RData
, and
IMAGE-NAME-blue.RData
.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.