forked from daviddaiweizhang/istar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrescale.py
103 lines (83 loc) · 2.92 KB
/
rescale.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
import argparse
import os
from time import time
from skimage.transform import rescale
import numpy as np
from utils import (
load_image, save_image, read_string, write_string,
load_tsv, save_tsv)
def get_image_filename(prefix):
file_exists = False
for suffix in ['.jpg', '.png', '.tiff']:
filename = prefix + suffix
if os.path.exists(filename):
file_exists = True
break
if not file_exists:
raise FileNotFoundError('Image not found')
return filename
# def rescale_image(img, scale):
# if img.ndim == 2:
# img = rescale(img, scale, preserve_range=True)
# elif img.ndim == 3:
# channels = img.transpose(2, 0, 1)
# channels = [rescale_image(c, scale) for c in channels]
# img = np.stack(channels, -1)
# else:
# raise ValueError('Unrecognized image ndim')
# return img
def rescale_image(img, scale):
if img.ndim == 2:
scale = [scale, scale]
elif img.ndim == 3:
scale = [scale, scale, 1]
else:
raise ValueError('Unrecognized image ndim')
img = rescale(img, scale, preserve_range=True)
return img
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('prefix', type=str)
parser.add_argument('--image', action='store_true')
parser.add_argument('--mask', action='store_true')
parser.add_argument('--locs', action='store_true')
parser.add_argument('--radius', action='store_true')
args = parser.parse_args()
return args
def main():
args = get_args()
pixel_size_raw = float(read_string(args.prefix+'pixel-size-raw.txt'))
pixel_size = float(read_string(args.prefix+'pixel-size.txt'))
scale = pixel_size_raw / pixel_size
if args.image:
img = load_image(get_image_filename(args.prefix+'he-raw'))
img = img.astype(np.float32)
print(f'Rescaling image (scale: {scale:.3f})...')
t0 = time()
img = rescale_image(img, scale)
print(int(time() - t0), 'sec')
img = img.astype(np.uint8)
save_image(img, args.prefix+'he-scaled.jpg')
if args.mask:
mask = load_image(args.prefix+'mask-raw.png')
mask = mask > 0
if mask.ndim == 3:
mask = mask.any(2)
print(f'Rescaling mask (scale: {scale:.3f})...')
t0 = time()
mask = rescale_image(mask.astype(np.float32), scale)
print(int(time() - t0))
mask = mask > 0.5
save_image(mask, args.prefix+'mask-scaled.png')
if args.locs:
locs = load_tsv(args.prefix+'locs-raw.tsv')
locs = locs * scale
locs = locs.round().astype(int)
save_tsv(locs, args.prefix+'locs.tsv')
if args.radius:
radius = float(read_string(args.prefix+'radius-raw.txt'))
radius = radius * scale
radius = np.round(radius).astype(int)
write_string(radius, args.prefix+'radius.txt')
if __name__ == '__main__':
main()