-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractROI.py
64 lines (45 loc) · 1.73 KB
/
extractROI.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
import collections
import glob, os, os.path
import json
import warnings
import numpy as np
from skimage import (transform, io, exposure, util)
import click
import yaml
import tifffile as TIFF
#--------------------------------------------------------------------------------
def extract_region(img, minr, minc, maxr, maxc):
return img[minr:maxr, minc:maxc]
#--------------------------------------------------------------------------------
@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))
def main(roifile, imgfiles, outdir):
"""Extract regions of interest (ROI) from one or more image files.
ROIs are defined in a JSON formatted input file. Sub-images are
written to the user provided sub-directory.
"""
# get ROIs from infile
with open(roifile, "r") as f:
roidict = json.load(f)
# Create output subdirectories
region_names = roidict.keys()
for name in region_names:
dirname = os.path.join(outdir, name)
if not os.path.exists(dirname):
os.makedirs(os.path.join(outdir, name))
for imgfile in imgfiles:
img = np.squeeze(io.imread(imgfile))
root = os.path.basename(imgfile)
for (name, bbox) in roidict.items():
subimg = extract_region(img, *bbox)
outfile = os.path.join(outdir, name, "{}-{}".format(name, root))
TIFF.imwrite(outfile, subimg)
if __name__ == "__main__":
main()