Salt&Pepper set/unset à optimiser (1 fonction pour rebords et 1 fct pour enlever tout les pixels s&p

This commit is contained in:
xdrm-brackets 2015-09-16 15:37:19 +02:00
parent 999f8580ae
commit 2e6482055a
2 changed files with 109 additions and 39 deletions

View File

@ -29,14 +29,13 @@ class Noise:
width = len( pixelMap[0] )
height = len( pixelMap )
seuil = int( .5 * 256 );
seuil = int( .01 * 256 );
for y in range(0, len(pixelMap)):
for x in range(0, len(pixelMap[y])):
pMoy = ( pixelMap[y][x].r + pixelMap[y][x].g + pixelMap[y][x].b ) / 3
# traitement si couleur extreme
if pMoy >= 235 or pMoy <= 20:
xmin, ymin, xmax, ymap = x, y, x, y;
rMoy, gMoy, bMoy, count = 0.0, 0.0, 0.0, 0 # moyennes des couleurs
rInterval, gInterval, bInterval, rgbInterval = 0, 0, 0, 0 # décalage avec le pixel
@ -50,13 +49,22 @@ class Noise:
if x+1 < width:
xmax = x+1
for j in range(0, ymax-xmin): # on parcourt les pixels autour
for i in range(0, xmax-xmin):
# pixels = [ pixelMap[y][xmin], pixelMap[y][xmax], pixelMap[ymin][x], pixelMap[ymax][x] ];
# for p in pixels:
# if p != pixelMap[y][x]:
# rMoy += p.r;
# gMoy += p.g;
# bMoy += p.b;
# count += 1
for j in pixelMap[ymin:ymax]: # on parcourt les pixels autour
for pix in j[xmin:xmax]:
# calcul de la moyenne autour du pixel
if i != x and j != y:
rMoy += pixelMap[j][i].r;
gMoy += pixelMap[j][i].g;
bMoy += pixelMap[j][i].b;
if pix != pixelMap[y][x]:
rMoy += pix.r;
gMoy += pix.g;
bMoy += pix.b;
count += 1
if count > 0:
@ -78,9 +86,6 @@ class Noise:
def getShapeRecursive(self, coords, pixelMap, pixelList ): # return PixelList
# coords = [lastx, lasty, x, y]
width = len( pixelMap[0] )
@ -106,6 +111,8 @@ class Noise:
# 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]
@ -116,17 +123,18 @@ class Noise:
pixelList.append( pixelMap[y][x] ) # ajout au tableau
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.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.getShape( [x, y, x-1, y ], pixelMap, pixelList) # 4
self.getShapeRecursive( [x, y, x-1, y ], pixelMap, pixelList) # 4
# current pixel
self.getShape( [x, y, x+1, y ], pixelMap, pixelList) # 6
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
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
def getShape(self, coords, pixelMap, pixelList):
# coords = [lastx, lasty, x, y]
@ -153,8 +161,6 @@ class Noise:
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];

View File

@ -11,14 +11,6 @@ import sys
import time
if len(sys.argv) < 3:
print "Require 2 args : \n* input image\n* output image"
exit()
class Timer:
def __init__(self):
self.timer = time.time();
@ -38,7 +30,7 @@ def testFileIntegrity():
# lecture du fichier
print "Reading Image -",; t.reset();
print "Reading Image -",; t.reset();
with open( sys.argv[1] ) as file:
binFile = file.read()
print "Done in %s s" % (t.get())
@ -49,29 +41,86 @@ def testFileIntegrity():
# Parsing
print "Parsing file -",; t.reset();
print "Parsing file -",; t.reset();
img.parse( binFile );
print "Done in %s s" % (t.get())
print "Creating random extreme pixels -",; t.reset();
noise.SaltAndPepper_set(10, img.content.map)
print "Done in %s s" % (t.get())
print "Removing salt and pepper -",; t.reset();
noise.SaltAndPepper_unset(img.content.map)
# print "Creating Salt&Pepper -",; t.reset();
# noise.SaltAndPepper_set(10, img.content.map)
# print "Done in %s s" % (t.get())
# # Unparsing
# print "Unparsing file -",; t.reset();
# img.unparse(newBpp=24)
# print "Done in %s s" % (t.get())
# # image to stdout
# print "Writing file -",; t.reset();
# img.write( "SaltAndPepper.bmp" )
# print "Done in %s s" % (t.get())
# print "Removing Salt&Pepper -",; t.reset();
# noise.SaltAndPepper_unset(img.content.map)
# print "Done in %s s" % (t.get())
# # Unparsing
# print "Unparsing file -",; t.reset();
# img.unparse(newBpp=24)
# 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 "Getting Shape -",; t.reset();
pixelList = [];
try:
noise.getShapeRecursive([0,0,0,0], img.content.map, pixelList);
except RuntimeError as e:
noise.getShapeRecursive([0,0,0,0], img.content.map, pixelList);
print "Done in %s s" % (t.get())
# Unparsing
print "Unparsing file -",; t.reset();
print "Unparsing file -",; t.reset();
img.unparse(newBpp=24)
print "Done in %s s" % (t.get())
# image to stdout
print "Writing file -",; t.reset();
img.write( sys.argv[2] )
print "Writing file -",; t.reset();
img.write( "shape.bmp" )
print "Done in %s s" % (t.get())
print "\nExecution Time: %s seconds" % total.get()
@ -93,6 +142,21 @@ def testManualCreation():
print img.binData
if len(sys.argv) < 3:
print "Require 2 args : \n* input image\n* output image"
exit()
# MAIN
#testManualCreation()
testFileIntegrity()