【问题标题】:Conversion of objects position in pixel to geographical coordinates将像素中的对象位置转换为地理坐标
【发布时间】:2020-07-06 13:20:00
【问题描述】:

我在 98 宽 x 81 高的 *jpg 图像 (sample_7958) 中有一些以像素为单位的对象位置 (left_x,top_y,width,height):

im.name<-rep("sample_7958",3)
obj<-c(1,2,3)
left_x<-c(2,56,61)
top_y<-c(-0,23,37)
width<-c(9,15,32)
height<-c(8,14,23)
total_im_width<-c(98,98,98)
total_im_height<-c(81,81,81)
im.ds<-data.frame(cbind(im.name,obj,left_x,top_y,width,height,total_im_width,total_im_height))

im.ds
      im.name obj left_x top_y width height total_width total_height
1 sample_7958   1      2     0     9      8          98           81
2 sample_7958   2     56    23    15     14          98           81
3 sample_7958   3     61    37    32     23          98           81

我知道在 *tiff 的原始图像中,图像的中心具有X,Y(803958,7674280) 坐标和0.05686330017443669976,-0.05686329721392673064 像素大小,那么我想找到任何方法来转换并在im.ds 中创建新列每个对象(1 到 3)的 X、Y 边界地理坐标(Xmin, Xmax, Ymin, Ymax)。这可能吗?

【问题讨论】:

    标签: r coordinates raster r-raster coordinate-systems


    【解决方案1】:

    是的,我认为这只是简单的算术:

    x_centre     <- 803958
    y_centre     <- 7674280
    image_height <- 81
    image_width  <- 98
    pixel        <- 0.05686330017443669976
    
    y0 <- y_centre + pixel * (image_height) / 2
    x0 <- x_centre - pixel * (image_width) / 2
    
    im.ds$Xmin <- left_x * pixel + x0
    im.ds$Xmax <- (left_x + width) * pixel + x0
    im.ds$Ymax <- y0 - top_y * pixel
    im.ds$Ymin <- y0 - (top_y + height) * pixel
    
    im.ds
    #>       im.name obj left_x top_y width height total_im_width total_im_height
    #> 1 sample_7958   1      2     0     9      8             98              81
    #> 2 sample_7958   2     56    23    15     14             98              81
    #> 3 sample_7958   3     61    37    32     23             98              81
    #>       Xmin     Xmax    Ymax    Ymin
    #> 1 803955.3 803955.8 7674282 7674282
    #> 2 803958.4 803959.3 7674281 7674280
    #> 3 803958.7 803960.5 7674280 7674279
    

    也许绘制结果有助于可视化:

    library(ggplot2)
    
    ggplot(im.ds, aes(Xmin, Ymin)) + 
      geom_segment(aes(xend = Xmax, yend = Ymin)) + 
      geom_segment(aes(x = Xmax, xend = Xmax, yend = Ymax)) + 
      geom_segment(aes(x = Xmax, xend = Xmin, y = Ymax, yend = Ymax)) + 
      geom_segment(aes(xend = Xmin, y = Ymax, yend = Ymin)) + 
      labs(x = "X", y = "Y") +
      coord_cartesian(xlim = c(x0, x0 + image_width * pixel),
                      ylim = c(y0 - pixel * image_height, y0))
    

    reprex package (v0.3.0) 于 2020-07-06 创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-24
      • 1970-01-01
      相关资源
      最近更新 更多