From 56ef1ce08126535309b3f8f06bf26201e00c9cc4 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Fri, 18 Sep 2015 13:55:22 +0200 Subject: [PATCH] Agrandissement taille interface --- code/Noise.py | 63 ++++++++++++++++++++ code/bmp.py | 105 ++++++++++++++++++--------------- code/tests.py | 157 ++++++++++++++++++++++++++++++++++---------------- 3 files changed, 228 insertions(+), 97 deletions(-) diff --git a/code/Noise.py b/code/Noise.py index 7e02849..488c86b 100644 --- a/code/Noise.py +++ b/code/Noise.py @@ -251,7 +251,70 @@ class Noise: + # lissage de l'image + def smooth(self, pixelMap, seuil=5): + width = len( pixelMap[0] ) + height = len( pixelMap ) + if seuil < 0 or seuil > 255: # si le seuil est incohérent => valeur par défaut (5) + seuil = 5; + + + # on parcourt tout les pixels + for y in range(0, len(pixelMap)): + for x in range(0, len(pixelMap[y])): + + # on calcule la moyenne des valeurs R G B du pixel courant + pMoy = ( pixelMap[y][x].r + pixelMap[y][x].g + pixelMap[y][x].b ) / 3 + + + xmin, ymin, xmax, ymap = x, y, x, y; # les bornes ducarré 3x3 autour du pixel + rMoy, gMoy, bMoy, count = 0.0, 0.0, 0.0, 0 # initialisation des variables de moyennes et de total + rInterval, gInterval, bInterval, rgbInterval = 0, 0, 0, 0 # initialisation des variables d'intervalles entre les couleurs + + + # GESTION DES ANGLES + + # ordonnées: borne inférieure + if y-1 > -1: + ymin = y-1 + # ordonnées: borne supérieure + if y+1 < height: + ymax = y+1 + # abscisses: borne inférieure + if x-1 > -1: + xmin = x-1 + # abscisses: borne supérieure + if x+1 < width: + xmax = x+1 + + + pixels = [ pixelMap[y][xmin], pixelMap[y][xmax], pixelMap[ymin][x], pixelMap[ymax][x] ]; + for p in pixels: + if p != pixelMap[y][x]: + rMoy += p.r; + gMoy += p.g; + bMoy += p.b; + count += 1 + + # si il y a au moins un pixel autour (normalement tjs mais évite l'erreur div par zéro) + if count > 0: + # on calcule les moyennes somme(xi) / n + rMoy = int( rMoy / count ) + gMoy = int( gMoy / count ) + bMoy = int( bMoy / count ) + + # calcul de la différence entre les couleurs du pixel et la moyenne des couleurs des pixels autour + rInterval = abs( pixelMap[y][x].r - rMoy ) + gInterval = abs( pixelMap[y][x].g - gMoy ) + bInterval = abs( pixelMap[y][x].b - bMoy ) + + # calcul de la différence en nuance de gris (moyenne des couleurs) + rgbInterval = ( rInterval + gInterval + bInterval ) / 3 + + # si la couleur est trop "différente" (dépend du seuil) alors on remplace sa couleur par la moyenne des couleurs alentours + if rgbInterval > seuil: + pixelMap[y][x].setRGB(rMoy, gMoy, bMoy); diff --git a/code/bmp.py b/code/bmp.py index 19a0865..624fb3f 100644 --- a/code/bmp.py +++ b/code/bmp.py @@ -19,74 +19,83 @@ if len(sys.argv) < 3: ################" INTERFACE "################### -print "+-------------------------+" -print "| |" -print "| TRAITEMENT D'IMAGE |" -print "| |" -print "+-------------------------+" -print "| %s |" % exactLength( sys.argv[1], 17, -1) -print "| %s |" % exactLength( sys.argv[2], 17, -1) -print "+-------------------------+" -print "| %s |" % exactLength("TESTS DE FICHIER", 23, 0) -print "| %s |" % exactLength("", 23, 0) -print "| 0) %s |" % exactLength("Creation manuelle", 20, -1) -print "| 1) %s |" % exactLength("Parse/Unparse", 20, -1) -print "| 2) %s |" % exactLength("Afficher palette", 20, -1) -print "+-------------------------+" -print "| %s |" % exactLength("TESTS DE BRUIT", 23, 0) -print "| %s |" % exactLength("", 23, 0) -print "| 3) %s |" % exactLength("Salt&Pepper", 20, -1) -print "| 4) %s |" % exactLength("Additif", 20, -1) -print "+-------------------------+" -print "| %s |" % exactLength("TESTS DE DIFFERENCES", 23, 0) -print "| %s |" % exactLength("", 23, 0) -print "| 5) %s |" % exactLength("Difference en %", 20, -1) -print "| 6) %s |" % exactLength("Difference par P", 20, -1) -print "| 7) %s |" % exactLength("Difference en image", 20, -1) -print "| 8) %s |" % exactLength("Fusion d'images (+)", 20, -1) -print "| 9) %s |" % exactLength("Fusion d'images (-)", 20, -1) -print "+-------------------------+" +print "+---------------------------+" +print "| |" +print "| TRAITEMENT D'IMAGE |" +print "| |" +print "+---------------------------+" +print "| %s |" % exactLength( sys.argv[1], 19, -1) +print "| %s |" % exactLength( sys.argv[2], 19, -1) +print "+---------------------------+" +print "| %s |" % exactLength("TESTS DE FICHIER", 25, 0) +print "| %s |" % exactLength("", 25, 0) +print "| 0) %s |" % exactLength("Creation manuelle", 21, -1) +print "| 1) %s |" % exactLength("Parse/Unparse", 21, -1) +print "| 2) %s |" % exactLength("Afficher palette", 21, -1) +print "+---------------------------+" +print "| %s |" % exactLength("TESTS DE BRUIT", 25, 0) +print "| %s |" % exactLength("", 25, 0) +print "| 10) %s |" % exactLength("Salt&Pepper", 21, -1) +print "| 11) %s |" % exactLength("Additif", 21, -1) +print "| 12) %s |" % exactLength("Lissage", 21, -1) +print "+---------------------------+" +print "| %s |" % exactLength("TESTS DE DIFFERENCES", 25, 0) +print "| %s |" % exactLength("", 25, 0) +print "| 20) %s |" % exactLength("Difference en %", 21, -1) +print "| 21) %s |" % exactLength("Difference par P", 21, -1) +print "| 22) %s |" % exactLength("Difference en image", 21, -1) +print "| 23) %s |" % exactLength("Fusion d'images (+)", 21, -1) +print "| 24) %s |" % exactLength("Fusion d'images (-)", 21, -1) +print "+---------------------------+" print while True: action = int( raw_input("choix: ") ) - if action >= 0 and action <= 9: + if action >= 0 and action < 30: break; print -print "+-------------------------+---------+" +print "+---------------------------+---------+" execTime = Timer(); execTime.reset(); result = "" +# fichier if action == 0: - testManualCreation() # teste la création d'un fichier à partir d'une matrice uniquement + testManualCreation() # teste la création d'un fichier à partir d'une matrice uniquement elif action == 1: result = testFileIntegrity() # teste le PARSE/UNPARSE elif action == 2: result = printIntPalette() # affiche la palette d'une image -elif action == 3: - testSaltAndPepper() # teste le bruitage/débruitage de type "Sel & Poivre" -elif action == 4: - testAdditiveNoise() # teste le bruitage/débruitage de type "Additif" -elif action == 5: - printImageQuality() # compare 2 images et donne le pourcentage de ressemblance/différence -elif action == 6: + +# bruits +elif action == 10: + testSaltAndPepper() # teste le bruitage/débruitage de type "Sel & Poivre" +elif action == 11: + testAdditiveNoise() # teste le bruitage/débruitage de type "Additif" +elif action == 12: + testSmooth() # teste le lissage + +# performances +elif action == 20: + printImageQuality() # compare 2 images et donne le pourcentage de ressemblance/différence +elif action == 21: print "not implemented yet" exit() - printImageQualityByPower() # compare 2 images et donne le pourcentage de ressemblance/différence (utilisant la puissance) -elif action == 7: - imageForImageQuality() # crée une image correspondant aux différences de 2 images -elif action == 8: - mergeImagesAdditive() # crée une image étant la fusion (addition) de 2 images -elif action == 9: - mergeImagesSubstractive() # crée une image étant la fusion (soustractive) de 2 images + printImageQualityByPower() # compare 2 images et donne le pourcentage de ressemblance/différence (utilisant la puissance) +elif action == 22: + imageForImageQuality() # crée une image correspondant aux différences de 2 images +elif action == 23: + mergeImagesAdditive() # crée une image étant la fusion (addition) de 2 images +elif action == 24: + mergeImagesSubstractive() # crée une image étant la fusion (soustractive) de 2 images else: - print "Error! aborting" + print "Wrong choice" + exit(); -print "+-------------------------+---------+" -print "| EXECUTION TIME | %s |" % execTime.get() -print "+-------------------------+---------+" +print "+---------------------------+---------+" +print "| EXECUTION TIME | %s |" % execTime.get() +print "+---------------------------+---------+" print print result diff --git a/code/tests.py b/code/tests.py index 02c3523..6beae4c 100644 --- a/code/tests.py +++ b/code/tests.py @@ -92,7 +92,7 @@ def testFileIntegrity(): returnValue = "" # lecture du fichier - print "| Reading Image |",; t.reset(); + print "| Reading Image |",; t.reset(); with open( sys.argv[1] ) as file: binFile = file.read() print "%s |" % (t.get()) @@ -102,7 +102,7 @@ def testFileIntegrity(): # Parsing - print "| Parsing file |",; t.reset(); + print "| Parsing file |",; t.reset(); img.parse( binFile ); print "%s |" % (t.get()) @@ -113,23 +113,23 @@ def testFileIntegrity(): # Unparsing - print "| Unparsing file |",; t.reset(); + print "| Unparsing file |",; t.reset(); img.unparse(); print "%s |" % (t.get()) # Writing - print "| Writing file |",; t.reset(); + print "| Writing file |",; t.reset(); img.write( sys.argv[2] ) print "%s |" % (t.get()) # lecture du fichier - print "| Reading Image |",; t.reset(); + print "| Reading Image |",; t.reset(); with open( sys.argv[2] ) as file: binFile = file.read() print "%s |" % (t.get()) # Parsing - print "| Parsing file |",; t.reset(); + print "| Parsing file |",; t.reset(); img.parse( binFile ); print "%s |" % (t.get()) @@ -159,7 +159,7 @@ def testSaltAndPepper(): # lecture du fichier - print "| Reading Image |",; t.reset(); + print "| Reading Image |",; t.reset(); with open( sys.argv[1] ) as file: binFile = file.read() print "%s |" % (t.get()) @@ -170,44 +170,44 @@ def testSaltAndPepper(): # Parsing - print "| Parsing file |",; t.reset(); + print "| Parsing file |",; t.reset(); img.parse( binFile ); print "%s |" % (t.get()) - print "| Creating Salt&Pepper |",; t.reset(); + print "| Creating Salt&Pepper |",; t.reset(); noise.SaltAndPepper_set(img.content.map, seuil=20) print "%s |" % (t.get()) # Unparsing - print "| Unparsing file |",; t.reset(); + print "| Unparsing file |",; t.reset(); img.unparse() print "%s |" % (t.get()) # image to stdout - print "| Writing file |",; t.reset(); + print "| Writing file |",; t.reset(); img.write( "SaltAndPepper.bmp" ) print "%s |" % (t.get()) - print "| Removing Salt&Pepper |",; t.reset(); + print "| Removing Salt&Pepper |",; t.reset(); noise.SaltAndPepper_unset(img.content.map, seuil=1, borne=1) print "%s |" % (t.get()) - print "| Débruitage additif |",; t.reset(); - noise.AdditiveNoise_unset(img.content.map); + print "| Lissage |",; t.reset(); + noise.smooth(img.content.map); print "%s |" % (t.get()) # Unparsing - print "| Unparsing file |",; t.reset(); + print "| Unparsing file |",; t.reset(); img.unparse() print "%s |" % (t.get()) # image to stdout - print "| Writing file |",; t.reset(); + print "| Writing file |",; t.reset(); img.write( sys.argv[2] ) print "%s |" % (t.get()) @@ -236,7 +236,7 @@ def testAdditiveNoise(): # lecture du fichier - print "| Reading Image |",; t.reset(); + print "| Reading Image |",; t.reset(); with open( sys.argv[1] ) as file: binFile = file.read() print "%s |" % (t.get()) @@ -247,40 +247,40 @@ def testAdditiveNoise(): # Parsing - print "| Parsing file |",; t.reset(); + print "| Parsing file |",; t.reset(); img.parse( binFile ); print "%s |" % (t.get()) - print "| Creating Additive |",; t.reset(); + print "| Creating Additive |",; t.reset(); noise.AdditiveNoise_set(img.content.map, seuil=50) print "%s |" % (t.get()) # Unparsing - print "| Unparsing file |",; t.reset(); + print "| Unparsing file |",; t.reset(); img.unparse() print "%s |" % (t.get()) # image to stdout - print "| Writing file |",; t.reset(); + print "| Writing file |",; t.reset(); img.write( "AdditiveNoise.bmp" ) print "%s |" % (t.get()) - print "| Removing Additive |",; t.reset(); + print "| Removing Additive |",; t.reset(); noise.AdditiveNoise_unset(img.content.map) print "%s |" % (t.get()) # Unparsing - print "| Unparsing file |",; t.reset(); + print "| Unparsing file |",; t.reset(); img.unparse() print "%s |" % (t.get()) # image to stdout - print "| Writing file |",; t.reset(); + print "| Writing file |",; t.reset(); img.write( sys.argv[2] ) print "%s |" % (t.get()) @@ -296,6 +296,65 @@ def testAdditiveNoise(): +# teste les fonction de bruitage et débruitage de type "Additif" # +######################################################################## +# @sysarg 1 le fichier d'origine +# @stsarg 2 le fichier de sortie (lissé) +# +# @history +# Parse le fichier d'origine +# Lisse le fichier +# Unparse l'image et l'enregistre dans le fichier de sortie +def testSmooth(): + + t = Timer(); + + + # lecture du fichier + print "| Reading Image |",; t.reset(); + with open( sys.argv[1] ) as file: + binFile = file.read() + print "%s |" % (t.get()) + + + img = BMPFile(); # Instanciation du BMPFile + noise = Noise(); # Instanciation du NoiseObject + + + # Parsing + print "| Parsing file |",; t.reset(); + img.parse( binFile ); + print "%s |" % (t.get()) + + print "| Smooth image |",; t.reset(); + noise.smooth(img.content.map, seuil=5) + print "%s |" % (t.get()) + + # Unparsing + print "| Unparsing file |",; t.reset(); + img.unparse() + print "%s |" % (t.get()) + + # image to stdout + print "| Writing file |",; t.reset(); + img.write( sys.argv[2] ) + print "%s |" % (t.get()) + + + + + + + + + + + + + + + + # teste la création d'image manuelle (UNPARSE) à partir d'une matrice uniquement # ################################################################################## # @sysarg 1 le fichier de sortie @@ -308,7 +367,7 @@ def testManualCreation(): t = Timer(); - print "| Creating Image |",; t.reset(); + print "| Creating Image |",; t.reset(); img = BMPFile() for y in range(0, 100): img.content.map.append( [] ) @@ -324,7 +383,7 @@ def testManualCreation(): print "%s |" % (t.get()) - print "| Writing Image |",; t.reset(); + print "| Writing Image |",; t.reset(); img.write( sys.argv[2] ) print "%s |" % (t.get()) @@ -343,13 +402,13 @@ def printIntPalette(): t = Timer(); - print "| Reading Image |",; t.reset(); + print "| Reading Image |",; t.reset(); with open( sys.argv[1] ) as file: binFile = file.read() print "%s |" % (t.get()) - print "| Parsing File |",; t.reset(); + print "| Parsing File |",; t.reset(); img.parse(binFile); print "%s |" % (t.get()) @@ -384,7 +443,7 @@ def printImageQuality(): # lecture des fichiers - print "| Reading files |",; t.reset(); + print "| Reading files |",; t.reset(); with open( sys.argv[1] ) as f: imageFile = f.read(); with open( sys.argv[2] ) as f: @@ -392,7 +451,7 @@ def printImageQuality(): print "%s |" % (t.get()) # parsage - print "| Parsing images |",; t.reset(); + print "| Parsing images |",; t.reset(); image = BMPFile(); image.parse( imageFile ); model = BMPFile(); model.parse( modelFile ); print "%s |" % (t.get()) @@ -407,7 +466,7 @@ def printImageQuality(): # comparaison - print "| Comparaison |",; t.reset(); + print "| Comparaison |",; t.reset(); count, totalCount = [0,0,0], imagePixelCount*256*3 for y in range(0, image.header.height): for x in range(0, image.header.width): @@ -419,9 +478,9 @@ def printImageQuality(): percentage = 100.0 * (totalCount-differenceCount) / totalCount percentage = int(100*percentage)/100.0 print "%s |" % (t.get()) - print "+-------------------------+---------+" - print "| Commun = %s | |" % exactLength( str(percentage)+"%", 10, -1 ); - print "| Difference = %s | |" % exactLength( str(100-percentage)+"%", 10, -1 ); + print "+---------------------------+---------+" + print "| Commun = %s | |" % exactLength( str(percentage)+"%", 12, -1 ); + print "| Difference = %s | |" % exactLength( str(100-percentage)+"%", 12, -1 ); @@ -448,7 +507,7 @@ def imageForImageQuality(): # lecture des fichiers - print "| Reading files |",; t.reset(); + print "| Reading files |",; t.reset(); with open( sys.argv[1] ) as f: imageFile = f.read(); with open( sys.argv[2] ) as f: @@ -456,7 +515,7 @@ def imageForImageQuality(): print "%s |" % (t.get()) # parsage - print "| Parsing images |",; t.reset(); + print "| Parsing images |",; t.reset(); image.parse( imageFile ); model.parse( modelFile ); print "%s |" % (t.get()) @@ -471,7 +530,7 @@ def imageForImageQuality(): # comparaison - print "| Comparaison |",; t.reset(); + print "| Comparaison |",; t.reset(); count, totalCount = [0,0,0], imagePixelCount*256*3 for y in range(0, image.header.height): newImg.content.map.append( [] ); @@ -483,11 +542,11 @@ def imageForImageQuality(): ) ) print "%s |" % (t.get()) - print "| Unparsing |",; t.reset(); + print "| Unparsing |",; t.reset(); newImg.unparse(); print "%s |" % (t.get()) - print "| Writing File |",; t.reset(); + print "| Writing File |",; t.reset(); with open("compare.bmp", "w") as f: f.write( newImg.binData ); print "%s |" % (t.get()) @@ -518,7 +577,7 @@ def mergeImagesAdditive(): # lecture des fichiers - print "| Reading files |",; t.reset(); + print "| Reading files |",; t.reset(); with open( sys.argv[1] ) as f: AFile = f.read(); with open( sys.argv[2] ) as f: @@ -526,7 +585,7 @@ def mergeImagesAdditive(): print "%s |" % (t.get()) # parsage - print "| Parsing images |",; t.reset(); + print "| Parsing images |",; t.reset(); A.parse( AFile ); B.parse( BFile ); print "%s |" % (t.get()) @@ -542,7 +601,7 @@ def mergeImagesAdditive(): # comparaison - print "| Merging |",; t.reset(); + print "| Merging |",; t.reset(); for y in range(0, A.header.height): newImg.content.map.append( [] ); for x in range(0, A.header.width): @@ -554,11 +613,11 @@ def mergeImagesAdditive(): print "%s |" % (t.get()) - print "| Unparsing |",; t.reset(); + print "| Unparsing |",; t.reset(); newImg.unparse(newBpp=24); print "%s |" % (t.get()) - print "| Writing File |",; t.reset(); + print "| Writing File |",; t.reset(); with open("mergeAdd.bmp", "w") as f: f.write( newImg.binData ); print "%s |" % (t.get()) @@ -589,7 +648,7 @@ def mergeImagesSubstractive(): # lecture des fichiers - print "| Reading files |",; t.reset(); + print "| Reading files |",; t.reset(); with open( sys.argv[1] ) as f: AFile = f.read(); with open( sys.argv[2] ) as f: @@ -597,7 +656,7 @@ def mergeImagesSubstractive(): print "%s |" % (t.get()) # parsage - print "| Parsing images |",; t.reset(); + print "| Parsing images |",; t.reset(); A.parse( AFile ); B.parse( BFile ); print "%s |" % (t.get()) @@ -613,7 +672,7 @@ def mergeImagesSubstractive(): # comparaison - print "| Merging |",; t.reset(); + print "| Merging |",; t.reset(); for y in range(0, A.header.height): newImg.content.map.append( [] ); for x in range(0, A.header.width): @@ -625,11 +684,11 @@ def mergeImagesSubstractive(): print "%s |" % (t.get()) - print "| Unparsing |",; t.reset(); + print "| Unparsing |",; t.reset(); newImg.unparse(newBpp=24); print "%s |" % (t.get()) - print "| Writing File |",; t.reset(); + print "| Writing File |",; t.reset(); with open("mergeSub.bmp", "w") as f: f.write( newImg.binData ); print "%s |" % (t.get())