From 9ae2fdf8f41c2510446938fa4d06904d8ed97954 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Thu, 17 Sep 2015 17:49:58 +0200 Subject: [PATCH] =?UTF-8?q?Additive=20Noise=20/=20MultiplicativeNoise=20cr?= =?UTF-8?q?=C3=A9=C3=A9s=20mais=20uniquement=20le=20SET=20du=20additive=20?= =?UTF-8?q?=C3=A0=20suivre...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/Noise.py | 298 ++++++++++++++++++++++++++++++++++------------ code/Noise.readme | 125 ++++++++++++++++++- code/bmp.py | 65 ++++++++++ 3 files changed, 413 insertions(+), 75 deletions(-) diff --git a/code/Noise.py b/code/Noise.py index 66f0f6a..7effb85 100644 --- a/code/Noise.py +++ b/code/Noise.py @@ -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=0 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=0 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 \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + 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 \ No newline at end of file diff --git a/code/Noise.readme b/code/Noise.readme index dcd47f2..6372d66 100644 --- a/code/Noise.readme +++ b/code/Noise.readme @@ -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 \ No newline at end of file + 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=0 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=0 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 \ No newline at end of file diff --git a/code/bmp.py b/code/bmp.py index 7081c12..e989a9c 100644 --- a/code/bmp.py +++ b/code/bmp.py @@ -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()