How do I rotate an image by an arbitrary amount?

672 views Asked by At

I have a DigitalMicrograph image which I would like to rotate by a certain amount (not 90 degree). This can be done by the menu item "Process...\Rotate" but I would like to do this by script. I have a script which computes the angle based on a line-ROI found on the image. (see below), but I do not know how I can then rotate the image.

Number kPi = 3.14159265359

Image front := GetFrontImage()
ImageDisplay fDisp = front.ImageGetImageDisplay(0)
ROI line = fDisp.ImageDisplayGetRoi(0)
if ( line.RoiIsValid() )
{
    if ( line.RoiIsLine() )
    {
        number sx,sy,ex,ey
        line.RoiGetLine(sx,sy,ex,ey)
        number dy = ey - sy
        number dx = ex - sx
        number angle = atan( dy / dx ) * 180/kPi
        if ( dx < 0 ) 
            angle = angle + 180

        Result("\n Rotate image by " + angle + " degree.")
    }
}
1

There are 1 answers

4
BmyGuest On BEST ANSWER

Nice idea for a tool and good coding so far.

The command you are looking for is simply called Rotate(). You 've possibly missed it in the help documentation because it is (unfortunately) not listed in the 'image' section. There is a dedicated "reference" section meant to summarize the "simple" script commands of everyday quick-scripting:

enter image description here

With this command, your script can be modified to the one below. Note that I've also replaced your kPi by the command PI(). The Rotate() command requires the rotation angle to be given in radians. The size of the resulting image will be enlarged and the image is zero-padded. Also: I've replaced your If - statement with an abbreviated version.

Image front := GetFrontImage()
ImageDisplay fDisp = front.ImageGetImageDisplay(0)
ROI line = fDisp.ImageDisplayGetRoi(0)
if ( line.RoiIsValid() )
{
    if ( line.RoiIsLine() )
    {
        number sx, sy, ex, ey
        line.RoiGetLine( sx, sy, ex, ey )
        number dy = ey - sy
        number dx = ex - sx
        number angle = atan( dy / dx ) + ( dx < 0 ? Pi() : 0 )
        image out := front.Rotate( angle )
        out.ShowImage()
    }
}