-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConversions.java
282 lines (217 loc) · 9.43 KB
/
Conversions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package de.mpicbg.scf.segtools;
import ij.IJ;
import ij.ImagePlus;
import ij.gui.Roi;
import ij.plugin.filter.ThresholdToSelection;
import ij.plugin.frame.RoiManager;
import ij.process.ImageProcessor;
import java.util.ArrayList;
import java.util.List;
/*
* Author: Noreen Walker, Scientific Computing Facility, MPI-CBG
* Date: 2019-10
*/
/**
* static utility functions to convert between list of 2d rois and 3d binary mask and 3d label images
*/
public class Conversions {
/**
* Creates a binary image mask of all roi's stored in roi manager. Works in 2D & 3D.
*
* @param rm roi manager with rois
* @param imp sets the target size of mask.
* @param associate if true the ROIs are only drawn in the associated slices, otherwise they are drawn in all slices.
* @return mask. 8 bit binary mask with foreground (=roi regions) value 255, background=0.
* mask is calibrated like imp.
*/
static public ImagePlus BinaryMaskFromRois(RoiManager rm, ImagePlus imp, boolean associate) {
ImagePlus mask = IJ.createImage("binary mask", "8-bit black", imp.getWidth(), imp.getHeight(), imp.getNSlices());
mask.setCalibration(imp.getCalibration());
int numrois = rm.getCount();
for (int idx = 0; idx < numrois; idx++) {
Roi roi = rm.getRoi(idx);
UpdateMask(mask,roi,associate,idx);
}
mask.setSlice(1);
return mask;
}
/**
* Creates a binary image mask of all roi's stored in roi manager. Works in 2D & 3D.
*
* @param rm roi manager with rois
* @param width, height, nslices: target dimensions
* @param associate if true the ROIs are only drawn in the associated slices, otherwise they are drawn in all slices.
* @return mask. 8 bit binary mask with foreground (=roi regions) value 255, background=0. uncalibrated.
*/
static public ImagePlus BinaryMaskFromRois(RoiManager rm, int width, int height, int nslices, boolean associate) {
if (width < 1 || height < 1 || nslices < 1) {
IJ.log("Target size for mask is zero or negative! Returning null");
return null;
}
ImagePlus mask = IJ.createImage("binary mask", "8-bit black", width, height, nslices);
int numrois = rm.getCount();
for (int idx = 0; idx < numrois; idx++) {
Roi roi = rm.getRoi(idx);
UpdateMask(mask, roi, associate, idx);
}
mask.setSlice(1);
return mask;
}
/**
* Creates a binary image mask of all roi's in the roi array. Works in 2D & 3D.
* Rois for a 3D mask must be associated with a slices.
*
* @param roiarray Array of rois
* @param width, height, nslices: target dimensions
* @param associate if true the ROIs are only drawn in the associated slices, otherwise they are drawn in all slices.
* @return mask. 8 bit binary mask with foreground (=roi regions) value 255, background=0. uncalibrated
*/
static public ImagePlus BinaryMaskFromRois(Roi[] roiarray, int width, int height, int nslices, boolean associate) {
if (width < 1 || height < 1 || nslices < 1) {
IJ.log("Target size for mask is zero or negative! Returning null");
return null;
}
ImagePlus mask = IJ.createImage("binary mask", "8-bit black", width, height, nslices);
int numrois = roiarray.length;
for (int idx = 0; idx < numrois; idx++) {
Roi roi = roiarray[idx];
UpdateMask(mask, roi, associate, idx);
}
mask.setSlice(1);
return mask;
}
/**
* Creates a binary image mask of all roi's in the roi array. Works in 2D & 3D.
* Rois for a 3D mask must be associated with a slices.
*
* @param roiarray Array of rois
* @param imp: sets target dimensions of mask
* @param associate if true the ROIs are only drawn in the associated slices, otherwise they are drawn in all slices.
* @return mask. 8 bit binary mask with foreground (=roi regions) value 255, background=0.
* mask is calibrated like imp.
*/
static public ImagePlus BinaryMaskFromRois(Roi[] roiarray, ImagePlus imp, boolean associate) {
ImagePlus mask = IJ.createImage("binary mask", "8-bit black", imp.getWidth(), imp.getHeight(), imp.getNSlices());
mask.setCalibration(imp.getCalibration());
int numrois = roiarray.length;
for (int idx = 0; idx < numrois; idx++) {
Roi roi = roiarray[idx];
UpdateMask(mask, roi, associate, idx);
}
mask.setSlice(1);
return mask;
}
/** Helper for the the BinaryMaskFromROIs function family. Draws a single roi.
*/
private static void UpdateMask(ImagePlus mask, final Roi roi, final boolean associate, final int idx) {
int nslices=mask.getNSlices();
int roislice = roi.getZPosition();
// check if roi has an associated slice
boolean associateThisRoi=associate;
if (associate && nslices > 1 && roislice == 0) {
IJ.log("Warning: Roi " + idx + " is not associated to a specific slice. Drawing into all slices");
associateThisRoi=false;
}
// draw roi into single slice
if (associateThisRoi) {
if (roislice<=nslices){
mask.setSliceWithoutUpdate(roislice);
ImageProcessor maskip = mask.getProcessor();
maskip.setValue(255);
maskip.fill(roi);
}
}
else {
// draw into all slices
for (int sliceid = 1; sliceid < 1+nslices; sliceid++) {
mask.setSlice(sliceid);
ImageProcessor maskip = mask.getProcessor();
maskip.setValue(255);
maskip.fill(roi);
}
}
}
/**
* Creates a ROI from each slice in a binary image and adds the ROIs (associated to slices) to the ROI manager.
* Null Rois (from black slices) are skipped.
* Works in 2D & 3D
*
* @param rm Roi manager where Rois will be stored
* @param mask binary image (e.g. 0 background, 255 foreground). Image threshold for roi creation is internally set
* to 1, therefore a label image as input works as well.
*/
static public void RoisFromBinaryMask(RoiManager rm, ImagePlus mask) {
int nSlices = mask.getNSlices();
for (int slice = 1; slice < nSlices + 1; slice++) {
mask.setSliceWithoutUpdate(slice);
ImageProcessor ip = mask.getProcessor();
// threshold at 1
ip.setThreshold(1, ip.maxValue(), ImageProcessor.NO_LUT_UPDATE); // maxValue() = max possible value
Roi roi = new ThresholdToSelection().convert(ip);
if (roi != null) {
if (nSlices > 1) {
roi.setPosition(1, slice, 1);
}
rm.add(mask, roi, -1);
}
}
}
/**
* Creates a ROI from each slice in a binary image and returns the ROIs (associated to slices) as an array.
* Null Rois (from black slices) are skipped.
* Works in 2D & 3D
*
* @param mask binary image (e.g. 0 background, 255 foreground). Image threshold for roi creation is internally set
* to 1, therefore a label image as input works as well.
* @return roiarray an array with all roi's
*/
static public Roi[] RoisFromBinaryMask(ImagePlus mask) {
List<Roi> roiList = new ArrayList<>();
int nSlices = mask.getNSlices();
for (int slice = 1; slice < nSlices + 1; slice++) {
mask.setSliceWithoutUpdate(slice);
ImageProcessor ip = mask.getProcessor();
// threshold at 1
ip.setThreshold(1, ip.maxValue(), ImageProcessor.NO_LUT_UPDATE); // maxValue() = max possible value
Roi roi = new ThresholdToSelection().convert(ip);
if (roi != null) {
if (nSlices > 1) {
roi.setPosition(1, slice, 1);
}
roiList.add(roi);
}
}
// convert to array
Roi[] roiArray = roiList.toArray(new Roi[roiList.size()]);
return roiArray;
}
/**
* Like RoisFromBinaryMask(ImagePlus mask) but creates Rois for a single label from a label image.
* Rois are returned as array and associated with slices. Null Rois (from black slices) are skipped.
* Works in 2D & 3D
*
* @param labelImp label image (connected components) with regions of value 1,2,3,....
* @param labelId region for which Rois are extracted
* @return roiarray an array with all roi's
*/
static public Roi[] RoisFromOneLabel(ImagePlus labelImp, int labelId) {
List<Roi> roiList = new ArrayList<>();
int nSlices = labelImp.getNSlices();
for (int slice = 1; slice < nSlices + 1; slice++) {
labelImp.setSliceWithoutUpdate(slice);
ImageProcessor ip = labelImp.getProcessor();
// threshold at labelId
ip.setThreshold(labelId, labelId+0.1, ImageProcessor.NO_LUT_UPDATE);
Roi roi = new ThresholdToSelection().convert(ip);
if (roi != null) {
if (nSlices > 1) {
roi.setPosition(1, slice, 1);
}
roiList.add(roi);
}
}
// convert to array
Roi[] roiArray = roiList.toArray(new Roi[roiList.size()]);
return roiArray;
}
}