Для изображения JPEG вы можете использовать библиотеку jpeg и библиотека ggplot2 .
Обычно я считаю полезным иметь градуированную ось в пикселях и вертикальную ось.идет в положительном направлении вниз, и изображение сохраняет свое первоначальное соотношение сторон.Таким образом, я могу напрямую подавать R с выводом, полученным с помощью алгоритма компьютерного зрения, например, алгоритм может обнаружить пулевое отверстие и извлечь координаты отверстия из изображения цели стрельбы, а затем R может построить двухмерную гистограмму, используя целевое изображение в качестве фона.
Мой код основан на коде Крестителя , найденного по адресу https://stackoverflow.com/a/16418186/15485
library(ggplot2)
library(jpeg)
img <- readJPEG("bersaglio.jpg") # http://www.tiropratico.com/bersagli/forme/avancarica.jpg
h<-dim(img)[1] # image height
w<-dim(img)[2] # image width
df<-data.frame(x=rnorm(100000,w/1.99,w/100),y=rnorm(100000,h/2.01,h/97))
plot(ggplot(df, aes(x,y)) +
annotation_custom(grid::rasterGrob(img, width=unit(1,"npc"), height=unit(1,"npc")), 0, w, 0, -h) + # The minus is needed to get the y scale reversed
scale_x_continuous(expand=c(0,0),limits=c(0,w)) +
scale_y_reverse(expand=c(0,0),limits=c(h,0)) + # The y scale is reversed because in image the vertical positive direction is typically downward
# Also note the limits where h>0 is the first parameter.
coord_equal() + # To keep the aspect ratio of the image.
stat_bin2d(binwidth=2,aes(fill = ..density..)) +
scale_fill_gradient(low = "dark red", high = "red")
)
![enter image description here](https://i.stack.imgur.com/5oukD.png)
df<-data.frame(x=rnorm(100000,100,w/70),y=rnorm(100000,400,h/100))
plot(ggplot(df, aes(x,y)) +
annotation_custom(grid::rasterGrob(img, width=unit(1,"npc"), height=unit(1,"npc")), 0, w, 0, -h) + # The minus is needed to get the y scale reversed
scale_x_continuous(expand=c(0,0),limits=c(0,w)) +
scale_y_reverse(expand=c(0,0),limits=c(h,0)) + # The y scale is reversed because in image the vertical positive direction is typically downward
# Also note the limits where h>0 is the first parameter.
coord_equal() + # To keep the aspect ratio of the image.
stat_bin2d(binwidth=2,aes(fill = ..density..)) +
scale_fill_gradient(low = "dark red", high = "red")
)
![enter image description here](https://i.stack.imgur.com/NVwuu.png)