Upload photo, superimpose on rectangular grid


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?


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

marge <- image_read("marge.jpg")
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")

If I understand your intent, here's an alternative

#> Linking to ImageMagick
#> Enabled features: cairo, fontconfig, freetype, heic, lcms, pango, raw, rsvg, webp
#> Disabled features: fftw, ghostscript, x11

# 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) +

# Convert the Frink image to a raster object
frink_raster <- as.raster(frink)

# Add the Frink image to the ggplot2 plot
p +
    grob = rasterGrob(frink_raster),
    xmin = 0, xmax = 300,
    ymin = -10, ymax = 600

Created on 2023-07-25 with reprex v2.0.2

Technocrat: Perfect, thank you!

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.

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.