initial commit
This commit is contained in:
commit
654b6b0860
3 changed files with 186 additions and 0 deletions
59
compression.py
Normal file
59
compression.py
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#import image
|
||||||
|
imCol = plt.imread("mini.jpg", format=None)
|
||||||
|
imNb = 1/3 * imCol[:,:,0]+ 1/3 * imCol[:,:,1] + 1/3 * imCol[:,:,2];
|
||||||
|
|
||||||
|
#R
|
||||||
|
|
||||||
|
# compress
|
||||||
|
iterationsGlobales = 5
|
||||||
|
iterations = 20
|
||||||
|
|
||||||
|
imComp = imNb
|
||||||
|
FF= [[]]*len(imComp[:,0])
|
||||||
|
GG= [[]]*len(imComp[0,:])
|
||||||
|
|
||||||
|
for x in range(iterationsGlobales):
|
||||||
|
F=np.ones((len(imComp[:,0]), 1))
|
||||||
|
G=np.ones((len(imComp[0,:]), 1))
|
||||||
|
for y in range(iterations):
|
||||||
|
F = np.dot(imComp, G) / np.dot(np.transpose(G), G)
|
||||||
|
G = np.dot(np.transpose(imComp), F) / np.dot(np.transpose(F), F)
|
||||||
|
FF = np.append(FF, F, 1)
|
||||||
|
GG = np.append(GG, G, 1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
imComp = imComp - np.dot(F,np.transpose(G))
|
||||||
|
|
||||||
|
|
||||||
|
imRecons = np.dot(FF,np.transpose(GG))
|
||||||
|
#imRecons = np.dot(FF[:,None],GG[None,:])
|
||||||
|
|
||||||
|
plt.figure(dpi=1200)
|
||||||
|
|
||||||
|
|
||||||
|
plt.subplot(1, 3, 1)
|
||||||
|
plt.axis('off')
|
||||||
|
|
||||||
|
plt.title("Image importée", fontdict=None, loc='center', pad=None)
|
||||||
|
plt.imshow(imNb, interpolation = "nearest", cmap = 'gray')
|
||||||
|
|
||||||
|
|
||||||
|
plt.subplot(1, 3, 2)
|
||||||
|
plt.axis('off')
|
||||||
|
plt.title("Erreur", fontdict=None, loc='center', pad=None)
|
||||||
|
plt.imshow(np.dot(F,np.transpose(G)), interpolation = "nearest", cmap = 'gray')
|
||||||
|
|
||||||
|
|
||||||
|
plt.subplot(1, 3, 3)
|
||||||
|
plt.axis('off')
|
||||||
|
plt.title("Image compressée", fontdict=None, loc='center', pad=None)
|
||||||
|
plt.imshow(imRecons, interpolation = "nearest", cmap = 'gray')
|
||||||
|
plt.show()
|
127
compressionRGB.py
Normal file
127
compressionRGB.py
Normal file
|
@ -0,0 +1,127 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import numpy as np
|
||||||
|
from PIL import Image
|
||||||
|
import PIL
|
||||||
|
|
||||||
|
|
||||||
|
#import image
|
||||||
|
imCol = plt.imread("mini.jpg", format=None)
|
||||||
|
imRecons = np.zeros((len(imCol[:,0]), len(imCol[0,:]), 3))
|
||||||
|
erreur = np.ones((len(imCol[:,0]), len(imCol[0,:]), 3))
|
||||||
|
|
||||||
|
imRed = imCol[:,:,0]
|
||||||
|
imGreen = imCol[:,:,1]
|
||||||
|
imBlue = imCol[:,:,2];
|
||||||
|
|
||||||
|
iterationsGlobales = 5
|
||||||
|
iterations = 20
|
||||||
|
|
||||||
|
#RED
|
||||||
|
|
||||||
|
# compress
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
FF= [[]]*len(imRed[:,0])
|
||||||
|
GG= [[]]*len(imRed[0,:])
|
||||||
|
|
||||||
|
for x in range(iterationsGlobales):
|
||||||
|
F=np.ones((len(imRed[:,0]), 1))
|
||||||
|
G=np.ones((len(imRed[0,:]), 1))
|
||||||
|
for y in range(iterations):
|
||||||
|
F = np.dot(imRed, G) / np.dot(np.transpose(G), G)
|
||||||
|
G = np.dot(np.transpose(imRed), F) / np.dot(np.transpose(F), F)
|
||||||
|
FF = np.append(FF, F, 1)
|
||||||
|
GG = np.append(GG, G, 1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
imRed = imRed - np.dot(F,np.transpose(G))
|
||||||
|
|
||||||
|
erreur[:,:,0] = imRed - np.dot(FF,np.transpose(GG))
|
||||||
|
imRecons[:,:,0] = np.dot(FF,np.transpose(GG))
|
||||||
|
|
||||||
|
#GREEN
|
||||||
|
|
||||||
|
# compress
|
||||||
|
|
||||||
|
|
||||||
|
FF= [[]]*len(imGreen[:,0])
|
||||||
|
GG= [[]]*len(imGreen[0,:])
|
||||||
|
|
||||||
|
for x in range(iterationsGlobales):
|
||||||
|
F=np.ones((len(imGreen[:,0]), 1))
|
||||||
|
G=np.ones((len(imGreen[0,:]), 1))
|
||||||
|
for y in range(iterations):
|
||||||
|
F = np.dot(imGreen, G) / np.dot(np.transpose(G), G)
|
||||||
|
G = np.dot(np.transpose(imGreen), F) / np.dot(np.transpose(F), F)
|
||||||
|
FF = np.append(FF, F, 1)
|
||||||
|
GG = np.append(GG, G, 1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
imGreen = imGreen - np.dot(F,np.transpose(G))
|
||||||
|
|
||||||
|
erreur[:,:,1] = imGreen - np.dot(FF,np.transpose(GG))
|
||||||
|
imRecons[:,:,1] = np.dot(FF,np.transpose(GG))
|
||||||
|
#BLUE
|
||||||
|
|
||||||
|
# compress
|
||||||
|
|
||||||
|
|
||||||
|
FF= [[]]*len(imBlue[:,0])
|
||||||
|
GG= [[]]*len(imBlue[0,:])
|
||||||
|
|
||||||
|
for x in range(iterationsGlobales):
|
||||||
|
F=np.ones((len(imBlue[:,0]), 1))
|
||||||
|
G=np.ones((len(imBlue[0,:]), 1))
|
||||||
|
for y in range(iterations):
|
||||||
|
F = np.dot(imBlue, G) / np.dot(np.transpose(G), G)
|
||||||
|
G = np.dot(np.transpose(imBlue), F) / np.dot(np.transpose(F), F)
|
||||||
|
FF = np.append(FF, F, 1)
|
||||||
|
GG = np.append(GG, G, 1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
imBlue = imBlue - np.dot(F,np.transpose(G))
|
||||||
|
|
||||||
|
|
||||||
|
erreur[:,:,2] = imBlue - np.dot(FF,np.transpose(GG))
|
||||||
|
imRecons[:,:,2] = np.dot(FF,np.transpose(GG))
|
||||||
|
|
||||||
|
|
||||||
|
imRecons = imRecons*255
|
||||||
|
|
||||||
|
imRecons = imRecons-100
|
||||||
|
imRecons = imRecons*2
|
||||||
|
imRecons = imRecons/255
|
||||||
|
#erreur = erreur/255
|
||||||
|
|
||||||
|
|
||||||
|
imRecons = np.round(imRecons, 1)
|
||||||
|
# plot
|
||||||
|
plt.figure(dpi=1200)
|
||||||
|
|
||||||
|
|
||||||
|
plt.subplot(1, 2, 1)
|
||||||
|
plt.axis('off')
|
||||||
|
|
||||||
|
plt.title("Title", fontdict=None, loc='center', pad=None)
|
||||||
|
plt.imshow(imCol, interpolation = "nearest")
|
||||||
|
|
||||||
|
|
||||||
|
#plt.subplot(1, 3, 2)
|
||||||
|
#plt.axis('off')
|
||||||
|
#plt.title("Erreur", fontdict=None, loc='center', pad=None)
|
||||||
|
#plt.imshow(erreur, interpolation = "nearest")
|
||||||
|
|
||||||
|
|
||||||
|
plt.subplot(1, 2, 2)
|
||||||
|
plt.axis('off')
|
||||||
|
plt.title("Title", fontdict=None, loc='center', pad=None)
|
||||||
|
plt.imshow(imRecons, interpolation = "nearest")
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
tauxComp = round(100-((len(imCol[:,0])+len(imCol[0,:]))*iterationsGlobales)/((len(imCol[:,0])*len(imCol[0,:])))*100,0)
|
||||||
|
print(tauxComp, "% de compression")
|
BIN
mini.jpg
Normal file
BIN
mini.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 806 KiB |
Loading…
Add table
Add a link
Reference in a new issue