Help with macro


#21

I follow the protocol provided. I’ve tried using different LUTs when generating the initial GP image by selecting different LUTs on the initial menu. I think the message is indicating that there is no blue image to merge into a HSB image, but I’m not sure why its not generating the blue image. I’m sort of stuck now…


#22

although it might not be necessary, I just found a paper that used a GP image in their publication. https://www.ncbi.nlm.nih.gov/pubmed/28069944


#23

Hello @Helene @Reyn!

I have exactly the same problem with trying to convert into HSB image. ´Blue hue is not found´. It may be because at the first window of the macre there should be an option to choose greys. (As written in the article). This option is not available for me.

If you figure out how to create a HSB image I would be very grateful for the solution.

Thanks!


#24

I tried that and it didn’t work. You can download more imagej LUTs and add them to your library. I also tried other combinations to no avail.


#25

Hello @Praveen @Helene, I’m performing the same experiment and quantify the GP value. However, I meet with the same problem. I tried your macros, and changed the file name to A.ch00 and A.ch01, but still did not work. I followed the Nature Protocol, but I can’t find the choice of “Grays” in “scale color for GP images”. Do you use 16_Color.lut instead? I’m really very appreciated if you could provide me any suggestions.


#26

@Reyn
Hi guys,
Sorry for my late reply. But i haven’t found a solution for the ‘Blue hue is not found’ But for now i have the GP values in an excel so i just use them to represent my data.


#27

Hi,
Could you post an image of your error message please?
Praveen


#28

Hi Praveen,
I met a problem using this macro and can’t creat colorful HSB but only gray figures. I followed exactlly in the paper and did not help. Do you have some solutions to that?
Thanks
Liangcui Chu


#29

Hi guys,
I just found a solution to the ‘Blue hue is not found’’ error. If anyone needs the fix, let me know :slight_smile:
Praveen


#30

Hi @Praveen,
I’ve just started to use this macro to analyze my images and was able to troubleshoot all the errors, thanks to you guys. But I could not create the HSB image when I chose “Yes” for the option “Do you want to generate HSB image”. I got the same error you mentioned (Blue hue is not found). If you know a fix, can you post it here. Thank you
Tuan


#31

Hey @Tran_Minh_Tuan
I can send you the macro which i use if you wish. Please inbox me your email. I can shoot it right away.
Praveen


#35

Why not simply paste it here on the forum, so that others can benefit from it as well?


#36

Is there some reason you cannot post it publicly here? It would have a potentially much higher impact. Otherwise, when the next person comes along to this thread later, they will have to post another request for another private email, and so on.


#37

Sorry guys, forgot that i could just post it here. Here is what i use

/* ImageJ macro for GP images analysis
   Quantitative Imaging of Membrane Lipid Order in Cells and Organisms
   Dylan M. Owen, Carles Rentero, Astrid Magenau, Ahmed Abu-Siniyeh and Katharina Gaus
   Nature Protocols 2011
   version July 2011
*/

/* This macro calculates GP images from Laurdan and di-4-ANEPPDHQ ratiometric images in
   bactch mode (whole chosen folder) obtained using a Leica microscope. The generation
   of HSB images of these GP images has been also implemented.
*/

print("\\Clear");

requires("1.44d");
closeAllImages();

// Select images folder
dir = getDirectory("Choose a Directory ");

// Inicialize choice variables
CHANNEL1 = newArray("none","ch00","ch01","ch02","ch03","ch04","ch05");
CHANNEL2 = newArray("ch00","ch01","ch02","ch03","ch04","ch05");
QUESTION = newArray("Yes","No");
Lut_Dir = getDirectory("luts");
lut = getFileList(Lut_Dir);

// Choose image channels and threshold value
Dialog.create("GP analysis parameters");
Dialog.addChoice("Acquisition ordered channel:  ", CHANNEL2, "ch00");
Dialog.addChoice("Acquisition disordered channel:  ", CHANNEL2, "ch01");
Dialog.addNumber("Lower Threshold Value for GP the mask:  ", 5);
Dialog.addChoice("Scale color for GP images:  ", lut, "16_colors.lut");
Dialog.addMessage("\n");
Dialog.addChoice("Immunofluorescence channel:  ", CHANNEL1, "none");
Dialog.addNumber("Lower Threshold Value for the IF mask:  ", 50);
Dialog.addMessage("\n");
Dialog.addNumber("G factor (1 if unknown):  ", 0.277);
Dialog.addChoice("Do you want to use G factor for GP image calculation?",QUESTION, "Yes");
Dialog.addMessage("\n");
Dialog.addChoice("Do you want to generate HSB images?",QUESTION, "Yes");
Dialog.addMessage("\n");
Dialog.show();

// Feeding variables from dialog choices
chA = Dialog.getChoice();
chB = Dialog.getChoice();
t = Dialog.getNumber();
lut = Dialog.getChoice();
chC = Dialog.getChoice();
tc = Dialog.getNumber();
Gf = Dialog.getNumber();
Ques = Dialog.getChoice();
HSB = Dialog.getChoice();

lut1= substring(lut,0,lengthOf(lut)-4);

time0 = getTime();
setBatchMode(true);

// Folder management
getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
MonthNames = newArray("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
DayNames = newArray("Sun", "Mon","Tue","Wed","Thu","Fri","Sat");
if (hour<10) {hours = "0"+hour;}
else {hours=hour;}
if (minute<10) {minutes = "0"+minute;}
else {minutes=minute;}
if (month<10) {months = "0"+(month+1);}
else {months=month+1;}
if (dayOfMonth<10) {dayOfMonths = "0"+dayOfMonth;}
else {dayOfMonths=dayOfMonth;}

var Folder = 0;
results_Dir = dir + "Results "+year+"-"+months+"-"+dayOfMonths+" "+hours+"h"+minutes+ File.separator;
File.makeDirectory(results_Dir);

ordered_images_Dir = results_Dir + "Ordered Images" + File.separator;
Folder = ordered_images_Dir;
newFolder();
disordered_images_Dir = results_Dir + "Disordered Images" + File.separator;
Folder = disordered_images_Dir;
newFolder();
GP_images_Dir = results_Dir + "GP images" + File.separator;
Folder = GP_images_Dir;
newFolder();
histogramGP_Dir = GP_images_Dir + "Histograms" + File.separator;
Folder = histogramGP_Dir;
newFolder();
rawGP_images_Dir = results_Dir + "raw GP images" + File.separator;
Folder = rawGP_images_Dir;
newFolder();

IF_images_Dir = results_Dir + "Immunofluorescence Images" + File.separator;
GP_IF_images_Dir = results_Dir + "GP-IF images" + File.separator;
histogramIF_Dir = GP_IF_images_Dir + "Histograms" + File.separator;
HSB_Dir = results_Dir + "HSB images" + File.separator;

// Open and save images in 32bit format 
listDir = getFileList(dir);
var s = 0;
for (i = 0; i < listDir.length; i++) {
	if (endsWith(listDir[i], chA+".tif")) {
		open(dir + listDir[i]);
		prepareImage();
		saveAs("tiff", ordered_images_Dir + substring(s,0,lengthOf(s)-9) + "_chA_32bits");
		close(); }
	if (endsWith(listDir[i], chB+".tif")) {
		open(dir + listDir[i]);
		prepareImage();
		Gf1=Gf;
		if (Ques=="Yes") {
			run("Multiply...","value=" + Gf);
			Gf1=1; }
		saveAs("tiff", disordered_images_Dir + substring(s,0,lengthOf(s)-9) + "_chB_32bits");
		close(); }
}

// GP and GPc Arrays 
GP=newArray(256);
for (i=0; i<256; i++) {
	GP[i]=((i-127)/127); }

GPc=newArray(256);
for (i=0; i<256; i++) {
	GPc[i]=-(1+GP[i]+Gf1*GP[i]-Gf1)/(-1-GP[i]+Gf1*GP[i]-Gf1); }
		
// GP analysis
listOrd = getFileList(ordered_images_Dir);
listDisord = getFileList(disordered_images_Dir);

var histoDir=0;
for (h = 0, j = h; h < listDisord.length; h++, j++) {
	Name=newArray(listOrd.length);
	open(ordered_images_Dir+listOrd[h]);
	name = getTitle;
	Name[j] = substring(name,0,lengthOf(name)-15);
	rename("Image_1a.tif");
	run("Duplicate...","title=Image_1b.tif");
	open(disordered_images_Dir+listDisord[j]);
	rename("Image_2a.tif");
	run("Duplicate...","title=Image_2b.tif");
	imageCalculator("Substract create 32-bit", "Image_1a.tif", "Image_2a.tif");
	rename("Image_Subs.tif");
	imageCalculator("Add create 32-bit", "Image_1b.tif", "Image_2b.tif");
	rename("Image_Add.tif");
	imageCalculator("Divide create 32-bit", "Image_Subs.tif", "Image_Add.tif");
	saveAs("tiff", rawGP_images_Dir + Name[j] + "_preGP");
	rename("Image_preGP.tif");
	setMinAndMax(-1.0000, 1.0000);
	call("ij.ImagePlus.setDefault16bitRange", 0);
	selectImage("Image_Add.tif");
	setThreshold(t*2,510);
	run("Convert to Mask");
	run("Subtract...", "value=254");
	rename("Image_1bit.tif");
	imageCalculator("Multiply create", "Image_1bit.tif", "Image_preGP.tif");
	run(lut1);
	saveAs("tiff", GP_images_Dir + Name[j] + "_GP");

// Histogram generation
	histoDir=histogramGP_Dir;
	HistogramGeneration();
}

// If there is not fluorescent immunostaining
if (chC == "none") {
// HSB image generation
	if (HSB=="Yes") {
		HSBgeneration();
	}
// Print information
	listGP = getFileList(GP_images_Dir);
	time1 = getTime();
	TOTALtime = (time1-time0)/1000;
	printInfo();
}

// If there is fluorescent immunostaining
else {
// Folder management
	temp_Dir = getDirectory("temp");

	temp_IFmask_Dir = temp_Dir + "IF mask" + File.separator;
	Folder = temp_IFmask_Dir;
	newFolder();

	File.makeDirectory(IF_images_Dir);
	File.makeDirectory(GP_IF_images_Dir);
	File.makeDirectory(histogramIF_Dir);

// Create a 1-bit mask from Immunofluorescence image and save for each image
	listDir = getFileList(dir);
	Name=newArray(listDir.length);
	for (i = 0; i < listDir.length; i++) {
		if (endsWith(listDir[i], chC + ".tif")) {
			open(dir + listDir[i]);
			name = getTitle;
			Name[i]=substring(name,0,lengthOf(name)-9);
			run("32-bit");
			saveAs("tiff",IF_images_Dir + Name[i] + "_IF_32bits");
			run("8-bit");
			setThreshold(tc,255);
			run("Convert to Mask");
			run("Divide...","value=255");
			saveAs("tiff", temp_IFmask_Dir + Name[i] + "_GP-IF_1bit");
			close();
		}
	}

// GP-IF image
	listTempIF = getFileList(temp_IFmask_Dir);
//	listL = getFileList(GP_images_Dir);
	Name=newArray(listTempIF.length);
	for (h = 0, j = h; h < listTempIF.length; h++, j++) {
		open(temp_IFmask_Dir+listTempIF[h]);
		k = getTitle;
		Name[j]=substring(k,0,lengthOf(k)-15);
		open(GP_images_Dir + Name[j] + "_GP.tif");
		l = getTitle;
		run("8-bit");
		imageCalculator("Multiply create", k, l);
		run(lut1);
		saveAs("tiff", GP_IF_images_Dir + Name[j] + "_GP-IF");

// Histogram and Normal Distribution
		histoDir=histogramIF_Dir;
		HistogramGeneration();
	}

// Folder management
	listDel = getFileList(temp_IFmask_Dir);
	for (i = 0; i < listDel.length; i++) {
		File.delete(temp_IFmask_Dir+listDel[i]); }
	File.delete(temp_IFmask_Dir);

// HSB image generation
	if (HSB=="Yes") {
		HSBgeneration();
	}

// Print information
	listGP = getFileList(GP_images_Dir);
	time2 = getTime();
	TOTALtime = (time2-time0)/1000;
	printInfo();
}


///////////////// FUNCTIONS ////////////////////

function closeAllImages() {				// This function closes all images
	while (nImages>0) {
		selectImage(nImages);
		close(); }
}

function newFolder() {					// This function creates a folder, removing any existing file in a folder with the same name
	File.makeDirectory(Folder);
	listFolder = getFileList(Folder);
	for (i = 0; i < listFolder.length; i++) {
		File.delete(Folder+listFolder[i]); }
}

function prepareImage () {				// This funcion prepares each image for the analysis
	s=getTitle;
	run("8-bit");
	run("Grays");
	run("32-bit");
	return s;
}

function HistogramGeneration () {		// This funcion obtains the intensity frequency histogram of the images, makes it smoother,
										// calculates the normal average distribution and also includes the GP value (and GP value
										// corrected by the Gfactor) corresponding to each intensity. An MS Excel file is generated
										// with all this data
	Int=newArray(256);
	Cou=newArray(256);
	Smo=newArray(256);
	NAvDist=newArray(256);
	nBins = 256;

	getHistogram(values, counts, nBins);
	close();
	while (nImages>0) {
		selectImage(nImages);
		close(); }

	for (u=0; u<nBins; u++) {
		Int[u]=u;
		Cou[u]=counts[u];
		if (u<=1) {
			Smo[u]=0; }
		else if (u==255) {
			Smo[u]=0; }
		else {
			Smo[u]=(counts[u-1]+counts[u]+counts[u+1])/3;}
	}
	Array.getStatistics(Cou,min,max,mean,stdDev);
	Sa=(mean*256)-counts[0]-counts[255];
	d=File.open(histoDir + Name[j]+"_Histogram.xls");
	print(d, "Intensity	Counts	Smooth	Norm Av Dist	GP	GP corrected");
	for (k=0; k<256; k++) {
		NAvDist[k]=100*Smo[k]/Sa;
		print(d, Int[k]+"	"+Cou[k]+"	"+Smo[k]+"	"+NAvDist[k]+"	"+GP[k]+"	"+GPc[k]);
	}
	File.close(d);
}

function HSBgeneration() {				// This function generates the HSB image of the GP images as explained in the paper
	closeAllImages();
	setBatchMode(false);

// Select images folder
	listRAW = getFileList(rawGP_images_Dir);
	
	BRIGHTNESS = newArray("Order channel","Disorder channel","IF channel");
	BRIGHTNESSnoIF = newArray("Order channel","Disorder channel");

	HSB_Dir = results_Dir + "HSB images" + File.separator;
	Folder = HSB_Dir;
	newFolder();

	Lut_Dir = getDirectory("luts");
	lut = getFileList(Lut_Dir);

	Dialog.create("Choose images and LUT");
	Dialog.addChoice("Select Hue (GP) image: ", listRAW, "none");
	if (chC == "none") {
		Dialog.addChoice("Brightness folder: ", BRIGHTNESSnoIF, "Order channel");}
	else {
		Dialog.addChoice("Brightness folder: ", BRIGHTNESS, "Order channel");}
	Dialog.addMessage("\n");
	Dialog.addChoice("Select color LUT: ",lut, "32_colors.lut");
	Dialog.addMessage("\n");
	Dialog.addChoice("Do you want to convert the whole folder into HSB images?", QUESTION, "Yes");
	Dialog.show();

// Feeding variables from dialog choices
	H = Dialog.getChoice();
	BRIGHT = Dialog.getChoice();
	Lut = Dialog.getChoice();
	WholeDir=Dialog.getChoice();

	if (BRIGHT=="Order channel") {
		mark = "_chA_32bits.tif";
		brightness_Dir = results_Dir + "Ordered Images" + File.separator;
	}
	else if (BRIGHT=="Disorder channel") {
		mark = "_chB_32bits.tif";
		brightness_Dir = results_Dir + "Disordered Images" + File.separator;
	}
	else {
		mark = "_IF_32bits.tif";
		brightness_Dir = results_Dir + "Immunofluorescence Images" + File.separator;
	}

	run("Set Measurements...", "  min limit display redirect=None decimal=5");
	index2=indexOf(Lut,".lut");
	L=substring(Lut,0,index2);

	open(rawGP_images_Dir+H);
	name=getTitle();
	Name=substring(name,0,lengthOf(name)-10);
	run(L);
	rename("Hue");

	run("Brightness/Contrast...");
	waitForUser("set min & max","set min & max");
	getMinAndMax(min,max);
	time0 = getTime();

	open(brightness_Dir+Name+mark);
	Bri=getTitle();
	run("Enhance Contrast", "saturated=0.5 normalize");

	selectWindow("Hue");
	run("RGB Color");
	run("Split Channels");

	imageCalculator("Multiply create 32-bit", Bri,"Hue (red)");
	rename("bR");
	run("8-bit");

	imageCalculator("Multiply create 32-bit", Bri,"Hue (green)");
	rename("bG");
	run("8-bit");

	//imageCalculator("Multiply create 32-bit", Bri,"Hue (blue)");
	//rename("bB");
	//run("8-bit");

	run("Merge Channels...", "red=bR green=bG gray=*None*");
	saveAs("tiff", HSB_Dir + Name + "_HSB");

	closeAllImages();

// HSB whole folder processing
	if (WholeDir == "Yes") {
		for (j = 0; j < listRAW.length; j++) {
			open(rawGP_images_Dir+listRAW[j]);
			name1=getTitle();
			Name1=substring(name1,0,lengthOf(name1)-10);
			rename("Hue");
			run(L);
			setMinAndMax(min,max);

			open(brightness_Dir+Name1+mark);
			run("Enhance Contrast", "saturated=0.5 normalize");
			Bri=getTitle;

			selectWindow("Hue");
			run("RGB Color");
			run("Split Channels");

			imageCalculator("Multiply create 32-bit", Bri,"Hue (red)");
			rename("bR");
			run("8-bit");

			imageCalculator("Multiply create 32-bit", Bri,"Hue (green)");
			rename("bG");
			run("8-bit");

			//imageCalculator("Multiply create 32-bit", Bri,"Hue (blue)");
			//rename("bB");
			//run("8-bit");

			run("Merge Channels...", "red=bR green=bG gray=*None*");
			saveAs("tiff", HSB_Dir + Name1 + "_HSB");

			closeAllImages();
		}
	}

// Make HSB LUT bar
	newImage("Hue", "8-bit Ramp", 256, 256, 1);
	run(L);
	run("Duplicate...", "title=Brightness");
	run("Rotate 90 Degrees Left");
	run("32-bit");
	selectWindow("Hue");
	run("RGB Color");
	run("Split Channels");
	imageCalculator("Multiply create 32-bit", "Brightness","Hue (red)");
	rename("bR");
	run("8-bit");
	imageCalculator("Multiply create 32-bit", "Brightness","Hue (green)");
	rename("bG");
	run("8-bit");
	//imageCalculator("Multiply create 32-bit", "Brightness","Hue (blue)");
	//rename("bB");
	//run("8-bit");
	run("Merge Channels...", "red=bR green=bG gray=*None*");
	rename("HSB LUT");
	selectWindow("Hue (red)");
	close();
	selectWindow("Hue (green)");
	close();
	//selectWindow("Hue (blue)");
	//close();
	selectWindow("Brightness");
	close();

	selectWindow("HSB LUT");
	run("Rotate 90 Degrees Left");
	run("Size...", "width=32 height=256 interpolation=None");


// Copy LUT bar to the image
	run("Copy");
	newImage("Panel LUT", "RGB White", 70, 264, 1);
	setBatchMode(false);
	makeRectangle(4,4,32,256);
	run("Paste");
	run("Colors...", "foreground=black background=black selection=yellow");
	run("Line Width...", "line="+2);
	run("Draw");
	run("Select None");
	setFont("Arial", 12);
	setColor(0, 0, 0);
	drawString(d2s(max,2),39,15);
	drawString(d2s(min,2),39,264);
	run("Select None");

	if (WholeDir == "Yes") {
		saveAs("tiff", HSB_Dir + "Lut bar");
		close();
	}
	else {
		saveAs("tiff", HSB_Dir + Name + "_Lut bar");
		open(HSB_Dir + Name + "_HSB.tif");
	}
	closeAllImages();
}

function printInfo () {					// This function prints and saves a summary of the results of the macro
	print("\\Clear");
	print("GP images analysis macro");
	print("   Quantitative Imaging of Membrane Lipid Order in Cells and Organisms");
	print("   Dylan M. Owen, Carles Rentero, Astrid Magenau, Ahmed Abu-Siniyeh and Katharina Gaus");
	print("   Nature Protocols 2011");
	print("   version July 2011");
	print("\n");
	print("Ordered channel: " + chA);
	print("Disordered channel: " + chB);
	print("   Lower threshold value: " + t);
	if (chC != "none") {
		print("Channel IF: " + chC);
		print("   Lower threshold value IF mask: " + tc); }
	print("");
	print("GP images (" + (listGP.length-1) + ") saved at:");
	print("  " + GP_images_Dir);
	print("G factor: " + Gf);
	print("Scale color for GP images: " + lut1);
	print("");
	if (chC != "none") {
		listGP2 = getFileList(GP_IF_images_Dir);
		print("GP-IF images (" + (listGP2.length-1) + ") saved at:");
		print("  " + GP_IF_images_Dir);
		print(""); }
	if (HSB=="Yes") {
		print("HSB images saved at:");
		print("  " + HSB_Dir);
		print("");
	}
	print("");
	print("Execution time: " + d2s(TOTALtime,2) + " seg.");
	print("");
	print("Analysis date: "+DayNames[dayOfWeek]+", "+dayOfMonth+" "+MonthNames[month]+" "+year+" - "+hours+":"+minutes);
	print("ImageJ version " + getVersion());

	selectWindow("Log");
	saveAs("Text", results_Dir + "Log GP images generation "+year+"-"+months+"-"+dayOfMonths+" "+hours+"h"+minutes);

	setBatchMode("exit and display");	
}

Let me know if you have any trouble running it. Good luck!
Praveen


#38

Thanks for sharing, @Praveen!

Note that I just edited your post to use code formatting instead of quotations.
You can use the button to format your code, or add triple-backticks (```) above and below the code. For the latter, you can add syntax highlighting by adding the language name after the opening backticks (use javascript for ImageJ macros).

(To see what I changed in my edit, click on the little orange pencil icon and switch to Raw view.)

See also:


#39

Hi @Praveen,

Thank you for sharing. I have tried your macro, and it works. But I still have one problem: the LUT bar is not in color but in grayscale. Do you have the same issue? Or do I need to apply color to the LUT bar by myself? I am pretty new to imageJ.


#40

Hi, Glad i could help. I normally end up putting the LUT bar on my own :slight_smile: