-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
95 lines (83 loc) · 2.5 KB
/
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
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
import os
import cv2
import glob
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
from skimage.segmentation import find_boundaries
import scipy.signal
import scipy.linalg
import scipy.sparse
def read_image(folder, name, scale=1, gray=False):
for filename in glob.glob(folder + "/*"):
if os.path.splitext(os.path.basename(filename))[0] == name:
break
img = Image.open(os.path.join(filename))
if scale != 1:
w, h = img.size
img = img.resize((int(w * scale), int(h * scale)), Image.BICUBIC)
if gray:
img = img.convert("L")
img = np.array(img)
if len(img.shape) == 3:
img = img[..., :3]
return img.astype(np.float64) / 255 # only first 3
def rgb2gray(img):
r, g, b = img[..., 0], img[..., 1], img[..., 2]
return 0.2126 * r + 0.7152 * g + 0.0722 * b
def process_mask(mask):
boundary = find_boundaries(mask, mode="inner").astype(int)
inner = mask - boundary
return inner, boundary
def compute_laplacian(img):
kernel = np.array([
[0, 1, 0],
[1, -4, 1],
[0, 1, 0]
])
laplacian = scipy.signal.fftconvolve(img, kernel, mode="same")
return laplacian
def compute_gradient(img, forward=True):
if forward:
kx = np.array([
[0, 0, 0],
[0, -1, 1],
[0, 0, 0]
])
ky = np.array([
[0, 0, 0],
[0, -1, 0],
[0, 1, 0]
])
else:
kx = np.array([
[0, 0, 0],
[-1, 1, 0],
[0, 0, 0]
])
ky = np.array([
[0, -1, 0],
[0, 1, 0],
[0, 0, 0]
])
Gx = scipy.signal.fftconvolve(img, kx, mode="same")
Gy = scipy.signal.fftconvolve(img, ky, mode="same")
return Gx, Gy
def get_pixel_ids(img):
pixel_ids = np.arange(img.shape[0] * img.shape[1]).reshape(img.shape[0], img.shape[1])
return pixel_ids
def get_masked_values(values, mask):
assert values.shape == mask.shape
nonzero_idx = np.nonzero(mask) # get mask 1
return values[nonzero_idx]
def get_alpha_blended_img(src, target, alpha_mask):
return src * alpha_mask + target * (1 - alpha_mask)
def dilate_img(img, k):
kernel = np.ones((k, k), np.uint8)
return cv2.dilate(img, kernel, iterations = 1)
def estimate_sparse_rank(A):
def mv(v):
return A @ v
L = scipy.sparse.linalg.LinearOperator(A.shape, matvec=mv, rmatvec=mv)
rank = scipy.linalg.interpolative.estimate_rank(L, 0.1)
return rank