• Sign Up
• Login
• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Apr 8th, 2020 175 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import numpy as np
2. import cv2
3. import pickle
4.
5. # def arith_coding(fileVector, blockSize, probability):
6. #     cumulative_p = {}
7. #     cumulative_p_prev = {}
8. #     tags = []
9.
10. #     cum_sum = 0
11. #     for p in probability:
12. #         cumulative_p[p] = (probability[p] + cum_sum)
13. #         cumulative_p_prev[p] = cum_sum
14. #         cum_sum += probability[p]
15.
16. #     idx = 0
17. #     while(idx < len(fileVector)):
18. #         l = 0
19. #         u = 1
20. #         for blockNum in range(blockSize):
21. #             letter = fileVector[idx]
22. #             new_l = l + (u-l) * cumulative_p_prev[letter]
23. #             new_u = l + (u-l) * cumulative_p[letter]
24. #             u = new_u
25. #             l = new_l
26. #             idx += 1
27. #         tags.append((u+l)/2)
28.
29. #     return tags
30.
31.
32. def get_binaryStr_within_range(l, u):
33.     i = 1
34.     num = 0
35.     binaryStr = ''
36.     while (not(num >= l and num < u)):
37.         decimal_pnt = 2**(-i)
38.         new_decimal = num + decimal_pnt
39.         if(new_decimal > u):
40.             binaryStr += '0'
41.         else:
42.             binaryStr += '1'
43.             num = new_decimal
44.         i += 1
45.     return binaryStr
46.
47.
48. def arith_coding(fileVector, blockSize, probability):
49.     cumulative_p = {}
50.     cumulative_p_prev = {}
51.     tags = []
52.
53.     last_prob = 0
54.     for p in probability:
55.         cumulative_p_prev[p] = last_prob
56.         cumulative_p[p] = last_prob + probability[p]
57.         last_prob = cumulative_p[p]
58.
59.     idx = 0
60.     while(idx < len(fileVector)):
61.         l = 0.0
62.         u = 1.0
63.         tag = ''
64.         blockNum = 0
65.         c = 0
66.         while (blockNum < blockSize):
67.             if(l >= 0 and u < 0.5):
68.                 l = 2 * l
69.                 u = 2 * u
70.                 tag += '0'
71.                 for ic in range(c):
72.                     tag += '1'
73.                 c = 0
74.             elif (l >= 0.5 and u <= 1.0):
75.                 l = 2 * (l - 0.5)
76.                 u = 2 * (u - 0.5)
77.                 tag += '1'
78.                 for ic in range(c):
79.                     tag += '0'
80.                 c = 0
81.             elif(l >= 0.25 and u < 0.75):
82.                 l = 2 * (l - 0.25)
83.                 u = 2 * (u - 0.25)
84.                 c += 1
85.             else:
86.                 if(blockNum == blockSize-1):
87.                     letter = fileVector[idx]
88.                     new_l = l + (u-l) * cumulative_p_prev[letter]
89.                     new_u = l + (u-l) * cumulative_p[letter]
90.                     extra_bits = get_binaryStr_within_range(new_l, new_u)
91.                     tag += extra_bits
92.                 else:
93.                     letter = fileVector[idx]
94.                     delta = u-l
95.                     new_l = l + delta * cumulative_p_prev[letter]
96.                     new_u = min(
97.                         l + delta * cumulative_p[letter], 1.0)
98.                     l = new_l
99.                     u = new_u
100.                 idx += 1
101.                 blockNum += 1
102.         tags.append(tag)
103.     return tags
104.
105.
106. img = cv2.imread('shoma.jpg', cv2.IMREAD_GRAYSCALE)
107.
108. dimensions = np.array([img.shape[0], img.shape[1]])  # height x width
109.
110. img = img.flatten()
111. blockSize = 16
112.
113. extraPixels = blockSize - (img.size % blockSize)
114. img = np.pad(img, (0, extraPixels), 'constant', constant_values=(0))
115.
116. probability = {}
117.
118. for pix in img:
119.     if pix in probability.keys():
120.         probability[pix] += 1
121.     else:
122.         probability[pix] = 1
123.
124. sortedProbability = {}
125. for shade in range(256):
126.     if shade in probability.keys():
127.         sortedProbability[shade] = probability[shade]
128.
129. probability = sortedProbability
130.
131. for p in probability:
132.     probability[p] /= len(img)
133.
134. probabilityVector = []
135.
136. for shade in range(256):
137.     if shade in probability.keys():
138.         probabilityVector.append(probability[shade])
139.     else:
140.         probabilityVector.append(0)
141.
142. probabilityVector = np.array(probabilityVector, dtype=float)
143.
144. # tags = np.array(arith_coding(img, blockSize, sortedProbability),
145. #                 dtype=float)
146.
147. tags = arith_coding(img, blockSize, sortedProbability)
148.
149. with open("test.txt", "wb") as fp:  # Pickling
150.     pickle.dump(tags, fp)
151.
152. # tags.tofile('tags.dat')
153. probabilityVector.tofile('probabilities.dat')
154. dimensions.tofile('dimensions.dat')
155. np.array([blockSize]).tofile('blocksize.dat')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top