Rick Turner
2013-Oct-21 13:57 UTC
[R] Problem with coordinates when trying to draw lines into a raster (image) file
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"))
Adams, Jean
2013-Oct-24 17:45 UTC
[R] Problem with coordinates when trying to draw lines into a raster (image) file
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 <rickturner@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]]
Adams, Jean
2013-Oct-28 11:59 UTC
[R] Problem with coordinates when trying to draw lines into a raster (image) file
Rick, I see a couple errors in my code ... you will need to attach the MASS package for the eqscplot() function and that function's first arguments should be any coordinates, I use (1, 1) library(jpeg) library(MASS) # 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(1, 1, 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") Jean On Mon, Oct 28, 2013 at 4:12 AM, Rick Turner <rickturner@btconnect.com>wrote:> I forgot to mention that I get an error from your code - the 'lines()' > command fails with the error 'object px.x not found'. I am not sure why > (yet) but need to dig into it... sigh. > > Rick > >[[alternative HTML version deleted]]