Provides useful conversions between size (cm), number of pixels (px) and dots per inch (dpi).
dpi_to_cm()
converts a known dpi value to centimeters.cm_to_dpi()
converts a known centimeter values to dpi.pixels_to_cm()
converts the number of pixels to centimeters, given a known resolution (dpi).cm_to_pixels()
converts a distance (cm) to number of pixels, given a known resolution (dpi).distance()
Computes the distance between two points in an image based on the Pythagorean theorem.dpi()
An interactive function to compute the image resolution given a known distance informed by the user. See more information in the Details section.npixels()
returns the number of pixels of an image.
Usage
dpi_to_cm(dpi)
cm_to_dpi(cm)
pixels_to_cm(px, dpi)
cm_to_pixels(cm, dpi)
npixels(img)
dpi(img, viewer = get_pliman_viewer(), downsample = NULL, max_pixels = 1e+06)
distance(
img,
viewer = get_pliman_viewer(),
downsample = NULL,
max_pixels = 1e+06
)
Arguments
- dpi
The image resolution in dots per inch.
- cm
The size in centimeters.
- px
The number of pixels.
- img
An image object.
- viewer
The viewer option. If not provided, the value is retrieved using
get_pliman_viewer()
. This option controls the type of viewer to use for interactive plotting. The available options are "base" and "mapview". If set to "base", the base R graphics system is used for interactive plotting. If set to "mapview", the mapview package is used. To set this argument globally for all functions in the package, you can use theset_pliman_viewer()
function. For example, you can runset_pliman_viewer("mapview")
to set the viewer option to "mapview" for all functions.- downsample
integer; for each dimension the number of pixels/lines/bands etc that will be skipped; Defaults to
NULL
, which will find the best downsampling factor to approximate themax_pixels
value.- max_pixels
integer > 0. Maximum number of cells to use for the plot. If
max_pixels < npixels(img)
, regular sampling is used before plotting.
Value
dpi_to_cm()
,cm_to_dpi()
,pixels_to_cm()
, andcm_to_pixels()
return a numeric value or a vector of numeric values if the input data is a vector.dpi()
returns the computed dpi (dots per inch) given the known distance informed in the plot.
Details
dpi()
only run in an interactive section. To compute the image
resolution (dpi) the user must use the left button mouse to create a line
of known distance. This can be done, for example, using a template with
known distance in the image (e.g., la_leaves.jpg
).
Author
Tiago Olivoto tiagoolivoto@gmail.com
Examples
library(pliman)
# Convert dots per inch to centimeter
dpi_to_cm(c(1, 2, 3))
#> [1] 2.5400000 1.2700000 0.8466667
# Convert centimeters to dots per inch
cm_to_dpi(c(1, 2, 3))
#> [1] 0.3937008 0.7874016 1.1811024
# Convert centimeters to number of pixels with resolution of 96 dpi.
cm_to_pixels(c(1, 2, 3), 96)
#> [1] 37.79528 75.59055 113.38583
# Convert number of pixels to cm with resolution of 96 dpi.
pixels_to_cm(c(1, 2, 3), 96)
#> [1] 0.02645833 0.05291667 0.07937500
if(isTRUE(interactive())){
#### compute the dpi (dots per inch) resolution ####
# only works in an interactive section
# objects_300dpi.jpg has a known resolution of 300 dpi
img <- image_pliman("objects_300dpi.jpg")
# Higher square: 10 x 10 cm
# 1) Run the function dpi()
# 2) Use the left mouse button to create a line in the higher square
# 3) Declare a known distance (10 cm)
# 4) See the computed dpi
dpi(img)
img2 <- image_pliman("la_leaves.jpg")
# square leaf sample (2 x 2 cm)
dpi(img2)
}