Chargement unique de l'image + affichage en temps réel (pas fini)
After Width: | Height: | Size: 257 KiB |
After Width: | Height: | Size: 257 KiB |
|
@ -1,5 +1,7 @@
|
|||
# ~*~ encoding: utf-8 ~*~ #
|
||||
|
||||
from utility.Drawer import *
|
||||
|
||||
#################################################
|
||||
# classe qui parse le header (binaire) en objet #
|
||||
#################################################
|
||||
|
@ -308,14 +310,13 @@ class RGBPixel:
|
|||
else:
|
||||
self.bpp = bpp
|
||||
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.x = self.x if x == None else x;
|
||||
self.y = self.y if y == None else y;
|
||||
|
||||
self.r = r
|
||||
self.g = g
|
||||
self.b = b
|
||||
|
||||
|
||||
# gestion des différents bpp
|
||||
if bpp == 1:
|
||||
self.intData = [ int( (r+g+b)/3 >= 128 ) ]
|
||||
|
@ -331,7 +332,7 @@ class RGBPixel:
|
|||
self.binData = chr(g) + chr(b) + chr(r)
|
||||
|
||||
|
||||
def setRGB(self, r=0, g=0, b=0, x=-1, y=-1, bpp=24):
|
||||
def setRGB(self, r=0, g=0, b=0, x=None, y=None, bpp=24):
|
||||
self.__init__(r=r, g=g, b=b, x=x, y=y, bpp=bpp);
|
||||
|
||||
def setBin(self, binData, width, padding, index, bpp=24):
|
||||
|
@ -416,6 +417,8 @@ class BMPFile:
|
|||
self.binPalette = ""
|
||||
self.intPalette = []
|
||||
|
||||
self.drawer = None;
|
||||
|
||||
# parse à partir de <binFile> en objets <BMPHeader> et <BMPContent>
|
||||
def parse(self, binFile=""):
|
||||
# si on a défini le fichier
|
||||
|
@ -446,6 +449,11 @@ class BMPFile:
|
|||
for byte in self.binPalette:
|
||||
self.intPalette.append( ord(byte) )
|
||||
|
||||
self.drawer = Drawer(
|
||||
len(self.content.map[0]),
|
||||
len(self.content.map)
|
||||
);
|
||||
|
||||
# unparse à partir d'un <BMPHeader> et d'un <BMPContent>
|
||||
def unparse(self, newBpp=None):
|
||||
|
||||
|
@ -485,4 +493,5 @@ class BMPFile:
|
|||
# écrit l'image dans un fichier
|
||||
def write(self, filename):
|
||||
with open(filename,"w") as file:
|
||||
file.write( self.binData );
|
||||
file.write( self.binData );
|
||||
|
||||
|
|
Before Width: | Height: | Size: 257 KiB After Width: | Height: | Size: 257 KiB |
65
code/bmp.py
|
@ -1,65 +0,0 @@
|
|||
# ~*~ encoding: utf-8 ~*~ #
|
||||
###########################
|
||||
# TRAITEMENT D'IMAGES #
|
||||
###########################
|
||||
|
||||
# classes
|
||||
from BMPFile import *
|
||||
from Noise import *
|
||||
import random
|
||||
import sys
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def testFileIntegrity():
|
||||
# lecture du fichier
|
||||
with open( sys.argv[1] ) as file:
|
||||
binFile = file.read()
|
||||
|
||||
# Instanciation du BMPFile
|
||||
img = BMPFile()
|
||||
# Instanciation du NoiseObject
|
||||
noise = Noise();
|
||||
|
||||
# Parsing
|
||||
img.parse( binFile );
|
||||
|
||||
noise.SaltAndPepper_set(10, img.content.map)
|
||||
|
||||
|
||||
# Unparsing
|
||||
img.unparse(newBpp=24)
|
||||
|
||||
print img.binData
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def testManualCreation():
|
||||
img = BMPFile()
|
||||
for y in range(0, 100):
|
||||
img.content.map.append( [] )
|
||||
for x in range(0, 100):
|
||||
img.content.map[y].append( RGBPixel(
|
||||
random.randint(0, 255),
|
||||
random.randint(0, 255),
|
||||
random.randint(0, 255),
|
||||
bpp=24
|
||||
) );
|
||||
|
||||
img.unparse();
|
||||
|
||||
print img.binData
|
||||
|
||||
# MAIN
|
||||
#testManualCreation()
|
||||
testFileIntegrity()
|
|
@ -1,77 +0,0 @@
|
|||
import pygtk
|
||||
pygtk.require('2.0')
|
||||
import gtk
|
||||
from random import *
|
||||
|
||||
#renvoi une valeur aleatoire
|
||||
def random2():
|
||||
a = random()
|
||||
aa = a*255.0*257.0
|
||||
b = round(aa)
|
||||
c = int(b)
|
||||
return c
|
||||
|
||||
#fonction qui genere un evenement quitter
|
||||
def click_quite(widget):
|
||||
gtk.main_quit()
|
||||
|
||||
#seconde gestionn de pixel
|
||||
def expose_handler_remplir(widget, event):
|
||||
widget.set_size_request(512,512)
|
||||
w, h = widget.window.get_size()
|
||||
print "w = " + str(w) + " | h = " + str(h)
|
||||
xgc = widget.window.new_gc()
|
||||
cptW = 0
|
||||
cptH = 0
|
||||
while cptH < 512:
|
||||
while cptW < 512:
|
||||
xgc.set_rgb_fg_color(gtk.gdk.Color(random2(),0,0, pixel = 0))
|
||||
widget.window.draw_point(xgc, cptW, cptH)
|
||||
cptW += 1
|
||||
cptH += 1
|
||||
cptW = 0
|
||||
print "cptW = " + str(cptW) + "| cptH = " + str(cptH)
|
||||
|
||||
#definition des elements de base de la fenetre
|
||||
def param_main_window(widget):
|
||||
widget.set_title("Test object drawable")
|
||||
widget.set_size_request(500,500)
|
||||
|
||||
def param_main_manual_window(widget, src1, sizeX, sizeY):
|
||||
widget.set_title(src1)
|
||||
widget.set_size_request(sizeX, sizeY)
|
||||
|
||||
|
||||
#creation de la fneetre principal
|
||||
fen = gtk.Window(gtk.WINDOW_TOPLEVEL)
|
||||
#definition des elements de base de la fenetre
|
||||
param_main_manual_window(fen, "Test object drawable manual", 800, 800)
|
||||
button = gtk.Button("Quit") #bouton
|
||||
button2 = gtk.Button("Quit") # seocnd bouton
|
||||
zone = gtk.DrawingArea() #zone de dessin
|
||||
vBox = gtk.VBox() #boite de placement
|
||||
|
||||
|
||||
#raccordement a la fenetre de la box
|
||||
fen.add(vBox)
|
||||
|
||||
#connection des differents elements
|
||||
button.connect("clicked", click_quite)
|
||||
button2.connect("clicked", click_quite)
|
||||
zone.set_size_request(512,512)
|
||||
zone.connect("expose-event", expose_handler_remplir)
|
||||
|
||||
#ajout des elemnts dans les box
|
||||
vBox.pack_start(button)
|
||||
vBox.pack_start(zone)
|
||||
vBox.pack_start(button2)
|
||||
|
||||
#affichage des differents elements
|
||||
button.show()
|
||||
button2.show()
|
||||
zone.show()
|
||||
vBox.show()
|
||||
fen.show()
|
||||
gtk.main()
|
||||
|
||||
|
82
code/main.py
|
@ -22,6 +22,8 @@ if len(sys.argv) < 3:
|
|||
exit()
|
||||
|
||||
|
||||
|
||||
|
||||
################" INTERFACE "###################
|
||||
|
||||
print "+---------------------------+"
|
||||
|
@ -29,8 +31,38 @@ print "| |"
|
|||
print "| TRAITEMENT D'IMAGE |"
|
||||
print "| |"
|
||||
print "+---------------------------+"
|
||||
|
||||
# ON CREE LES IMAGES
|
||||
image1 = BMPFile();
|
||||
image2 = BMPFile();
|
||||
|
||||
|
||||
with open( sys.argv[1] ) as file:
|
||||
image1.parse( file.read() );
|
||||
|
||||
for line in image1.content.map:
|
||||
for pix in line:
|
||||
image1.drawer.setPixel( pix );
|
||||
image1.drawer.refresh();
|
||||
|
||||
print "| <in> %s |" % exactLength( sys.argv[1], 19, -1)
|
||||
|
||||
|
||||
with open( sys.argv[2] ) as file:
|
||||
image2.parse( file.read() )
|
||||
print "| <out> %s |" % exactLength( sys.argv[2], 19, -1)
|
||||
|
||||
|
||||
|
||||
################" INTERFACE "###################
|
||||
|
||||
# print "+---------------------------+"
|
||||
# print "| |"
|
||||
# print "| TRAITEMENT D'IMAGE |"
|
||||
# print "| |"
|
||||
# print "+---------------------------+"
|
||||
# print "| <in> %s |" % exactLength( sys.argv[1], 19, -1)
|
||||
# print "| <out> %s |" % exactLength( sys.argv[2], 19, -1)
|
||||
print "+---------------------------+"
|
||||
print "| %s |" % exactLength("TESTS DE FICHIER", 25, 0)
|
||||
print "| %s |" % exactLength("", 25, 0)
|
||||
|
@ -94,10 +126,10 @@ if action == 0:
|
|||
testManualCreation(arg1, arg2) # teste la création d'un fichier à partir d'une matrice uniquement
|
||||
elif action == 1:
|
||||
print startStr
|
||||
result = testFileIntegrity() # teste le PARSE/UNPARSE
|
||||
result = testFileIntegrity(image1) # teste le PARSE/UNPARSE
|
||||
elif action == 2:
|
||||
print startStr
|
||||
result = printIntPalette() # affiche la palette d'une image
|
||||
result = printIntPalette(image1) # affiche la palette d'une image
|
||||
|
||||
# bruits
|
||||
elif action == 10:
|
||||
|
@ -115,7 +147,7 @@ elif action == 10:
|
|||
if s != "":
|
||||
arg4 = int(s)
|
||||
print startStr
|
||||
testSaltAndPepper(arg1, arg2, arg3, arg4) # teste le bruitage/débruitage de type "Sel & Poivre"
|
||||
testSaltAndPepper(image1, arg1, arg2, arg3, arg4) # teste le bruitage/débruitage de type "Sel & Poivre"
|
||||
elif action == 11:
|
||||
inS = raw_input("seuil bruitage [10]: ")
|
||||
outS = raw_input("seuil débruitage [35] : ")
|
||||
|
@ -125,7 +157,7 @@ elif action == 11:
|
|||
if outS != "":
|
||||
arg2 = int(outS)
|
||||
print startStr
|
||||
testAdditiveBernouilliNoise(arg1, arg2) # teste le bruitage/débruitage de type "Additif"
|
||||
testAdditiveBernouilliNoise(image1, arg1, arg2) # teste le bruitage/débruitage de type "Additif"
|
||||
elif action == 12:
|
||||
inS = raw_input("sigma [10]: ")
|
||||
outS = raw_input("seuil débruitage [35] : ")
|
||||
|
@ -135,23 +167,23 @@ elif action == 12:
|
|||
if outS != "":
|
||||
arg2 = int(outS)
|
||||
print startStr
|
||||
testAdditiveGaussianNoise(arg1, arg2) # teste le bruitage/débruitage de type "Additif"
|
||||
testAdditiveGaussianNoise(image1, arg1, arg2) # teste le bruitage/débruitage de type "Additif"
|
||||
# performances
|
||||
elif action == 20:
|
||||
print startStr
|
||||
printImageQuality() # compare 2 images et donne le pourcentage de ressemblance/différence
|
||||
printImageQuality(image1) # compare 2 images et donne le pourcentage de ressemblance/différence
|
||||
elif action == 21:
|
||||
print startStr
|
||||
printSNR() # compare 2 images et retourne le SNR
|
||||
printSNR(image1) # compare 2 images et retourne le SNR
|
||||
elif action == 22:
|
||||
print startStr
|
||||
imageForImageQuality() # crée une image correspondant aux différences de 2 images
|
||||
imageForImageQuality(image1) # crée une image correspondant aux différences de 2 images
|
||||
elif action == 23:
|
||||
print startStr
|
||||
mergeImagesAdditive() # crée une image étant la fusion (addition) de 2 images
|
||||
mergeImagesAdditive(image1) # crée une image étant la fusion (addition) de 2 images
|
||||
elif action == 24:
|
||||
print startStr
|
||||
mergeImagesSubstractive() # crée une image étant la fusion (soustractive) de 2 images
|
||||
mergeImagesSubstractive(image1) # crée une image étant la fusion (soustractive) de 2 images
|
||||
elif action == 30:
|
||||
r = raw_input("rouge [0]: ")
|
||||
g = raw_input("vert [0]: ")
|
||||
|
@ -167,7 +199,7 @@ elif action == 30:
|
|||
if s != "":
|
||||
arg4 = int(s)
|
||||
print startStr
|
||||
revealShapes(arg1, arg2, arg3, arg4) # révèle la couleur spécifiée
|
||||
revealShapes(image1, arg1, arg2, arg3, arg4) # révèle la couleur spécifiée
|
||||
elif action == 31:
|
||||
x = raw_input("abscisses(x) [0]: ")
|
||||
y = raw_input("ordonnées(y) [0]: ")
|
||||
|
@ -177,40 +209,40 @@ elif action == 31:
|
|||
if y != "":
|
||||
arg2 = int(y)
|
||||
print startStr
|
||||
colorShape(arg1, arg2) # colorie la forme contenant le pixel de coordonnées donné
|
||||
colorShape(image1, 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é
|
||||
colorAllShapes(image1) # colorie la forme contenant le pixel de coordonnées donné
|
||||
elif action == 33:
|
||||
print startStr
|
||||
testStroke() # trace les contours uniquement à partir de formes pleines
|
||||
testStroke(image1) # trace les contours uniquement à partir de formes pleines
|
||||
|
||||
|
||||
# filtres
|
||||
elif action == 40:
|
||||
print startStr
|
||||
testAverageFilter() # teste le lissage
|
||||
testAverageFilter(image1) # teste le lissage
|
||||
elif action == 41:
|
||||
print startStr
|
||||
testLaplace() # teste le filtre de Laplace
|
||||
testLaplace(image1) # teste le filtre de Laplace
|
||||
elif action == 42:
|
||||
print startStr
|
||||
testRoberts() # teste le filtre de Roberts
|
||||
testRoberts(image1) # teste le filtre de Roberts
|
||||
elif action == 43:
|
||||
print startStr
|
||||
testPrewitt() # teste le filtre de Prewitt
|
||||
testPrewitt(image1) # teste le filtre de Prewitt
|
||||
elif action == 44:
|
||||
print startStr
|
||||
testSobel() # teste le filtre de Sobel
|
||||
testSobel(image1) # teste le filtre de Sobel
|
||||
elif action == 45:
|
||||
print startStr
|
||||
testConvolution() # teste le filtre de Convolution
|
||||
testConvolution(image1) # teste le filtre de Convolution
|
||||
elif action == 46:
|
||||
print startStr
|
||||
testBichrome() # teste le passage au bichromatique
|
||||
testBichrome(image1) # teste le passage au bichromatique
|
||||
elif action == 47:
|
||||
print startStr
|
||||
testHighPass() # teste le filtre passe haut
|
||||
testHighPass(image1) # teste le filtre passe haut
|
||||
|
||||
else:
|
||||
print "Wrong choice"
|
||||
|
@ -220,4 +252,8 @@ print "+---------------------------+---------+"
|
|||
print "| EXECUTION TIME | %s |" % execTime.get()
|
||||
print "+---------------------------+---------+"
|
||||
print
|
||||
print result
|
||||
print result
|
||||
|
||||
|
||||
|
||||
raw_input('- [PRESS ANY KEY TO EXIT] -');
|
BIN
code/new.bmp
Before Width: | Height: | Size: 352 KiB After Width: | Height: | Size: 257 KiB |
BIN
code/test.bmp
Before Width: | Height: | Size: 257 KiB |
145
code/tests.py
|
@ -7,6 +7,7 @@
|
|||
from BMPFile import *
|
||||
from Noise import *
|
||||
from tests import *
|
||||
from utility import Drawer
|
||||
|
||||
import random
|
||||
import sys
|
||||
|
@ -71,24 +72,24 @@ def exactLength(text, length, position=0):
|
|||
|
||||
|
||||
|
||||
def defaultTest():
|
||||
def defaultTest(img):
|
||||
t = Timer();
|
||||
|
||||
|
||||
# lecture du fichier
|
||||
print "| Reading Image |",; t.reset();
|
||||
with open( sys.argv[1] ) as file:
|
||||
binFile = file.read()
|
||||
print "%s |" % (t.get())
|
||||
# print "| Reading Image |",; t.reset();
|
||||
# with open( sys.argv[1] ) as file:
|
||||
# binFile = file.read()
|
||||
# print "%s |" % (t.get())
|
||||
|
||||
|
||||
img = BMPFile(); # Instanciation du BMPFile
|
||||
# img = BMPFile(); # Instanciation du BMPFile
|
||||
|
||||
|
||||
# Parsing
|
||||
print "| Parsing file |",; t.reset();
|
||||
img.parse( binFile );
|
||||
print "%s |" % (t.get())
|
||||
# # Parsing
|
||||
# print "| Parsing file |",; t.reset();
|
||||
# img.parse( binFile );
|
||||
# print "%s |" % (t.get())
|
||||
|
||||
|
||||
|
||||
|
@ -124,6 +125,8 @@ def testManualCreation(width=100, height=100):
|
|||
|
||||
t = Timer();
|
||||
|
||||
d = Drawer(width, height); # AFFICHAGE
|
||||
|
||||
print "| Creating Image |",; t.reset();
|
||||
img = BMPFile()
|
||||
for y in range(0, height):
|
||||
|
@ -136,6 +139,8 @@ def testManualCreation(width=100, height=100):
|
|||
bpp=24
|
||||
) );
|
||||
|
||||
d.setPixel( (x, y), (img.content.map[y][x].r, img.content.map[y][x].g, img.content.map[y][x].b) );
|
||||
|
||||
img.unparse();
|
||||
print "%s |" % (t.get())
|
||||
|
||||
|
@ -158,35 +163,35 @@ def testManualCreation(width=100, height=100):
|
|||
# Unparse à partir de la matrice de pixels récupérée dans l'image de sortie
|
||||
# Relis l'image crée pour vérifier que les infos sont identiques [affiche les infos du header]
|
||||
#
|
||||
def testFileIntegrity():
|
||||
def testFileIntegrity(img):
|
||||
|
||||
t = Timer();
|
||||
returnValue = ""
|
||||
|
||||
# lecture du fichier
|
||||
print "| Reading Image |",; t.reset();
|
||||
with open( sys.argv[1] ) as file:
|
||||
binFile = file.read()
|
||||
print "%s |" % (t.get())
|
||||
# print "| Reading Image |",; t.reset();
|
||||
# with open( sys.argv[1] ) as file:
|
||||
# binFile = file.read()
|
||||
# print "%s |" % (t.get())
|
||||
|
||||
|
||||
A = BMPFile(); # Instanciation du BMPFile
|
||||
# A = BMPFile(); # Instanciation du BMPFile
|
||||
|
||||
# Parsing
|
||||
print "| Parsing file |",; t.reset();
|
||||
A.parse( binFile );
|
||||
print "%s |" % (t.get())
|
||||
# # Parsing
|
||||
# print "| Parsing file |",; t.reset();
|
||||
# A.parse( binFile );
|
||||
# print "%s |" % (t.get())
|
||||
|
||||
returnValue += A.header.info();
|
||||
returnValue += img.header.info();
|
||||
|
||||
# Unparsing
|
||||
print "| Unparsing file |",; t.reset();
|
||||
A.unparse();
|
||||
img.unparse();
|
||||
print "%s |" % (t.get())
|
||||
|
||||
# Writing
|
||||
print "| Writing file |",; t.reset();
|
||||
A.write( sys.argv[2] )
|
||||
img.write( sys.argv[2] )
|
||||
print "%s |" % (t.get())
|
||||
|
||||
|
||||
|
@ -219,20 +224,20 @@ def testFileIntegrity():
|
|||
#
|
||||
# @history
|
||||
# Affiche la palette au format <int>[]
|
||||
def printIntPalette():
|
||||
img = BMPFile();
|
||||
def printIntPalette(img):
|
||||
# img = BMPFile();
|
||||
|
||||
t = Timer();
|
||||
|
||||
print "| Reading Image |",; t.reset();
|
||||
with open( sys.argv[1] ) as file:
|
||||
binFile = file.read()
|
||||
print "%s |" % (t.get())
|
||||
# print "| Reading Image |",; t.reset();
|
||||
# with open( sys.argv[1] ) as file:
|
||||
# binFile = file.read()
|
||||
# print "%s |" % (t.get())
|
||||
|
||||
|
||||
print "| Parsing File |",; t.reset();
|
||||
img.parse(binFile);
|
||||
print "%s |" % (t.get())
|
||||
# print "| Parsing File |",; t.reset();
|
||||
# img.parse(binFile);
|
||||
# print "%s |" % (t.get())
|
||||
|
||||
return img.intPalette;
|
||||
|
||||
|
@ -256,30 +261,30 @@ def printIntPalette():
|
|||
# Parse le fichier d'origine
|
||||
# Bruite l'image' et l'enregistre dans "SaltAndPepper.bmp"
|
||||
# Débruite l'image et l'enregistre dans le fichier de sortie
|
||||
def testSaltAndPepper(seuilSet=50, seuilUnset=1, borneUnset=1, smooth=1):
|
||||
def testSaltAndPepper(img, seuilSet=50, seuilUnset=1, borneUnset=1, smooth=1):
|
||||
|
||||
t = Timer();
|
||||
|
||||
|
||||
# lecture du fichier
|
||||
print "| Reading Image |",; t.reset();
|
||||
with open( sys.argv[1] ) as file:
|
||||
binFile = file.read()
|
||||
print "%s |" % (t.get())
|
||||
# print "| Reading Image |",; t.reset();
|
||||
# with open( sys.argv[1] ) as file:
|
||||
# binFile = file.read()
|
||||
# print "%s |" % (t.get())
|
||||
|
||||
|
||||
img = BMPFile(); # Instanciation du BMPFile
|
||||
# img = BMPFile(); # Instanciation du BMPFile
|
||||
|
||||
|
||||
# Parsing
|
||||
print "| Parsing file |",; t.reset();
|
||||
img.parse( binFile );
|
||||
print "%s |" % (t.get())
|
||||
# # Parsing
|
||||
# print "| Parsing file |",; t.reset();
|
||||
# img.parse( binFile );
|
||||
# print "%s |" % (t.get())
|
||||
|
||||
|
||||
|
||||
print "| Creating Salt&Pepper |",; t.reset();
|
||||
FX.SaltAndPepper.set(img.content.map, seuil=seuilSet)
|
||||
FX.SaltAndPepper.set(img.drawer, img.content.map, seuil=seuilSet)
|
||||
print "%s |" % (t.get())
|
||||
|
||||
# Unparsing
|
||||
|
@ -296,12 +301,12 @@ def testSaltAndPepper(seuilSet=50, seuilUnset=1, borneUnset=1, smooth=1):
|
|||
|
||||
|
||||
print "| Removing Salt&Pepper |",; t.reset();
|
||||
FX.SaltAndPepper.unset(img.content.map, seuil=seuilUnset, borne=borneUnset)
|
||||
FX.SaltAndPepper.unset(img.drawer, img.content.map, seuil=seuilUnset, borne=borneUnset)
|
||||
print "%s |" % (t.get())
|
||||
|
||||
if smooth != 0:
|
||||
print "| Filtre moyen |",; t.reset();
|
||||
img.content.map = FX.Filter.averageFilter(img.content.map);
|
||||
img.content.map = FX.Filter.averageFilter(img.drawer, img.content.map);
|
||||
print "%s |" % (t.get())
|
||||
|
||||
# Unparsing
|
||||
|
@ -333,30 +338,30 @@ def testSaltAndPepper(seuilSet=50, seuilUnset=1, borneUnset=1, smooth=1):
|
|||
# Parse le fichier d'origine
|
||||
# Bruite l'image' et l'enregistre dans "AdditiveNoise.bmp"
|
||||
# Débruite l'image et l'enregistre dans le fichier de sortie
|
||||
def testAdditiveBernouilliNoise(seuilA=10, seuilB=35):
|
||||
def testAdditiveBernouilliNoise(img, seuilA=10, seuilB=35):
|
||||
|
||||
t = Timer();
|
||||
|
||||
|
||||
# lecture du fichier
|
||||
print "| Reading Image |",; t.reset();
|
||||
with open( sys.argv[1] ) as file:
|
||||
binFile = file.read()
|
||||
print "%s |" % (t.get())
|
||||
# print "| Reading Image |",; t.reset();
|
||||
# with open( sys.argv[1] ) as file:
|
||||
# binFile = file.read()
|
||||
# print "%s |" % (t.get())
|
||||
|
||||
|
||||
img = BMPFile(); # Instanciation du BMPFile
|
||||
# img = BMPFile(); # Instanciation du BMPFile
|
||||
|
||||
|
||||
# Parsing
|
||||
print "| Parsing file |",; t.reset();
|
||||
img.parse( binFile );
|
||||
print "%s |" % (t.get())
|
||||
# # Parsing
|
||||
# print "| Parsing file |",; t.reset();
|
||||
# img.parse( binFile );
|
||||
# print "%s |" % (t.get())
|
||||
|
||||
|
||||
|
||||
print "| Creating Additive |",; t.reset();
|
||||
FX.Additive.setBernouilli(img.content.map, seuil=seuilA)
|
||||
FX.Additive.setBernouilli(img.drawer, img.content.map, seuil=seuilA)
|
||||
print "%s |" % (t.get())
|
||||
|
||||
# Unparsing
|
||||
|
@ -373,8 +378,8 @@ def testAdditiveBernouilliNoise(seuilA=10, seuilB=35):
|
|||
|
||||
|
||||
print "| Removing Additive |",; t.reset();
|
||||
# img.content.map = FX.Additive.unset(img.content.map, seuil=seuilB)
|
||||
img.content.map = FX.Additive.unset2(img.content.map, seuil=seuilB)
|
||||
# img.content.map = FX.Additive.unset(img.drawer, img.content.map, seuil=seuilB)
|
||||
img.content.map = FX.Additive.unset2(img.drawer, img.content.map, seuil=seuilB)
|
||||
print "%s |" % (t.get())
|
||||
|
||||
# Unparsing
|
||||
|
@ -408,30 +413,30 @@ def testAdditiveBernouilliNoise(seuilA=10, seuilB=35):
|
|||
# Parse le fichier d'origine
|
||||
# Bruite l'image' et l'enregistre dans "AdditiveNoise.bmp"
|
||||
# Débruite l'image et l'enregistre dans le fichier de sortie
|
||||
def testAdditiveGaussianNoise(sigma=10, seuil=35):
|
||||
def testAdditiveGaussianNoise(img, sigma=10, seuil=35):
|
||||
|
||||
t = Timer();
|
||||
|
||||
|
||||
# lecture du fichier
|
||||
print "| Reading Image |",; t.reset();
|
||||
with open( sys.argv[1] ) as file:
|
||||
binFile = file.read()
|
||||
print "%s |" % (t.get())
|
||||
# print "| Reading Image |",; t.reset();
|
||||
# with open( sys.argv[1] ) as file:
|
||||
# binFile = file.read()
|
||||
# print "%s |" % (t.get())
|
||||
|
||||
|
||||
img = BMPFile(); # Instanciation du BMPFile
|
||||
# img = BMPFile(); # Instanciation du BMPFile
|
||||
|
||||
|
||||
# Parsing
|
||||
print "| Parsing file |",; t.reset();
|
||||
img.parse( binFile );
|
||||
print "%s |" % (t.get())
|
||||
# # Parsing
|
||||
# print "| Parsing file |",; t.reset();
|
||||
# img.parse( binFile );
|
||||
# print "%s |" % (t.get())
|
||||
|
||||
|
||||
|
||||
print "| Creating Additive |",; t.reset();
|
||||
FX.Additive.setGaussian(img.content.map, sigma=sigma)
|
||||
FX.Additive.setGaussian(img.drawer, img.content.map, sigma=sigma)
|
||||
print "%s |" % (t.get())
|
||||
|
||||
# Unparsing
|
||||
|
@ -453,7 +458,7 @@ def testAdditiveGaussianNoise(sigma=10, seuil=35):
|
|||
[seuil, 0, seuil],
|
||||
[seuil, seuil, seuil]
|
||||
]
|
||||
img.content.map = FX.Filter.Convolution(img.content.map, kernel)
|
||||
img.content.map = FX.Filter.Convolution(img.drawer, img.content.map, kernel)
|
||||
print "%s |" % (t.get())
|
||||
|
||||
# Unparsing
|
||||
|
|
|
@ -3,6 +3,9 @@
|
|||
import random
|
||||
import time
|
||||
|
||||
|
||||
from Drawer import Drawer
|
||||
|
||||
import sys
|
||||
sys.path.append(sys.path[0]+'/..')
|
||||
from BMPFile import RGBPixel
|
||||
|
@ -14,7 +17,7 @@ class Additive_Noise:
|
|||
# @param pixelMap Matrice de pixel à traiter (modifier)
|
||||
# @param seuil pourcentage de l'image à bruiter (50% <=> 1 pixel sur 2 est bruité)
|
||||
#
|
||||
def setBernouilli(self, pixelMap, seuil=10):
|
||||
def setBernouilli(self, drawer, pixelMap, seuil=10):
|
||||
seuil = float(seuil);
|
||||
|
||||
while seuil >= 1:
|
||||
|
@ -36,11 +39,15 @@ class Additive_Noise:
|
|||
randomAdd = - random.randint(0, minColor / 2 )
|
||||
|
||||
pixelMap[y][x].setRGB(
|
||||
pixelMap[y][x].r + randomAdd,
|
||||
pixelMap[y][x].g + randomAdd,
|
||||
pixelMap[y][x].b + randomAdd
|
||||
r=pixelMap[y][x].r + randomAdd,
|
||||
g=pixelMap[y][x].g + randomAdd,
|
||||
b=pixelMap[y][x].b + randomAdd,
|
||||
x=x,
|
||||
y=y
|
||||
);
|
||||
|
||||
drawer.fill( pixelMap );
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -50,7 +57,7 @@ class Additive_Noise:
|
|||
# @param pixelMap Matrice de pixel à traiter (modifier)
|
||||
# @param seuil pourcentage de l'image à bruiter (50% <=> 1 pixel sur 2 est bruité)
|
||||
#
|
||||
def setGaussian(self, pixelMap, sigma=10):
|
||||
def setGaussian(self, drawer, pixelMap, sigma=10):
|
||||
width = len( pixelMap[0] )
|
||||
height = len( pixelMap )
|
||||
|
||||
|
@ -82,8 +89,11 @@ class Additive_Noise:
|
|||
pixel.setRGB(
|
||||
r = 0 if r<0 else ( 255 if r > 255 else r),
|
||||
g = 0 if g<0 else ( 255 if g > 255 else g),
|
||||
b = 0 if b<0 else ( 255 if b > 255 else b)
|
||||
b = 0 if b<0 else ( 255 if b > 255 else b),
|
||||
x = pixel.x,
|
||||
y = pixel.y
|
||||
);
|
||||
drawer.setPixel( pixel );
|
||||
|
||||
|
||||
|
||||
|
@ -93,7 +103,7 @@ class Additive_Noise:
|
|||
# @param seuil Seuil à partir duquel on doit traiter les pixels (écart entre la moyenne des pixels avoisinant et le pixel concerné)
|
||||
#
|
||||
# @return cleanMatrix matrice propre qui est retournée
|
||||
def unset(self, pixelMap, seuil=10):
|
||||
def unset(self, drawer, pixelMap, seuil=10):
|
||||
width = len( pixelMap[0] )
|
||||
height = len( pixelMap )
|
||||
|
||||
|
@ -173,7 +183,9 @@ class Additive_Noise:
|
|||
|
||||
# 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:
|
||||
cleanMatrix[y][x].setRGB(rMoy, gMoy, bMoy);
|
||||
cleanMatrix[y][x].setRGB(r=rMoy, g=gMoy, b=bMoy, x=x, y=y);
|
||||
|
||||
drawer.setPixel( cleanMatrix[y][x] );
|
||||
|
||||
return cleanMatrix;
|
||||
|
||||
|
@ -187,7 +199,7 @@ class Additive_Noise:
|
|||
# @param seuil Seuil de "poids statistique" à partir duquel on doit traiter les pixels compris entre 0 et 100
|
||||
#
|
||||
# @return cleanMatrix matrice propre qui est retournée
|
||||
def unset2(self, pixelMap, seuil=10):
|
||||
def unset2(self, drawer, pixelMap, seuil=10):
|
||||
width = len( pixelMap[0] )
|
||||
height = len( pixelMap )
|
||||
ordre = 3 # ordre matrice carré
|
||||
|
@ -217,6 +229,7 @@ class Additive_Noise:
|
|||
y = pixelMap[y][x].y,
|
||||
bpp = pixelMap[y][x].bpp,
|
||||
));
|
||||
drawer.setPixel( cleanMatrix[y][x] );
|
||||
|
||||
# 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
|
||||
|
@ -280,6 +293,7 @@ class Additive_Noise:
|
|||
|
||||
# si la couleur est trop "différente" (dépend du seuil) alors on remplace sa couleur par la moyenne des couleurs alentours
|
||||
if statisticWeight > seuil:
|
||||
cleanMatrix[y][x].setRGB(rMoy, gMoy, bMoy);
|
||||
cleanMatrix[y][x].setRGB(r=rMoy, g=gMoy, b=bMoy, x=x, y=y);
|
||||
drawer.setPixel( cleanMatrix[y][x] );
|
||||
|
||||
return cleanMatrix;
|
|
@ -0,0 +1,39 @@
|
|||
# ~*~ encoding: utf-8 ~*~ #
|
||||
|
||||
import pygame
|
||||
|
||||
class Drawer():
|
||||
|
||||
# initialise l'image en fonction de sa taille
|
||||
def __init__(self, width, height):
|
||||
self.width = width;
|
||||
self.height = height;
|
||||
self.screen = pygame.display.set_mode( (width, height) );
|
||||
self.count = 0;
|
||||
|
||||
|
||||
# remet le compteur à zéro
|
||||
def reset(self):
|
||||
self.count = 0;
|
||||
|
||||
|
||||
# rafraîchit l'image
|
||||
def refresh(self):
|
||||
pygame.display.flip()
|
||||
|
||||
|
||||
# dessine un pixel de type RGBPixel(BMPFile)
|
||||
def setPixel(self, rgbpix):
|
||||
self.screen.set_at( (rgbpix.x, rgbpix.y), (rgbpix.r, rgbpix.g, rgbpix.b) );
|
||||
self.count = (self.count + 1) % self.width; # incrémente le compteur
|
||||
|
||||
if( self.count >= self.width-1 ): # si le compteur a fait une ligne complète
|
||||
self.refresh(); # on rafraîchi l'image
|
||||
|
||||
# dessine toute la matrice
|
||||
def fill(self, matrix):
|
||||
for line in matrix:
|
||||
for pixel in line:
|
||||
self.setPixel( pixel );
|
||||
|
||||
self.refresh();
|
|
@ -22,8 +22,8 @@ class Filter:
|
|||
# 1 1 1
|
||||
# 1 1 1
|
||||
#
|
||||
def averageFilter(self, pixelMap):
|
||||
return self.Convolution(pixelMap, kernel=[
|
||||
def averageFilter(self, drawer, pixelMap):
|
||||
return self.Convolution(drawer, pixelMap, kernel=[
|
||||
[1, 1, 1],
|
||||
[1, 1, 1],
|
||||
[1, 1, 1]
|
||||
|
@ -40,7 +40,7 @@ class Filter:
|
|||
# 1 0
|
||||
# 0 -1
|
||||
#
|
||||
def Roberts(self, pixelMap):
|
||||
def Roberts(self, drawer, pixelMap):
|
||||
return self.Convolution(pixelMap, kernel=[
|
||||
[1, 0],
|
||||
[0, -1]
|
||||
|
@ -57,7 +57,7 @@ class Filter:
|
|||
# -1 8 -1
|
||||
# -1 -1 -1
|
||||
#
|
||||
def Laplace(self, pixelMap):
|
||||
def Laplace(self, drawer, pixelMap):
|
||||
return self.Convolution(pixelMap, kernel=[
|
||||
[-1, -1, -1],
|
||||
[-1, 8, -1],
|
||||
|
@ -76,7 +76,7 @@ class Filter:
|
|||
# -2 0 2
|
||||
# -1 0 1
|
||||
#
|
||||
def Sobel(self, pixelMap):
|
||||
def Sobel(self, drawer, pixelMap):
|
||||
return self.Convolution(pixelMap, kernel=[
|
||||
[-1, 0, 1],
|
||||
[-2, 0, 2],
|
||||
|
@ -95,7 +95,7 @@ class Filter:
|
|||
# -2 0 2
|
||||
# -1 0 1
|
||||
#
|
||||
def HighPass(self, pixelMap):
|
||||
def HighPass(self, drawer, pixelMap):
|
||||
return self.Convolution(pixelMap, kernel=[
|
||||
[ 0, -1, 0],
|
||||
[-1, 5, -1],
|
||||
|
@ -112,7 +112,7 @@ class Filter:
|
|||
#
|
||||
# @history
|
||||
# applique le filtre
|
||||
def Convolution(self, pixelMap, kernel=None):
|
||||
def Convolution(self, drawer, pixelMap, kernel=None):
|
||||
width = len( pixelMap[0] )
|
||||
height = len( pixelMap )
|
||||
|
||||
|
@ -182,8 +182,10 @@ class Filter:
|
|||
y = y,
|
||||
bpp = pixel.bpp
|
||||
) )
|
||||
drawer.setPixel( convolvedMap[y][x] );
|
||||
|
||||
|
||||
drawer.refresh();
|
||||
|
||||
return convolvedMap
|
||||
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
import random
|
||||
import time
|
||||
|
||||
|
||||
class SaltAndPepper_Noise:
|
||||
|
||||
|
||||
|
@ -11,31 +12,32 @@ class SaltAndPepper_Noise:
|
|||
# @param pixelMap Matrice de pixel à traiter (modifier)
|
||||
# @param seuil pourcentage de l'image à bruiter (50% <=> 1 pixel sur 2 est bruité)
|
||||
#
|
||||
def set(self, pixelMap, seuil=10):
|
||||
def set(self, drawer, pixelMap, seuil=10):
|
||||
seuil = float(seuil);
|
||||
|
||||
while seuil >= 1:
|
||||
seuil /= 100.0
|
||||
|
||||
nbPixel = int( len(pixelMap) * len(pixelMap[0]) * seuil )
|
||||
nbPixel = int( len(pixelMap) * len(pixelMap[0]) * seuil )
|
||||
|
||||
for bruit in range(0, nbPixel):
|
||||
x = random.randint(0, len(pixelMap[0]) - 1 )
|
||||
y = random.randint(0, len(pixelMap) - 1 )
|
||||
|
||||
if random.randint(0,1) == 1:
|
||||
pixelMap[y][x].setRGB(255,255,255);
|
||||
pixelMap[y][x].setRGB(r=255,g=255,b=255, x=x, y=y, bpp=pixelMap[y][x].bpp);
|
||||
else:
|
||||
pixelMap[y][x].setRGB(0,0,0);
|
||||
pixelMap[y][x].setRGB(r=0,g=0,b=0, x=x, y=y, bpp=pixelMap[y][x].bpp);
|
||||
|
||||
drawer.fill(pixelMap);
|
||||
|
||||
|
||||
# Applique le débruitage de type "Poivre & Sel" sur la matrice de pixels #
|
||||
##########################################################################
|
||||
# @param pixelMap Matrice de pixel à traiter (modifier)
|
||||
# @param seuil Seuil à partir duquel on doit traiter les pixels (écart entre la moyenne des pixels avoisinant et le pixel concerné)
|
||||
# @param borne 0 = Noir pur et blanc pur sont enlevés; 255 ou + = tout les pixels sont traités
|
||||
#
|
||||
def unset(self, pixelMap, seuil=5, borne=5):
|
||||
def unset(self, drawer, pixelMap, seuil=5, borne=5):
|
||||
width = len( pixelMap[0] )
|
||||
height = len( pixelMap )
|
||||
|
||||
|
@ -113,4 +115,8 @@ class SaltAndPepper_Noise:
|
|||
|
||||
# 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);
|
||||
pixelMap[y][x].setRGB(r=rMoy, g=gMoy, b=bMoy, x=x, y=y, bpp=pixelMap[y][x].bpp);
|
||||
|
||||
drawer.setPixel( pixelMap[y][x] );
|
||||
|
||||
drawer.refresh();
|