-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrandom_image_segments.py
89 lines (76 loc) · 3.32 KB
/
random_image_segments.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
import numpy as np
import glob
import os
import gcsfs
import cv2
import math
import random
import imageio
def main():
# root_dir needs a trailing slash (i.e. /root/dir/)
root_dir = 'gs://octopi-malaria-uganda-2022/Ju46y9GSqf6DNU2TI6m1BQEo33APSB1n/'#'gs://octopi-codex-data-processing/' #"/home/prakashlab/Documents/kmarx/pipeline/tstflat/"# 'gs://octopi-codex-data-processing/TEST_1HDcVekx4mrtl0JztCXLn9xN6GOak4AU/'#
dest_dir = '/media/prakashlab/T7/subsets/' # must be a local path
exp_id = "analysis/"
channel = "BF_LED_matrix_dpc_seg" # only run segmentation on this channel
zstack = 'f' # select which z to run segmentation on. set to 'f' to select the focus-stacked
key = '/home/prakashlab/Documents/kmarx/malaria_deepzoom/deepzoom uganda 2022/uganda-2022-viewing-keys.json'
ftype = 'npy'
gcs_project = 'soe-octopi'
n_rand = 10
nsub = 1 # cut into a 3x3 grid and return a random selection
get_rand(root_dir, dest_dir, exp_id, channel, zstack, n_rand, key, nsub, gcs_project, ftype)
def get_rand(root_dir, dest_dir, exp_id, channel, zstack, n_rand, key, nsub, gcs_project, ftype):
# Load remote files if necessary
root_remote = False
if root_dir[0:5] == 'gs://':
root_remote = True
fs = None
if root_remote:
fs = gcsfs.GCSFileSystem(project=gcs_project,token=key)
print("Reading image paths")
# filter - only look for specified channel and cycle 0
path = root_dir + exp_id + "**/0/**_" + zstack + "_" + channel + '.' + ftype
print(path)
if root_remote:
allpaths = [p for p in fs.glob(path, recursive=True) if p.split('/')[-2] == '0']
else:
allpaths = [p for p in glob.iglob(path, recursive=True) if p.split('/')[-2] == '0']
# remove duplicates
imgpaths = list(dict.fromkeys(allpaths))
print(str(len(imgpaths)) + " images to select from")
savepath = dest_dir + exp_id
os.makedirs(savepath, exist_ok=True)
selected = random.sample(imgpaths, n_rand)
for impath in selected:
print(impath)
fs.get(impath, savepath + impath.split('/')[-3] + "_s_" + impath.split('/')[-1])
# if root_remote:
# im = np.array(imread_gcsfs(fs, impath), dtype=np.uint8)
# else:
# im = np.array(cv2.imread(impath), dtype=np.uint8)
# shape = im.shape
# x = math.floor(shape[0]/nsub)
# y = math.floor(shape[1]/nsub)
# xslice = random.choice(range(nsub))
# yslice = random.choice(range(nsub))
# im = im[x*xslice:(x*xslice + x), y*yslice:(y*yslice + y)]
# im = im - np.min(im)
# im = np.uint8(255 * np.array(im, dtype=np.float64)/float(np.max(im)))
# fname = savepath + "s_" + impath.split('/')[-1]
# cv2.imwrite(fname, im)
def imread_gcsfs(fs,file_path):
'''
imread_gcsfs gets the image bytes from the remote filesystem and convets it into an image
Arguments:
fs: a GCSFS filesystem object
file_path: a string containing the GCSFS path to the image (e.g. 'gs://data/folder/image.bmp')
Returns:
I: an image object
This code has no side effects
'''
img_bytes = fs.cat(file_path)
im_type = file_path.split('.')[-1]
I = imageio.core.asarray(imageio.v2.imread(img_bytes, im_type))
return I
if __name__ == "__main__":
main()