59 lines
No EOL
1.4 KiB
Python
59 lines
No EOL
1.4 KiB
Python
# -*- 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() |