-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathslideMaskEntropy.py
executable file
·93 lines (64 loc) · 2.54 KB
/
slideMaskEntropy.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
#!/usr/local/bin/python
print "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
print " slideToolkit Entropy Filter"
print " version 1.0"
print ""
print "* Written by : Tim Bezemer"
print "* E-mail : [email protected]"
print "* Suggested for by : Sander W. van der Laan | [email protected]"
print "* Last update : 2016-08-12"
print "* Version : 1.2.20160812"
print ""
print "* Description : Makes maskers of PNG files using a entropy-filter. To be"
print " used when the regular mask-making program (slideMask)"
print " is insufficient."
print ""
print "* USAGE: for DIR in $(ls) ; do echo \"* Processing [ \"$DIR\" ]...\"; $(command -v slideMaskEntropy.py) \"$DIR\"/\"$DIR\".macro.png; done"
print " slideMaskEntropy.py [FILE.png]"
print ""
print ""
print "* PREREQUISITES: The packages 'scikit-image', 'numpy', and 'matplotlib' are required."
print " Installation instructions:"
print " pip install numpy"
print " pip install matplotlib"
print " pip install scikit-image"
print ""
print "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from skimage import data
from skimage.filters.rank import entropy
from skimage.morphology import disk
from skimage.util import img_as_ubyte
from skimage.io import imread, imsave
from sys import exit
from sys import argv
import os.path
import numpy as np
import warnings
warnings.filterwarnings("ignore")
def filter_entropy_image(image, filter):
eimage = entropy(image, disk(5))
new_picture = np.ndarray(shape=eimage.shape) #[[False] * image.shape[1]] * image.shape[0]
for rn, row in enumerate(eimage):
for pn, pixel in enumerate(row):
if pixel < filter:
new_picture[rn,pn] = True
else:
new_picture[rn,pn] = False
return new_picture.astype('b')
fn = argv[1]
if not os.path.isfile(fn):
print "No such file"
sys.exit()
print "\tCreating tissue mask (entropy filter)..."
image = imread(fn, True)
entropy_image = filter_entropy_image(image, 3.5)
plt.axis('off')
newfile = fn
newfile = newfile.replace(".macro.png", ".emask.png")
#np.save(newfile, entropy_image)
plt.imsave(newfile, entropy_image, cmap=plt.cm.binary)
print "\t>>> Saved image as:" + newfile