dm-script

Delay of line profile update


I'd like to get the updated ROI length in profile after I changed the scale of the image. But when I run the code below, the ROI length before and after changing the scale of the image is the same. It seems that the update of profile has some delay. Is there any method to solve this?

Object get_profile_img_obj(Component profile_comp)
{
    TagGroup profile_tg = profile_comp.ComponentGetTagGroup()

    Number n0, n1, n2, n3
    if (!profile_tg.TagGroupGetTagAsLong("Profile UID:[0]",n0)) Result("Profile Tags not found.")
    if (!profile_tg.TagGroupGetTagAsLong("Profile UID:[1]",n1)) Result("Profile Tags not found.")
    if (!profile_tg.TagGroupGetTagAsLong("Profile UID:[2]",n2)) Result("Profile Tags not found.")
    if (!profile_tg.TagGroupGetTagAsLong("Profile UID:[3]",n3)) Result("Profile Tags not found.")
    Object profile_img_obj = Alloc(ImageID).Init(n0, n1, n2, n3)
    return profile_img_obj
}

Void show_line_profile_len(Image img)
{
    Number k_profile = 12
    Number k_line_start = 1
    Number k_line_end = 2
    Number k_width_control = 16
    ImageDisplay img_disp = img.ImageGetImageDisplay(0)

    Component profile_comp = img_disp.ComponentGetNthChildOfType(k_profile, 0)
    Object profile_img_obj = get_profile_img_obj(profile_comp)

    Image profile_img := profile_img_obj.Open()
    ImageDisplay profile_img_disp = profile_img.ImageGetImageDisplay(0)
    number start, end
    ROI profile_img_roi = profile_img_disp.ImageDisplayGetROI(0)
    profile_img_roi.ROIGetRange(start, end)

    Number line_len = abs(start - end) * profile_img.ImageGetDimensionScale(0)
    String line_len_unit = profile_img.ImageGetDimensionUnitString(0)

    Result(line_len + "\n")
}

Image img := GetFrontImage()

show_line_profile_len(img)

Number new_scale = 1
img.ImageSetDimensionScale(0, new_scale)
img.ImageSetDimensionScale(1, new_scale)

show_line_profile_len(img)

Solution

  • The main issue you are running up against is that image display updates, such as those related to a calibration change, are all carried out on DM's foreground thread. Unfortunately, your example script also runs on the foreground thread and blocks any other foreground activity until it is finished executing. Normally, one needs to make sure any script code used to do live interaction with image displays is running on a background thread. One also needs to make sure that the background script pauses a bit when updates to image displays are expected.

    The answer to "How to live update box field in DM" provides a sort of "quick and dirty" alternative approach by calling the DoEvents function from a foreground script in order to give DM a chance to update image displays on the foreground thread. This approach also works in your script, as shown in the modified version below:

    Object get_profile_img_obj(Component profile_comp)
    {
        TagGroup profile_tg = profile_comp.ComponentGetTagGroup()
    
        Number n0, n1, n2, n3
        if (!profile_tg.TagGroupGetTagAsLong("Profile UID:[0]",n0)) Result("Profile Tags not found.")
        if (!profile_tg.TagGroupGetTagAsLong("Profile UID:[1]",n1)) Result("Profile Tags not found.")
        if (!profile_tg.TagGroupGetTagAsLong("Profile UID:[2]",n2)) Result("Profile Tags not found.")
        if (!profile_tg.TagGroupGetTagAsLong("Profile UID:[3]",n3)) Result("Profile Tags not found.")
        Object profile_img_obj = Alloc(ImageID).Init(n0, n1, n2, n3)
        return profile_img_obj
    }
    
    Void show_line_profile_len(Image img)
    {
        Number k_profile = 12
        Number k_line_start = 1
        Number k_line_end = 2
        Number k_width_control = 16
        ImageDisplay img_disp = img.ImageGetImageDisplay(0)
    
        Component profile_comp = img_disp.ComponentGetNthChildOfType(k_profile, 0)
        Object profile_img_obj = get_profile_img_obj(profile_comp)
    
        Image profile_img := profile_img_obj.Open()
        ImageDisplay profile_img_disp = profile_img.ImageGetImageDisplay(0)
        number start, end
        ROI profile_img_roi = profile_img_disp.ImageDisplayGetROI(0)
        profile_img_roi.ROIGetRange(start, end)
    
        Number line_len = abs(start - end) * profile_img.ImageGetDimensionScale(0)
        String line_len_unit = profile_img.ImageGetDimensionUnitString(0)
    
        Result(line_len + "\n")
    }
    
    Image img := GetFrontImage()
    
    show_line_profile_len(img)
    
    Number new_scale = img.ImageGetDimensionScale(0)
    new_scale *= 2
    
    img.ImageSetDimensionScale(0, new_scale)
    img.ImageSetDimensionScale(1, new_scale)
    
    DoEvents()
    Sleep(0.1)
    
    show_line_profile_len(img)