[R] Problem with coordinates when trying to draw lines into a raster (image) file

Rick Turner rickturner at btconnect.com
Mon Oct 21 15:57:34 CEST 2013


   Hi All,

   I am struggling with something, and could use some help

   I have a scenario where I need to draw lines onto a base image using R –
   briefly, the image has what amounts to an outline ‘map’ of locations, and
   the lines will correspond to ‘routes’ between two locations.

   The locations are known in terms of image pixel coordinates – let’s call
   them (px1, py1) and (px2, py2), but when I try and plot a line into the
   image using these coordinates, the visual positions are incorrect – the
   start and end points of the line are offset from the desired position, and
   the amount of offset changes as I resize the window. I've tried such things
   as normalising them into the [0,1] range used by the viewport but this does
   not correct the problem.

   So,  I  figured  that I must have made some mistake with my scaling of
   coordinates from image to viewport, but I cannot find where or what. I’ve
   fiddled around a bit (well, a lot!) but cannot get the desired result. So,
   it is time to ask for help, hence this message….

   Any  suggestions  gratefully  received…   I’ve done a fair amount of R
   programming, but have not used these extended graphics capabilities much at
   all, so I really am getting frustrated....

   Regards and thanks in advance,
   Rick

   ----------------------------------------------------------------------------
   --------------------------------------------------

   The code segment in question is:

   # load packages

   library(jpeg)

   library(grid)


   # read the image file

   baseimg <- readJPEG("loc_map.jpg", native=FALSE)

   xsize <- ncol(baseimg)                   # Get image size – this one is 1344
   px wide

   ysize <- nrow(baseimg)                 # and 1008 px high


   # create a viewport

   xrange <- c(0, xsize)                        # set up the viewport range to
   match the image size

   yrange <- c(0, ysize)

   vp  <-  viewport(x=0.5,  y=0.5,  width=0.9, height=0.9, xscale=xrange,
   yscale=yrange)

   pushViewport(vp)

   grid.rect(gp=gpar(lty="dashed"))        # draw a dashed line around it.


   # display the base image

   grid.raster(baseimg)


   # First location – image pixel coordinates (748, 177). Normalise these to
   [0,1] to

   # match the viewpoint coordinate scheme. Note that we need to invert the

   # y coordinate as R coords run from bottom up, but image ones are top down

   px1 <- (748/xsize)                                            # 748/1344 ~=
   0.556, so in range [0,1]

   py1 <- (1.0 - (177/ysize))                               # 1-(177/1008) ~=
   0.824, so also in range [0,1]


   # position of the St Johns Hill enterance (image coords

   # [769, 892]) normalised to the viewport

     x2 <- (769/xsize)

     y2 <- (1.0 - (892/ysize))


     # draw a line from pixel (px1,py1) to pixel (px2,py2) in blue

     xx <- c(px1, px2)

     yy <- c(py1, py2)

     grid.lines(xx, yy, gp=gpar(col="blue"))


More information about the R-help mailing list