Title: | Image Processing for Simulated Cameras |
---|---|
Description: | Uses convolution-based techniques to generate simulated camera bokeh, depth of field, and other camera effects, using an image and an optional depth map. Accepts both filename inputs and in-memory array representations of images and matrices. Includes functions to perform 2D convolutions, reorient and resize images/matrices, add image andtext overlays, generate camera vignette effects, and add titles to images. |
Authors: | Tyler Morgan-Wall [aut, cph, cre] , Sean Barrett [ctb, cph] |
Maintainer: | Tyler Morgan-Wall <[email protected]> |
License: | GPL-3 |
Version: | 0.15.1 |
Built: | 2025-01-03 05:19:45 UTC |
Source: | https://github.com/tylermorganwall/rayimage |
Takes an RGB array/filename and adds an image overlay.
add_image_overlay(...)
add_image_overlay(...)
... |
to pass to |
3-layer RGB array of the processed image.
if(run_documentation()){ #Plot the dragon plot_image(dragon) } if(run_documentation()){ #Add an overlay of a red semi-transparent circle: circlemat = generate_2d_disk(min(dim(dragon)[1:2])) circlemat = circlemat/max(circlemat) #Create RGBA image, with a transparency of 0.5 rgba_array = array(1, dim=c(nrow(circlemat),ncol(circlemat),4)) rgba_array[,,1] = circlemat rgba_array[,,2] = 0 rgba_array[,,3] = 0 dragon_clipped = dragon dragon_clipped[dragon_clipped > 1] = 1 add_image_overlay(dragon_clipped, image_overlay = rgba_array, alpha=0.5, preview = TRUE) }
if(run_documentation()){ #Plot the dragon plot_image(dragon) } if(run_documentation()){ #Add an overlay of a red semi-transparent circle: circlemat = generate_2d_disk(min(dim(dragon)[1:2])) circlemat = circlemat/max(circlemat) #Create RGBA image, with a transparency of 0.5 rgba_array = array(1, dim=c(nrow(circlemat),ncol(circlemat),4)) rgba_array[,,1] = circlemat rgba_array[,,2] = 0 rgba_array[,,3] = 0 dragon_clipped = dragon dragon_clipped[dragon_clipped > 1] = 1 add_image_overlay(dragon_clipped, image_overlay = rgba_array, alpha=0.5, preview = TRUE) }
Add Title Function (deprecated)
add_title(...)
add_title(...)
... |
Arguments to pass to |
A 3-layer RGB array of the processed image if filename = NULL
and preview = FALSE
.
Otherwise, writes the image to the specified file or displays it if preview = TRUE
.
#Deprecated add_title function. Will display a warning. if(run_documentation()) { render_title(dragon, preview = TRUE, title_text = "Dragon", title_size=20) }
#Deprecated add_title function. Will display a warning. if(run_documentation()) { render_title(dragon, preview = TRUE, title_text = "Dragon", title_size=20) }
Takes an RGB array/filename and adds a camera vignette effect.
add_vignette(...)
add_vignette(...)
... |
Arguments to pass to |
3-layer RGB array of the processed image.
if(run_documentation()){ #Plot the dragon plot_image(dragon) } if(run_documentation()){ #Add a vignette effect: add_vignette(dragon, preview = TRUE, vignette = 0.5) }
if(run_documentation()){ #Plot the dragon plot_image(dragon) } if(run_documentation()){ #Add a vignette effect: add_vignette(dragon, preview = TRUE, vignette = 0.5) }
Dragon Image
dragon
dragon
An RGB 3-layer HDR array with 200 rows and 200 columns, generated using the rayrender package.
Dragon Depthmap
dragondepth
dragondepth
An matrix with 200 rows and 200 columns, representing
the depth into the dragon
image scene. Generated using the rayrender package.
Distances range from 847 to 1411.
Generates a 2D disk with a gradual falloff.
Disk generated using the following formula:
(-22.35 * cos(1.68 * r2) + 85.91 * sin(1.68 * r2) ) * exp(-4.89 * r2) + (35.91 * cos(4.99 * r2) - 28.87 * sin(4.99 * r2)) * exp(-4.71 * r2) + (-13.21 * cos(8.24 * r2) - 1.57 * sin(8.24 * r2)) * exp(-4.05 * r2) + (0.50 * cos(11.90 * r2) + 1.81 * sin(11.90 * r2)) * exp(-2.92 * r2) + (0.13 * cos(16.11 * r2) - 0.01 * sin(16.11 * r2)) * exp(-1.51 * r2)The origin of the coordinate system is the center of the matrix.
generate_2d_disk(dim = c(11, 11), radius = 1, rescale_unity = FALSE)
generate_2d_disk(dim = c(11, 11), radius = 1, rescale_unity = FALSE)
dim |
Default |
radius |
Default |
rescale_unity |
Default |
if(run_documentation()){ image(generate_2d_disk(101), asp=1) }
if(run_documentation()){ image(generate_2d_disk(101), asp=1) }
Generates a 2D exponential distribution, with an optional argument to take the exponential to a user-defined power.
generate_2d_exponential( falloff = 1, dim = c(11, 11), width = 3, rescale_unity = FALSE )
generate_2d_exponential( falloff = 1, dim = c(11, 11), width = 3, rescale_unity = FALSE )
falloff |
Default |
dim |
Default |
width |
Default |
rescale_unity |
Default |
if(run_documentation()){ image(generate_2d_exponential(1,31,3), asp=1) }
if(run_documentation()){ image(generate_2d_exponential(1,31,3), asp=1) }
Generates a 2D gaussian distribution, with an optional argument to take the gaussian to a user-defined power.
generate_2d_gaussian( sd = 1, power = 1, dim = c(11, 11), width = 3, rescale_unity = FALSE )
generate_2d_gaussian( sd = 1, power = 1, dim = c(11, 11), width = 3, rescale_unity = FALSE )
sd |
Default |
power |
Default |
dim |
Default |
width |
Default |
rescale_unity |
Default |
if(run_documentation()){ image(generate_2d_gaussian(1,1,31), asp=1) }
if(run_documentation()){ image(generate_2d_gaussian(1,1,31), asp=1) }
Calculates font metrics for a specified font, font size, and style.
get_string_dimensions(string, font = "sans", size = 12, align = "center", ...)
get_string_dimensions(string, font = "sans", size = 12, align = "center", ...)
string |
The string to be measured. |
font |
Default |
size |
A numeric value specifying the size of the font in points. |
align |
Default '"left"“. The string alignment. |
... |
Other arguments to pass to 'systemfonts::shape_string()“ |
The function renders specific characters ("d"
for ascender, "g"
for descender, "x"
for neutral) using the specified font parameters. It calculates the bounding box of each character to determine the necessary adjustments for accurate text positioning.
A data.frame listing the string dimensions.
# Get height of basic sans font get_string_dimensions("This is a string", size=24)
# Get height of basic sans font get_string_dimensions("This is a string", size=24)
Given a series of X and Y coordinates and an array/matrix, interpolates the Z coordinate using bilinear interpolation.
interpolate_array(image, x, y)
interpolate_array(image, x, y)
image |
Image filename, a matrix, or a 3-layer RGB array. |
x |
X indices (or fractional index) to interpolate. |
y |
Y indices (or fractional index) to interpolate. |
Either a vector of values (if image is a matrix) or a list of interpolated values from each layer.
#if(interactive()){ #Interpolate a matrix interpolate_array(volcano,c(10,10.1,11),c(30,30.5,33)) #Interpolate a 3-layer array (returns list for each channel) interpolate_array(dragon,c(10,10.1,11),c(30,30.5,33)) #end}
#if(interactive()){ #Interpolate a matrix interpolate_array(volcano,c(10,10.1,11),c(30,30.5,33)) #Interpolate a 3-layer array (returns list for each channel) interpolate_array(dragon,c(10,10.1,11),c(30,30.5,33)) #end}
Displays the image in the current device.
plot_image( image, rotate = 0, draw_grid = FALSE, ignore_alpha = FALSE, asp = 1, new_page = TRUE, return_grob = FALSE, gp = grid::gpar() )
plot_image( image, rotate = 0, draw_grid = FALSE, ignore_alpha = FALSE, asp = 1, new_page = TRUE, return_grob = FALSE, gp = grid::gpar() )
image |
Image array or filename of an image to be plotted. |
rotate |
Default 0. Rotates the output. Possible values: 0, 90, 180, 270. |
draw_grid |
Default |
ignore_alpha |
Default |
asp |
Default |
new_page |
Default |
return_grob |
Default |
gp |
A |
#if(interactive()){ #Plot the dragon array plot_image(dragon) #Make pixels twice as wide as tall plot_image(dragon, asp = 2) #Plot non-square images plot_image(dragon[1:100,,]) #Make pixels twice as tall as wide plot_image(dragon[1:100,,], asp = 1/2) #end}
#if(interactive()){ #Plot the dragon array plot_image(dragon) #Make pixels twice as wide as tall plot_image(dragon, asp = 2) #Plot non-square images plot_image(dragon[1:100,,]) #Make pixels twice as tall as wide plot_image(dragon[1:100,,], asp = 1/2) #end}
Displays the image in the current device.
plot_image_grid( input_list, dim = c(1, 1), asp = 1, draw_grid = FALSE, gp = grid::gpar() )
plot_image_grid( input_list, dim = c(1, 1), asp = 1, draw_grid = FALSE, gp = grid::gpar() )
input_list |
List of array (or matrix) image inputs. |
dim |
Default |
asp |
Default |
draw_grid |
Default |
gp |
A |
if(run_documentation()){ #Plot the dragon array plot_image_grid(list(dragon, 1-dragon), dim = c(1,2)) } if(run_documentation()){ plot_image_grid(list(dragon, 1-dragon), dim = c(2,1)) } if(run_documentation()){ plot_image_grid(list(dragon, NULL, 1-dragon), dim = c(2,2), asp = c(2,1,1/2)) } if(run_documentation()){ plot_image_grid(list(dragon, NULL, NULL, dragon), dim = c(2,2), asp = c(2,1,1,1/2)) } if(run_documentation()){ #Plot alongside the depth matrix dragon_depth_reoriented = render_reorient(dragondepth, transpose = TRUE, flipx = TRUE)/2000 plot_image_grid(list(dragondepth/2000, dragon, dragon, dragondepth/2000), dim = c(2,2)) }
if(run_documentation()){ #Plot the dragon array plot_image_grid(list(dragon, 1-dragon), dim = c(1,2)) } if(run_documentation()){ plot_image_grid(list(dragon, 1-dragon), dim = c(2,1)) } if(run_documentation()){ plot_image_grid(list(dragon, NULL, 1-dragon), dim = c(2,2), asp = c(2,1,1/2)) } if(run_documentation()){ plot_image_grid(list(dragon, NULL, NULL, dragon), dim = c(2,2), asp = c(2,1,1,1/2)) } if(run_documentation()){ #Plot alongside the depth matrix dragon_depth_reoriented = render_reorient(dragondepth, transpose = TRUE, flipx = TRUE)/2000 plot_image_grid(list(dragondepth/2000, dragon, dragon, dragondepth/2000), dim = c(2,2)) }
Takes an RGB array/filename and adds an image overlay.
ray_read_image(image, convert_to_array = TRUE, preview = FALSE, ...)
ray_read_image(image, convert_to_array = TRUE, preview = FALSE, ...)
image |
Image filename or 3-layer RGB array. |
convert_to_array |
Default |
preview |
Default |
... |
Arguments to pass to either |
3-layer RGB array of the processed image.
if(run_documentation()){ #Write as a png tmparr = tempfile(fileext=".png") ray_read_image(dragon) |> ray_write_image(tmparr) ray_read_image(tmparr) |> plot_image() } if(run_documentation()){ #Write as a JPEG (passing quality arguments via ...) tmparr = tempfile(fileext=".jpg") ray_read_image(dragon) |> ray_write_image(tmparr, quality = 0.2) ray_read_image(tmparr) |> plot_image() } if(run_documentation()){ #Write as a tiff tmparr = tempfile(fileext=".tiff") ray_read_image(dragon) |> ray_write_image(tmparr) ray_read_image(tmparr) |> plot_image() }
if(run_documentation()){ #Write as a png tmparr = tempfile(fileext=".png") ray_read_image(dragon) |> ray_write_image(tmparr) ray_read_image(tmparr) |> plot_image() } if(run_documentation()){ #Write as a JPEG (passing quality arguments via ...) tmparr = tempfile(fileext=".jpg") ray_read_image(dragon) |> ray_write_image(tmparr, quality = 0.2) ray_read_image(tmparr) |> plot_image() } if(run_documentation()){ #Write as a tiff tmparr = tempfile(fileext=".tiff") ray_read_image(dragon) |> ray_write_image(tmparr) ray_read_image(tmparr) |> plot_image() }
Takes an RGB array/filename and writes it to file.
ray_write_image(image, filename, clamp = TRUE, ...)
ray_write_image(image, filename, clamp = TRUE, ...)
image |
Image filename or 3-layer RGB array. |
filename |
File to write to, with filetype determined by extension. Filetype can be
|
clamp |
Default |
... |
Arguments to pass to either |
3-layer RGB array of the processed image.
if(run_documentation()){ #Write as a png tmparr = tempfile(fileext=".png") ray_read_image(dragon) |> ray_write_image(tmparr) ray_read_image(tmparr) |> plot_image() } if(run_documentation()){ #Write as a JPEG (passing quality arguments via ...) tmparr = tempfile(fileext=".jpg") ray_read_image(dragon) |> ray_write_image(tmparr, quality = 0.2) ray_read_image(tmparr) |> plot_image() } if(run_documentation()){ #Write as a tiff tmparr = tempfile(fileext=".tiff") ray_read_image(dragon) |> ray_write_image(tmparr) ray_read_image(tmparr) |> plot_image() }
if(run_documentation()){ #Write as a png tmparr = tempfile(fileext=".png") ray_read_image(dragon) |> ray_write_image(tmparr) ray_read_image(tmparr) |> plot_image() } if(run_documentation()){ #Write as a JPEG (passing quality arguments via ...) tmparr = tempfile(fileext=".jpg") ray_read_image(dragon) |> ray_write_image(tmparr, quality = 0.2) ray_read_image(tmparr) |> plot_image() } if(run_documentation()){ #Write as a tiff tmparr = tempfile(fileext=".tiff") ray_read_image(dragon) |> ray_write_image(tmparr) ray_read_image(tmparr) |> plot_image() }
Takes an image and a depth map to render the image with depth of field (i.e. similar to "Portrait Mode" in an iPhone). User can specify a custom bokeh shape, or use one of the built-in bokeh types.
render_bokeh( image, depthmap, focus = 0.5, focallength = 100, fstop = 4, filename = NULL, preview = TRUE, preview_focus = FALSE, bokehshape = "circle", bokehintensity = 1, bokehlimit = 0.8, rotation = 0, aberration = 0, gamma_correction = TRUE, progress = interactive(), ... )
render_bokeh( image, depthmap, focus = 0.5, focallength = 100, fstop = 4, filename = NULL, preview = TRUE, preview_focus = FALSE, bokehshape = "circle", bokehintensity = 1, bokehlimit = 0.8, rotation = 0, aberration = 0, gamma_correction = TRUE, progress = interactive(), ... )
image |
Image filename or 3-layer RGB array. |
depthmap |
Depth map filename or 1d array. |
focus |
Defaults |
focallength |
Default |
fstop |
Default |
filename |
Default |
preview |
Default |
preview_focus |
Default |
bokehshape |
Default |
bokehintensity |
Default |
bokehlimit |
Default |
rotation |
Default |
aberration |
Default |
gamma_correction |
Default |
progress |
Default |
... |
Additional arguments to pass to |
3-layer RGB array of the processed image.
if(run_documentation()){ #Plot the dragon plot_image(dragon) } if(run_documentation()){ #Plot the depth map plot_image(dragondepth/1500) } if(run_documentation()){ #Preview the focal plane: render_bokeh(dragon, dragondepth, focus=950, preview_focus = TRUE) } if(run_documentation()){ #Change the focal length: render_bokeh(dragon, dragondepth, focus=950, focallength=300) } if(run_documentation()){ #Add chromatic aberration: render_bokeh(dragon, dragondepth, focus=950, focallength=300, aberration = 0.5) } if(run_documentation()){ #Change the focal distance: render_bokeh(dragon, dragondepth, focus=600, focallength=300) render_bokeh(dragon, dragondepth, focus=1300, focallength=300) } if(run_documentation()){ #Change the bokeh shape to a hexagon: render_bokeh(dragon, dragondepth, bokehshape = "hex", focallength=300, focus=600) } if(run_documentation()){ #Change the bokeh intensity: render_bokeh(dragon, dragondepth, focallength=400, focus=900, bokehintensity = 1) render_bokeh(dragon, dragondepth, focallength=400, focus=900, bokehintensity = 3) } if(run_documentation()){ #Rotate the hexagonal shape: render_bokeh(dragon, dragondepth, bokehshape = "hex", rotation=15, focallength=300, focus=600) }
if(run_documentation()){ #Plot the dragon plot_image(dragon) } if(run_documentation()){ #Plot the depth map plot_image(dragondepth/1500) } if(run_documentation()){ #Preview the focal plane: render_bokeh(dragon, dragondepth, focus=950, preview_focus = TRUE) } if(run_documentation()){ #Change the focal length: render_bokeh(dragon, dragondepth, focus=950, focallength=300) } if(run_documentation()){ #Add chromatic aberration: render_bokeh(dragon, dragondepth, focus=950, focallength=300, aberration = 0.5) } if(run_documentation()){ #Change the focal distance: render_bokeh(dragon, dragondepth, focus=600, focallength=300) render_bokeh(dragon, dragondepth, focus=1300, focallength=300) } if(run_documentation()){ #Change the bokeh shape to a hexagon: render_bokeh(dragon, dragondepth, bokehshape = "hex", focallength=300, focus=600) } if(run_documentation()){ #Change the bokeh intensity: render_bokeh(dragon, dragondepth, focallength=400, focus=900, bokehintensity = 1) render_bokeh(dragon, dragondepth, focallength=400, focus=900, bokehintensity = 3) } if(run_documentation()){ #Rotate the hexagonal shape: render_bokeh(dragon, dragondepth, bokehshape = "hex", rotation=15, focallength=300, focus=600) }
Takes an matrix (or and returns the nearest distance to each TRUE.
render_boolean_distance(boolean, rescale = FALSE)
render_boolean_distance(boolean, rescale = FALSE)
boolean |
Logical matrix (or matrix of 1s and 0s), where distance will be measured to the |
rescale |
Default |
Matrix of distance values.
if(run_documentation()){ #Measure distance to plot_image(render_boolean_distance(t(volcano) > 150)) plot_image(render_boolean_distance(t(volcano) < 150)) } if(run_documentation()){ #If we want to rescale this to zero to one (to visualize like an image), set rescale=TRUE plot_image(render_boolean_distance(t(volcano) > 150,rescale=TRUE)) }
if(run_documentation()){ #Measure distance to plot_image(render_boolean_distance(t(volcano) > 150)) plot_image(render_boolean_distance(t(volcano) < 150)) } if(run_documentation()){ #If we want to rescale this to zero to one (to visualize like an image), set rescale=TRUE plot_image(render_boolean_distance(t(volcano) > 150,rescale=TRUE)) }
Transforms an image to black and white, preserving luminance.
render_bw( image, rgb_coef = c(0.2126, 0.7152, 0.0722), filename = NULL, preview = FALSE )
render_bw( image, rgb_coef = c(0.2126, 0.7152, 0.0722), filename = NULL, preview = FALSE )
image |
Image filename, 3-layer RGB array, or matrix. |
rgb_coef |
Default |
filename |
Default |
preview |
Default |
3-layer RGB resized array or matrix.
if(run_documentation()){ #Plot the image with a title dragon |> render_title("Dragon", title_offset=c(10,10), title_bar_color="black", title_size=20, title_color = "white") |> render_bw(preview = TRUE) }
if(run_documentation()){ #Plot the image with a title dragon |> render_title("Dragon", title_offset=c(10,10), title_bar_color="black", title_size=20, title_color = "white") |> render_bw(preview = TRUE) }
Clamps an image to a user-specified range
render_clamp(image, min_value = 0, max_value = 1, preview = FALSE, ...)
render_clamp(image, min_value = 0, max_value = 1, preview = FALSE, ...)
image |
Image filename or 3-layer RGB array. |
min_value |
Default |
max_value |
Default |
preview |
Default |
... |
Arguments to pass to either |
3-layer RGB array of the processed image.
if(run_documentation()){ #The rnage of the unchanged image range(dragon) } if(run_documentation()){ #Clamp the maximum and minimum values to one and zero render_clamp(dragon) |> range() }
if(run_documentation()){ #The rnage of the unchanged image range(dragon) } if(run_documentation()){ #Clamp the maximum and minimum values to one and zero render_clamp(dragon) |> range() }
Takes an image and applys a convolution operation to it, using a user-supplied or built-in kernel. Edges are calculated by limiting the size of the kernel to only that overlapping the actual image (renormalizing the kernel for the edges).
render_convolution( image, kernel = "gaussian", kernel_dim = 11, kernel_extent = 3, absolute = TRUE, min_value = NULL, filename = NULL, preview = FALSE, gamma_correction = FALSE, progress = FALSE )
render_convolution( image, kernel = "gaussian", kernel_dim = 11, kernel_extent = 3, absolute = TRUE, min_value = NULL, filename = NULL, preview = FALSE, gamma_correction = FALSE, progress = FALSE )
image |
Image filename or 3-layer RGB array. |
kernel |
Default |
kernel_dim |
Default |
kernel_extent |
Default |
absolute |
Default |
min_value |
Default |
filename |
Default |
preview |
Default |
gamma_correction |
Default |
progress |
Default |
3-layer RGB array of the processed image.
if(run_documentation()){ #Perform a convolution with the default gaussian kernel plot_image(dragon) } if(run_documentation()){ #Perform a convolution with the default gaussian kernel render_convolution(dragon, preview = TRUE) } if(run_documentation()){ #Increase the width of the kernel render_convolution(dragon, kernel = 2, kernel_dim=21,kernel_extent=6, preview = TRUE) } if(run_documentation()){ #Perform edge detection using a edge detection kernel edge = matrix(c(-1,-1,-1,-1,8,-1,-1,-1,-1),3,3) render_convolution(render_bw(dragon), kernel = edge, preview = TRUE, absolute=FALSE) } if(run_documentation()){ #Perform edge detection with Sobel matrices sobel1 = matrix(c(1,2,1,0,0,0,-1,-2,-1),3,3) sobel2 = matrix(c(1,2,1,0,0,0,-1,-2,-1),3,3,byrow=TRUE) sob1 = render_convolution(render_bw(dragon), kernel = sobel1) sob2 = render_convolution(render_bw(dragon), kernel = sobel2) sob_all = sob1 + sob2 plot_image(sob1) plot_image(sob2) plot_image(sob_all) } if(run_documentation()){ #Only perform the convolution on bright pixels (bloom) render_convolution(dragon, kernel = 5, kernel_dim=24, kernel_extent=24, min_value=1, preview = TRUE) } if(run_documentation()){ #Use a built-in kernel: render_convolution(dragon, kernel = generate_2d_exponential(falloff=2, dim=31, width=21), preview = TRUE) } if(run_documentation()){ #We can also apply this function to matrices: volcano |> image() volcano |> render_convolution(kernel=generate_2d_gaussian(sd=1,dim=31)) |> image() } if(run_documentation()){ #Use a custom kernel (in this case, an X shape): custom = diag(10) + (diag(10)[,10:1]) plot_image(custom) render_convolution(dragon, kernel = custom, preview = TRUE) }
if(run_documentation()){ #Perform a convolution with the default gaussian kernel plot_image(dragon) } if(run_documentation()){ #Perform a convolution with the default gaussian kernel render_convolution(dragon, preview = TRUE) } if(run_documentation()){ #Increase the width of the kernel render_convolution(dragon, kernel = 2, kernel_dim=21,kernel_extent=6, preview = TRUE) } if(run_documentation()){ #Perform edge detection using a edge detection kernel edge = matrix(c(-1,-1,-1,-1,8,-1,-1,-1,-1),3,3) render_convolution(render_bw(dragon), kernel = edge, preview = TRUE, absolute=FALSE) } if(run_documentation()){ #Perform edge detection with Sobel matrices sobel1 = matrix(c(1,2,1,0,0,0,-1,-2,-1),3,3) sobel2 = matrix(c(1,2,1,0,0,0,-1,-2,-1),3,3,byrow=TRUE) sob1 = render_convolution(render_bw(dragon), kernel = sobel1) sob2 = render_convolution(render_bw(dragon), kernel = sobel2) sob_all = sob1 + sob2 plot_image(sob1) plot_image(sob2) plot_image(sob_all) } if(run_documentation()){ #Only perform the convolution on bright pixels (bloom) render_convolution(dragon, kernel = 5, kernel_dim=24, kernel_extent=24, min_value=1, preview = TRUE) } if(run_documentation()){ #Use a built-in kernel: render_convolution(dragon, kernel = generate_2d_exponential(falloff=2, dim=31, width=21), preview = TRUE) } if(run_documentation()){ #We can also apply this function to matrices: volcano |> image() volcano |> render_convolution(kernel=generate_2d_gaussian(sd=1,dim=31)) |> image() } if(run_documentation()){ #Use a custom kernel (in this case, an X shape): custom = diag(10) + (diag(10)[,10:1]) plot_image(custom) render_convolution(dragon, kernel = custom, preview = TRUE) }
Takes an image and applys a convolution operation to it, using a user-supplied or built-in kernel. This function uses a fast-fourier transform and does the convolution in the frequency domain, so it should be faster for much larger kernels.
render_convolution_fft( image, kernel = "gaussian", kernel_dim = c(11, 11), kernel_extent = 3, absolute = TRUE, pad = 50, filename = NULL, preview = FALSE, gamma_correction = FALSE )
render_convolution_fft( image, kernel = "gaussian", kernel_dim = c(11, 11), kernel_extent = 3, absolute = TRUE, pad = 50, filename = NULL, preview = FALSE, gamma_correction = FALSE )
image |
Image filename or 3-layer RGB array. |
kernel |
Default |
kernel_dim |
Default |
kernel_extent |
Default |
absolute |
Default |
pad |
Default |
filename |
Default |
preview |
Default |
gamma_correction |
Default |
3-layer RGB array of the processed image.
if(run_documentation()){ #Perform a convolution with the default gaussian kernel plot_image(dragon) } if(run_documentation()){ #Perform a convolution with the default gaussian kernel render_convolution_fft(dragon, kernel=0.1,preview = TRUE) } if(run_documentation()){ #Increase the width of the kernel render_convolution_fft(dragon, kernel = 2, kernel_dim=21,kernel_extent=6, preview = TRUE) } if(run_documentation()){ #Use a built-in kernel: render_convolution_fft(dragon, kernel = generate_2d_exponential(falloff=2, dim=31, width=21), preview = TRUE) } if(run_documentation()){ #Perform edge detection edge = matrix(c(-1,-1,-1,-1,8,-1,-1,-1,-1),3,3) render_convolution_fft(render_bw(dragon), kernel = edge, preview = TRUE) } if(run_documentation()){ #Perform edge detection with Sobel matrices sobel1 = matrix(c(1,2,1,0,0,0,-1,-2,-1),3,3) sobel2 = matrix(c(1,2,1,0,0,0,-1,-2,-1),3,3,byrow=TRUE) sob1 = render_convolution_fft(render_bw(dragon), kernel = sobel1) sob2 = render_convolution_fft(render_bw(dragon), kernel = sobel2) sob_all = sob1 + sob2 plot_image(sob1) plot_image(sob2) plot_image(sob_all) } if(run_documentation()){ #We can also apply this function to matrices: volcano |> image() volcano |> render_convolution_fft(kernel=generate_2d_gaussian(sd=1,dim=31)) |> image() } if(run_documentation()){ # Because this function uses the fast-fourier transform, large kernels will be much faster # than the same size kernels in `render_convolution()` render_convolution_fft(dragon, kernel_dim = c(200,200) , preview = TRUE) } if(run_documentation()){ #Use a custom kernel (in this case, an X shape): custom = diag(10) + (diag(10)[,10:1]) #Normalize custom = custom / 20 plot_image(custom*20) render_convolution_fft(dragon, kernel = custom, preview = TRUE) }
if(run_documentation()){ #Perform a convolution with the default gaussian kernel plot_image(dragon) } if(run_documentation()){ #Perform a convolution with the default gaussian kernel render_convolution_fft(dragon, kernel=0.1,preview = TRUE) } if(run_documentation()){ #Increase the width of the kernel render_convolution_fft(dragon, kernel = 2, kernel_dim=21,kernel_extent=6, preview = TRUE) } if(run_documentation()){ #Use a built-in kernel: render_convolution_fft(dragon, kernel = generate_2d_exponential(falloff=2, dim=31, width=21), preview = TRUE) } if(run_documentation()){ #Perform edge detection edge = matrix(c(-1,-1,-1,-1,8,-1,-1,-1,-1),3,3) render_convolution_fft(render_bw(dragon), kernel = edge, preview = TRUE) } if(run_documentation()){ #Perform edge detection with Sobel matrices sobel1 = matrix(c(1,2,1,0,0,0,-1,-2,-1),3,3) sobel2 = matrix(c(1,2,1,0,0,0,-1,-2,-1),3,3,byrow=TRUE) sob1 = render_convolution_fft(render_bw(dragon), kernel = sobel1) sob2 = render_convolution_fft(render_bw(dragon), kernel = sobel2) sob_all = sob1 + sob2 plot_image(sob1) plot_image(sob2) plot_image(sob_all) } if(run_documentation()){ #We can also apply this function to matrices: volcano |> image() volcano |> render_convolution_fft(kernel=generate_2d_gaussian(sd=1,dim=31)) |> image() } if(run_documentation()){ # Because this function uses the fast-fourier transform, large kernels will be much faster # than the same size kernels in `render_convolution()` render_convolution_fft(dragon, kernel_dim = c(200,200) , preview = TRUE) } if(run_documentation()){ #Use a custom kernel (in this case, an X shape): custom = diag(10) + (diag(10)[,10:1]) #Normalize custom = custom / 20 plot_image(custom*20) render_convolution_fft(dragon, kernel = custom, preview = TRUE) }
Takes an RGB array/filename and adds an image overlay.
render_image_overlay( image, image_overlay = NULL, rescale_original = FALSE, alpha = NULL, filename = NULL, preview = FALSE )
render_image_overlay( image, image_overlay = NULL, rescale_original = FALSE, alpha = NULL, filename = NULL, preview = FALSE )
image |
Image filename or 3-layer RGB array. |
image_overlay |
Default |
rescale_original |
Default |
alpha |
Default |
filename |
Default |
preview |
Default |
3-layer RGB array of the processed image.
if(run_documentation()){ #Plot the dragon plot_image(dragon) } if(run_documentation()){ #Add an overlay of a red semi-transparent circle: circlemat = generate_2d_disk(min(dim(dragon)[1:2])) circlemat = circlemat/max(circlemat) #Create RGBA image, with a transparency of 0.5 rgba_array = array(1, dim=c(nrow(circlemat),ncol(circlemat),4)) rgba_array[,,1] = circlemat rgba_array[,,2] = 0 rgba_array[,,3] = 0 dragon_clipped = dragon dragon_clipped[dragon_clipped > 1] = 1 render_image_overlay(dragon_clipped, image_overlay = rgba_array, alpha=0.5, preview = TRUE) }
if(run_documentation()){ #Plot the dragon plot_image(dragon) } if(run_documentation()){ #Add an overlay of a red semi-transparent circle: circlemat = generate_2d_disk(min(dim(dragon)[1:2])) circlemat = circlemat/max(circlemat) #Create RGBA image, with a transparency of 0.5 rgba_array = array(1, dim=c(nrow(circlemat),ncol(circlemat),4)) rgba_array[,,1] = circlemat rgba_array[,,2] = 0 rgba_array[,,3] = 0 dragon_clipped = dragon dragon_clipped[dragon_clipped > 1] = 1 render_image_overlay(dragon_clipped, image_overlay = rgba_array, alpha=0.5, preview = TRUE) }
Reorients an image or matrix. Transformations are applied in this order: x, y, and transpose.
render_reorient( image, flipx = FALSE, flipy = FALSE, transpose = FALSE, filename = NULL, preview = FALSE )
render_reorient( image, flipx = FALSE, flipy = FALSE, transpose = FALSE, filename = NULL, preview = FALSE )
image |
Image filename, 3-layer RGB array, or matrix. |
flipx |
Default |
flipy |
Default |
transpose |
Default |
filename |
Default |
preview |
Default |
3-layer RGB reoriented array or matrix.
if(run_documentation()){ #Original orientation plot_image(dragon) } if(run_documentation()){ #Flip the dragon image horizontally dragon |> render_reorient(flipx = TRUE) |> plot_image() } if(run_documentation()){ #Flip the dragon image vertically dragon |> render_reorient(flipy = TRUE) |> plot_image() } if(run_documentation()){ #Transpose the dragon image dragon |> render_reorient(transpose = TRUE) |> plot_image() }
if(run_documentation()){ #Original orientation plot_image(dragon) } if(run_documentation()){ #Flip the dragon image horizontally dragon |> render_reorient(flipx = TRUE) |> plot_image() } if(run_documentation()){ #Flip the dragon image vertically dragon |> render_reorient(flipy = TRUE) |> plot_image() } if(run_documentation()){ #Transpose the dragon image dragon |> render_reorient(transpose = TRUE) |> plot_image() }
Resizes an image or a matrix, using bilinear interpolation.
render_resized( image, mag = 1, dims = NULL, filename = NULL, preview = FALSE, method = "tri" )
render_resized( image, mag = 1, dims = NULL, filename = NULL, preview = FALSE, method = "tri" )
image |
Image filename, 3-layer RGB array, or matrix. |
mag |
Default |
dims |
Default |
filename |
Default |
preview |
Default |
method |
Default |
3-layer RGB resized array or matrix.
if(run_documentation()){ #Plot the image with a title dragon |> render_title("Dragon", title_offset=c(10,10), title_bar_color="black", title_size=20, title_color = "white") |> plot_image() } if(run_documentation()){ #Half of the resolution render_resized(dragon, mag = 1/2) |> render_title("Dragon (half res)", title_offset=c(5,5), title_bar_color="black", title_size=10, title_color = "white") |> plot_image() } if(run_documentation()){ #Double the resolution render_resized(dragon, mag = 2) |> render_title("Dragon (2x res)", title_offset=c(20,20), title_bar_color="black", title_size=40, title_color = "white") |> plot_image() } if(run_documentation()){ #Specify the exact resulting dimensions render_resized(dragon, dim = c(320,160)) |> render_title("Dragon (custom size)", title_offset=c(10,10), title_bar_color="black", title_size=20, title_color = "white") |> plot_image() }
if(run_documentation()){ #Plot the image with a title dragon |> render_title("Dragon", title_offset=c(10,10), title_bar_color="black", title_size=20, title_color = "white") |> plot_image() } if(run_documentation()){ #Half of the resolution render_resized(dragon, mag = 1/2) |> render_title("Dragon (half res)", title_offset=c(5,5), title_bar_color="black", title_size=10, title_color = "white") |> plot_image() } if(run_documentation()){ #Double the resolution render_resized(dragon, mag = 2) |> render_title("Dragon (2x res)", title_offset=c(20,20), title_bar_color="black", title_size=40, title_color = "white") |> plot_image() } if(run_documentation()){ #Specify the exact resulting dimensions render_resized(dragon, dim = c(320,160)) |> render_title("Dragon (custom size)", title_offset=c(10,10), title_bar_color="black", title_size=20, title_color = "white") |> plot_image() }
Generates an image which tightly fits text.
render_text_image( text, lineheight = 1, color = "black", size = 12, font = "sans", just = "left", background_color = "white", background_alpha = 1, use_ragg = TRUE, width = NA, height = NA, filename = NULL, check_text_width = TRUE, check_text_height = FALSE, preview = FALSE )
render_text_image( text, lineheight = 1, color = "black", size = 12, font = "sans", just = "left", background_color = "white", background_alpha = 1, use_ragg = TRUE, width = NA, height = NA, filename = NULL, check_text_width = TRUE, check_text_height = FALSE, preview = FALSE )
text |
Text to turn into an image. |
lineheight |
Default |
color |
Default |
size |
Default |
font |
Default |
just |
Default |
background_color |
Default |
background_alpha |
Default |
use_ragg |
Default |
width |
Default |
height |
Default |
filename |
Default |
check_text_width |
Default |
check_text_height |
Default |
preview |
Default |
A 3-layer RGB array of the processed image if filename = NULL
and preview = FALSE
.
Otherwise, writes the image to the specified file or displays it if preview = TRUE
.
if (run_documentation()) { #Generate an image of some text render_text_image("Some text", preview = TRUE) } if (run_documentation()) { #Change the font size render_text_image("Some text", size = 100, preview = TRUE) } if (run_documentation()) { #Change the font color render_text_image("Some text", size = 100, color="red",preview = TRUE) } if (run_documentation()) { #Change the background color and transparency render_text_image("Some text", size = 50, color="purple", background_color="purple", background_alpha = 0.5, preview = TRUE) } if (run_documentation()) { #Plot an emoji with the agg device. render_text_image("😀🚀", size = 50, color="purple", use_ragg = TRUE, background_alpha = 0, preview = TRUE) } if (run_documentation()) { # Plot an emoji with the agg device and adjust the height and width (which # is on by default) to be a tight fit. render_text_image("😀🚀", size = 50, color="purple", use_ragg = TRUE, background_alpha = 0, check_text_width = TRUE, check_text_height = TRUE, preview = TRUE) }
if (run_documentation()) { #Generate an image of some text render_text_image("Some text", preview = TRUE) } if (run_documentation()) { #Change the font size render_text_image("Some text", size = 100, preview = TRUE) } if (run_documentation()) { #Change the font color render_text_image("Some text", size = 100, color="red",preview = TRUE) } if (run_documentation()) { #Change the background color and transparency render_text_image("Some text", size = 50, color="purple", background_color="purple", background_alpha = 0.5, preview = TRUE) } if (run_documentation()) { #Plot an emoji with the agg device. render_text_image("😀🚀", size = 50, color="purple", use_ragg = TRUE, background_alpha = 0, preview = TRUE) } if (run_documentation()) { # Plot an emoji with the agg device and adjust the height and width (which # is on by default) to be a tight fit. render_text_image("😀🚀", size = 50, color="purple", use_ragg = TRUE, background_alpha = 0, check_text_width = TRUE, check_text_height = TRUE, preview = TRUE) }
Adds a title with optional styling and a title bar to an image.
The image can be previewed or saved to a file. Supports both the grid
-based
method and (deprecated) magick
package for rendering the title.
render_title( image, title_text = "", title_size = 30, title_offset = rep(title_size/2, 2), title_lineheight = 1, title_color = "black", title_font = "Arial", title_style = "plain", title_bar_color = NA, title_bar_alpha = 0.5, title_bar_width = NULL, title_position = NA, title_just = "left", use_magick = FALSE, filename = NULL, preview = FALSE )
render_title( image, title_text = "", title_size = 30, title_offset = rep(title_size/2, 2), title_lineheight = 1, title_color = "black", title_font = "Arial", title_style = "plain", title_bar_color = NA, title_bar_alpha = 0.5, title_bar_width = NULL, title_position = NA, title_just = "left", use_magick = FALSE, filename = NULL, preview = FALSE )
image |
Image filename or 3-layer RGB array. Specifies the image to process. |
title_text |
Default |
title_size |
Default |
title_offset |
Default |
title_lineheight |
Default |
title_color |
Default |
title_font |
Default |
title_style |
Default |
title_bar_color |
Default |
title_bar_alpha |
Default |
title_bar_width |
Default |
title_position |
Default |
title_just |
Default |
use_magick |
Default |
filename |
Default |
preview |
Default |
A 3-layer RGB array of the processed image if filename = NULL
and preview = FALSE
.
Otherwise, writes the image to the specified file or displays it if preview = TRUE
.
The use_magick
parameter and all functionality tied to the magick
package are
planned for deprecation. It is recommended to use the grid
-based method for
future compatibility.
if(run_documentation()){ #Plot the dragon render_title(dragon, preview = TRUE, title_text = "Dragon", title_size=20) } if(run_documentation()){ #That's hard to see--let's add a title bar: render_title(dragon, preview = TRUE, title_text = "Dragon", title_size=20, title_bar_color="white") } if(run_documentation()){ #Change the width of the bar: render_title(dragon, preview = TRUE, title_text = "Dragon", title_size=20, title_bar_color="white", title_offset = c(8,8)) } if(run_documentation()){ #The width of the bar will also automatically adjust for newlines: render_title(dragon, preview = TRUE, title_text = "Dragon\n(Blue)", title_size=20, title_bar_color="white") } if(run_documentation()){ #Change the color and title color: render_title(dragon, preview = TRUE, title_text = "Dragon", title_size=20, title_bar_color="red", title_color = "white") } if(run_documentation()){ #Change the transparency: render_title(dragon, preview = TRUE, title_text = "Dragon", title_size=20, title_bar_alpha = 0.8, title_bar_color="red", title_color = "white") } if(run_documentation()){ #Read directly from a file temp_image = tempfile(fileext = ".png") ray_write_image(dragon, temp_image) render_title(temp_image, preview = TRUE, title_text = "Dragon", title_size=20, title_bar_alpha = 0.8, title_bar_color="red", title_color = "white") }
if(run_documentation()){ #Plot the dragon render_title(dragon, preview = TRUE, title_text = "Dragon", title_size=20) } if(run_documentation()){ #That's hard to see--let's add a title bar: render_title(dragon, preview = TRUE, title_text = "Dragon", title_size=20, title_bar_color="white") } if(run_documentation()){ #Change the width of the bar: render_title(dragon, preview = TRUE, title_text = "Dragon", title_size=20, title_bar_color="white", title_offset = c(8,8)) } if(run_documentation()){ #The width of the bar will also automatically adjust for newlines: render_title(dragon, preview = TRUE, title_text = "Dragon\n(Blue)", title_size=20, title_bar_color="white") } if(run_documentation()){ #Change the color and title color: render_title(dragon, preview = TRUE, title_text = "Dragon", title_size=20, title_bar_color="red", title_color = "white") } if(run_documentation()){ #Change the transparency: render_title(dragon, preview = TRUE, title_text = "Dragon", title_size=20, title_bar_alpha = 0.8, title_bar_color="red", title_color = "white") } if(run_documentation()){ #Read directly from a file temp_image = tempfile(fileext = ".png") ray_write_image(dragon, temp_image) render_title(temp_image, preview = TRUE, title_text = "Dragon", title_size=20, title_bar_alpha = 0.8, title_bar_color="red", title_color = "white") }
Takes an RGB array/filename and adds a camera vignette effect.
render_vignette( image, vignette = 0.5, color = "#000000", radius = 1.3, filename = NULL, preview = FALSE )
render_vignette( image, vignette = 0.5, color = "#000000", radius = 1.3, filename = NULL, preview = FALSE )
image |
Image filename or 3-layer RGB array. |
vignette |
Default |
color |
Default |
radius |
Default |
filename |
Default |
preview |
Default |
3-layer RGB array of the processed image.
if(run_documentation()){ #Plot the dragon plot_image(dragon) } if(run_documentation()){ #Add a vignette effect: render_vignette(dragon, preview = TRUE, vignette = 0.5) } if(run_documentation()){ #Darken the vignette effect: render_vignette(dragon, preview = TRUE, vignette = 1) } if(run_documentation()){ #Change the radius: render_vignette(dragon, preview = TRUE, vignette = 1, radius=1.5) render_vignette(dragon, preview = TRUE, vignette = 1, radius=0.5) } if(run_documentation()){ #Change the color: render_vignette(dragon, preview = TRUE, vignette = 1, color="white") } if(run_documentation()){ #Increase the width of the blur by 50%: render_vignette(dragon, preview = TRUE, vignette = c(1,1.5)) }
if(run_documentation()){ #Plot the dragon plot_image(dragon) } if(run_documentation()){ #Add a vignette effect: render_vignette(dragon, preview = TRUE, vignette = 0.5) } if(run_documentation()){ #Darken the vignette effect: render_vignette(dragon, preview = TRUE, vignette = 1) } if(run_documentation()){ #Change the radius: render_vignette(dragon, preview = TRUE, vignette = 1, radius=1.5) render_vignette(dragon, preview = TRUE, vignette = 1, radius=0.5) } if(run_documentation()){ #Change the color: render_vignette(dragon, preview = TRUE, vignette = 1, color="white") } if(run_documentation()){ #Increase the width of the blur by 50%: render_vignette(dragon, preview = TRUE, vignette = c(1,1.5)) }
This function determines if the examples are being run in pkgdown. It is not meant to be called by the user.
run_documentation()
run_documentation()
Boolean value.
# See if the documentation should be run. run_documentation()
# See if the documentation should be run. run_documentation()