Search code examples
image-processingroidm-script

How to crop multi-ROIs as images in DM by scripting?


I would like to crop these two ROIs as two images:

enter image description here

I am thinking to crop these two ROI synchronously by scripting, I found a code about creating image from ROI by D. R. G. Mitchell. So I wrote this:

image front := GetFrontImage()
imagedisplay imgdisp = front.ImageGetImageDisplay(0)
number roinum = imgdisp.ImageDisplayCountRois()
number x

for (x=0; x<roinum; x++)
{
roidisp = ImageDisplayGetROI(imgdisp,x)
image crop = front []
ShowImage(crop)
}

But it actually only works for one ROI. I'll appreciate if you could give me a clue about how to deal with more than one ROI's cropping. Thanks!


Solution

  • This example should help you:

    // ROIs are objects of the image display, so get that one first
    Image input := GetFrontImage()
    ImageDisplay disp = input.ImageGetImageDisplay(0)
    
    // Iterate over all ROIs found on the display
    number nRoi = disp.ImageDisplayCountROIs()
    for (number i=0; i<nRoi; i++ )
    {
        ROI myRoi = disp.ImageDisplayGetROI( i )
    
        // Test if the ROI has the properties you want
        if ( !myROI.ROIIsRectangle() )  continue
        if ( !myROI.ROIGetVolatile() )  continue
    
        // Read the ROI region and use that to copy the data
        number t, l, b, r 
        myROI.ROIGetRectangle( t, l, b, r )
    
        // Use slice2 to address the area and ImageClone to get a clone
        // inclusing tags, calibration etc.
        image cut := input.Slice2(t,l,0, 0,(r-l),1, 1,(b-t),1 ).ImageClone()
        cut.ShowImage()
    
        // Optionally set some names
        cut.SetName( input.GetName()+" #"+(i+1) )
    
        // Optionally modify the roi
        myRoi.ROISetVolatile( 0 )
        myRoi.ROISetMoveable( 0 )
        myRoi.ROISetLabel( "cut #"+(i+1) )
    }