-
Notifications
You must be signed in to change notification settings - Fork 11
/
convertor.py
102 lines (85 loc) · 3.5 KB
/
convertor.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
96
97
98
99
100
101
102
import pandas as pd
import numpy as np
from skimage import color
from PIL import Image
def skimage_rgb2lab(rgb):
return color.rgb2lab(rgb.reshape(1,1,3))
def rgb2df(img):
h, w, _ = img.shape
x_l, y_l = np.meshgrid(np.arange(h), np.arange(w), indexing='ij')
r, g, b = img[:,:,0], img[:,:,1], img[:,:,2]
df = pd.DataFrame({
"x_l": x_l.ravel(),
"y_l": y_l.ravel(),
"r": r.ravel(),
"g": g.ravel(),
"b": b.ravel(),
})
return df
def mask2df(mask):
h, w = mask.shape
x_l, y_l = np.meshgrid(np.arange(h), np.arange(w), indexing='ij')
flg = mask.astype(int)
df = pd.DataFrame({
"x_l_m": x_l.ravel(),
"y_l_m": y_l.ravel(),
"m_flg": flg.ravel(),
})
return df
def rgba2df(img):
h, w, _ = img.shape
x_l, y_l = np.meshgrid(np.arange(h), np.arange(w), indexing='ij')
r, g, b, a = img[:,:,0], img[:,:,1], img[:,:,2], img[:,:,3]
df = pd.DataFrame({
"x_l": x_l.ravel(),
"y_l": y_l.ravel(),
"r": r.ravel(),
"g": g.ravel(),
"b": b.ravel(),
"a": a.ravel()
})
return df
def hsv2df(img):
x_l, y_l = np.meshgrid(np.arange(img.shape[0]), np.arange(img.shape[1]), indexing='ij')
h, s, v = np.transpose(img, (2, 0, 1))
df = pd.DataFrame({'x_l': x_l.flatten(), 'y_l': y_l.flatten(), 'h': h.flatten(), 's': s.flatten(), 'v': v.flatten()})
return df
def df2rgba(img_df):
r_img = img_df.pivot_table(index="x_l", columns="y_l",values= "r").reset_index(drop=True).values
g_img = img_df.pivot_table(index="x_l", columns="y_l",values= "g").reset_index(drop=True).values
b_img = img_df.pivot_table(index="x_l", columns="y_l",values= "b").reset_index(drop=True).values
a_img = img_df.pivot_table(index="x_l", columns="y_l",values= "a").reset_index(drop=True).values
df_img = np.stack([r_img, g_img, b_img, a_img], 2).astype(np.uint8)
return df_img
def df2bgra(img_df):
r_img = img_df.pivot_table(index="x_l", columns="y_l",values= "r").reset_index(drop=True).values
g_img = img_df.pivot_table(index="x_l", columns="y_l",values= "g").reset_index(drop=True).values
b_img = img_df.pivot_table(index="x_l", columns="y_l",values= "b").reset_index(drop=True).values
a_img = img_df.pivot_table(index="x_l", columns="y_l",values= "a").reset_index(drop=True).values
df_img = np.stack([b_img, g_img, r_img, a_img], 2).astype(np.uint8)
return df_img
def df2rgb(img_df):
r_img = img_df.pivot_table(index="x_l", columns="y_l",values= "r").reset_index(drop=True).values
g_img = img_df.pivot_table(index="x_l", columns="y_l",values= "g").reset_index(drop=True).values
b_img = img_df.pivot_table(index="x_l", columns="y_l",values= "b").reset_index(drop=True).values
df_img = np.stack([r_img, g_img, b_img], 2).astype(np.uint8)
return df_img
def pil2cv(image):
new_image = np.array(image, dtype=np.uint8)
if new_image.ndim == 2:
pass
elif new_image.shape[2] == 3:
new_image = new_image[:, :, ::-1]
elif new_image.shape[2] == 4:
new_image = new_image[:, :, [2, 1, 0, 3]]
return new_image
def cv2pil(image):
new_image = image.copy()
if new_image.ndim == 2:
pass
elif new_image.shape[2] == 3:
new_image = new_image[:, :, ::-1]
elif new_image.shape[2] == 4:
new_image = new_image[:, :, [2, 1, 0, 3]]
new_image = Image.fromarray(new_image)
return new_image