0

I am tracking objects in images. I want to show my results on a plot. So I use image2D() function to plot two consecutive images and label the objects by numbers then shows how they maps from image1 to image2 as 16 => 0, 17 => 16, 18 => 0, 19 => 17, 20 => 18, 21 => 20, 22 => 19, 23 => 22, 24 => 23, 25 => 25, 26 => 24 0 = object died.

Now, I want to show arrow from n^th object (say 26) in image1 to m^th in image2 (i.e. 24) for visually intuitive presentation. Is it possible to draw an arrow from given coordinates in plot1 p1(x1, y1) to coordinates in plot2 p2(x2, y2)?

enter image description here

EDIT: Adding a sample script showing dummy image plots.

library(plot3D)

#we will create dummy images first
img1 <- matrix(0, nrow = 100, ncol = 100)
img2 <- matrix(0, nrow = 100, ncol = 100)

#add an objects to img1 and img2
img1[41:44, 31:35] <- rnorm(20) #object1 in image 1
img2[44:47, 33:37] <- rnorm(20) #object1 in image 2

img1[11:14, 71:75] <- rnorm(20) #object2 in image 1
img2[14:17, 73:77] <- rnorm(20) #object2 in image 2

# cordinates are x and y
x <- 1:100
y <- 1:100

#now plot them
par(mfrow=c(1,2))
image2D(img1, x, y)
image2D(img2, x, y)

Now, I want to plot two arrows from objects in image1 to objects in image2 to show association.

XtrimA
  • 143
  • 11
  • It would be easier to help if you provided a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data and the code you are using to generate the plots. – MrFlick May 02 '16 at 03:45
  • Please provide a reproducible example.... – Cyrus Mohammadian May 02 '16 at 03:49
  • The challenge is that the `lines`, segments` and `arrows` functions will only span the addressed plot region. So can you accept arrows that point to an annotation label in the middle? – IRTFM May 02 '16 at 04:53
  • @42 Yes, as long as I can tell my audience that "the arrows show how my technique maps objects from image1 to image2", without confusing them. I want them to get a feel that technique is working/not working. – XtrimA May 02 '16 at 05:23

1 Answers1

1

You could use grid graphics' limited facilities to draw lines between different viewports,

library(grid)
library(gridGraphics)

par(mfrow=c(1,2), xaxs="i", yaxs="i")
plot(1:10, 1:10, xlim=c(0,10), ylim=c(0,10))
plot(1:10, 1:10, xlim=c(0,10), ylim=c(0,10))

grid.echo()
grid.ls(viewports = T, grobs = F, flatten = T, recursive = F)
seekViewport("graphics-plot-1")
grid.rect(gp=gpar(col="red", fill=NA))
grid.move.to(unit(8/10, "npc"), unit(8/10, "npc"))
upViewport()
seekViewport("graphics-plot-2")
grid.rect(gp=gpar(col="red", fill=NA))
grid.line.to(unit(4/10, "npc"), unit(4/10, "npc"), gp=gpar(lty=2), arrow = arrow())

enter image description here

baptiste
  • 75,767
  • 19
  • 198
  • 294
  • Thanks @baptiste ! I can see that your solution is working for your plots. Do you think I can do it easily with my image2D plots? – XtrimA May 02 '16 at 07:16