<commit_before>

import Image
from PIL import ImageFilter
import numpy

import time

def hmedian(hist, med_loc):
    hsum=0;
    for i in range(0, hist.size):
        hsum+=hist[i];
        if(hsum>=med_loc):
            return i;
    return hist.size;


kernel_size=5;
kernel_border=kernel_size/2;
med_loc=(kernel_size*kernel_size)/2


img = Image.open("barbara.bmp").convert('L')
cols,rows=img.size
im = numpy.array(img); #.reshape(rows,cols);
#outIm=numpy.zeros(img.size)+255
outIm=im;


start=time.time()
hist=numpy.zeros([rows,256])
H=numpy.zeros(256)


for r in range(0,rows):
    for wc in range(1, kernel_size+1):
        val=im[r,wc];
        hist[r,val]+=1
    outIm[r,kernel_border]=0  # TODO - this value needs to be updated.

H=0
for r in range(kernel_border+1,kernel_size):
        H+=hist[r];


for c in range(kernel_border+1,cols-kernel_border-1):
#    print  range(kernel_border+1,cols-kernel_border-1)
# Initializing Whole histogram            
    for r in range(kernel_border+1,rows-kernel_border):
        oldval = im[r+kernel_border, c-kernel_border-1]
        newval = im[r+kernel_border, c+kernel_border]
        hist[r+kernel_border,oldval]-=1
        hist[r+kernel_border,newval]+=1        
        H+=( hist[r+kernel_border]-hist[r-kernel_border-1])

        outIm[r,c]= hmedian(H, med_loc)

    #Initializing first rows.
    for r in range(0,kernel_size):
        hist[r]=0
        for wc in range(c-kernel_border, c+kernel_border+1):
            val=im[r,wc];
            hist[r,val]+=1
    H=0
    for r in range(0,kernel_size):
        H+=hist[r];
                  
        
stop=time.time();
print hist[0]
print (stop-start)
result = Image.fromarray((outIm).astype(numpy.uint8))
result.save('out.bmp')


start=time.time()
pyMF = img.filter(ImageFilter.MedianFilter(kernel_size))
stop=time.time();
print (stop-start)
pyMF.save('out1.bmp')

exit()

#fft_mag = numpy.abs(numpy.fft.fftshift(numpy.fft.fft2(im)))
#visual = numpy.log(fft_mag)
#visual = (visual - visual.min()) / (visual.max() - visual.min())

#result = Image.fromarray((visual * 255).astype(numpy.uint8))
#result.save('out.bmp')
<commit_msg>Seems like I added a none working version in previous commit. this one is the correct version.<commit_after>

import Image
from PIL import ImageFilter
import numpy
import time
import math

def psnr(img1, img2):
    mse = numpy.mean( (img1 - img2) ** 2 ) 
    if mse == 0:
        return 100
    PIXEL_MAX = 255.0
#    return 20 * math.log10(PIXEL_MAX / math.sqrt(mse))
    return -10 * math.log10(mse/(PIXEL_MAX*PIXEL_MAX))

def hmedian(hist, med_loc):
    hsum=0;
    for i in range(0, hist.size):
        hsum+=hist[i];
        if(hsum>=med_loc):
            return i;
    return hist.size;

def addHistogram(H,hist,col,hist_size):
    for hs in range (0,hist_size):
        H[hs]+=hist[hs,col]
        
def subHistogram(H,hist,col,hist_size):
    for hs in range (0,hist_size):
        H[hs]-=hist[hs,col]

diam=5
r=(diam-1)/2;

#med_loc=(r*r)/2;
med_loc=2*r*r+2*r;


img = Image.open("barbara.bmp").convert('L')
cols,rows=img.size
X=im = numpy.array(img); #.reshape(rows,cols);
#Y=im;
Y=numpy.zeros(img.size)


start=time.time()


H=numpy.zeros(256)
hist=numpy.zeros([256,cols]);

for j in range(0,cols):
    hist[X[0,j],j]=r+2

for i in range(1,r):
    pos=min(i,rows-1)
    for j in range(0,cols):
        tempVal=X[pos,j];
        hist[tempVal,j]+=1
#print sum(sum(hist))

for i in range(0,rows):
    H=numpy.zeros(256);
    
    possub=max(0,i-r-1);
    posadd=min(rows-1,i+r);
    for j in range(0,cols):
        hist[X[possub,j],j]-=1
        hist[X[posadd,j],j]+=1
    
    for j in range(0,2*r):
        addHistogram(H,hist,j,256)

    for j in range(r,cols-r):
        possub=max(j-r,0)
        posadd=min(j+r,cols-1)
        addHistogram(H,hist,posadd,256)        
#        Y[i,j]=hmedian(H,med_loc);
#        subHistogram(H,hist,possub,256)

        
stop=time.time();

print (stop-start)
result = Image.fromarray((Y*255).astype(numpy.uint8))
result.save('out.bmp')


start=time.time()
pyMF = img.filter(ImageFilter.MedianFilter(diam))
stop=time.time();
print (stop-start)
pyMF.save('out1.bmp')

print psnr(Y, pyMF)

diff=numpy.abs(Y-pyMF)
#print (sum(diff))/(rows*cols)
tmpdiff = Image.fromarray((diff).astype(numpy.uint8))
tmpdiff.save('diff.bmp')


exit()
{'fraction_non_alphanumeric': 0.16643454038997216, 'fraction_numerical': 0.027158774373259052, 'mean_word_length': 3.356926188068756, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4387176', 'n_tokens_mistral': 1807, 'n_tokens_neox': 1733, 'n_words': 306}