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

如何用DM Script锁定FFT图像圆形注释中心并维持圆形形态?

锁定中心的等比例圆形选区实现(DM Script)

要实现FFT图像上中心锁定且调整大小保持圆形的选区,需要通过自定义交互式工具控制选区更新逻辑,替代默认圆形选区工具。以下是完整示例代码及关键说明:

示例代码

class LockedCircleROITool : UIFrame
{
    ImageDisplay disp
    number imgWidth, imgHeight
    number centerX, centerY
    number radius

    void Init(ImageDisplay targetDisp)
    {
        disp = targetDisp
        Image img = disp.GetImage()
        img.GetSize(imgWidth, imgHeight)
        // 计算FFT图像中心(适配偶数尺寸图像的整数中心坐标)
        centerX = imgWidth / 2
        centerY = imgHeight / 2
        radius = 50 // 初始半径
        UpdateROI()
        
        // 注册鼠标事件回调
        disp.AddMouseDownNotification(self, "OnMouseDown")
        disp.AddMouseDragNotification(self, "OnMouseDrag")
    }

    void UpdateROI()
    {
        // 创建中心锁定的圆形ROI:宽高等于2*radius,左上角坐标为中心偏移半径
        ROI circleROI = NewROI()
        circleROI.SetType(ROI_TYPE_CIRCLE)
        circleROI.SetRect(centerX - radius, centerY - radius, 2*radius, 2*radius)
        disp.ImageGetDisplayLayer(0).ROISet(circleROI)
        disp.WindowRedraw()
    }

    void OnMouseDown(Object self, number x, number y, number flags)
    {
        // 鼠标按下时计算初始半径
        radius = sqrt((x - centerX)^2 + (y - centerY)^2)
        UpdateROI()
    }

    void OnMouseDrag(Object self, number x, number y, number flags)
    {
        // 拖动时更新半径,始终以图像中心为圆心
        radius = sqrt((x - centerX)^2 + (y - centerY)^2)
        // 限制半径不超过图像最小半长,避免选区超出范围
        radius = min(radius, min(imgWidth, imgHeight)/2)
        UpdateROI()
    }
}

// 调用示例:激活FFT图像窗口后运行
ImageDisplay activeDisp = GetFrontImageDisplay()
if(activeDisp.ImageIsValid())
{
    LockedCircleROITool tool
    tool.Init(activeDisp)
    OKDialog("已启用中心锁定圆形选区工具,拖动鼠标调整半径")
}
else
{
    ErrorDialog("请先激活FFT图像窗口")
}

关键逻辑说明

  • 中心锁定:通过imgWidth/2和imgHeight/2计算图像中心坐标,后续更新ROI时始终以此为圆心,避免偏移。
  • 保持圆形形态:设置ROI宽高均为2*radius,确保选区为正圆形;拖动时仅计算鼠标到中心的距离作为半径,保证宽高同步变化。
  • 事件监听:通过AddMouseDownNotification和AddMouseDragNotification捕获鼠标操作,实时更新选区并刷新显示。

使用时打开FFT图像窗口,运行脚本即可通过鼠标拖动调整选区大小,选区始终以图像中心为圆心且保持圆形。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 04:49:54