def testhist(): fname='/home/eg309/Data/AquaTermi_lowcontrast.JPG' #fname='/home/eg01/Data/AquaTermi_lowcontrast.JPG' #imhist,bins = sp.histogram(im.flatten(),nbr_bins,normed=True,new=True) im=form.loadpic(fname) print im.shape nbr_bins=256 #Return the hist imhist,bins = sp.histogram(im.flatten(),nbr_bins,normed=True,new=True) #pylab.hist(im.flatten(),nbr_bins,normed=True,new=True) pyplot.figure(1) print 'imhist',imhist.shape pyplot.plot(imhist) pyplot.figure(2) #fig.add_subplot(2,1,1) pyplot.imshow(im, cmap=mpl.cm.gray) pyplot.show() pyplot.figure(3) cdf = imhist.cumsum() #cumulative distribution function cdf = 255 * cdf / cdf[-1] #normalize print 'bins.shape',bins.shape print 'cdf.shape',cdf.shape #use linear interpolation of cdf to find new pixel values im2 = sp.interp(im.flatten(),bins[10:-1],cdf[10:]) print 'im.min',im.min() print 'im.max',im.max() print 'im.min',im2.min() print 'im.max',im2.max() pyplot.imshow(im2.reshape(im.shape), cmap=mpl.cm.gray) pyplot.show()
def denoise2DICM(): ''' Example taken from Bishop's book p.389 Iterated Conditional Modes (ICM) Discrete Case Just a cross neighbourhood is allowed here. Other configurations are easy to apply as well. ''' im=form.loadpic('mrf2.png') im=sp.asarray(im,dtype='float64') im[im>127]=255 im[im<=127]=0 print im.min(),im.max() plt.matshow(im) plt.show() #observed noisy image Y=sp.asarray(im,dtype='float64') Y[Y==255]=1 Y[Y==0]=-1 #state of pixel #X=sp.ones(Y.shape) X=Y beta,eta,h=1,1,0 E=sp.zeros(Y.shape) converge=False while(not converge): Eprev=sp.sum(E) for r in xrange(1,Y.shape[0]-1): for c in xrange(1,Y.shape[1]-1): #calculate energy for positive x=1 Ep=-eta*x*Y[r,c]-beta*x*X[r,c+1]-beta*x*X[r,c-1] - beta*x*X[r-1,c]-beta*x*X[r+1,c] #calculate energy for negative x=-1 En=- eta*x*Y[r,c] - beta*x*X[r,c+1] - beta*x*X[r,c-1] - beta*x*X[r-1,c] - beta*x*X[r+1,c] if En<Ep: X[r,c]=-1 E[r,c]=En else: X[r,c]=1 E[r,c]=Ep #E[r,c]=-eta*X[r,c]*Y[r,c]-beta*X[r,c]*X[r-1,c]-beta*X[r,c]*X[r+1,c] #plt.matshow(E) if Eprev-sp.sum(E)==0: converge=True plt.matshow(X) return E,Y,X