Created
October 24, 2024 04:47
-
-
Save ekatrukha/280a207008f4e91d8c19b2d574131ac6 to your computer and use it in GitHub Desktop.
zoom in macro for stacks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//ImageJ macro making a movie (stack) of zooming on selected rectangle (ROI) | |
//Eugene Katrukha katpyxa at gmail.com | |
requires("1.48h"); | |
//check if there is rectangular selection | |
if(selectionType() ==0) | |
{ | |
sTitle=getTitle(); | |
//see if it is a timelapse or z-stack | |
Stack.getDimensions(widthS, heightS, channelsS, slicesS, framesS); | |
Stack.getPosition(channelN, sliceN, frameN); | |
bZStack = false; | |
bTStack = false; | |
if(framesS>0) | |
{ | |
bZStack = false; | |
bTStack = true; | |
} | |
if(slicesS>0) | |
{ | |
bZStack = true; | |
bTStack = false; | |
} | |
sMovieTitle=sTitle+"_zoom_movie"; | |
setBatchMode(true); | |
//ROI parameters | |
Roi.getBounds(nX, nY, nW, nH); | |
//print(nX); | |
//print(nY); | |
//print(nW); | |
//print(nH); | |
//Dialog | |
Dialog.create("Zoom-in parameters:"); | |
Dialog.addNumber("Number of frames for zoom-in:", 10); | |
Dialog.addNumber("Add static number of frames in the end:", 10); | |
if(bTStack) | |
{ | |
Dialog.addNumber("first timeframe:", 1); | |
Dialog.addNumber("last timeframe:", framesS); | |
} | |
if(bZStack) | |
{ | |
Dialog.addNumber("first z-slice:", 1); | |
Dialog.addNumber("last z-slice:", slicesS); | |
} | |
minMax=newArray("min","max"); | |
Dialog.addChoice("Zoom to min/max ROI size:", minMax); | |
sScaleChoice=newArray("same as ROI","Specified below"); | |
Dialog.addChoice("Final movie dimensions (px):", sScaleChoice); | |
Dialog.addNumber("Final movie width:", nW); | |
Dialog.addNumber("Final movie width:", nH); | |
Dialog.show(); | |
nFrames=Dialog.getNumber(); | |
nFramesLast=Dialog.getNumber(); | |
if(bTStack) | |
{ | |
nZTFirst = Math.max(Dialog.getNumber(),1); | |
nZTLast = Math.min(Dialog.getNumber(),framesS); | |
} | |
if(bZStack) | |
{ | |
nZTFirst = Math.max(Dialog.getNumber(),1); | |
nZTLast = Math.min(Dialog.getNumber(),slicesS); | |
} | |
sChoice=Dialog.getChoice(); | |
sSizeChoice=Dialog.getChoice(); | |
nFinalW=Dialog.getNumber(); | |
nFinalH=Dialog.getNumber(); | |
//print(nFinalW); | |
//print(nFinalH); | |
imageH=getHeight(); | |
imageW=getWidth(); | |
rawID=getImageID(); | |
nCenterX=nX+0.5*nW; | |
//print("CenterX"); | |
//print(nCenterX); | |
nCenterY=nY+0.5*nH; | |
//print("CenterY"); | |
//print(nCenterY); | |
nScaleX=nW/imageW; | |
nScaleY=nH/imageH; | |
//adjust roi x/y ratio to image x/y ratio | |
//depending on user choice | |
if(startsWith(sChoice, "min")) | |
nScaleFin=minOf(nScaleX,nScaleY); | |
else | |
nScaleFin=maxOf(nScaleX,nScaleY); | |
nW=nScaleFin*imageW; | |
nH=nScaleFin*imageH; | |
//print("New nW"); | |
//print(nW); | |
//print("New nH"); | |
//print(nH); | |
nX=nCenterX-(nW*0.5); | |
nY=nCenterY-(nH*0.5); | |
//print(nX); | |
//print(nY); | |
//distance from (0,0) to left top corner of selsction | |
length=sqrt(nX*nX+nY*nY); | |
//print("length"); | |
//print(length); | |
if(nX==0) | |
angle=3.14/2; | |
else | |
angle=atan(nY/nX); | |
//print("angle"); | |
//print(angle); | |
//final movie size | |
if(startsWith(sSizeChoice,"same as")) | |
{ | |
nFinalW=nW; | |
nFinalH=nH; | |
} | |
else | |
{ | |
nMovieScaleX=nW/nFinalW; | |
nMovieScaleY=nH/nFinalH; | |
nMovieFinalScale = minOf(nMovieScaleX,nMovieScaleY); | |
nFinalW=nW/nMovieFinalScale; | |
nFinalH=nH/nMovieFinalScale; | |
} | |
//print(nFinalW); | |
//print(nFinalH); | |
nScaleStep=(1-nScaleFin)/nFrames; | |
bNotFirstIt=false; | |
dCount =0; | |
//for(nScale=1;nScale>=nScaleFin;nScale=nScale-nScaleStep) | |
nScale=1; | |
for(dCount =0;dCount<nFrames;dCount++) | |
{ | |
selectImage(rawID); | |
if(bTStack) | |
{ | |
Stack.setPosition(channelN, sliceN, nZTFirst + Math.round((nZTLast-nZTFirst+1)*dCount/nFrames)); | |
} | |
if(bZStack) | |
{ | |
Stack.setPosition(channelN, nZTFirst + Math.round((nZTLast-nZTFirst+1)*dCount/nFrames),frameN); | |
} | |
//change viewport position/scale | |
dLen=length*dCount/nFrames; | |
//print("dlen"); | |
//print(dLen); | |
offsetX=dLen*cos(angle); | |
offsetY=dLen*sin(angle); | |
newW=imageW*nScale; | |
newH=imageH*nScale; | |
run("Specify...", "width="+toString(newW)+" height="+toString(newH)+" x="+toString(offsetX)+" y="+toString(offsetY)); | |
run("Duplicate...", "title="+sTitle+toString(nScale)); | |
unscaledID=getImageID(); | |
run("Scale...", "x=- y=- width="+toString(nFinalW)+" height="+toString(nFinalH)+" interpolation=Bicubic average create"); | |
scaledID=getImageID(); | |
selectImage(unscaledID); | |
close(); | |
selectImage(scaledID); | |
//not a first frame, let's add to existing movie stack | |
if(bNotFirstIt) | |
{ | |
sCurrTitle=sTitle+"x"+toString(nScale); | |
rename(sCurrTitle); | |
run("Concatenate...", " title=["+sMovieTitle+"] image1=["+sMovieTitle+"] image2=["+sCurrTitle+"]"); | |
} | |
//first frame, let's make accumulating stack out of it | |
else | |
{ | |
rename(sMovieTitle); | |
sMovieID=getImageID(); | |
bNotFirstIt=true; | |
} | |
nScale=nScale-nScaleStep; | |
} | |
//adding static frames part | |
nScale=nScaleFin; | |
for(nAddFrames=1;nAddFrames<=nFramesLast;nAddFrames++) | |
{ | |
selectImage(rawID); | |
offsetX=dLen*cos(angle); | |
offsetY=dLen*sin(angle); | |
newW=imageW*nScale; | |
newH=imageH*nScale; | |
run("Specify...", "width="+toString(newW)+" height="+toString(newH)+" x="+toString(offsetX)+" y="+toString(offsetY)); | |
run("Duplicate...", "title="+sTitle+toString(nScale)); | |
unscaledID=getImageID(); | |
run("Scale...", "x=- y=- width="+toString(nFinalW)+" height="+toString(nFinalH)+" interpolation=Bilinear average create"); | |
scaledID=getImageID(); | |
selectImage(unscaledID); | |
close(); | |
selectImage(scaledID); | |
sCurrTitle=sTitle+"x"+toString(nScale); | |
rename(sCurrTitle); | |
run("Concatenate...", " title=["+sMovieTitle+"] image1=["+sMovieTitle+"] image2=["+sCurrTitle+"]"); | |
} | |
setBatchMode(false); | |
} | |
//no rectangular ROI selection, error | |
else | |
{ | |
exit("Please choose rectangular selection (ROI) first."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment