Creating ImageJ Macros with User Inputs

ImageJ is a wonderful, free program that has become somewhat of a standard for image processing and quantification.  The applications of this program are only limited by one’s imagination and familiarity of the internal functions…but that doesn’t mean everyone needs to be highly proficient to use it. Creating GUI (Graphical User-Input) macros, where the user is prompted with inputs and guided through a workflow, is a great way to create tools accessible to all. This blog will walk through the creation of a macro that asks the user to make alterations to an image before saving to a new folder… in less than 35 lines of code!

Figure 1: The Dialog functions are used to both the user input prompt, and extract the information once submitted.  

The Dialog functions (Figure 1) can be used to extract path directories, strings, and even create drop-down menus.  In lines 2-5 c, a directory path is initialized, the Dialog is named, a “Browse” option is made available to select the input path, and the option to bin the image is provided. Once all the Dialog options have been established, the Dialog.show function is used to generate the GUI.  The Dialog.getString function is then used in lines 9&10 to extract the information from the Dialog in the order it was presented.

creatinglist

Figure 2: Creating lists and folders are essential to creating a macro that is completely automated!  

Now that all the user-input information has been satisfied, we can move forward with establishing the list of input images and pointing the resulting images into a new folder (Figure 2). The list of images in the directory is first initialized, then alphabetically sorted (lines 13-14). A subdirectory is generated, if it does not exist already, where the figures will be saved (lines 17-21).

Figure 3: Changing the main functions within this loop is a great way to create your own custom application

Lastly, we loop through the images in the list (line 24), open and bin the image (lines 26-28) gives the user the opportunity to make any alterations before saving (line 32) and closing the image (line 33). The waitForUser (line 30) is a built-in function of imageJ that alerts the user that some action is required and will pause the loop until the user selects “OK”; you can also exit the macro by selecting “Cancel” (Figure 3). In this macro, it is expected that the user makes some alterations to the image, like cropping, changing the contrast, or even making annotations for the purpose of creating representative figures.

The built-in macro functions within ImageJ are not limited to the ones above- there are an abundance of functions for dealing with arrays, filtering images, creating user inputs, file navigation, and so on. All the internal functions and their descriptions can be found here.

The Image Analysis team at Visikol is extremely proficient in the use of imageJ, including creating custom applications by combining the imageJ macro language with python (known as Jython). If you need a custom application, or have a unique challenge that you would like to tackle using imageJ, reach out today.

2022-10-04T08:46:10-05:00Tags: , , |

Share This Page, Choose Your Platform!