Skip to content

Commit 39b2d7e

Browse files
committed
Added some more method of compression
1 parent 4299eb0 commit 39b2d7e

File tree

4 files changed

+291
-1
lines changed

4 files changed

+291
-1
lines changed

CompressionCheck.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def SNR(original, compressed):
1919

2020
def main():
2121
original = cv2.imread("raw.png")
22-
compressed = cv2.imread("lossless.png", 1)
22+
compressed = cv2.imread("lossy.png", 1)
2323
mse = np.mean((original - compressed) ** 2)
2424
snr = SNR(original, compressed)
2525
psnr = PSNR(original, compressed)

DCT.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import io
2+
import os
3+
from PIL import Image
4+
import numpy as np
5+
import matplotlib.pyplot as plt
6+
from scipy import fftpack
7+
from urllib.request import urlopen
8+
import IPython
9+
10+
image_url='file:///D:/downloads/LossyComparison/raw.jpg'
11+
def get_image_from_url(image_url='http://i.imgur.com/8vuLtqi.png', size=(128, 128)):
12+
file_descriptor = urlopen(image_url)
13+
image_file = io.BytesIO(file_descriptor.read())
14+
image = Image.open(image_file)
15+
img_color = image.resize(size, 1)
16+
img_grey = img_color.convert('L')
17+
img = np.array(img_grey, dtype=float)
18+
return img
19+
20+
def get_2D_dct(img):
21+
return fftpack.dct(fftpack.dct(img.T, norm='ortho').T, norm='ortho')
22+
23+
def get_2d_idct(coefficients):
24+
return fftpack.idct(fftpack.idct(coefficients.T, norm='ortho').T, norm='ortho')
25+
26+
def get_reconstructed_image(raw):
27+
img = raw.clip(0, 255)
28+
img = img.astype('uint8')
29+
img = Image.fromarray(img)
30+
return img
31+
32+
pixels = get_image_from_url(image_url=image_url, size=(256, 256))
33+
dct_size = pixels.shape[0]
34+
dct = get_2D_dct(pixels)
35+
reconstructed_images = []
36+
37+
for ii in range(dct_size):
38+
dct_copy = dct.copy()
39+
dct_copy[ii:,:] = 0
40+
dct_copy[:,ii:] = 0
41+
42+
r_img = get_2d_idct(dct_copy);
43+
reconstructed_image = get_reconstructed_image(r_img);
44+
reconstructed_images.append(reconstructed_image);
45+
46+
fig = plt.figure(figsize=(16, 16))
47+
for ii in range(64):
48+
plt.subplot(8, 8, ii + 1)
49+
plt.imshow(reconstructed_images[ii], cmap=plt.cm.gray)
50+
plt.grid(False);
51+
plt.xticks([]);
52+
plt.yticks([]);
53+
if ii == 63:
54+
plt.imsave('hasilDCT.jpg', reconstructed_images[ii], cmap=plt.cm.gray);
55+
56+
plt.show();

Fractal.py

+187
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
import matplotlib.pyplot as plt
2+
import matplotlib.image as mpimg
3+
from scipy import ndimage
4+
from scipy import optimize
5+
import numpy as np
6+
import math
7+
8+
# Manipulate channels
9+
10+
def get_greyscale_image(img):
11+
return np.mean(img[:,:,:2], 2)
12+
13+
def extract_rgb(img):
14+
return img[:,:,0], img[:,:,1], img[:,:,2]
15+
16+
def assemble_rbg(img_r, img_g, img_b):
17+
shape = (img_r.shape[0], img_r.shape[1], 1)
18+
return np.concatenate((np.reshape(img_r, shape), np.reshape(img_g, shape),
19+
np.reshape(img_b, shape)), axis=2)
20+
21+
# Transformations
22+
23+
def reduce(img, factor):
24+
result = np.zeros((img.shape[0] // factor, img.shape[1] // factor))
25+
for i in range(result.shape[0]):
26+
for j in range(result.shape[1]):
27+
result[i,j] = np.mean(img[i*factor:(i+1)*factor,j*factor:(j+1)*factor])
28+
return result
29+
30+
def rotate(img, angle):
31+
return ndimage.rotate(img, angle, reshape=False)
32+
33+
def flip(img, direction):
34+
return img[::direction,:]
35+
36+
def apply_transformation(img, direction, angle, contrast=1.0, brightness=0.0):
37+
return contrast*rotate(flip(img, direction), angle) + brightness
38+
39+
# Contrast and brightness
40+
41+
def find_contrast_and_brightness1(D, S):
42+
# Fix the contrast and only fit the brightness
43+
contrast = 0.75
44+
brightness = (np.sum(D - contrast*S)) / D.size
45+
return contrast, brightness
46+
47+
def find_contrast_and_brightness2(D, S):
48+
# Fit the contrast and the brightness
49+
A = np.concatenate((np.ones((S.size, 1)), np.reshape(S, (S.size, 1))), axis=1)
50+
b = np.reshape(D, (D.size,))
51+
x, _, _, _ = np.linalg.lstsq(A, b)
52+
#x = optimize.lsq_linear(A, b, [(-np.inf, -2.0), (np.inf, 2.0)]).x
53+
return x[1], x[0]
54+
55+
# Compression for greyscale images
56+
57+
def generate_all_transformed_blocks(img, source_size, destination_size, step):
58+
factor = source_size // destination_size
59+
transformed_blocks = []
60+
for k in range((img.shape[0] - source_size) // step + 1):
61+
for l in range((img.shape[1] - source_size) // step + 1):
62+
# Extract the source block and reduce it to the shape of a destination block
63+
S = reduce(img[k*step:k*step+source_size,l*step:l*step+source_size], factor)
64+
# Generate all possible transformed blocks
65+
for direction, angle in candidates:
66+
transformed_blocks.append((k, l, direction, angle, apply_transformation(S, direction, angle)))
67+
return transformed_blocks
68+
69+
def compress(img, source_size, destination_size, step):
70+
transformations = []
71+
transformed_blocks = generate_all_transformed_blocks(img, source_size, destination_size, step)
72+
i_count = img.shape[0] // destination_size
73+
j_count = img.shape[1] // destination_size
74+
for i in range(i_count):
75+
transformations.append([])
76+
for j in range(j_count):
77+
print("{}/{} ; {}/{}".format(i, i_count, j, j_count))
78+
transformations[i].append(None)
79+
min_d = float('inf')
80+
# Extract the destination block
81+
D = img[i*destination_size:(i+1)*destination_size,j*destination_size:(j+1)*destination_size]
82+
# Test all possible transformations and take the best one
83+
for k, l, direction, angle, S in transformed_blocks:
84+
contrast, brightness = find_contrast_and_brightness2(D, S)
85+
S = contrast*S + brightness
86+
d = np.sum(np.square(D - S))
87+
if d < min_d:
88+
min_d = d
89+
transformations[i][j] = (k, l, direction, angle, contrast, brightness)
90+
return transformations
91+
92+
def decompress(transformations, source_size, destination_size, step, nb_iter=8):
93+
factor = source_size // destination_size
94+
height = len(transformations) * destination_size
95+
width = len(transformations[0]) * destination_size
96+
iterations = [np.random.randint(0, 256, (height, width))]
97+
cur_img = np.zeros((height, width))
98+
for i_iter in range(nb_iter):
99+
print(i_iter)
100+
for i in range(len(transformations)):
101+
for j in range(len(transformations[i])):
102+
# Apply transform
103+
k, l, flip, angle, contrast, brightness = transformations[i][j]
104+
S = reduce(iterations[-1][k*step:k*step+source_size,l*step:l*step+source_size], factor)
105+
D = apply_transformation(S, flip, angle, contrast, brightness)
106+
cur_img[i*destination_size:(i+1)*destination_size,j*destination_size:(j+1)*destination_size] = D
107+
iterations.append(cur_img)
108+
cur_img = np.zeros((height, width))
109+
return iterations
110+
111+
# Compression for color images
112+
113+
def reduce_rgb(img, factor):
114+
img_r, img_g, img_b = extract_rgb(img)
115+
img_r = reduce(img_r, factor)
116+
img_g = reduce(img_g, factor)
117+
img_b = reduce(img_b, factor)
118+
return assemble_rbg(img_r, img_g, img_b)
119+
120+
def compress_rgb(img, source_size, destination_size, step):
121+
img_r, img_g, img_b = extract_rgb(img)
122+
return [compress(img_r, source_size, destination_size, step), \
123+
compress(img_g, source_size, destination_size, step), \
124+
compress(img_b, source_size, destination_size, step)]
125+
126+
def decompress_rgb(transformations, source_size, destination_size, step, nb_iter=8):
127+
img_r = decompress(transformations[0], source_size, destination_size, step, nb_iter)[-1]
128+
img_g = decompress(transformations[1], source_size, destination_size, step, nb_iter)[-1]
129+
img_b = decompress(transformations[2], source_size, destination_size, step, nb_iter)[-1]
130+
return assemble_rbg(img_r, img_g, img_b)
131+
132+
# Plot
133+
134+
def plot_iterations(iterations, target=None):
135+
# Configure plot
136+
plt.figure()
137+
nb_row = math.ceil(np.sqrt(len(iterations)))
138+
nb_cols = nb_row
139+
# Plot
140+
for i, img in enumerate(iterations):
141+
plt.subplot(nb_row, nb_cols, i+1)
142+
plt.imshow(img, cmap='gray', vmin=0, vmax=255, interpolation='none')
143+
if target is None:
144+
plt.title(str(i))
145+
else:
146+
# Display the RMSE
147+
plt.title(str(i) + ' (' + '{0:.2f}'.format(np.sqrt(np.mean(np.square(target - img)))) + ')')
148+
frame = plt.gca()
149+
frame.axes.get_xaxis().set_visible(False)
150+
frame.axes.get_yaxis().set_visible(False)
151+
plt.tight_layout()
152+
153+
# Parameters
154+
155+
directions = [1, -1]
156+
angles = [0, 90, 180, 270]
157+
candidates = [[direction, angle] for direction in directions for angle in angles]
158+
159+
# Tests
160+
161+
def test_greyscale():
162+
img = mpimg.imread('raw.jpg')
163+
img = get_greyscale_image(img)
164+
img = reduce(img, 4)
165+
plt.figure()
166+
plt.imshow(img, cmap='gray', interpolation='none')
167+
transformations = compress(img, 8, 4, 8)
168+
iterations = decompress(transformations, 8, 4, 8)
169+
plot_iterations(iterations, img)
170+
plt.show()
171+
172+
def test_rgb():
173+
img = mpimg.imread('raw.jpg')
174+
img = reduce_rgb(img, 8)
175+
transformations = compress_rgb(img, 8, 4, 8)
176+
retrieved_img = decompress_rgb(transformations, 8, 4, 8)
177+
plt.figure()
178+
plt.subplot(121)
179+
plt.imshow(np.array(img).astype(np.uint8), interpolation='none')
180+
plt.subplot(122)
181+
plt.imshow(retrieved_img.astype(np.uint8), interpolation='none')
182+
plt.show()
183+
plt.imsave("hasilFractal.jpg",retrieved_img.astype(np.uint8))
184+
185+
if __name__ == '__main__':
186+
#test_greyscale()
187+
test_rgb()

RLE.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import cv2 as cv
2+
import numpy as np
3+
4+
image = cv.imread('raw.png',1)
5+
grayimg = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
6+
rows, cols = grayimg.shape
7+
image1 = grayimg.flatten()
8+
9+
for i in range(len(image1)):
10+
if image1[i] >= 127:
11+
image1[i] = 255
12+
if image1[i] < 127:
13+
image1[i] = 0
14+
15+
data = []
16+
image3 = []
17+
count = 1
18+
19+
for i in range(len(image1)-1):
20+
if (count == 1):
21+
image3.append(image1[i])
22+
if image1[i] == image1[i+1]:
23+
count = count + 1
24+
if i == len(image1) - 2:
25+
image3.append(image1[i])
26+
data.append(count)
27+
else:
28+
data.append(count)
29+
count = 1
30+
31+
if(image1[len(image1)-1] != image1[-1]):
32+
image3.append(image1[len(image1)-1])
33+
data.append(1)
34+
35+
#Compression ratio
36+
ys_rate = len(image3)/len(image1)*100
37+
print(str(ys_rate) + '%')
38+
39+
rec_image = []
40+
for i in range(len(data)):
41+
for j in range(data[i]):
42+
rec_image.append(image3[i])
43+
44+
rec_image = np.reshape(rec_image,(rows,cols))
45+
46+
cv.imshow('rec_image',rec_image)
47+
cv.waitKey(0)

0 commit comments

Comments
 (0)