This notebook is part of the PyImageJ Tutorial Series, and assumes familiarity with the ImageJ API. Dedicated tutorials for ImageJ can be found here.

8 Discover ImageJ commands with the Recorder

The original ImageJ contains many useful commands that can be hard to use in PyImageJ without some prior knowledge on the their parameters. This notebook demonstrates how to use ImageJ’s Recorder feature to record ImageJ commands in a supported language (IJ Macro, BeanScript, Java and JavaScript).

8.1 Example 1: Apply “Find Maxima…” command to an image

In this example we will use “Find Maxima” on a test image. Once the image is loaded, open the Recorder (see image below) and begin running commands to capture the code lines.

Open macro recorder

Now that the Recorder window is open and listening for commands, set the recorder language to either Java or JavaScript before running the “Find Maxima…” command. Next run the “Find Maxima…” command (Process > Find Maxima…) to capture the code lines.

Macro recorder

Running “Find Maxima…” (set to prominence=1000 and Point Selection) on test_still.tif results in 21 detections which are then overlayed over the input image.:

Result

Now that we have the Java code for the “Find Maxima…” command we can replicate this workflow in PyImageJ. The “Find Maxima…” command will overlay detections ontop of a displayed input image, therefore we will need to initialize ImageJ in interactive mode. Please note that MacOS users will have to change the mode to gui due to architecture limitations. For more information please visit the initialization documentation.

import imagej

# initialize ImageJ2
ij = imagej.init(mode='headless')
print(f"ImageJ2 version: {ij.getVersion()}")
ImageJ2 version: 2.14.0/1.54f

Because the “Find Maxima…” and other original ImageJ commands work with the ImagePlus object type (instead of the newer ImageJ2/ImgLib2 Dataset and ImgPlus object types) we need to first convert the Dataset returned from ij.io().open() to an ImagePlus.

# open test image and convert from Dataset to ImagePlus
dataset = ij.io().open('sample-data/test_still.tif')
imp = ij.py.to_imageplus(dataset)

# show the image
ij.py.show(imp)
Operating in headless mode - the original ImageJ will have limited functionality.
_images/3c229b73635dae0d5ce02ad5a4eed2d781ce436c285591134f6e83811f356272.png

Next, show the image with ImageJ and run Find Maxima... using the same Java syntax generated from the recorder.

# show image and then find maxima
imp.getProcessor().resetMinAndMax()
ij.ui().show(imp)
ij.IJ.run(imp, "Find Maxima...", "prominence=1000 output=[Point Selection]")
Operating in headless mode - the IJ class will not be fully functional.
[INFO] test_still.tif = net.imagej.display.DefaultDatasetView@1e4c6583
[INFO] null = img["test_still.tif" (-2), 16-bit, 300x300x1x1x1]

8.2 Example 2: Extract a slice and run “Analyze Particles…”

Let’s try a more complicated example next. This Java code was generated with the ImageJ Recorder while analyzing some data.

imp = IJ.openImage("sample-data/test_timeseries.tif");
imp2 = new Duplicator().run(imp, 3, 3, 1, 1, 14, 14);
IJ.run(imp, "Enhance Contrast", "saturated=0.35");
IJ.setAutoThreshold(imp, "Moments dark");
IJ.run(imp, "Analyze Particles...", "  show=Overlay display clear");

The Java code takes the test_timeseries.tif sample data (4D: [X, Y, Channel, Time]) and performs the following operations:

  1. Open the test data.

  2. Duplicate channel 3, frame 14 (extracts a single still from the timeseries).

  3. Enhance the contrast of the image.

  4. Threshold with “Moments dark”.

  5. Analyze particles and display results via overlay.

Results:

Analyze Particles

Note that in this example we will use the orginal ImageJ’s image opener (IJ.openImage()) instead of ImageJ2’s (ij.io().open()). The original ImageJ’s opener is more limited than ImageJ2’s however it will return an ImagePlus image object instead of a Dataset, thus no conversion step is needed like in the previous example.

Just like in the Example 1 the Java code generated from the Recorder can be typically used with little to no modification for language syntax (take note of the different syntax needed to use the Duplicator):

from scyjava import jimport

# get ImageJ's duplicator
Duplicator = jimport('ij.plugin.Duplicator')

# run ImageJ commands
imp_timeseries = ij.IJ.openImage("sample-data/test_timeseries.tif")
imp_extract = Duplicator().run(imp_timeseries, 3, 3, 1, 1, 14, 14) # visit the Javadoc for more info https://imagej.nih.gov/ij/developer/api/ij/ij/plugin/Duplicator.html
ij.IJ.run(imp_extract, "Enhance Contrast", "saturated=0.35")
ij.ui().show(imp_extract)
ij.IJ.setAutoThreshold(imp_extract, "Moments dark")
ij.IJ.run(imp_extract, "Analyze Particles...", " show=Overlay display clear")
[INFO] null = img["DUP_test_timeseries.tif" (-5), 16-bit, 250x250x1x1x1]
 	Area	Mean	Min	Max	X	Y	Circ.	Feret	FeretX	FeretY	FeretAngle	MinFeret	AR	Round	Solidity
1	0.211	1293	1141	1445	48.588	13.975	1.000	0.727	149	42	116.565	0.325	2.000	0.500	1.000
2	0.739	1216.143	1085	1473	28.716	14.927	0.800	1.340	88	44	104.036	0.975	1.387	0.721	0.737
3	0.106	1333.000	1333	1333	25.838	16.738	1.000	0.460	79	51	135.000	0.325	1.000	1.000	1.000
4	0.528	1193.400	1099	1295	31.753	17.518	1.000	1.172	96	53	146.310	0.650	1.553	0.644	0.909
5	0.317	1241.667	1087	1384	43.604	17.604	1.000	0.919	133	55	45.000	0.650	1.464	0.683	0.857
6	180.091	1513.540	1083	4462	52.667	27.076	0.077	32.384	112	68	148.543	13.747	2.528	0.396	0.542
7	0.106	1104.000	1104	1104	58.988	17.388	1.000	0.460	181	53	135.000	0.325	1.000	1.000	1.000
8	3.803	1478.194	1085	2510	29.728	19.121	0.600	3.250	89	54	126.870	2.103	1.913	0.523	0.742
9	0.211	1268.000	1161	1375	50.213	17.875	1.000	0.727	154	54	116.565	0.325	2.000	0.500	1.000
10	0.211	1171.000	1108	1234	25.838	18.200	1.000	0.727	79	55	116.565	0.325	2.000	0.500	1.000
11	0.106	1257.000	1257	1257	32.663	18.038	1.000	0.460	100	55	135.000	0.325	1.000	1.000	1.000
12	0.423	1371.250	1186	1686	32.825	19.175	1.000	0.919	100	58	135.000	0.650	1.000	1.000	1.000
13	0.317	1140.667	1109	1157	43.929	19.446	1.000	0.919	134	59	135.000	0.650	1.464	0.683	0.857
14	0.423	1187.500	1097	1241	54.925	19.825	1.000	0.919	168	60	135.000	0.650	1.000	1.000	1.000
15	0.528	1182.800	1132	1312	55.998	20.053	0.873	1.379	171	63	45.000	0.872	1.882	0.531	0.714
16	0.423	1302.500	1117	1634	21.938	20.556	1.000	1.028	66	63	161.565	0.650	1.468	0.681	0.800
17	0.739	1215.286	1102	1352	57.223	20.684	0.898	1.340	174	63	165.964	0.975	1.387	0.721	0.737
18	0.423	1234.500	1192	1276	49.075	21.125	1.000	0.919	150	64	135.000	0.650	1.000	1.000	1.000
19	1.479	1192.214	1086	1398	50.468	21.473	0.587	2.081	153	68	38.660	1.453	1.436	0.697	0.737
20	0.845	1422.125	1150	1897	22.750	21.938	1.000	1.340	68	67	165.964	0.975	1.195	0.837	0.800
21	0.211	1116.500	1113	1120	55.088	21.775	1.000	0.727	169	66	116.565	0.325	2.000	0.500	1.000
22	0.211	1141.500	1128	1155	27.463	22.425	1.000	0.727	84	68	116.565	0.325	2.000	0.500	1.000
23	0.106	1168.000	1168	1168	19.013	22.588	1.000	0.460	58	69	135.000	0.325	1.000	1.000	1.000
24	0.106	1163.000	1163	1163	39.813	22.588	1.000	0.460	122	69	135.000	0.325	1.000	1.000	1.000
25	0.211	1126.000	1102	1150	59.150	23.075	0.785	0.919	181	72	45.000	0.460	2.646	0.378	0.667
26	0.423	1222.750	1086	1383	34.775	23.400	1.000	0.919	106	71	135.000	0.650	1.000	1.000	1.000
27	22.498	1762.784	1084	5446	26.759	25.966	0.317	8.387	74	90	35.538	5.378	1.747	0.573	0.652
28	1.162	1330.364	1092	1829	32.278	24.951	0.976	1.625	97	78	36.870	1.300	1.147	0.871	0.815
29	0.634	1167.667	1110	1227	37.375	26.163	1.000	1.172	114	79	123.690	0.650	1.500	0.667	1.000
30	0.739	1328.571	1137	1482	18.641	27.138	0.564	1.750	55	82	158.199	0.788	2.898	0.345	0.636
31	1.162	1216.818	1089	1455	23.031	27.403	0.691	1.895	69	82	120.964	1.149	2.046	0.489	0.647
32	0.106	1124.000	1124	1124	48.263	27.788	1.000	0.460	148	85	135.000	0.325	1.000	1.000	1.000
33	0.317	1166.333	1101	1208	64.296	28.004	1.000	0.919	197	85	135.000	0.650	1.464	0.683	0.857
34	0.106	1092.000	1092	1092	48.588	28.438	1.000	0.460	149	87	135.000	0.325	1.000	1.000	1.000
35	0.106	1136.000	1136	1136	43.063	28.763	1.000	0.460	132	88	135.000	0.325	1.000	1.000	1.000
36	0.211	1106.000	1092	1120	40.788	29.250	1.000	0.727	125	89	116.565	0.325	2.000	0.500	1.000
37	3.063	1362.310	1083	2069	20.772	30.219	0.705	2.907	63	98	63.435	1.950	1.463	0.684	0.817
38	0.106	1088.000	1088	1088	29.413	29.413	1.000	0.460	90	90	135.000	0.325	1.000	1.000	1.000
39	0.211	1150.500	1104	1197	44.850	29.413	1.000	0.727	137	90	153.435	0.325	2.000	0.500	1.000
40	0.211	1123.500	1110	1137	49.563	29.900	1.000	0.727	152	91	116.565	0.325	2.000	0.500	1.000
41	1.901	1434.944	1103	2129	41.600	31.038	1.000	2.081	126	98	51.340	1.300	1.362	0.734	0.947
42	0.211	1128.500	1100	1157	24.700	30.713	1.000	0.727	75	94	153.435	0.325	2.000	0.500	1.000
43	8.344	1414.139	1091	2321	44.638	33.275	0.353	6.106	134	111	64.799	3.417	2.518	0.397	0.568
44	0.951	1351.111	1134	1652	48.479	31.074	1.000	1.453	147	95	153.435	0.975	1.340	0.746	0.857
45	0.951	1435.444	1088	1798	64.838	31.038	1.000	1.379	198	94	135.000	0.975	1.000	1.000	1.000
46	0.528	1161.400	1106	1248	28.178	31.233	1.000	1.172	85	97	33.690	0.650	1.553	0.644	0.909
47	0.528	1120.200	1102	1144	49.758	31.428	1.000	1.172	152	98	56.310	0.650	1.553	0.644	0.909
48	0.528	1277.400	1110	1547	21.743	31.623	1.000	1.172	66	99	56.310	0.650	1.553	0.644	0.909
49	0.951	1182.667	1088	1279	27.643	32.735	0.832	1.750	84	103	68.199	0.650	2.386	0.419	0.947
50	0.211	1184.000	1110	1258	48.425	32.338	1.000	0.727	148	99	153.435	0.325	2.000	0.500	1.000
51	3.274	1360.774	1098	1866	19.453	33.805	0.795	3.083	55	106	18.435	1.625	1.947	0.514	0.873
52	0.423	1267.250	1116	1458	41.031	33.313	0.857	1.028	126	101	108.435	0.650	1.468	0.681	0.800
53	3.908	1232.703	1083	1597	54.894	34.577	0.272	3.500	164	103	158.199	2.758	1.327	0.754	0.565
54	11.302	1550.327	1096	2918	48.742	37.273	0.583	6.629	149	125	78.690	2.600	2.647	0.378	0.849
55	0.211	1088.500	1087	1090	57.525	34.288	1.000	0.727	176	105	153.435	0.325	2.000	0.500	1.000
56	0.106	1138.000	1138	1138	59.638	34.288	1.000	0.460	183	105	135.000	0.325	1.000	1.000	1.000
57	1.901	1470.667	1102	2386	21.847	35.624	1.000	1.895	66	107	120.964	1.609	1.130	0.885	0.857
58	0.106	1084.000	1084	1084	57.038	35.263	1.000	0.460	175	108	135.000	0.325	1.000	1.000	1.000
59	1.373	1266.846	1145	1573	64.263	35.538	0.956	1.895	195	108	149.036	0.975	1.796	0.557	0.929
60	2.113	1330.600	1131	1581	54.584	37.278	0.898	2.344	166	118	56.310	1.532	1.701	0.588	0.851
61	2.958	1209.321	1083	1670	58.187	37.607	0.667	2.796	176	112	125.538	1.912	1.573	0.636	0.737
62	1.056	1364.300	1106	1748	65.033	37.148	0.887	1.625	199	112	126.870	1.149	1.587	0.630	0.800
63	0.423	1334.500	1289	1374	19.500	37.375	1.000	0.919	59	114	135.000	0.650	1.000	1.000	1.000
64	0.106	1141.000	1141	1141	17.388	37.863	1.000	0.460	53	116	135.000	0.325	1.000	1.000	1.000
65	1.162	1330.818	1083	1694	52.103	38.601	0.976	1.625	158	120	36.870	1.300	1.147	0.871	0.815
66	0.423	1250.500	1131	1375	54.113	39.081	1.000	1.028	165	120	161.565	0.650	1.468	0.681	0.800
67	0.423	1182.250	1102	1337	51.594	39.813	0.857	1.028	158	121	108.435	0.650	1.468	0.681	0.800
68	0.211	1120.500	1120	1121	21.775	39.813	1.000	0.727	66	122	153.435	0.325	2.000	0.500	1.000
69	0.106	1097.000	1097	1097	36.888	40.138	1.000	0.460	113	123	135.000	0.325	1.000	1.000	1.000
70	25.561	1346.455	1084	2808	62.117	45.300	0.197	10.205	178	149	52.765	5.891	1.883	0.531	0.593
71	1.162	1133.455	1084	1221	36.976	41.142	0.333	2.538	111	129	39.806	1.117	3.160	0.316	0.537
72	6.338	1389.550	1113	2426	59.508	41.540	0.600	4.420	177	127	162.897	2.275	1.781	0.561	0.822
73	0.634	1148.333	1089	1231	45.717	41.329	0.686	1.625	139	129	53.130	0.727	2.778	0.360	0.706
74	1.796	1360.118	1084	1974	55.202	41.380	0.763	2.081	167	129	38.660	1.625	1.209	0.827	0.791
75	0.951	1351.000	1095	1746	67.113	41.763	1.000	1.379	205	127	135.000	0.975	1.000	1.000	1.000
76	0.106	1113.000	1113	1113	24.538	42.088	1.000	0.460	75	129	135.000	0.325	1.000	1.000	1.000
77	0.634	1129.167	1089	1212	35.913	43.496	0.769	1.379	109	132	135.000	0.975	1.295	0.772	0.750
78	0.106	1108.000	1108	1108	25.838	44.038	1.000	0.460	79	135	135.000	0.325	1.000	1.000	1.000
79	0.106	1090.000	1090	1090	59.638	44.688	1.000	0.460	183	137	135.000	0.325	1.000	1.000	1.000
80	0.106	1105.000	1105	1105	26.813	45.013	1.000	0.460	82	138	135.000	0.325	1.000	1.000	1.000
81	0.106	1138.000	1138	1138	31.688	45.013	1.000	0.460	97	138	135.000	0.325	1.000	1.000	1.000
82	1.584	1573.533	1103	2296	67.156	45.489	0.804	2.081	204	138	141.340	1.300	1.279	0.782	0.833
83	0.106	1108.000	1108	1108	34.938	45.338	1.000	0.460	107	139	135.000	0.325	1.000	1.000	1.000
84	0.211	1103.500	1085	1122	37.050	45.338	1.000	0.727	113	139	153.435	0.325	2.000	0.500	1.000
85	0.106	1092.000	1092	1092	42.088	45.338	1.000	0.460	129	139	135.000	0.325	1.000	1.000	1.000
86	0.211	1103.000	1084	1122	56.550	45.338	1.000	0.727	173	139	153.435	0.325	2.000	0.500	1.000
87	2.852	1268.963	1091	1598	25.705	47.035	0.836	2.796	77	141	125.538	1.838	1.662	0.602	0.806
88	0.423	1100.750	1083	1122	36.806	46.556	0.857	1.172	112	142	146.310	0.650	2.000	0.500	0.800
89	0.106	1110.000	1110	1110	43.063	46.313	1.000	0.460	132	142	135.000	0.325	1.000	1.000	1.000
90	0.317	1136.333	1102	1188	27.354	46.854	1.000	0.919	83	145	45.000	0.650	1.464	0.683	0.857
91	0.106	1126.000	1126	1126	44.038	47.288	1.000	0.460	135	145	135.000	0.325	1.000	1.000	1.000
92	0.106	1116.000	1116	1116	25.188	47.938	1.000	0.460	77	147	135.000	0.325	1.000	1.000	1.000
93	0.317	1141.667	1084	1233	56.279	48.696	1.000	0.919	172	149	135.000	0.650	1.464	0.683	0.857
94	0.106	1083.000	1083	1083	57.038	48.588	1.000	0.460	175	149	135.000	0.325	1.000	1.000	1.000
95	1.479	1183.571	1085	1314	23.354	49.284	0.911	2.180	69	150	153.435	0.975	2.363	0.423	0.875
96	0.211	1138.000	1135	1141	57.200	49.238	1.000	0.727	175	151	153.435	0.325	2.000	0.500	1.000
97	2.429	1494.130	1084	2113	65.643	50.142	0.938	2.344	199	152	146.310	1.609	1.465	0.683	0.836
98	0.211	1125.000	1102	1148	18.850	50.050	0.785	0.919	57	153	135.000	0.460	2.646	0.378	0.667
99	0.211	1098.500	1085	1112	34.125	49.888	1.000	0.727	104	153	153.435	0.325	2.000	0.500	1.000
100	0.423	1157.250	1100	1208	57.200	50.050	1.000	0.919	175	153	135.000	0.650	1.000	1.000	1.000
101	0.739	1273.714	1125	1414	27.370	50.491	1.000	1.379	83	157	45.000	0.975	1.380	0.725	0.875
102	0.634	1142.833	1096	1169	62.075	50.375	0.916	1.453	189	156	26.565	0.650	2.082	0.480	0.857
103	0.211	1127.500	1099	1156	29.900	50.863	1.000	0.727	91	156	153.435	0.325	2.000	0.500	1.000
104	0.739	1239.714	1095	1509	28.345	51.513	1.000	1.172	86	157	123.690	0.975	1.069	0.936	0.875
105	0.106	1091.000	1091	1091	34.938	51.513	1.000	0.460	107	158	135.000	0.325	1.000	1.000	1.000
106	0.845	1167.000	1084	1255	31.159	52.325	0.785	1.625	94	163	36.870	0.919	2.014	0.496	0.727
107	0.211	1085.000	1085	1085	60.125	51.838	1.000	0.727	184	159	153.435	0.325	2.000	0.500	1.000
108	1.479	1309.357	1097	1704	62.307	52.998	0.993	1.750	189	162	158.199	1.300	1.164	0.859	0.824
109	0.528	1316.600	1211	1587	26.098	53.593	1.000	1.172	79	166	33.690	0.650	1.553	0.644	0.909
110	0.951	1314.778	1133	1693	29.124	54.221	1.000	1.453	89	169	63.435	0.975	1.340	0.746	0.857
111	0.423	1148.250	1097	1244	56.144	54.519	0.857	1.172	172	166	123.690	0.650	2.000	0.500	0.800
112	0.423	1146.500	1103	1165	59.719	54.438	0.857	1.028	183	166	108.435	0.650	1.468	0.681	0.800
113	1.373	1357.538	1092	1626	61.513	54.513	1.000	1.750	187	169	21.801	1.300	1.420	0.704	0.812
114	0.106	1156.000	1156	1156	33.313	54.763	1.000	0.460	102	168	135.000	0.325	1.000	1.000	1.000
115	10.563	1473.170	1084	2716	56.940	57.346	0.364	5.592	170	183	54.462	3.627	1.591	0.629	0.725
116	0.211	1161.000	1156	1166	59.963	55.575	1.000	0.727	184	170	116.565	0.325	2.000	0.500	1.000
117	0.106	1095.000	1095	1095	26.488	55.738	1.000	0.460	81	171	135.000	0.325	1.000	1.000	1.000
118	0.106	1101.000	1101	1101	63.863	56.063	1.000	0.460	196	172	135.000	0.325	1.000	1.000	1.000
119	5.176	1373.796	1089	2209	32.397	57.502	0.433	4.275	93	178	8.746	2.432	2.008	0.498	0.645
120	0.317	1266.333	1203	1303	61.046	57.146	1.000	0.919	187	177	45.000	0.650	1.464	0.683	0.857
121	0.528	1288.200	1110	1490	64.253	57.493	1.000	1.172	196	176	146.310	0.650	1.553	0.644	0.909
122	2.113	1429.950	1098	2307	36.465	58.760	0.838	2.538	109	178	140.194	1.498	1.710	0.585	0.833
123	1.584	1249.067	1090	1491	39.033	58.858	0.804	2.081	118	179	141.340	1.300	1.739	0.575	0.769
124	2.007	1558.421	1099	2453	59.638	58.919	1.000	1.895	181	180	149.036	1.625	1.121	0.892	0.864
125	0.106	1112.000	1112	1112	33.963	58.988	1.000	0.460	104	181	135.000	0.325	1.000	1.000	1.000
126	1.056	1243.600	1085	1465	48.750	60.353	0.887	1.838	148	184	135.000	1.149	1.864	0.537	0.800
127	2.218	1518.762	1094	2666	53.261	60.613	1.000	2.180	161	185	153.435	1.609	1.355	0.738	0.857
128	0.211	1172.000	1159	1185	39.650	60.613	1.000	0.727	121	186	153.435	0.325	2.000	0.500	1.000
129	0.634	1275.167	1099	1602	50.808	60.883	1.000	1.172	155	186	123.690	0.919	1.291	0.775	0.800
130	0.106	1131.000	1131	1131	41.113	60.938	1.000	0.460	126	187	135.000	0.325	1.000	1.000	1.000
131	0.106	1277.000	1277	1277	62.563	60.938	1.000	0.460	192	187	135.000	0.325	1.000	1.000	1.000
132	0.106	1119.000	1119	1119	43.063	61.263	1.000	0.460	132	188	135.000	0.325	1.000	1.000	1.000
133	0.317	1191.000	1130	1236	56.171	61.479	1.000	0.919	172	188	135.000	0.650	1.464	0.683	0.857
134	0.106	1163.000	1163	1163	41.438	61.588	1.000	0.460	127	189	135.000	0.325	1.000	1.000	1.000
135	0.211	1141.500	1113	1170	44.688	62.075	1.000	0.727	137	190	116.565	0.325	2.000	0.500	1.000
136	0.106	1119.000	1119	1119	53.463	64.188	1.000	0.460	164	197	135.000	0.325	1.000	1.000	1.000

ImageJ macro language

It is possible to record workflows in the original ImageJ macro language and run those macros in PyImageJ with ij.py.run_macro() (see the07-Running-Macros-Scripts-and-Plugins notebook for more information). While this is functional, we do not recommend using the macro language as it is more fragile and less powerful due to the orignal ImageJ internal operations. For example, macros are single threaded only and do not take advantage of multi-threaded processing. For more information on ImageJ macro limitations please visit the macro wiki page.