import os, sys #The program begins here #provisorial name count_noise1_0.py # It calculates the noise as number of pixels that are different between two images recorded from the same subject # Provided under GPL licence # Author Alessio Papini, Department of Plant Biology University of Florence Italy, Via La Pira, 4 Firenze, mail alpapiniATunifi.it print('It calculates the number of different pixels between 2 images taken consecutively from the same subject') print('supported formats bmp, jpg, tif') print('The images must be of the same dimensions (widhxheight) and in frame one with the other') # It explains what the program does i1=raw_input('Write the name of the first of the images (with its path to directory if different from the current working directory): ') i2=raw_input('Write the name of the second of the images (with its path to directory if different from the current working directory): ') # It gets the names of the two images import Image im1 = Image.open(i1) im2 = Image.open(i2) # it loads the two images x, y = im1.size # width and height for the two nested cycle needed to go through the image: the pixel position will be in a(x,y) #ora inizio i due cicli annidati lo esterno con la width e lo interno con la heighth della immagine countnoise_im1_im2=0 #initializes counter for i in range (x): for j in range (y): a=im1.getpixel((i,j)) b=im2.getpixel((i,j)) if a!=b: countnoise_im1_im2=countnoise_im1_im2 + 1 #if the value of the pixel is different in position x,y in the two images the counter increases by 1 print('number of pixels that are different between the first and the second image (noise) is ') print countnoise_im1_im2 print('of a total of image pixels of ') print x*y print ('fraction of differing pixels between the two images') fract = float(countnoise_im1_im2)/(x*y) print fract # in the starting images, after the formula #probab di due pixel divers (calcolata da countnoise) su due immagini dato rumore x nelle immagini di partenza n!/(k!(n-k)!)*(x**k)*((1-x)**(n-k))=2/2*(x**2)*((1-x)**0)=x**2 plus the probability di avere 1 solo pixel rumoroso: n!/(k!(n-k)!)*(x**k)*((1-x)**(n-k))=2/1*x*((1-x)**1)=2x(1-x)=2x-2x**2 #Total numero pixel da countnoise=y=2x-2x**2+x**2=2x-x**2 (since y will be a constant: x**2-2x+y=0) For instance per y=75% i will have x=0.5 (50% pixels rumorosi nella immagine di partenza) # i will have x1=(2-(squareroot(4*countnoise_im1_im2)))/2 and x2=(2+(squareroot(4*countnoise_im1_im2)))/2 delta = (4-(4*(float(countnoise_im1_im2)/(x*y)))) import math sqdelta = math.sqrt(float(delta)) x_1 = float(2-sqdelta)/2 x_2 = float(2+sqdelta)/2 print('Evaluation of the fraction of noisy pixels in the starting images') if x_1 < 1: print x_1 elif x_2 < 1: print x_2