rgb in for loop
This commit is contained in:
parent
94e77e2b14
commit
14d8b8704b
3 changed files with 42 additions and 91 deletions
|
@ -1,8 +1,6 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from PIL import Image
|
|
||||||
import PIL
|
|
||||||
|
|
||||||
|
|
||||||
#import image
|
#import image
|
||||||
|
@ -10,114 +8,65 @@ imCol = plt.imread("mini.jpg", format=None)
|
||||||
imRecons = np.zeros((len(imCol[:,0]), len(imCol[0,:]), 3))
|
imRecons = np.zeros((len(imCol[:,0]), len(imCol[0,:]), 3))
|
||||||
erreur = np.ones((len(imCol[:,0]), len(imCol[0,:]), 3))
|
erreur = np.ones((len(imCol[:,0]), len(imCol[0,:]), 3))
|
||||||
|
|
||||||
imRed = imCol[:,:,0]
|
iterationsGlobales = 10
|
||||||
imGreen = imCol[:,:,1]
|
iterations = 10
|
||||||
imBlue = imCol[:,:,2]
|
|
||||||
|
|
||||||
iterationsGlobales = 30
|
imRecons[:,:,:] = imCol[:,:,:]
|
||||||
iterations = 20
|
|
||||||
|
|
||||||
#RED
|
# Compress all channels one by one
|
||||||
|
|
||||||
# compress
|
for channel in range(len(imCol[0,0,:])): # for each color channel of original image
|
||||||
|
|
||||||
|
FF= [[]]*len(imCol[:,:,channel]) # all channels should have the same dimsneions but eh
|
||||||
|
GG= [[]]*len(imCol[0,:,channel])
|
||||||
|
|
||||||
|
for x in range(iterationsGlobales):
|
||||||
FF= [[]]*len(imRed[:,0])
|
F=np.ones((len(imRecons[:,0,channel]), 1))
|
||||||
GG= [[]]*len(imRed[0,:])
|
G=np.ones((len(imRecons[0,:,channel]), 1))
|
||||||
|
|
||||||
for x in range(iterationsGlobales):
|
|
||||||
F=np.ones((len(imRed[:,0]), 1))
|
|
||||||
G=np.ones((len(imRed[0,:]), 1))
|
|
||||||
for y in range(iterations):
|
for y in range(iterations):
|
||||||
F = np.dot(imRed, G) / np.dot(np.transpose(G), G)
|
F = np.dot(imRecons[:,:,channel], G) / np.dot(np.transpose(G), G)
|
||||||
G = np.dot(np.transpose(imRed), F) / np.dot(np.transpose(F), F)
|
G = np.dot(np.transpose(imRecons[:,:,channel]), F) / np.dot(np.transpose(F), F)
|
||||||
FF = np.append(FF, F, 1)
|
FF = np.append(FF, F, 1)
|
||||||
GG = np.append(GG, G, 1)
|
GG = np.append(GG, G, 1)
|
||||||
|
|
||||||
|
imRecons[:,:,channel] = imRecons[:,:,channel] - np.dot(F,np.transpose(G))
|
||||||
|
|
||||||
|
erreur[:,:,channel] = imRecons[:,:,channel] - np.dot(FF,np.transpose(GG))
|
||||||
imRed = imRed - np.dot(F,np.transpose(G))
|
imRecons[:,:,channel] = np.dot(FF,np.transpose(GG))
|
||||||
|
|
||||||
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))
|
#why does imRecons not fall into the 255 range ?
|
||||||
|
# why does plt need it to be between 0 and 1 where imCol is fine being 0 to 255 ? because it's not an int ?
|
||||||
erreur[:,:,1] = imGreen - np.dot(FF,np.transpose(GG))
|
print(np.max(imRecons))
|
||||||
imRecons[:,:,1] = np.dot(FF,np.transpose(GG))
|
imRecons = imRecons/np.max(imRecons)
|
||||||
|
|
||||||
#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
|
|
||||||
#erreur = erreur/255
|
#erreur = erreur/255
|
||||||
#imRecons = np.round(imRecons, 4)
|
|
||||||
|
#compression rate
|
||||||
|
tauxComp = round(100-((len(imCol[:,0])+len(imCol[0,:]))*iterationsGlobales)/((len(imCol[:,0])*len(imCol[0,:])))*100,0)
|
||||||
|
print(tauxComp, "% de compression")
|
||||||
|
|
||||||
# plot
|
# plot
|
||||||
plt.figure(dpi=300)
|
plt.figure(dpi=300)
|
||||||
|
|
||||||
|
|
||||||
plt.subplot(1, 2, 1)
|
plt.subplot(1, 3, 1)
|
||||||
plt.axis('off')
|
plt.axis('off')
|
||||||
|
|
||||||
plt.title("Original", loc='center', pad=None)
|
plt.title("Original", loc='center', pad=None)
|
||||||
plt.imshow(imCol, interpolation = "nearest")
|
plt.imshow(imCol, interpolation = "nearest")
|
||||||
|
|
||||||
|
|
||||||
#plt.subplot(1, 3, 2)
|
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.axis('off')
|
||||||
plt.title("Compressed", fontdict=None, loc='center', pad=None)
|
plt.title("Error", fontdict=None, loc='center', pad=None)
|
||||||
|
plt.imshow(erreur, interpolation = "nearest")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
plt.subplot(1, 3, 3)
|
||||||
|
plt.axis('off')
|
||||||
|
plt.title("Compressed "+ str(tauxComp) +"%", fontdict=None, loc='center', pad=None)
|
||||||
plt.imshow(imRecons, interpolation = "nearest")
|
plt.imshow(imRecons, interpolation = "nearest")
|
||||||
plt.show()
|
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
example.png
BIN
example.png
Binary file not shown.
Before Width: | Height: | Size: 1.5 MiB After Width: | Height: | Size: 763 KiB |
|
@ -1,6 +1,8 @@
|
||||||
# Compression d'image
|
# Compression d'image
|
||||||
|
|
||||||
Exercice de compression d'image pour apprendre le concept de base et constater le gain en taille de fichier
|
Simple compression study in both RGB and monochrome images.
|
||||||
|
|
||||||
|
Takes an image as input and generates a plot with a comparison of the original, error, and compressed image, as well as the space saved compared to the raw image data
|
||||||
|
|
||||||
## Example with 30 & 20 as parameters
|
## Example with 30 & 20 as parameters
|
||||||
<img src="example.png" height="250" />
|
<img src="example.png" height="250" />
|
Loading…
Add table
Add a link
Reference in a new issue