-
Notifications
You must be signed in to change notification settings - Fork 0
/
img_utils.py
50 lines (39 loc) · 1.3 KB
/
img_utils.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
"""
Utility functions for processing images
Author: Robert Sare
E-mail: rmsare@stanford.edu
Date: 8 June 2017
"""
import numpy as np
import matplotlib.pyplot as plt
import skimage.color
from skimage.segmentation import slic, mark_boundaries
from skimage.io import imread
from skimage.util import img_as_float
import os
import fnmatch
def load_image_segments(name):
fname = 'images/' + name + '_image.npy'
image = np.load(fname)
fname = 'images/' + name + '_seg.npy'
segments = np.load(fname)
return image, segments
def parse_filename(fname):
fname = fname.split('/')
m = len(fname)
fname = fname[m-1]
name = fname.split('.')[0]
return name
def save_results(name, image, segments):
np.save('images/' + name + '_image', image)
np.save('images/' + name + '_seg', segments)
def segment_all_in_directory(dirname, factor, sigma):
files = os.listdir(dirname)
for filename in files:
if fnmatch.fnmatch(filename, '*.JPG'):
print("Processing " + filename + "...")
name = parse_filename(filename)
image = img_as_float(imread(dirname + filename)[0])
n_seg = int(np.prod(image[:,:,0].shape) / factor)
segments = slic(image, n_segments=n_seg, sigma=sigma)
save_results(name, image, segments)