forked from bacnguyencong/otoliths-identification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_segment.py
56 lines (40 loc) · 1.51 KB
/
test_segment.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
# Segment all images
from skimage.io import imread, imsave
from util.useful_imports import *
# ## 2. Create test data
# Prepare a list of test images, containing all paths
img_list = []
for dire in os.listdir(SAMPLE_DIR):
my_dir = os.path.join(SAMPLE_DIR, dire)
for subdir in os.listdir(my_dir):
cur_dir = os.path.join(my_dir, subdir)
if not os.path.isdir(cur_dir):
continue
# print(cur_dir)
jpg_list = glob.glob(cur_dir + '/*.jpg')
tif_list = glob.glob(cur_dir + '/*.tif')
img_list.extend(jpg_list)
for tif_file in tif_list:
name = os.path.basename(tif_file)
name = name[0:-4]
# check if format .jpg does not exist in the list
if name + '.jpg' not in jpg_list:
img_list.append(tif_file)
print("Segmenting the test data")
if os.path.exists(TEST_DIR):
shutil.rmtree(TEST_DIR)
os.makedirs(TEST_DIR)
remove_bg = False
conv_sigma = 2.0
opening_size = 30
for image_path in img_list:
image = imread(image_path)
images_names = os.path.basename(image_path)
images_names = images_names[0:-4] + '_{}.jpg'
regions = ut.segment_image(image, remove_bg, conv_sigma,
opening_size)
for i, reg in enumerate(regions):
if len(images_names):
min_row, min_col, max_row, max_col = reg.bbox
segm_im = image[min_row:max_row][:, min_col:max_col]
imsave(os.path.join(TEST_DIR, images_names.format(i+1)), segm_im)