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

使用DigitalMicrograph计算不同ROI总计数/强度结果异常求助

DigitalMicrograph ROI计数异常问题排查与解决方案

代码中的核心问题

  • 选区获取逻辑错误:处理第二个ROI时,手动硬编码t2=0;l2=x0+1;b2=1;r2=xmax+1覆盖了正确的选区获取,且后续错误调用img1.GetSelection(),此时前台图像已因showimage(cropped)切换,导致选区信息完全不符合预期。
  • 参数混用:计算第二个ROI的integral_cal2时,错误使用了第一个图像的scale参数,而非当前图像的scale2,导致校准结果偏差。
  • 依赖前台图像不稳定:GetFrontImage()依赖当前激活窗口,操作中切换图像会导致获取的图像对象与目标不符,结果不可靠。

修正后的代码

ROI roi_2 = NewROI()
ROISetRange( roi_2, 10, 30 )
cropdisp.ImageDisplayAddROI( roi_2)
imagedisplaysetroiselected(cropdisp, roi_2,1)

number count_l, count_r
image img_source := GetFrontImage()  // 提前锁定源图像,避免依赖前台窗口切换

// 处理第一个ROI
number t,l,b,r
img_source.GetSelection(t,l,b,r)
result("\n select1 is"+t+","+l+","+b+","+r)
image roi_img_1 := img_source[t:b, l:r]  // 直接截取ROI区域图像
number integral = sum( roi_img_1[] )
number scale = img_source.ImageGetIntensityScale()
number origin = img_source.ImageGetIntensityOrigin()
number nChannels = r - l
number integral_cal = (integral - origin * nChannels) * scale
count_l= integral_cal
result("\n count_l= "+count_l +", ")

// 处理第二个ROI
ROI roi_3 = NewROI()
ROISetRange( roi_3, 35, 55 )
cropdisp.ImageDisplayAddROI( roi_3)
imagedisplaysetroiselected(cropdisp, roi_3,1)

img_source.GetSelection(t,l,b,r)  // 从锁定的源图像获取最新选区
result("\n select2 is"+t+","+l+","+b+","+r)
image roi_img_2 := img_source[t:b, l:r]
number integral2 = sum( roi_img_2[] )
number scale2 = img_source.ImageGetIntensityScale()
number origin2 = img_source.ImageGetIntensityOrigin()
number nChannels2 = r - l
number integral_cal2 = (integral2 - origin2 * nChannels2) * scale2
count_r= integral_cal2
result("\n count_r= "+count_r +", ")

更优实现思路:直接基于ROI掩码计算

无需依赖选区,直接通过ROI生成掩码图像计算目标区域强度,彻底避免窗口切换带来的问题:

image img_source := GetFrontImage()
imageDisplay disp = img_source.ImageGetImageDisplay(0)

// 计算左侧ROI
ROI roi_left = NewROI()
ROISetRange( roi_left, 10, 30 )
disp.ImageDisplayAddROI( roi_left )
image mask_left = roi_left.ROIGetMaskImage(img_source)
number count_l = sum( img_source * mask_left )
// 应用强度校准
number scale = img_source.ImageGetIntensityScale()
number origin = img_source.ImageGetIntensityOrigin()
count_l = (count_l - origin * sum(mask_left)) * scale
result("\n count_l= "+count_l +", ")

// 计算右侧ROI
ROI roi_right = NewROI()
ROISetRange( roi_right, 35, 55 )
disp.ImageDisplayAddROI( roi_right )
image mask_right = roi_right.ROIGetMaskImage(img_source)
number count_r = sum( img_source * mask_right )
count_r = (count_r - origin * sum(mask_right)) * scale
result("\n count_r= "+count_r +", ")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 06:45:35