-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_binary_mask.py
executable file
·38 lines (31 loc) · 1.08 KB
/
get_binary_mask.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
import numpy as np
from PIL import Image
import os
import glob
def np2label(img, output_name):
row = img.shape[0]
col = img.shape[1]
output = np.zeros((row, col))
for i in range(row):
for j in range(col):
if (int(img[i, j, 1]) - int(img[i, j, 0]) <= 2) and (
img[i, j, 2] < img[i, j, 0]) and \
(img[i, j, 2] < img[i, j, 1]):
output[i,j] = 1
if img[i, j, 0] < img[i, j, 1] < img[i, j, 2]:
output[i,j] = 1
if (img[i, j, 0] > img[i, j, 1]) and (img[i, j, 0] > img[i, j, 2]):
output[i,j] = 1
print(output)
np.save('{}.npy'.format(output_name), output)
print('{}.npy'.format(output_name))
data_dir = "./labeled_images"
mask_dir = "./data/masks_binary"
paths = glob.glob(os.path.join(data_dir, '*.png'))
#print(paths)
for path in paths:
img=Image.open(path)
img = np.array(img)
print(img.shape)
fname = os.path.splitext(os.path.split(path)[1])[0]
np2label(img, os.path.join(mask_dir, fname))