-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRunLengthEncodeing.py
More file actions
51 lines (42 loc) · 1.42 KB
/
RunLengthEncodeing.py
File metadata and controls
51 lines (42 loc) · 1.42 KB
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
import numpy as np
import csv
a = []
# with open('./CSV/OriginalImage.csv', newline='') as csvfile:
# with open('./CSV/QuantizedImage16.csv', newline='') as csvfile:
with open('./CSV/QuantizedImage32.csv', newline='') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
a.append(row)
a = np.array(a)
# print(a.shape)
def run_length_encoding(image):
rle = []
for row in image:
current_value = row[0]
count = 1
for pixel in row[1:]:
if pixel == current_value:
count += 1
else:
rle.append((current_value, count))
current_value = pixel
count = 1
rle.append((current_value, count))
return rle
# image = np.array([
# [0, 0, 0, 0, 0, 0, 0, 0, 0,],
# [0, 0, 1, 1, 1, 1, 1, 0, 0,],
# [0, 0, 1, 0, 0, 0, 0, 0, 0,],
# [0, 0, 1, 1, 1, 1, 0, 0, 0,],
# [0, 0, 0, 0, 0, 0, 1, 0, 0,],
# [0, 0, 0, 0, 0, 0, 1, 0, 0,],
# [0, 0, 1, 0, 0, 0, 1, 0, 0,],
# [0, 0, 0, 1, 1, 1, 0, 0, 0,],
# [0, 0, 0, 0, 0, 0, 0, 0, 0,],
# ])
image = a
rle_encoded = run_length_encoding(image)
print(image.shape)
print("Run-Length Encoding:")
# print(rle_encoded)
np.savetxt('./CSV/rle_encoded.csv', rle_encoded, fmt='%s', delimiter=',')