Convert Macro to plugin

imagej
plugins

#1

Hello everyone, I am learning how to convert macros to plugins. I have read a tutorial but I did not understand whow to create dialogs or call ROI manager.
Here I copy my code, please note that I have not created any classes or loops to shorten the code, it is my first try. Any help is welcomed!!!

//Welcome dialog
Dialog.create("TH ab");
Dialog.addMessage("\n")
Dialog.addMessage("		MEASUREMENTS	")

Dialog.addMessage("* Total Neuron Count")
Dialog.addMessage("* % Stained Area")
Dialog.addMessage("* Labeled image")

Dialog.addMessage("		IMPORTANT	")
Dialog.addMessage("Please select a .tif file")
Dialog.show();
// file selection 
path = File.openDialog("Select a File");
  dir = File.getParent(path);
  name = File.getName(path);
  namenoextension = File.nameWithoutExtension
  print("Path:", path);
  print("Name:", name);
  print("Directory:", dir);
  list = getFileList(dir);
  print("Directory contains "+list.length+" files");
// image settings (pre processing)
imp=IJ.openImage(path);
IJ.saveAs(imp,"Tiff",  dir +"/Original " + namenoextension+ ".tif");
IJ.run(imp,"8-bit","");
IJ.run(imp,"Measure","");
IJ.run(imp,"CLAHE ", "blocksize=127 histogram=256 maximum=3");
IJ.saveAs(imp1,"Tiff",  dir +"/Enhanced " + namenoextension+ ".tif");
//edge detection
IJ.run(imp,"Find Edges","");
////// opcional IJ.run(imp,"edges","");
IJ.saveAs(imp1,"Tiff",  dir +"/Edges " + namenoextension+ ".tif");
//bw mask of enhanced
imp1=IJ.openImage(dir +"/Enhanced " + namenoextension+ ".tif");
Prefs.blackBackground=true;
IJ.run(imp1,"Convert to Mask","");
IJ.saveAs(imp2,"Tiff",  dir +"/Binary " + namenoextension+ ".tif");
IJ.run(imp2,"Measure","");
imp.close("*");
//Image calculator ADD Binary and Original
// Open images
ic = new ImageCalculator();
imp = WindowManager.getImage(dir +"/Original " + namenoextension+ ".tif");
imp2 = WindowManager.getImage(dir +"/Binary " + namenoextension+ ".tif");
imp3 = ic.run("Add create", imp, imp2);
imp3.show()
imp2=IJ.openImage(dir +"/Binary " + namenoextension+ ".tif");
IJ.saveAs(imp3,"Tiff", dir +"/Add B-O " + namenoextension+ ".tif");
IJ.run(imp3,"Smooth","");
IJ.run(imp3,"Smooth","");
IJ.run(imp3,"Remove Outliers...", "radius=20 threshold=50 which=Bright","");
Prefs.blackBackground=true;
IJ.run(imp3,"Make Binary","");
IJ.run(imp3,"Open","");
IJ.saveAs(imp4,"Tiff", dir +"/Opened " + namenoextension+ ".tif");
imp3.close("*");
imp4.close("*");
//Image calculator AND  Edges and Original
// Open images
ic = new ImageCalculator();
imp1 = WindowManager.getImage(dir +"/Edges " + namenoextension+ ".tif");
imp4 = WindowManager.getImage(dir +"/Opened " + namenoextension+ ".tif");
imp5 = ic.run("Add create", imp1, imp4);
imp5.show()
IJ.saveAs(imp5," Tiff", dir +"/And E-O" + namenoextension+ ".tif");
// added////
IJ.run(imp5,"Smooth","");
IJ.run(imp5,"Smooth","");
IJ.run(imp5,"Make Binary","");
IJ.run(imp5,"Outline","");
IJ.run(imp5,"Fill Holes","");
IJ.run(imp5,"Measure","");
//till here///
Prefs.blackBackground=true;
IJ.run(imp5,"Make Binary","");
IJ.run(imp5,"Open","");
IJ.saveAs(imp6," Tiff", dir +"/Processed " + namenoextension+ ".tif");
//Enhance contrast and find maxima
IJ.run("Set Measurements...", "area mean area_fraction limit display add redirect=None decimal=2");
IJ.run("Set Scale...", "distance=0 known=0 pixel=1 unit=pixel");
setAutoThreshold("Default dark");
IJ.run("Threshold...");
IJ.setAutoThreshold("Default dark");
IJ.setThreshold(0, 125);
Prefs.blackBackground=true;
IJ.run(imp6,"Convert to Mask","");
IJ.saveAs(imp7,"Tiff", dir +"/Stained area(%)" + namenoextension+ ".tif");
IJ.run(imp7, "Measure","");
IJ.saveAs(imp7,"results", dir +"/Stained area (%) " + namenoextension+ ".csv");
// image  processing- From stained area to particles
IJ.run(imp7,"Enhance Contrast...", "saturated=0.3");
IJ.run(imp7,"Find Maxima...", "noise=20 output=[Single Points] exclude light");
Prefs.blackBackground=true;
IJ.run(imp7,"Dilate","");
IJ.run(imp7,"Dilate","");
IJ.saveAs(imp8,"Tiff",  dir +"/Particles " + namenoextension+ ".tif");
IJ.run(imp8,"Analyze Particles...", "size=0-50 circularity=0.50-1.00 show=Outlines display exclude include summarize add in_situ");
IJ.run(imp8,"Measure","");
IJ.saveAs(imp9,"results", dir +"/ Counts " + namenoextension+ ".csv");
IJ.saveAs(imp9,"Tiff", dir +"/ Counts" + namenoextension+ ".tif");


roiManager("Show All with labels");
roiManager("save", dir +"/ROI " + namenoextension+ ".zip")
imp6.close();
imp7.close();
imp8.close();
imp9=IJ.openImage(dir + "/ Counts" + namenoextension + ".tif");

// Do calculation
ic = new ImageCalculator();
imp5 = WindowManager.getImage(dir +"/And E-O" + namenoextension+ ".tif");
imp6 = WindowManager.getImage(dir +"/Processed " + namenoextension+ ".tif");
imp10 = ic.run("XOR create", img5, img6);
imp10.show()
IJ.saveAs(imp10,"Tiff", dir +"/Original labeled" + namenoextension+ ".tif");

////Ending dialog and close
Dialog.create("Analysis status");
Dialog.addMessage("\n")
Dialog.addMessage("	       FINISHED")
Dialog.show();

close();
close("*");
IJ.run("Close");

#2

@Paubonaccorso

Welcome to the Forum! So great you are writing macros and now wanting to build plugins. What tutorial did you read that you didn’t find helpful?

This is really the place to start when deciding to develop/contribute to the ImageJ project (via writing plugins etc.): http://imagej.net/Development You can navigate all the side menus as you see fit - there is so much info in here - it can feel a bit daunting.

So start with:

But honestly - why do you feel you need to create a plugin for this macro? Is it functioning fine for you as is? You can always check out the Scripting section of the ImageJ wiki… and simply refine it with Script Parameters (will help call dialogs as you requested) and then add your script to the plugin drop down menu. Would that be enough??

eta


#3

Thank you very much for answering. I feel lost trying to write the plugin . Honestly, it works but I need to do it in order to add statistical analysis for further processing.
I have tried to write it in netbeans but I can`t download the IJ. modules.

I will continue trying. Thanks for your kindly help.


#4

@Paubonaccorso

If you have specific problems or questions with developing ImageJ in NetBeans - you can always post a new thread. There are developers here that can help you.

eta :slight_smile: