-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathutility_for_opinenet.py
81 lines (70 loc) · 2.61 KB
/
utility_for_opinenet.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
import numpy as np
import copy
import math
def rgb2ycbcr(rgb):
m = np.array([[ 65.481, 128.553, 24.966],
[-37.797, -74.203, 112],
[ 112, -93.786, -18.214]])
shape = rgb.shape
if len(shape) == 3:
rgb = rgb.reshape((shape[0] * shape[1], 3))
ycbcr = np.dot(rgb, m.transpose() / 255.)
ycbcr[:,0] += 16.
ycbcr[:,1:] += 128.
return ycbcr.reshape(shape)
# ITU-R BT.601
# https://en.wikipedia.org/wiki/YCbCr
# YUV -> RGB
def ycbcr2rgb(ycbcr):
m = np.array([[ 65.481, 128.553, 24.966],
[-37.797, -74.203, 112],
[ 112, -93.786, -18.214]])
shape = ycbcr.shape
if len(shape) == 3:
ycbcr = ycbcr.reshape((shape[0] * shape[1], 3))
rgb = copy.deepcopy(ycbcr)
rgb[:,0] -= 16.
rgb[:,1:] -= 128.
rgb = np.dot(rgb, np.linalg.inv(m.transpose()) * 255.)
return rgb.clip(0, 255).reshape(shape)
def imread_CS_py(Iorg):
block_size = 33
[row, col] = Iorg.shape
row_pad = block_size-np.mod(row,block_size)
col_pad = block_size-np.mod(col,block_size)
Ipad = np.concatenate((Iorg, np.zeros([row, col_pad])), axis=1)
Ipad = np.concatenate((Ipad, np.zeros([row_pad, col+col_pad])), axis=0)
[row_new, col_new] = Ipad.shape
return [Iorg, row, col, Ipad, row_new, col_new]
def img2col_py(Ipad, block_size):
[row, col] = Ipad.shape
row_block = row/block_size
col_block = col/block_size
block_num = int(row_block*col_block)
img_col = np.zeros([block_size**2, block_num])
count = 0
for x in range(0, row-block_size+1, block_size):
for y in range(0, col-block_size+1, block_size):
img_col[:, count] = Ipad[x:x+block_size, y:y+block_size].reshape([-1])
# img_col[:, count] = Ipad[x:x+block_size, y:y+block_size].transpose().reshape([-1])
count = count + 1
return img_col
def col2im_CS_py(X_col, row, col, row_new, col_new):
block_size = 33
X0_rec = np.zeros([row_new, col_new])
count = 0
for x in range(0, row_new-block_size+1, block_size):
for y in range(0, col_new-block_size+1, block_size):
X0_rec[x:x+block_size, y:y+block_size] = X_col[:, count].reshape([block_size, block_size])
# X0_rec[x:x+block_size, y:y+block_size] = X_col[:, count].reshape([block_size, block_size]).transpose()
count = count + 1
X_rec = X0_rec[:row, :col]
return X_rec
def psnr(img1, img2):
img1.astype(np.float32)
img2.astype(np.float32)
mse = np.mean((img1 - img2) ** 2)
if mse == 0:
return 100
PIXEL_MAX = 255.0
return 20 * math.log10(PIXEL_MAX / math.sqrt(mse))