Additive Noise / MultiplicativeNoise créés mais uniquement le SET du additive à suivre...

This commit is contained in:
xdrm-brackets 2015-09-17 17:49:58 +02:00
parent c92de13e29
commit 9ae2fdf8f4
3 changed files with 413 additions and 75 deletions

View File

@ -92,95 +92,245 @@ class Noise:
def getShapeRecursive(self, coords, pixelMap, pixelList ): # return PixelList
# coords = [lastx, lasty, x, y]
width = len( pixelMap[0] )
height = len( pixelMap )
lastx = coords[0]
lasty = coords[1]
x = coords[2]
y = coords[3]
newCoords = [x, y]
# si le pixel existe (pas de dépassement de la pixelMap)
if x<width and x>=0 and y<height and y>=0:
already = False;
for i in range(0, len(pixelList)):
if pixelList[i] == pixelMap[y][x]:
already = True;
break
# si le pixel n'est pas déjà dans le tableau
if not already:
pixelMap[y][x].setRGB(255,0,0);
# si trop de différence
lastP = pixelMap[lasty][lastx]
pix = pixelMap[y][x]
if abs(lastP.r-pix.r) <= 50 and abs(lastP.g-pix.g) <= 50 and abs(lastP.b-pix.b) <= 50:
pixelList.append( pixelMap[y][x] ) # ajout au tableau
self.getShapeRecursive( [x, y, x-1, y+1], pixelMap, pixelList) # 1
self.getShapeRecursive( [x, y, x, y+1], pixelMap, pixelList) # 2
self.getShapeRecursive( [x, y, x+1, y+1], pixelMap, pixelList) # 3
self.getShapeRecursive( [x, y, x-1, y ], pixelMap, pixelList) # 4
# current pixel
self.getShapeRecursive( [x, y, x+1, y ], pixelMap, pixelList) # 6
self.getShapeRecursive( [x, y, x-1, y-1], pixelMap, pixelList) # 7
self.getShapeRecursive( [x, y, x, y-1], pixelMap, pixelList) # 8
self.getShapeRecursive( [x, y, x+1, y-1], pixelMap, pixelList) # 9
def getShape(self, coords, pixelMap, pixelList):
# coords = [lastx, lasty, x, y]
width = len( pixelMap[0] )
height = len( pixelMap )
lastx = coords[0]
lasty = coords[1]
x = coords[2]
y = coords[3]
if x<width and x>=0 and y<height and y>=0:
already = False;
for pix in pixelList:
if pix == pixelMap[y][x]:
already = True;
break
# si le pixel n'est pas déjà dans le tableau
if not already:
# si trop de différence
lastP = pixelMap[lasty][lastx]
pix = pixelMap[y][x]
if abs(lastP.r-pix.r) <= 50 and abs(lastP.g-pix.g) <= 50 and abs(lastP.b-pix.b) <= 50:
return pixelMap[y][x];
# self.getShape( [x, y, x-1, y+1], pixelMap, pixelList) # 1
# self.getShape( [x, y, x, y+1], pixelMap, pixelList) # 2
# self.getShape( [x, y, x+1, y+1], pixelMap, pixelList) # 3
# self.getShape( [x, y, x-1, y ], pixelMap, pixelList) # 4
# # current pixel
# self.getShape( [x, y, x+1, y ], pixelMap, pixelList) # 6
# self.getShape( [x, y, x-1, y-1], pixelMap, pixelList) # 7
# self.getShape( [x, y, x, y-1], pixelMap, pixelList) # 8
# self.getShape( [x, y, x+1, y-1], pixelMap, pixelList) # 9
return None # return none si le pixel n'est plus de la forme
def AdditiveNoise_set(self, pixelMap, seuil=10):
seuil = float(seuil);
while seuil >= 1:
seuil /= 100.0
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:
maxColor = max(pixelMap[y][x].r, pixelMap[y][x].g, pixelMap[y][x].b)
randomAdd = random.randint(0, (255-maxColor) / 10 )
else:
minColor = min(pixelMap[y][x].r, pixelMap[y][x].g, pixelMap[y][x].b)
randomAdd = - random.randint(0, minColor / 10 )
pixelMap[y][x].setRGB(
pixelMap[y][x].r + randomAdd,
pixelMap[y][x].g + randomAdd,
pixelMap[y][x].b + randomAdd
);
def AdditiveNoise_unset(self, pixelMap, seuil=5):
pass
def MultiplicativeNoise_set(self, pixelMap, seuil=10):
seuil = float(seuil);
while seuil >= 1:
seuil /= 100.0
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:
maxColor = max(pixelMap[y][x].r, pixelMap[y][x].g, pixelMap[y][x].b)
randomAdd = random.randint(0, (255-maxColor) / 10 )
else:
minColor = min(pixelMap[y][x].r, pixelMap[y][x].g, pixelMap[y][x].b)
randomAdd = - random.randint(0, minColor / 10 )
pixelMap[y][x].setRGB(
pixelMap[y][x].r + randomAdd,
pixelMap[y][x].g + randomAdd,
pixelMap[y][x].b + randomAdd
);
def MultiplicativeNoise_unset(self, pixelMap, seuil=5):
pass
def ColorNoise_set(self, pixelMap, seuil=10):
seuil = float(seuil);
while seuil >= 1:
seuil /= 100.0
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 )
pixelMap[y][x].setRGB(
pixelMap[y][x].r + random.randint(0, (255-pixelMap[y][x].r)/4),
pixelMap[y][x].g + random.randint(0, (255-pixelMap[y][x].g)/4),
pixelMap[y][x].b + random.randint(0, (255-pixelMap[y][x].b)/4)
);
def ColorNoise_unset(self, pixelMap, seuil=5):
pass
def Gaussian_set(self, pixelMap, seuil=10):
pass
def Gaussian_unset(self, pixelMap, seuil=5):
pass

View File

@ -66,4 +66,127 @@ def getShape(self, coords, pixelMap, pixelList ): # return PixelList
self.getShape(newCoords+[x-1, y-1], pixelMap, pixelList) # 7
self.getShape(newCoords+[x, y-1], pixelMap, pixelList) # 8
self.getShape(newCoords+[x+1, y-1], pixelMap, pixelList) # 9
self.getShape(newCoords+[x+1, y-1], pixelMap, pixelList) # 9
def getShapeRecursive(self, coords, pixelMap, pixelList ): # return PixelList
# coords = [lastx, lasty, x, y]
width = len( pixelMap[0] )
height = len( pixelMap )
lastx = coords[0]
lasty = coords[1]
x = coords[2]
y = coords[3]
newCoords = [x, y]
# si le pixel existe (pas de dépassement de la pixelMap)
if x<width and x>=0 and y<height and y>=0:
already = False;
for i in range(0, len(pixelList)):
if pixelList[i] == pixelMap[y][x]:
already = True;
break
# si le pixel n'est pas déjà dans le tableau
if not already:
pixelMap[y][x].setRGB(255,0,0);
# si trop de différence
lastP = pixelMap[lasty][lastx]
pix = pixelMap[y][x]
if abs(lastP.r-pix.r) <= 50 and abs(lastP.g-pix.g) <= 50 and abs(lastP.b-pix.b) <= 50:
pixelList.append( pixelMap[y][x] ) # ajout au tableau
self.getShapeRecursive( [x, y, x-1, y+1], pixelMap, pixelList) # 1
self.getShapeRecursive( [x, y, x, y+1], pixelMap, pixelList) # 2
self.getShapeRecursive( [x, y, x+1, y+1], pixelMap, pixelList) # 3
self.getShapeRecursive( [x, y, x-1, y ], pixelMap, pixelList) # 4
# current pixel
self.getShapeRecursive( [x, y, x+1, y ], pixelMap, pixelList) # 6
self.getShapeRecursive( [x, y, x-1, y-1], pixelMap, pixelList) # 7
self.getShapeRecursive( [x, y, x, y-1], pixelMap, pixelList) # 8
self.getShapeRecursive( [x, y, x+1, y-1], pixelMap, pixelList) # 9
def getShape(self, coords, pixelMap, pixelList):
# coords = [lastx, lasty, x, y]
width = len( pixelMap[0] )
height = len( pixelMap )
lastx = coords[0]
lasty = coords[1]
x = coords[2]
y = coords[3]
if x<width and x>=0 and y<height and y>=0:
already = False;
for pix in pixelList:
if pix == pixelMap[y][x]:
already = True;
break
# si le pixel n'est pas déjà dans le tableau
if not already:
# si trop de différence
lastP = pixelMap[lasty][lastx]
pix = pixelMap[y][x]
if abs(lastP.r-pix.r) <= 50 and abs(lastP.g-pix.g) <= 50 and abs(lastP.b-pix.b) <= 50:
return pixelMap[y][x];
# self.getShape( [x, y, x-1, y+1], pixelMap, pixelList) # 1
# self.getShape( [x, y, x, y+1], pixelMap, pixelList) # 2
# self.getShape( [x, y, x+1, y+1], pixelMap, pixelList) # 3
# self.getShape( [x, y, x-1, y ], pixelMap, pixelList) # 4
# # current pixel
# self.getShape( [x, y, x+1, y ], pixelMap, pixelList) # 6
# self.getShape( [x, y, x-1, y-1], pixelMap, pixelList) # 7
# self.getShape( [x, y, x, y-1], pixelMap, pixelList) # 8
# self.getShape( [x, y, x+1, y-1], pixelMap, pixelList) # 9
return None # return none si le pixel n'est plus de la forme

View File

@ -135,6 +135,70 @@ def testSaltAndPepper():
def testAdditiveNoise():
t = Timer();
total = Timer(); total.reset();
# lecture du fichier
print "Reading Image -",; t.reset();
with open( sys.argv[1] ) as file:
binFile = file.read()
print "Done in %s s" % (t.get())
img = BMPFile(); # Instanciation du BMPFile
noise = Noise(); # Instanciation du NoiseObject
# Parsing
print "Parsing file -",; t.reset();
img.parse( binFile );
print "Done in %s s" % (t.get())
print "Creating Additive -",; t.reset();
noise.AdditiveNoise_set(img.content.map, seuil=50)
print "Done in %s s" % (t.get())
# Unparsing
print "Unparsing file -",; t.reset();
img.unparse()
print "Done in %s s" % (t.get())
# image to stdout
print "Writing file -",; t.reset();
img.write( "AdditiveNoise.bmp" )
print "Done in %s s" % (t.get())
print "Removing Additive -",; t.reset();
noise.AdditiveNoise_unset(img.content.map)
print "Done in %s s" % (t.get())
# Unparsing
print "Unparsing file -",; t.reset();
img.unparse()
print "Done in %s s" % (t.get())
# image to stdout
print "Writing file -",; t.reset();
img.write( sys.argv[2] )
print "Done in %s s" % (t.get())
print "\nExecution Time: %s seconds" % total.get()
def printIntPalette():
img = BMPFile();
@ -348,6 +412,7 @@ def mergeImages():
############ TESTS ############
# testManualCreation()
testSaltAndPepper()
# testAdditiveNoise()
# testFileIntegrity()
# printIntPalette()
printImageQuality()