将像素中的对象位置转换为地理坐标
Conversion of objects position in pixel to geographical coordinates
我有一些物体在 *jpg 图像 (sample_7958
) 中的像素位置 (left_x,top_y,width,height
),宽度为 98 x 81:
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
)。这可能吗?
是的,我认为这只是简单的算术:
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 创建
我有一些物体在 *jpg 图像 (sample_7958
) 中的像素位置 (left_x,top_y,width,height
),宽度为 98 x 81:
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
)。这可能吗?
是的,我认为这只是简单的算术:
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 创建