Convert Macro to plugin

Tags: #<Tag:0x00007fa304fc0b58>


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("		MEASUREMENTS	")

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

Dialog.addMessage("		IMPORTANT	")
Dialog.addMessage("Please select a .tif file");
// 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)
IJ.saveAs(imp,"Tiff",  dir +"/Original " + namenoextension+ ".tif");,"8-bit","");,"Measure","");,"CLAHE ", "blocksize=127 histogram=256 maximum=3");
IJ.saveAs(imp1,"Tiff",  dir +"/Enhanced " + namenoextension+ ".tif");
//edge detection,"Find Edges","");
////// opcional,"edges","");
IJ.saveAs(imp1,"Tiff",  dir +"/Edges " + namenoextension+ ".tif");
//bw mask of enhanced
imp1=IJ.openImage(dir +"/Enhanced " + namenoextension+ ".tif");
Prefs.blackBackground=true;,"Convert to Mask","");
IJ.saveAs(imp2,"Tiff",  dir +"/Binary " + namenoextension+ ".tif");,"Measure","");
//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 ="Add create", imp, imp2);
imp2=IJ.openImage(dir +"/Binary " + namenoextension+ ".tif");
IJ.saveAs(imp3,"Tiff", dir +"/Add B-O " + namenoextension+ ".tif");,"Smooth","");,"Smooth","");,"Remove Outliers...", "radius=20 threshold=50 which=Bright","");
Prefs.blackBackground=true;,"Make Binary","");,"Open","");
IJ.saveAs(imp4,"Tiff", dir +"/Opened " + namenoextension+ ".tif");
//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 ="Add create", imp1, imp4);
IJ.saveAs(imp5," Tiff", dir +"/And E-O" + namenoextension+ ".tif");
// added////,"Smooth","");,"Smooth","");,"Make Binary","");,"Outline","");,"Fill Holes","");,"Measure","");
//till here///
Prefs.blackBackground=true;,"Make Binary","");,"Open","");
IJ.saveAs(imp6," Tiff", dir +"/Processed " + namenoextension+ ".tif");
//Enhance contrast and find maxima"Set Measurements...", "area mean area_fraction limit display add redirect=None decimal=2");"Set Scale...", "distance=0 known=0 pixel=1 unit=pixel");
setAutoThreshold("Default dark");"Threshold...");
IJ.setAutoThreshold("Default dark");
IJ.setThreshold(0, 125);
Prefs.blackBackground=true;,"Convert to Mask","");
IJ.saveAs(imp7,"Tiff", dir +"/Stained area(%)" + namenoextension+ ".tif");, "Measure","");
IJ.saveAs(imp7,"results", dir +"/Stained area (%) " + namenoextension+ ".csv");
// image  processing- From stained area to particles,"Enhance Contrast...", "saturated=0.3");,"Find Maxima...", "noise=20 output=[Single Points] exclude light");
IJ.saveAs(imp8,"Tiff",  dir +"/Particles " + namenoextension+ ".tif");,"Analyze Particles...", "size=0-50 circularity=0.50-1.00 show=Outlines display exclude include summarize add in_situ");,"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")
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 ="XOR create", img5, img6);
IJ.saveAs(imp10,"Tiff", dir +"/Original labeled" + namenoextension+ ".tif");

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




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.): 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??



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.



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: