You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R中为图像叠加XY坐标点:坐标点出现比例失真问题

问题:R语言中坐标点与手部图像比例匹配失真

我是R语言初学者,正在开展硕士项目,用R脚本读取.csv文件中的XY坐标信息,叠加到分辨率为1280x1024的手部图像上。坐标点已成功绘制、形状正确,但存在比例失真问题,无法与手部图像精准匹配。相关代码及数据如下:

数据与代码

# 读取坐标数据
dbCoords <- read.csv("dbCoords.csv", header=TRUE, sep = ",")
xCoords <- as.integer(dbCoords$x)
yCoords <- as.integer(dbCoords$y * -1)

# 读取图像
image <- read.bitmap("s1_Blank_Img.jpg")
rImage <- rasterGrob(image, width=unit(33.866666667,"cm"), height=unit(27.093333333,"cm"))

# 绘图代码
ggplot(dbCoords, aes(xCoords,yCoords)) + 
  annotation_custom(rImage) +
  geom_point(colour="red") +
  scale_x_continuous(expand=c(0,0), lim=c(min(xCoords),max(xCoords))) +
  scale_y_continuous(expand=c(0,0), lim=c(min(yCoords),max(yCoords))) +
  theme_void() +
  theme(aspect.ratio = nrow(image)/ncol(image)) 

坐标数据dput输出

structure(list(x = c(711L, 826L, 426L, 791L, 624L, 577L, 573L, 
404L, 921L, 814L, 917L, 903L, 728L, 722L, 905L, 601L, 587L, 705L, 
415L, 721L, 588L, 902L, 733L, 795L, 716L, 803L, 915L, 845L, 404L, 
573L, 588L, 909L, 904L, 652L, 623L, 771L, 580L, 595L, 782L, 641L, 
402L, 779L, 810L, 917L, 893L, 905L, 592L, 418L, 616L, 824L, 915L, 
730L, 724L, 703L, 624L, 726L, 883L, 729L, 628L, 442L, 820L, 890L, 
803L, 708L, 799L, 794L, 580L, 914L, 903L, 585L, 419L, 591L, 590L, 
802L, 687L, 783L, 917L, 421L, 900L, 773L, 601L, 902L, 889L, 571L, 
791L, 620L, 726L, 422L, 821L, 617L, 816L, 822L, 768L, 404L, 917L, 
895L, 808L, 585L, 630L, 737L, 729L, 621L, 786L, 586L, 421L, 895L, 
891L, 598L), y = c(143L, 736L, 513L, 132L, 435L, 157L, 723L, 
440L, 318L, 298L, 401L, 535L, 317L, 340L, 218L, 80L, 326L, 220L, 
521L, 131L, 219L, 543L, 239L, 123L, 201L, 260L, 279L, 740L, 433L, 
700L, 161L, 227L, 392L, 428L, 88L, 293L, 336L, 737L, 320L, 392L, 
440L, 318L, 117L, 324L, 548L, 451L, 167L, 506L, 93L, 724L, 221L, 
273L, 134L, 171L, 82L, 205L, 554L, 121L, 415L, 472L, 701L, 307L, 
122L, 158L, 205L, 316L, 679L, 361L, 223L, 167L, 534L, 286L, 163L, 
728L, 166L, 238L, 313L, 518L, 566L, 346L, 343L, 430L, 235L, 704L, 
138L, 97L, 156L, 462L, 267L, 423L, 342L, 684L, 306L, 431L, 300L, 
203L, 116L, 179L, 408L, 274L, 100L, 95L, 235L, 230L, 522L, 440L, 
599L, 645L)), class = "data.frame", row.names = c(NA, -108L))

解决方法

核心问题是annotation_custom按绝对尺寸放置图像,和ggplot基于数据的坐标系统没有对齐,导致比例失真。直接让图像的像素坐标和数据坐标完全匹配即可,修正步骤如下:

  • 图像分辨率是1280x1024,所以x轴范围对应0-1280,y轴原始范围是0-1024(因你将y取反,所以对应-1024-0)
  • 去掉rImage中手动设置的cm单位,让图像自动适配坐标范围
  • 调整坐标轴极限与图像像素范围一致,确保坐标点位置完全匹配图像

修正后的代码

library(ggplot2)
library(grid)
library(bitmap)

# 读取坐标数据
dbCoords <- read.csv("dbCoords.csv", header=TRUE, sep = ",")
# 无需转integer,保持原始数值即可;直接对y取反匹配图像方向
dbCoords$y_neg <- -dbCoords$y

# 读取图像
image <- read.bitmap("s1_Blank_Img.jpg")
rImage <- rasterGrob(image)  # 不设置固定宽高,让图像适配坐标范围

# 绘图
ggplot() + 
  # 明确图像的坐标范围,和图像像素尺寸完全对应
  annotation_custom(rImage, xmin = 0, xmax = 1280, ymin = -1024, ymax = 0) +
  # 使用原始x坐标和取反后的y坐标绘制点
  geom_point(data = dbCoords, aes(x = x, y = y_neg), colour = "red") +
  # 设置x轴极限为图像宽度
  scale_x_continuous(expand = c(0, 0), limits = c(0, 1280)) +
  # 设置y轴极限为取反后的图像高度
  scale_y_continuous(expand = c(0, 0), limits = c(-1024, 0)) +
  theme_void() +
  # 保持图像原高宽比,防止拉伸变形
  theme(aspect.ratio = nrow(image)/ncol(image))

关键修改说明

  • annotation_custom添加了xmin/xmax/ymin/ymax参数,将图像精准定位在与像素尺寸匹配的坐标范围内
  • 坐标轴极限设置为图像的像素范围,确保坐标点的像素位置和图像完全重合
  • 取消手动设置的cm单位,避免尺寸换算误差,让ggplot自动处理坐标与图像的对齐

内容的提问来源于stack exchange,提问作者Ronan Timircan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 18:12:29