ImageDisplay箭头注释添加至组后坐标异常问题咨询
问题描述
使用ImageDisplay组件时遇到箭头注释坐标异常问题:逐个创建箭头注释时坐标正常,但先创建AnnotationGroup再将箭头注释添加至该组后,坐标出现错误,疑似组与ImageDisplay的坐标系不匹配。
已尝试操作
- 逐个生成箭头标记可正常显示,代码如下:
Image img := RealImage("RImg",4,256,256) img.ShowImage() ImageDisplay img_disp = img.ImageGetImageDisplay(0) ImageDocument img_doc = img.ImageGetOrCreateImageDocument() Number radius = 50 Number cx = 128 Number cy = 128 Number flag = 1 for (Number count = 0 ; count < 4 ; count++) { Number phi = count * 2 * Pi()/ 4 Number px = round(cx+radius*Cos(phi)) Number py = round(cy+radius*Sin(phi)) Component arrow = NewArrowAnnotation(cy, cx, py, px) img_disp.ComponentAddChildAtEnd(arrow) }
- 先创建标记组再添加箭头标记会导致坐标混乱,代码如下:
Image img := RealImage("RImg",4,256,256) img.ShowImage() ImageDisplay img_disp = img.ImageGetImageDisplay(0) ImageDocument img_doc = img.ImageGetOrCreateImageDocument() Component arrows = NewGroupAnnotation() Number radius = 50 Number cx = 128 Number cy = 128 for (Number count = 0 ; count < 4 ; count++) { Number phi = count * 2 * Pi()/ 4 Number px = round(cx+radius*Cos(phi)) Number py = round(cy+radius*Sin(phi)) Result(py + ", " + px + "\n") Component arrow = NewArrowAnnotation(cy, cx, py, px) arrows.ComponentAddChildAtEnd(arrow) } img_disp.ComponentAddChildAtEnd(arrows)
咨询问题
- 标记组与ImageDisplay之间坐标不一致的原因是什么?
- 如何确保标记组添加箭头标记时与ImageDisplay使用同一坐标系?
问题解答
1. 坐标不一致的原因
直接将箭头注释添加到ImageDisplay时,箭头坐标会自动适配ImageDisplay的图像坐标系(以图像左上角为原点,X轴向右、Y轴向下)。但新创建的AnnotationGroup默认使用屏幕坐标系(以窗口左上角为原点,坐标映射关系受图像缩放、偏移影响),且箭头注释添加到组时不会自动转换坐标系,最终导致显示位置偏离预期。
另外,NewArrowAnnotation的参数顺序(y1, x1, y2, x2)本身没问题,但组的坐标系未与ImageDisplay对齐时,会放大坐标偏差。
2. 解决方法
有两种方式可以确保标记组与ImageDisplay使用同一坐标系:
方式一:手动绑定坐标系
创建AnnotationGroup后,调用ComponentSetCoordinateSystem方法,将组的坐标系绑定到ImageDisplay的图像坐标系:
Image img := RealImage("RImg",4,256,256) img.ShowImage() ImageDisplay img_disp = img.ImageGetImageDisplay(0) ImageDocument img_doc = img.ImageGetOrCreateImageDocument() Component arrows = NewGroupAnnotation() // 关键:绑定组的坐标系到ImageDisplay的图像坐标系 arrows.ComponentSetCoordinateSystem(img_disp.ImageDisplayGetImageCoordinateSystem()) Number radius = 50 Number cx = 128 Number cy = 128 for (Number count = 0 ; count < 4 ; count++) { Number phi = count * 2 * Pi()/ 4 Number px = round(cx+radius*Cos(phi)) Number py = round(cy+radius*Sin(phi)) Result(py + ", " + px + "\n") Component arrow = NewArrowAnnotation(cy, cx, py, px) arrows.ComponentAddChildAtEnd(arrow) } img_disp.ComponentAddChildAtEnd(arrows)
方式二:先将组添加到ImageDisplay再添加箭头
先把AnnotationGroup添加到ImageDisplay,组会自动继承父容器(ImageDisplay)的坐标系,之后再往组里添加箭头注释:
Image img := RealImage("RImg",4,256,256) img.ShowImage() ImageDisplay img_disp = img.ImageGetImageDisplay(0) ImageDocument img_doc = img.ImageGetOrCreateImageDocument() Component arrows = NewGroupAnnotation() // 先将组添加到ImageDisplay,继承坐标系 img_disp.ComponentAddChildAtEnd(arrows) Number radius = 50 Number cx = 128 Number cy = 128 for (Number count = 0 ; count < 4 ; count++) { Number phi = count * 2 * Pi()/ 4 Number px = round(cx+radius*Cos(phi)) Number py = round(cy+radius*Sin(phi)) Result(py + ", " + px + "\n") Component arrow = NewArrowAnnotation(cy, cx, py, px) arrows.ComponentAddChildAtEnd(arrow) }
内容的提问来源于stack exchange,提问作者ChenZX
相关产品推荐
相关产品推荐

