[Image-SIG] performance of pixel by pixel processing

Maxime demers burton.1 at sympatico.ca
Sun Nov 21 17:22:37 CET 2010

Good day everyone,
I would like to have your suggestions about how it could be possible to improve process speed of my following script. The aim of the script is to scan each pixel of an image, and to look its validity with its neighborhood. I use a function that return the values of mask of 9x9 pixels around the scanned pixel and calculate the interval confidence of it. If the pixel is valid with the 9x9 mask its stay unchanged, if it is outside the interval confidence, the pixel receive the mean of the mask.
The problem is that this pixel by pixel processing is really slow. It could take many hours for image not bigger than 4-5 Mo. Anybody knows how I could increase the performance of such a script? I use PIL and numpy.
Thank youMaxime Demers--------------------------------------------------------------------------------------------------------------------------
import PILimport ImageOpsimport Imageimport numpyfrom OuvrirImg import * 				#function that open an imagefrom CreateSide import * from RebuildImg import *from IntervalConfiance import *

def CreateMask(dimensions,image,i,j):    'create a mask of N dimensions at the position i,j of a selected image and return the values of DN of each pixel in the mask in a list'    img = numpy.array(image)    dim = dimensions/2    cpt = 1    mask = []    y = j    x = i    while cpt <= dimensions:        dim2 = dimensions/2        cpt2 = 1        while cptr2 <= dimensions:            mask.append(img[x-dim][y-dim2])            dim2 = dim2-1            cpt2 = cpt2 + 1        dim = dim-1        cpt = cpt + 1    return mask

#This script scan each pixel of an image with a N dimension mask#If the the scanned pixel is between the interval confidence of the pixels in the mask#that pixel stay unchanged, if its outside the interval confidence, the pixel take the mean#of the pixels in the mask
test=OuvrirImg('f:\\mapaq_petit.tif')img=CreateSide(9,test) 				#create an image with side for a 9x9 maskNewImage=[0]*(img.shape[1]-8)*(img.shape[0]-8)h=0for i in range(4,img.shape[0]-4):    for j in range(4,img.shape[1]-4):        mask2 = CreateMask(9,img,i,j) 		#return a list of DN of the 9X9 mask at the position i,j        mean = IntervalConfiance(mask)[0]        interval = IntervalConfiance(mask)[1]        if img[i][j] >= mean+interval or img[i][j] <= mean-interval:            NewImage[h] = mean        else:            NewImage[h]=img[i][j]        h = h+1
print "process completed, the new image is created"Dilate=numpy.array(Dilate)Dilate=numpy.reshape(Dilate,(img.shape[0]-8,img.shape[1]-8))RebuildImg(Dilate,"f:\\newImage.tif") 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/image-sig/attachments/20101121/d866822d/attachment-0001.html>

More information about the Image-SIG mailing list