fcas80
July 24, 2023, 3:53am
1
Hi,
Is there a way to upload a photo into R, superimpose it on a rectangular grid, and then use coordinates of the grid to measure various lengths in the photo?
For example, here is a photo of cartoon character Marge Simpson. How would I use R to measure the height of hair and the length from her hairline to her toes?
fcas80
July 24, 2023, 1:55pm
3
Technocrat,
Nice! One more package I had never heard of.
I am not understanding the offset parameter, and I am not getting the image to fit within the first quadrant of a grid so that I can read both x and y axes
library(magick)
marge <- image_read("marge.jpg")
print(marge)
fig <- image_graph(width = 300, height = 605, res = 96)
ggplot2::qplot(xlim=c(0,300), ylim=c(0,650))
out <- image_composite(fig, marge, offset = "+70+30")
print(out)
If I understand your intent, here's an alternative
library(ggplot2)
library(magick)
#> Linking to ImageMagick 6.9.12.3
#> Enabled features: cairo, fontconfig, freetype, heic, lcms, pango, raw, rsvg, webp
#> Disabled features: fftw, ghostscript, x11
library(grid)
# Read the Frink image
frink <- image_read("https://jeroen.github.io/images/frink.png")
frink <- image_scale(frink, "300")
# Create a ggplot2 plot
p <- ggplot() +
xlim(0, 300) +
ylim(0, 606) +
theme_minimal()
# Convert the Frink image to a raster object
frink_raster <- as.raster(frink)
# Add the Frink image to the ggplot2 plot
p +
annotation_custom(
grob = rasterGrob(frink_raster),
xmin = 0, xmax = 300,
ymin = -10, ymax = 600
)
Created on 2023-07-25 with reprex v2.0.2
fcas80
July 25, 2023, 2:30pm
5
Technocrat: Perfect, thank you!
fcas80
July 25, 2023, 3:23pm
6
OK, one more thing, where would I put a statement to add a line segment such as
geom_segment(aes(x = 150, y = 200, xend = 175, yend = 200,
color = "black"))
I would think within annotation_custom, but it didn't take it.
system
Closed
August 1, 2023, 3:23pm
7
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.