View source: R/surreal-image.R
| surreal_image | R Documentation |
This function loads an image file, extracts pixel coordinates based on a brightness threshold, and applies the surreal method to create a dataset where the image appears in the residual plot.
surreal_image(
image_path,
mode = "auto",
threshold = NULL,
max_points = NULL,
invert_y = TRUE,
R_squared = 0.3,
p = 5,
n_add_points = 40,
max_iter = 100,
tolerance = 0.01,
verbose = FALSE
)
image_path |
Character. Path to an image file or a URL (PNG, JPEG, BMP, TIFF, or SVG). |
mode |
Character. Either |
threshold |
Numeric or |
max_points |
Integer or |
invert_y |
Logical. If |
R_squared |
Numeric. Desired R-squared value. Default is 0.3. |
p |
Integer. Desired number of columns for matrix X. Default is 5. |
n_add_points |
Integer. Number of points to add in border transformation. Default is 40. |
max_iter |
Integer. Maximum number of iterations for convergence. Default is 100. |
tolerance |
Numeric. Criteria for detecting convergence and stopping optimization early. Default is 0.01. |
verbose |
Logical. If TRUE, prints progress information. Default is FALSE. |
By default, all parameters are automatically detected:
mode: Detected from image histogram (dark subject on light background or vice versa)
threshold: Calculated using Otsu's method to optimally separate foreground/background
max_points: Estimated based on image dimensions (2000-5000 points)
You can override any of these by specifying explicit values.
Input Support:
Local file paths
URLs (http:// or https://) - images are downloaded to a temporary file
Format Support:
PNG: Supported via the png package (included)
JPEG: Requires the jpeg package
BMP: Requires the bmp package
TIFF: Requires the tiff package
SVG: Requires the rsvg package (renders vector graphics to bitmap)
A data.frame containing the results of the surreal method
application with columns y, X1, X2, ..., Xp.
surreal() for details on the surreal method parameters.
surreal_text() for embedding text instead of images.
## Not run:
# Simplest usage - everything auto-detected
result <- surreal_image("https://www.r-project.org/logo/Rlogo.png")
model <- lm(y ~ ., data = result)
plot(model$fitted, model$residuals, pch = 16)
# Override specific parameters
result <- surreal_image("image.png", mode = "dark", threshold = 0.3)
# Use all points (no downsampling)
result <- surreal_image("image.png", max_points = Inf)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.