Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(480084)

Side by Side Diff: tools/dump_ciede2000.py

Issue 1496: tools: Add 444, 444p10 support to dump_ciede2000.py
Patch Set: Created 2 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 from collections import deque 2 from collections import deque
3 import sys 3 import sys
4 import numpy as np 4 import numpy as np
5 from skimage import color 5 from skimage import color
6 import y4m 6 import y4m
7 7
8 # Assuming BT.709 8 # Assuming BT.709
9 yuv2rgb = np.array([ 9 yuv2rgb = np.array([
10 [1., 0., 1.28033], [1., -0.21482, -0.38059], [1., 2.12798, 0.] 10 [1., 0., 1.28033], [1., -0.21482, -0.38059], [1., 2.12798, 0.]
11 ]) 11 ])
12 12
13 # Simple box filter 13 # Simple box filter
14 box2 = np.ones((2, 2)) 14 box2 = np.ones((2, 2))
15 15
16 16
17 def decode_y4m_buffer(frame): 17 def decode_y4m_buffer(frame):
18 W, H, C, buf = frame.headers['W'], frame.headers['H'], frame.headers['C'], f rame.buffer 18 W, H, C, buf = frame.headers['W'], frame.headers['H'], frame.headers['C'], f rame.buffer
19 A, div2, dtype, scale = W * H, (H // 2, W // 2), 'uint8', 1. 19 A, div2, dtype, scale = W * H, (H // 2, W // 2), 'uint8', 1.
20 if C.endswith('p10'): 20 if C.endswith('p10'):
21 dtype, scale, A = 'uint16', 4., A * 2 21 dtype, scale, A = 'uint16', 4., A * 2
22 Y = (np.ndarray((H, W), dtype, buf) - 16. * scale) / (219. * scale) 22 Y = (np.ndarray((H, W), dtype, buf) - 16. * scale) / (219. * scale)
23 Cb = (np.ndarray(div2, dtype, buf, A) - 128. * scale) / (224. * scale) 23 if C.startswith('420'):
24 Cr = (np.ndarray(div2, dtype, buf, A + A // 4) - 128. * scale) / (224. * sca le) 24 Cb = (np.ndarray(div2, dtype, buf, A) - 128. * scale) / (224. * scale)
25 YCbCr444 = np.dstack((Y, np.kron(Cb, box2), np.kron(Cr, box2))) 25 Cr = (np.ndarray(div2, dtype, buf, A + A // 4) - 128. * scale) / (224. * scale)
luc.trudeau 2016/11/24 12:49:08 A // 4 != (H // 2) * (W //2) see https://review.x
26 YCbCr444 = np.dstack((Y, np.kron(Cb, box2), np.kron(Cr, box2)))
27 else:
28 Cb = (np.ndarray((H, W), dtype, buf, A) - 128. * scale) / (224. * scale)
29 Cr = (np.ndarray((H, W), dtype, buf, A * 2) - 128. * scale) / (224. * sc ale)
30 YCbCr444 = np.dstack((Y, Cb, Cr))
26 return np.dot(YCbCr444, yuv2rgb.T) 31 return np.dot(YCbCr444, yuv2rgb.T)
27 32
28 33
29 scores = [] 34 scores = []
30 35
31 36
32 def process_pair(ref, recons): 37 def process_pair(ref, recons):
33 ref_lab = color.rgb2lab(decode_y4m_buffer(ref)) 38 ref_lab = color.rgb2lab(decode_y4m_buffer(ref))
34 recons_lab = color.rgb2lab(decode_y4m_buffer(recons)) 39 recons_lab = color.rgb2lab(decode_y4m_buffer(recons))
35 # "Color Image Quality Assessment Based on CIEDE2000" 40 # "Color Image Quality Assessment Based on CIEDE2000"
(...skipping 14 matching lines...) Expand all
50 process_pair(ref_frames.popleft(), recons_frames.popleft()) 55 process_pair(ref_frames.popleft(), recons_frames.popleft())
51 56
52 57
53 def process_recons(frame): 58 def process_recons(frame):
54 recons_frames.append(frame) 59 recons_frames.append(frame)
55 if ref_frames: 60 if ref_frames:
56 process_pair(ref_frames.popleft(), recons_frames.popleft()) 61 process_pair(ref_frames.popleft(), recons_frames.popleft())
57 62
58 class Reader(y4m.Reader): 63 class Reader(y4m.Reader):
59 def _frame_size(self): 64 def _frame_size(self):
60 pixels = super(Reader, self)._frame_size() 65 area = self._stream_headers['W'] * self._stream_headers['H']
66 C = self._stream_headers['C']
67 if C.startswith('420'):
68 pixels = area * 3 // 2
69 elif C.startswith('444'):
70 pixels = area * 3
71 else:
72 raise Exception('Unknown pixel format: %s' % C)
61 if self._stream_headers['C'].endswith('p10'): 73 if self._stream_headers['C'].endswith('p10'):
62 return 2 * pixels 74 return 2 * pixels
63 return pixels 75 return pixels
64 76
65 def main(args): 77 def main(args):
66 OPENING = 'Opening %s...' 78 OPENING = 'Opening %s...'
67 BLOCK_SIZE = 4 * 1024 * 1024 79 BLOCK_SIZE = 4 * 1024 * 1024
68 ref_parser = Reader(process_ref) 80 ref_parser = Reader(process_ref)
69 recons_parser = Reader(process_recons) 81 recons_parser = Reader(process_recons)
70 print(OPENING % args[1]) 82 print(OPENING % args[1])
(...skipping 11 matching lines...) Expand all
82 ref_parser.decode(data) 94 ref_parser.decode(data)
83 if not recons_frames: 95 if not recons_frames:
84 data = recons_buf.read(BLOCK_SIZE) 96 data = recons_buf.read(BLOCK_SIZE)
85 if not data: break 97 if not data: break
86 recons_parser.decode(data) 98 recons_parser.decode(data)
87 print('Total: %2.4f' % np.array(scores).mean()) 99 print('Total: %2.4f' % np.array(scores).mean())
88 100
89 101
90 if __name__ == '__main__': 102 if __name__ == '__main__':
91 main(sys.argv) 103 main(sys.argv)
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld