-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathequalizer.py
59 lines (47 loc) · 1.69 KB
/
equalizer.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
import os.path
import json
import warnings
import numpy as np
from skimage import (transform, io, exposure, util)
import tifffile as TIFF
import click
def load_ROI_dict(fname):
with open(fname, "r") as f:
roidict = json.load(f)
return roidict
def equalize_from_ROI(img, roi_bbox):
mask = np.zeros(img.shape)
minr, minc, maxr, maxc = roi_bbox
mask[minr:maxr, minc:maxc] = 1
with warnings.catch_warnings():
warnings.simplefilter("ignore", UserWarning)
equalized_img = util.img_as_uint(exposure.equalize_hist(img, mask = mask))
return equalized_img
#--------------------------------------------------------------------------------
@click.command()
@click.argument("roifile",
type = click.Path(exists=True))
@click.argument("imgfiles",
type = click.Path(exists=True,
dir_okay = False),
nargs = -1)
@click.argument("outdir",
type = click.Path(exists = True,
file_okay = False,
dir_okay = True))
@click.option("-p", "--prefix",
help = "Prefix to prepend to equalized file names.",
default = "EQLZD")
def main(roifile, imgfiles, outdir, prefix):
"""Use ROI to equalize and image. .
"""
roi_dict = load_ROI_dict(roifile)
roi_bbox = list(roi_dict.values())[0]
for imgfile in imgfiles:
img = np.squeeze(io.imread(imgfile))
img = equalize_from_ROI(img, roi_bbox)
root = os.path.basename(imgfile)
outfile = os.path.join(outdir, "{}-{}".format(prefix, root))
TIFF.imwrite(outfile, img)
if __name__ == "__main__":
main()