Rick, This uses a different approach than what you propose, but it gets the job done and perhaps you will find it helpful.
Jean library(jpeg) # download image from internet for use in example # http://www.tootsie.com/wallpaper/wp_concord2_1344x1008.jpg img <- readJPEG("c:/temp/wp_concord2_1344x1008.jpg") # define location of two yellow gumballs, could use locator() to get these coordinates px.x <- c(172, 576) px.y <- c(433, 657) # set up plot par(xaxs="i", yaxs="i") eqscplot(x, y, type="n", xlim=c(0, 1334), ylim=c(0, 1008)) # add image rasterImage(img, 0, 0, 1334, 1008) # add line connecting two yellow gumballs lines(px.x, px.y, lwd=5, col="yellow") On Mon, Oct 21, 2013 at 8:57 AM, Rick Turner <ricktur...@btconnect.com>wrote: > > 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")) > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]]
______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.