Skip to content

Instantly share code, notes, and snippets.

@kwolbachia
Last active April 15, 2021 18:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwolbachia/a225e2307ba4167b567ffd0d10dbff74 to your computer and use it in GitHub Desktop.
Save kwolbachia/a225e2307ba4167b567ffd0d10dbff74 to your computer and use it in GitHub Desktop.
imageJ Fiji Gamma LUTs macro
/*
15/04/21 K Terretaz @kWolbachia
visual gamma correction applied on active LUT or all channel LUTs, no modification of pixels data
VERY much inspired by https://github.com/ndefrancesco/macro-frenzy/blob/master/color/gammify_LUT.ijm for its simplicity
and https://imagej.nih.gov/ij/macros/Gamma_LUT.txt for non linear LUTs compatibility
*/
macro "gammaLUT on all channels" {
setGammaLUTAllch(getNumber("gamma",0.7));
}
function gammaLUT(gamma,r, g, b) {
Gam = newArray(256);
for(i=0;i<256;i++) Gam[i] = pow(i,gamma);
scale = 255 / Gam[255];
for(i=0;i<256;i++) Gam[i] = round(Gam[i]*scale);
R = newArray(256); G = newArray(256); B = newArray(256);
for(i=0;i<256;i++) {
j = Gam[i];
R[i] = r[j];
G[i] = g[j];
B[i] = b[j];
}
setLut(R, G, B);
}
//Active channel only
function setGammaLUT(gamma){
if(nImages==0)exit("No opened image"); if ( bitDepth() == 24) exit("this is an RGB image");
getLut(r, g, b);
gammaLUT(gamma,r, g, b);
}
//All channels
function setGammaLUTAllch(gamma){
if(nImages==0)exit("No opened image"); if ( bitDepth() == 24) exit("this is an RGB image");
getDimensions(w,h,channels,s,f);
if(channels>1) {
for(A=1;A<=channels;A++){
Stack.setChannel(A);
getLut(r, g, b);
gammaLUT(gamma,r, g, b); }
}
else {
getLut(r, g, b);
gammaLUT(gamma,r, g, b);
}
}
/*
to add it in a keyboard shortcut in statupmacro or a toolset :
macro "gammaLUT [g]" {
if (isKeyDown("space")) setGammaLUTAllch(getNumber("gamma",0.7));
else setGammaLUT(getNumber("gamma",0.7));
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment