diff --git a/code/bmp.py b/code/main.py similarity index 85% rename from code/bmp.py rename to code/main.py index 3d9a416..eeb1f94 100644 --- a/code/bmp.py +++ b/code/main.py @@ -50,13 +50,17 @@ print "| %s |" % exactLength("TESTS DE FORMES", 25, 0) print "| %s |" % exactLength("", 25, 0) print "| 30) %s |" % exactLength("Reveler une teinte", 21, -1) print "| 31) %s |" % exactLength("Colorer une forme", 21, -1) +print "| 32) %s |" % exactLength("Colorer les formes", 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 "| 41) %s |" % exactLength("Laplace", 21, -1) +print "| 42) %s |" % exactLength("Roberts", 21, -1) +print "| 43) %s |" % exactLength("Prewitt", 21, -1) +print "| 44) %s |" % exactLength("Sobel", 21, -1) +print "| 45) %s |" % exactLength("Convolution", 21, -1) +print "| 46) %s |" % exactLength("bichrome", 21, -1) print "+---------------------------+" print while True: @@ -151,6 +155,9 @@ elif action == 31: arg2 = int(y) print startStr colorShape(arg1, arg2) # colorie la forme contenant le pixel de coordonnées donné +elif action == 32: + print startStr + colorAllShapes() # colorie la forme contenant le pixel de coordonnées donné # filtres @@ -160,16 +167,25 @@ elif action == 40: if s != "": arg1 = int(s) print startStr - testSmooth(arg1) # teste le lissage + testSmooth(arg1) # teste le lissage elif action == 41: print startStr - testRoberts() # teste le filtre de Roberts + testLaplace() # teste le filtre de Laplace elif action == 42: print startStr - testPrewitt() # teste le filtre de Prewitt + testRoberts() # teste le filtre de Roberts elif action == 43: print startStr - testSobel() # teste le filtre de Prewitt + testPrewitt() # teste le filtre de Prewitt +elif action == 44: + print startStr + testSobel() # teste le filtre de Sobel +elif action == 45: + print startStr + testConvolution() # teste le filtre de Convolution +elif action == 46: + print startStr + testBichrome() # teste le passage au bichromatique else: print "Wrong choice" diff --git a/code/mask.bmp b/code/mask.bmp deleted file mode 100644 index 2e49194..0000000 Binary files a/code/mask.bmp and /dev/null differ diff --git a/code/tests.py b/code/tests.py index c4f88d5..e8e2767 100644 --- a/code/tests.py +++ b/code/tests.py @@ -321,7 +321,7 @@ def testAdditiveNoise(): print "| Creating Additive |",; t.reset(); - FX.AdditiveNoise.set(img.content.map, seuil=50) + FX.Additive.set(img.content.map, seuil=50) print "%s |" % (t.get()) # Unparsing @@ -338,7 +338,7 @@ def testAdditiveNoise(): print "| Removing Additive |",; t.reset(); - FX.AdditiveNoise.unset(img.content.map) + FX.Additive.unset(img.content.map) print "%s |" % (t.get()) # Unparsing @@ -720,7 +720,6 @@ def revealShapes(red=0,green=0,blue=0, seuil=50): def colorShape(x=0, y=0): t = Timer(); img = BMPFile() - noise = Noise() # lecture du fichier print "| Reading file |",; t.reset(); @@ -735,8 +734,8 @@ def colorShape(x=0, y=0): - # condition (si blanc uniquement) - if img.content.map[y][x].r != 255 or img.content.map[y][x].g != 255 or img.content.map[y][x].b != 255: + # condition (si loin du noir uniquement) + if img.content.map[y][x].r + img.content.map[y][x].g + img.content.map[y][x].b <= 100: # si loin du noir print "\n*** must be a WHITE pixel" exit() @@ -773,6 +772,71 @@ def colorShape(x=0, y=0): +# Colore toutes les formes chacune avec des couleurs aléatoires # +################################################################# +# @sysarg 1 Image à traiter +# @stsarg 2 Image de sortie +# +# @history +# Parse le fichier d'entrée +# colore les formes +# Unparse le tout et l'enregistre dans le fichier de sortie +def colorAllShapes(): + t = Timer(); + img = BMPFile() + + # lecture du fichier + print "| Reading file |",; t.reset(); + with open( sys.argv[1] ) as f: + binFile = f.read(); + print "%s |" % (t.get()) + + # parsage + print "| Parsing image |",; t.reset(); + img.parse( binFile ); + print "%s |" % (t.get()) + + + + + # récupère les formes + print "| Getting shapes |",; t.reset(); + already = [] + for line in img.content.map: + for pixel in line: + # condition (si ce n'est pas le fond ~= noir) + if pixel.r + pixel.g + pixel.b > 3*100 and pixel not in already: # si loin du noir + shape = FX.Shape.get(pixel, img.content.map) + print "shape detected" + R, G, B = random.randint(0,255), random.randint(0,255), random.randint(0,255) + + # on colorie la forme en rouge + for p in shape: + p.setRGB(R, G, B); + already += shape + + print "%s |" % (t.get()) + + + + print "| Unparsing |",; t.reset(); + img.unparse(newBpp=24); + print "%s |" % (t.get()) + + print "| Writing File |",; t.reset(); + with open( sys.argv[2], "w") as f: + f.write( img.binData ); + print "%s |" % (t.get()) + + + + + + + + + + @@ -841,6 +905,51 @@ def testSmooth(seuil=5): +# teste le filtre de "Laplace" 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 testLaplace(): + 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.Laplace(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 "Roberts" sur d'une image # ################################################ # @sysarg 1 le fichier d'origine @@ -991,3 +1100,118 @@ def testSobel(): + +# teste le filtre de Convolution 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 testConvolution(): + 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.Convolution(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 passage au bichromatique # +##################################### +# @sysarg 1 le fichier d'origine +# @stsarg 2 le fichier de sortie (bichromé) +# +# @history +# Parse le fichier d'origine +# Applique le filtre +# Unparse l'image et l'enregistre dans le fichier de sortie +def testBichrome(): + 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(); + for line in img.content.map: + for pixel in line: + pixel.setRGB( + 255*int( (pixel.r+pixel.g+pixel.b)/3 >= 128 ), + 255*int( (pixel.r+pixel.g+pixel.b)/3 >= 128 ), + 255*int( (pixel.r+pixel.g+pixel.b)/3 >= 128 ) + ) + 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 83c6e93..2dea68d 100644 --- a/code/utility/Filter.py +++ b/code/utility/Filter.py @@ -78,6 +78,57 @@ class Filter: + + # applique le filtre de "Laplace" sur l'image # + ############################################### + # @param pixelMap la matrice de pixels à modifier + # + # @history + # applique le filtre + # + # -1 -1 -1 + # + # 1/8 * -1 8 -1 + # + # -1 -1 -1 + def Laplace(self, 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,-1,-1], [-1,8,-1], [-1,-1,-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])): + # 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/8 % 256 + g = g/8 % 256 + b = b/8 % 256 + + + # définition des couleurs + pixel.setRGB( + # print "%s - %s - %s" % ( + int( r ), + int( g ), + int( b ) + ) + + + # applique le filtre de "Roberts" sur l'image # ############################################### # @param pixelMap la matrice de pixels à modifier @@ -85,30 +136,50 @@ class Filter: # @history # applique le filtre # - # 0 -1 0 + # 1 0 # - # -1 5 -1 + # 0 -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]; + pixel = pixelMap[y][x] + + filters = [ + [ [1, 0], [0,-1] ], + [ [0, 1], [-1,0] ] + ] + + pixelM = [ pixelMap[y][x:x+1], pixelMap[y+1][x: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 % 256 + g = g % 256 + b = b % 256 + # 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 + int( r ), + int( g ), + int( b ) ) + # applique le filtre de "Prewitt" sur l'image # ############################################### # @param pixelMap la matrice de pixels à modifier @@ -122,17 +193,16 @@ class Filter: # # -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] ] + filters = [ + 3*[[-1, 0, 1]], + [ 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] ] @@ -140,13 +210,15 @@ class Filter: 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] + # 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/3 % 256 - g = g/3 % 256 - b = b/3 % 256 + r = r/4 % 256 + g = g/4 % 256 + b = b/4 % 256 # définition des couleurs @@ -172,9 +244,6 @@ class Filter: # # -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): @@ -204,6 +273,59 @@ class Filter: b = b/4 % 256 + # définition des couleurs + pixel.setRGB( + # print "%s - %s - %s" % ( + int( r ), + int( g ), + int( b ) + ) + + + # applique le filtre de "convolution" sur l'image # + ################################################### + # @param pixelMap la matrice de pixels à modifier + # + # @history + # applique le filtre + # + # -1 -1 -1 + # + # 1/8 * -1 8 -1 + # + # -1 -1 -1 + def Convolution(self, 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] + ] + ] + + 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" % ( diff --git a/docs/shapes/Chapitre4.pdf b/docs/shapes/Chapitre4.pdf new file mode 100644 index 0000000..ef18366 Binary files /dev/null and b/docs/shapes/Chapitre4.pdf differ diff --git a/docs/shapes/potrace.pdf b/docs/shapes/potrace.pdf new file mode 100644 index 0000000..a7cab37 Binary files /dev/null and b/docs/shapes/potrace.pdf differ