The goal of this document is to show an example of a processing flow using images. The steps to be followed to build new image processing pipes and how the new flow would be defined are described below. The script for this example can be found at the Github repository:
https://github.com/miferreiro/bdpar/blob/master/articleExample/exampleImage/exampleImage.R
First of all, it is necessary to include the bdpar package and the imager package, which will be used to read and transform the images.
library(bdpar)
library(imager)
First it is necessary to create the extractor that will allow reading the images to be processed.
library(R6)
<- R6Class(
ExtractorImage
classname = "ExtractorImage",
inherit = Instance,
public = list(
initialize = function(path) {
$initialize(path)
super
},obtainSource = function() {
<- imager::load.image(super$getPath())
source $setSource(source)
super$setData(source)
super
}
) )
Secondly, it is necessary to indicate to bdpar with which extension the created extractor is associated, in this case, .png.
<- ExtractorFactory$new()
extractors $registerExtractor(extension = "png", extractor = ExtractorImage) extractors
For this example, four pipes have been developed to treat the images. It should be noted that the first pipe is necessary to read the image with the extractor created previously and the next pipes manage the changes of the image.
library(R6)
<- R6Class(
Image2Pipe "Image2Pipe",
inherit = GenericPipe,
public = list(
initialize = function(propertyName = "",
alwaysBeforeDeps = list(),
notAfterDeps = list()) {
$initialize(propertyName, alwaysBeforeDeps, notAfterDeps)
super
},pipe = function(instance) {
$obtainSource()
instance
instance
}
)
)
<- R6Class(
ImageCroppingPipe "ImageCroppingPipe",
inherit = GenericPipe,
public = list(
initialize = function(propertyName = "",
alwaysBeforeDeps = list(),
notAfterDeps = list()) {
$initialize(propertyName, alwaysBeforeDeps, notAfterDeps)
super
},pipe = function(instance) {
<- instance$getData()
data <- imager::imsub(data, x > height/2)
data $setData(data)
instance
instance
}
)
)
<- R6Class(
ImageResizePipe "ImageResizePipe",
inherit = GenericPipe,
public = list(
initialize = function(propertyName = "",
alwaysBeforeDeps = list(),
notAfterDeps = list()) {
$initialize(propertyName, alwaysBeforeDeps, notAfterDeps)
super
},pipe = function(instance) {
<- instance$getData()
data <- imager::imrotate(data, 30)
data $setData(data)
instance
instance
}
) )
Once the pipes to be used have been created, it is time to build the flow of pipes to be used using the DynamicPipeline class.
<- DynamicPipeline$new(pipeline = list(Image2Pipe$new(),
pipeline $new(),
ImageCroppingPipe$new())) ImageResizePipe
Taking into account all the elements to be used to configure the preprocessing, the start of the pipe flow is launched as follows.
runPipeline(path = "imageExample/parrots.png",
extractors = extractors,
pipeline = pipeline,
cache = FALSE,
verbose = FALSE,
summary = FALSE)