Skip to content

Instantly share code, notes, and snippets.

@mkitti
Last active June 15, 2020 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkitti/1df14cf92f2b635db248d9205dfb9d61 to your computer and use it in GitHub Desktop.
Save mkitti/1df14cf92f2b635db248d9205dfb9d61 to your computer and use it in GitHub Desktop.
//rapidSquareROITool.ijm
//Creates six tools that allows you to quickly add either 64x64 or 32x32 square ROIs
//
// Mark Kittisopikul, September 11th, 2018
// Goldman Lab
// Northwestern University
//
// Tools:
// 1. Create a 64x64 px square ROI at the location clicked and add to ROI manager
// 2. Create a 32x32 px square ROI at the location clicked and add to ROI manager
// 3. Create a 64x64 px square ROI at the location clicked, add to ROI manager,
// advance to next frame
// 4. Create a 32x32 px square ROI at the location clicked, add to ROI manager,
// advance to next frame
// 5. Create a 64x64 px square ROI at the location clicked, add to ROI manager,
// go to previous frame
// 6. Create a 32x32 px square ROI at the location clicked, add to ROI manager,
// go to previous frame
//
// Keyboard modifiers for tools 2-6
// * Holding alt while clicking will update the last ROI rather than add a new one
// * Holding control while clicking will ignore the clicked location and duplicate the last ROI
// * Holding shift while clicking will prevent the frame from changing
macro "Square Tool 64- C000R00ff" {
// create a 64x64 square ROI and add it to the ROI manager
var size = 64;
var halfSize = size/2;
//setTool(0);
getCursorLoc(x, y, z, modifiers)
makeRectangle(x-halfSize,y-halfSize,size,size);
roiManager("add");
}
macro "Square Tool 32- C000R4488" {
//create a 32x32 square ROI and add it to the ROI manager
var size = 32;
var halfSize = size/2;
//setTool(0);
getCursorLoc(x, y, z, modifiers)
makeRectangle(x-halfSize,y-halfSize,size,size);
roiManager("add");
}
macro "SquareRoiNextFrame Tool 64- C0f0R00ffH00f80f00" {
squareROINextFrame(64);
}
macro "SquareRoiNextFrame Tool 32- C0f0R4488H44c84c44" {
squareROINextFrame(32);
}
macro "SquareRoiPrevFrame Tool 64- C0f0R00ffHf008fff0" {
squareROINextFrameBackwards(64);
}
macro "SquareRoiPrevFrame Tool 32- C0f0R4488Hc448ccc4" {
squareROINextFrameBackwards(32);
}
function squareROINextFrame(size) {
var halfSize = size/2;
var shift=1;
var ctrl=2;
//var rightButton=4;
var alt=8;
//var leftButton=16;
var roiCount = roiManager("count");
Stack.getDimensions(width, height, channels, slices, frames);
Stack.getPosition(channel,slice,frame);
getCursorLoc(x, y, z, modifiers);
// true if we are to add a new ROI, false if we are updating the last ROI
var addMode = roiCount == 0 || modifiers & alt == 0;
if( !addMode ) {
// select the last ROI to update it
roiManager("select",roiCount-1);
Stack.getPosition(channel,slice,frame);
}
if(modifiers & ctrl == 0) {
// make a new rectangle if control is not held
makeRectangle(x-halfSize,y-halfSize,size,size);
}
if( addMode ) {
// add this ROI if alt is not held
roiManager("add");
} else {
// update last ROI if alt is held
roiManager("update");
}
if(modifiers & shift == 0) {
//shift is not held, advance
Stack.setPosition(channel,slice,frame+1);
if(frame + 1 > frames) {
waitForUser("End of time lapse reached");
}
} else {
//shift is held, go backwards
Stack.setPosition(channel,slice,frame-1);
if(frame - 1 < 1) {
waitForUser("Beginning of time lapse reached");
}
}
}
function squareROINextFrameBackwards(size) {
var halfSize = size/2;
var shift=1;
var ctrl=2;
//var rightButton=4;
var alt=8;
//var leftButton=16;
var roiCount = roiManager("count");
Stack.getDimensions(width, height, channels, slices, frames);
Stack.getPosition(channel,slice,frame);
getCursorLoc(x, y, z, modifiers);
// true if we are to add a new ROI, false if we are updating the last ROI
var addMode = roiCount == 0 || modifiers & alt == 0;
if( !addMode ) {
// select the last ROI to update it
roiManager("select",roiCount-1);
Stack.getPosition(channel,slice,frame);
}
if(modifiers & ctrl == 0) {
// make a new rectangle if control is not held
makeRectangle(x-halfSize,y-halfSize,size,size);
}
if( addMode ) {
// add this ROI if alt is not held
roiManager("add");
} else {
// update last ROI if alt is held
roiManager("update");
}
if(modifiers & shift != 0) {
//shift is not held, advance
Stack.setPosition(channel,slice,frame+1);
if(frame + 1 > frames) {
waitForUser("End of time lapse reached");
}
} else {
//shift is held, go backwards
Stack.setPosition(channel,slice,frame-1);
if(frame - 1 < 1) {
waitForUser("Beginning of time lapse reached");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment