/
MasktoRoi.py
176 lines (153 loc) · 6.97 KB
/
MasktoRoi.py
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
#@ File(label='Choose Mask directory', style='directory') maskdir
#@ String(label='File types', value='tif') file_type_image
#@ String(label='Filter', value='._') filter_image
#@ Boolean(label='Recursive search', value=True) do_recursive
#@ File(label='Choose SaveRoi directory', style='directory') roidir
import os
from java.io import File
from ij import IJ
from ij.plugin.frame import RoiManager
from ij import WindowManager as wm
def batch_open_images(pathRoi, pathMask, file_typeImage=None, name_filterImage=None, recursive=False):
'''Open all files in the given folder.
:param path: The path from were to open the images. String and java.io.File are allowed.
:param file_type: Only accept files with the given extension (default: None).
:param name_filter: Reject files that contain the given string (default: wild characters).
:param recursive: Process directories recursively (default: False).
'''
# Converting a File object to a string.
if isinstance(pathMask, File):
pathMask = pathMask.getAbsolutePath()
def check_type(string):
'''This function is used to check the file type.
It is possible to use a single string or a list/tuple of strings as filter.
This function can access the variables of the surrounding function.
:param string: The filename to perform the check on.
'''
if file_typeImage:
# The first branch is used if file_type is a list or a tuple.
if isinstance(file_typeImage, (list, tuple)):
for file_type_ in file_typeImage:
if string.endswith(file_type_):
# Exit the function with True.
return True
else:
# Next iteration of the for loop.
continue
# The second branch is used if file_type is a string.
elif isinstance(file_typeImage, string):
if string.endswith(file_typeImage):
return True
else:
return False
return False
# Accept all files if file_type is None.
else:
return True
def check_filter(string):
'''This function is used to check for a given filter.
It is possible to use a single string or a list/tuple of strings as filter.
This function can access the variables of the surrounding function.
:param string: The filename to perform the filtering on.
'''
if name_filterImage:
# The first branch is used if name_filter is a list or a tuple.
if isinstance(name_filterImage, (list, tuple)):
for name_filter_ in name_filterImage:
if name_filter_ in string:
# Exit the function with True.
return True
else:
# Next iteration of the for loop.
continue
# The second branch is used if name_filter is a string.
elif isinstance(name_filterImage, string):
if name_filterImage in string:
return True
else:
return False
return False
else:
# Accept all files if name_filter is None.
return True
# We collect all files to open in a list.
path_to_Image = []
# Replacing some abbreviations (e.g. $HOME on Linux).
path = os.path.expanduser(pathMask)
path = os.path.expandvars(pathMask)
# If we don't want a recursive search, we can use os.listdir().
if not recursive:
for file_name in os.listdir(pathMask):
full_path = os.path.join(pathMask, file_name)
if os.path.isfile(full_path):
if check_type(file_name):
if check_filter(file_name):
path_to_Image.append(full_path)
# For a recursive search os.walk() is used.
else:
# os.walk() is iterable.
# Each iteration of the for loop processes a different directory.
# the first return value represents the current directory.
# The second return value is a list of included directories.
# The third return value is a list of included files.
for directory, dir_names, file_names in os.walk(pathMask):
# We are only interested in files.
for file_name in file_names:
# The list contains only the file names.
# The full path needs to be reconstructed.
full_path = os.path.join(directory, file_name)
# Both checks are performed to filter the files.
if check_type(file_name):
if check_filter(file_name) is False:
# Add the file to the list of images to open.
path_to_Image.append([full_path, os.path.basename(os.path.splitext(full_path)[0])])
# Create the list that will be returned by this function.
Masks = []
rois = []
ImageRois = []
for img_path, file_name in path_to_Image:
# IJ.openImage() returns an ImagePlus object or None.
if check_filter(file_name):
continue;
else:
MaskName = str(pathMask) + '/'+ file_name + '.tif'
Mask = IJ.openImage(MaskName)
Mask.show();
rm = RoiManager.getInstance()
if (rm==None):
rm = RoiManager()
rm.runCommand("Delete")
IJ.selectWindow(file_name+ '.tif');
IJ.run("Find Edges");
IJ.setAutoThreshold(Mask,"Default dark");
IJ.run("Threshold");
IJ.setThreshold(0, 0);
IJ.run("Convert to Mask");
IJ.run("Invert");
IJ.run("Create Selection");
rm.runCommand("Add");
# An object equals True and None equals False.
rois = rm.getRoisAsArray()
rm.runCommand("Save", str(pathRoi) + "/" + file_name + '.roi');
Mask.changes = False;
Mask.close();
ImageRois.append(rois)
return ImageRois
def split_string(input_string):
'''Split a string to a list and strip it
:param input_string: A string that contains semicolons as separators.
'''
string_splitted = input_string.split(';')
# Remove whitespace at the beginning and end of each string
strings_striped = [string.strip() for string in string_splitted]
return strings_striped
if __name__ in ['__builtin__','__main__']:
# Run the batch_open_images() function using the Scripting Parameters.
images = batch_open_images(roidir ,maskdir,
split_string(file_type_image),
split_string(filter_image),
do_recursive
)
for image in images:
# Call the toString() method of each ImagePlus object.
print(image)