diff --git a/code/bmp.py b/code/bmp.py index b00e2d4..3d9a416 100644 --- a/code/bmp.py +++ b/code/bmp.py @@ -37,7 +37,6 @@ 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) @@ -52,10 +51,17 @@ print "| %s |" % exactLength("", 25, 0) print "| 30) %s |" % exactLength("Reveler une teinte", 21, -1) print "| 31) %s |" % exactLength("Colorer une forme", 21, -1) print "+---------------------------+" +print "| %s |" % exactLength("TESTS DE FILTRES", 25, 0) +print "| %s |" % exactLength("", 25, 0) +print "| 40) %s |" % exactLength("Lissage", 21, -1) +print "| 41) %s |" % exactLength("Roberts", 21, -1) +print "| 42) %s |" % exactLength("Prewitt", 21, -1) +print "| 43) %s |" % exactLength("Sobel", 21, -1) +print "+---------------------------+" print while True: action = int( raw_input("choix: ") ) - if action >= 0 and action < 40: + if action >= 0 and action < 50: break; startStr = "\n+---------------------------+---------+" @@ -101,14 +107,6 @@ elif action == 10: elif action == 11: print startStr testAdditiveNoise() # teste le bruitage/débruitage de type "Additif" -elif action == 12: - s = raw_input("Seuil [5]: "); - arg1 = 5 - if s != "": - arg1 = int(s) - print startStr - testSmooth(arg1) # teste le lissage - # performances elif action == 20: print startStr @@ -153,6 +151,26 @@ elif action == 31: arg2 = int(y) print startStr colorShape(arg1, arg2) # colorie la forme contenant le pixel de coordonnées donné + + +# filtres +elif action == 40: + s = raw_input("Seuil [5]: "); + arg1 = 5 + if s != "": + arg1 = int(s) + print startStr + testSmooth(arg1) # teste le lissage +elif action == 41: + print startStr + testRoberts() # teste le filtre de Roberts +elif action == 42: + print startStr + testPrewitt() # teste le filtre de Prewitt +elif action == 43: + print startStr + testSobel() # teste le filtre de Prewitt + else: print "Wrong choice" exit(); diff --git a/code/mask.bmp b/code/mask.bmp index 2b000e4..2e49194 100644 Binary files a/code/mask.bmp and b/code/mask.bmp differ diff --git a/code/new.bmp b/code/new.bmp deleted file mode 100644 index 1d89c27..0000000 Binary files a/code/new.bmp and /dev/null differ diff --git a/code/tests.py b/code/tests.py index 43c0ebe..c4f88d5 100644 --- a/code/tests.py +++ b/code/tests.py @@ -363,61 +363,6 @@ 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(seuil=5): - 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 - - - # Parsing - print "| Parsing file |",; t.reset(); - img.parse( binFile ); - print "%s |" % (t.get()) - - - print "| Lissage |",; t.reset(); - FX.Filter.smooth(img.content.map, seuil=seuil); - 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()) - - - - - - - - - - - - - @@ -817,4 +762,232 @@ def colorShape(x=0, y=0): print "| Writing File |",; t.reset(); with open( sys.argv[2], "w") as f: f.write( img.binData ); - print "%s |" % (t.get()) \ No newline at end of file + print "%s |" % (t.get()) + + + + + + + + + + + + + + + + + + + + + + + +# teste la fonction de lissage d'une image (algorithme quelconque) # +#################################################################### +# @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(seuil=5): + 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 + + + # Parsing + print "| Parsing file |",; t.reset(); + img.parse( binFile ); + print "%s |" % (t.get()) + + + print "| Lissage |",; t.reset(); + FX.Filter.smooth(img.content.map, seuil=seuil); + 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 le filtre de "Roberts" sur d'une image # +################################################ +# @sysarg 1 le fichier d'origine +# @stsarg 2 le fichier de sortie (filtré) +# +# @history +# Parse le fichier d'origine +# Applique le filtre +# Unparse l'image et l'enregistre dans le fichier de sortie +def testRoberts(): + 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 + + + # Parsing + print "| Parsing file |",; t.reset(); + img.parse( binFile ); + print "%s |" % (t.get()) + + + print "| Application du filtre |",; t.reset(); + FX.Filter.Roberts(img.content.map); + 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 le filtre de "Prewitt" sur d'une image # +################################################ +# @sysarg 1 le fichier d'origine +# @stsarg 2 le fichier de sortie (filtré) +# +# @history +# Parse le fichier d'origine +# Applique le filtre +# Unparse l'image et l'enregistre dans le fichier de sortie +def testPrewitt(): + 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 + + + # Parsing + print "| Parsing file |",; t.reset(); + img.parse( binFile ); + print "%s |" % (t.get()) + + + print "| Application du filtre |",; t.reset(); + FX.Filter.Prewitt(img.content.map); + 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 le filtre de "Sobel" sur d'une image # +############################################## +# @sysarg 1 le fichier d'origine +# @stsarg 2 le fichier de sortie (filtré) +# +# @history +# Parse le fichier d'origine +# Applique le filtre +# Unparse l'image et l'enregistre dans le fichier de sortie +def testSobel(): + 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 + + + # Parsing + print "| Parsing file |",; t.reset(); + img.parse( binFile ); + print "%s |" % (t.get()) + + + print "| Application du filtre |",; t.reset(); + FX.Filter.Sobel(img.content.map); + 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()) + + + + + + + + + + diff --git a/code/utility/Filter.py b/code/utility/Filter.py index 397d12a..83c6e93 100644 --- a/code/utility/Filter.py +++ b/code/utility/Filter.py @@ -72,4 +72,142 @@ class Filter: # 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); \ No newline at end of file + pixelMap[y][x].setRGB(rMoy, gMoy, bMoy); + + + + + + # applique le filtre de "Roberts" sur l'image # + ############################################### + # @param pixelMap la matrice de pixels à modifier + # + # @history + # applique le filtre + # + # 0 -1 0 + # + # -1 5 -1 + # + # 0 -1 0 + def Roberts(self, pixelMap): + width = len( pixelMap[0] ) + height = len( pixelMap ) + + # on parcourt tout les pixels + for y in range(1, len(pixelMap)-1): + for x in range(1, len(pixelMap[y])-1): + + pixel = pixelMap[y][x]; + + # définition des couleurs + pixel.setRGB( + # print "%s - %s - %s" % ( + int( 128 + 5*pixel.r - ( pixelMap[y][x+1].r + pixelMap[y][x-1].r + pixelMap[y-1][x].r + pixelMap[y+1][x].r ) ) % 256, + int( 128 + 5*pixel.g - ( pixelMap[y][x+1].g + pixelMap[y][x-1].g + pixelMap[y-1][x].g + pixelMap[y+1][x].g ) ) % 256, + int( 128 + 5*pixel.b - ( pixelMap[y][x+1].b + pixelMap[y][x-1].b + pixelMap[y-1][x].b + pixelMap[y+1][x].b ) ) % 256 + ) + + + # applique le filtre de "Prewitt" sur l'image # + ############################################### + # @param pixelMap la matrice de pixels à modifier + # + # @history + # applique le filtre + # + # -1 0 1 -1 -1 -1 + # + # 1/3 * -1 0 1 + 0 0 0 + # + # -1 0 1 1 1 1 + def Prewitt(self, pixelMap): + width = len( pixelMap[0] ) + height = len( pixelMap ) + + # on parcourt tout les pixels + for y in range(1, len(pixelMap)-1): + for x in range(1, len(pixelMap[y])-1): + + pixel = pixelMap[y][x]; + + filterA = 3*[[-1, 0, 1]] + filterB = [ 3*[-1], 3*[0], 3*[1] ] + + pixelM = [ pixelMap[y-1][x-1:x+1], pixelMap[y][x-1:x+1], pixelMap[y+1][x-1:x+1] ] + + r,g,b = 0,0,0 + + for j in range(0,len(pixelM)): + for i in range(0,len(pixelM[j])): + r += pixelM[j][i].r * filterA[j][i] + g += pixelM[j][i].g * filterA[j][i] + b += pixelM[j][i].b * filterA[j][i] + + r = r/3 % 256 + g = g/3 % 256 + b = b/3 % 256 + + + # définition des couleurs + pixel.setRGB( + # print "%s - %s - %s" % ( + int( r ), + int( g ), + int( b ) + ) + + + + # applique le filtre de "Sobel" sur l'image # + ############################################### + # @param pixelMap la matrice de pixels à modifier + # + # @history + # applique le filtre + # + # -1 0 1 -1 -2 -1 + # + # 1/4 * -2 0 2 + 0 0 0 + # + # -1 0 1 1 2 1 + def Sobel(self, pixelMap): + width = len( pixelMap[0] ) + height = len( pixelMap ) + + # on parcourt tout les pixels + for y in range(1, len(pixelMap)-1): + for x in range(1, len(pixelMap[y])-1): + + pixel = pixelMap[y][x]; + + filters = [ + [ [-1,0,1], [-2,0,2], [-1,0,1] ], + [ [-1,-2,-1], [0,0,0], [1,2,1] ], + [ [0,1,2], [-1,0,1], [-2,-1,0] ] + ] + + pixelM = [ pixelMap[y-1][x-1:x+1], pixelMap[y][x-1:x+1], pixelMap[y+1][x-1:x+1] ] + + r,g,b = 0,0,0 + + for j in range(0,len(pixelM)): + for i in range(0,len(pixelM[j])): + # pour chacun des filtres + for f in filters: + r += pixelM[j][i].r * f[j][i] + g += pixelM[j][i].g * f[j][i] + b += pixelM[j][i].b * f[j][i] + + r = r/4 % 256 + g = g/4 % 256 + b = b/4 % 256 + + + # définition des couleurs + pixel.setRGB( + # print "%s - %s - %s" % ( + int( r ), + int( g ), + int( b ) + )