Crop a freehand roi to an new image after rotation

I want to crop a rod shaped freehand roi from an image to generate an new image with roi content only using macro:

    run("Create Mask");
    run("Subtract...", "value=254"); //make 255 to 1
    imageCalculator("Multiply create",name,"Mask");

. Sometimes the freehand roi are nearly vertical, horizontal or skewed. For vertical and horizontal, the cropped image occupy the majority, but for skewed one the image are larger.

Is there a way to rotate the raw image based on the roi drawn, then run the macro so that the roi fill most of image and file size can be reduced?


Maybe the following macro code will give you an idea how to answer your question.
Be aware of the pixel interpolation during rotation.

// Determine rotation angle by calculating moment of inertia
// or alternatively here: Measuring Fit Ellipse Parameters

// Make sure option 'Fit Ellipse' is activated !
// There must be a ROI !

//run("Set Measurements...", "fit redirect=None decimal=3");

run("Clear Results");
phi = getResult("Angle", 0);

run("Duplicate...", "title=NewImageTitle");
run("Rotate... ", "angle=" + phi + " grid=1 interpolation=Bilinear fill enlarge");
run("Select Bounding Box");





Thanks to the new function “Fit Rectangle” (by @NicoDF and @Wayne)

their might be a more elegant solution to crop a freehand roi to a new rotated image based on this new and awesome function “Fit Rectangle”. +++

1 Like

Also this post might be interesting

1 Like