Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- from scipy import misc as mc
- img = mc.imread('plik.png')
- def rgb2gray(img):
- r = img[:, :, 0]
- g = img[:, :, 1]
- b = img[:, :, 2]
- s1 = 0.2126 * r + 0.7152 * g + 0.0722 * b
- return s1
- def histogram(count,image):
- offset=255.0/count
- histogramValueArray = []
- i = 0
- while (i < 255.0):
- i=i+offset
- histogramValueArray.append(np.sum(((image >= (i-offset)) & (image <= i))))
- return histogramValueArray
- def linear_expansion(image):
- # linear expansion
- minVal = np.amin(image)
- maxVal = np.amax(image)
- test = 255
- height = img.shape[0]
- width = img.shape[1]
- s = (height,width)
- output = np.zeros(s)
- for i in range(0,height):
- for j in range(0,width):
- if (image[i][j] < 0):
- output[i][j] = 0
- elif (image[i][j] > test):
- output[i][j]=test
- else:
- output[i][j]=((image[i][j]-minVal)*(test/(maxVal-minVal)))
- return output
- def nonlinear_expansion(image):
- # nonlinear expansion
- height = img.shape[0]
- width = img.shape[1]
- size = (height,width)
- output = np.zeros(size)
- test1 = image/255
- test2 = np.zeros(size)
- #power
- test1 = np.power(test1,2)
- #sqrt
- #test1 = np.power(test1,0.5)
- for i in range(0,height):
- for j in range(0,width):
- #log10
- #pw.gda.pl -> czynnik normalizujacy
- #test1[i][j] = np.log(test1[i][j]+1)*1.4428
- #exp
- #test1[i][j] = np.exp(test1[i][j]-1)/1.7183
- if ((test1[i][j] > 0) & (test1[i][j] < 1)):
- test2[i][j]=test1[i][j]
- elif (test1[i][j] < 0):
- test2[i][j]=0
- elif (test1[i][j] > 1):
- test2[i][j]=1
- test2=test2*255
- return test2
- def balance(image):
- # returns balanced image
- height = img.shape[0]
- width = img.shape[1]
- size = (height,width)
- output = np.zeros(size)
- D0 = 0
- D = np.zeros(256)
- LUT = np.zeros(256)
- D[0] = (np.sum(image)/(height*width))
- if( D[0] != 0 ):
- D0 = D[0]
- for i in range(0,255):
- D[i+1] = D[i] + (np.sum(image)/(height*width))
- if (D0.astype('int') != 0 & D[i+1].astype('int') != 0 ):
- D0 = D[i+1]
- LUT = (D - D0) / (1 - D0)
- for i in range(0,height):
- for j in range(0,width):
- output[i][j] = LUT[image[i][j].astype('int') + 1]
- output = output * -1
- return output
- imggray = rgb2gray(img)
- # first histogram
- histogramArr = (histogram(1000,imggray))
- tab = np.linspace(0,255,len( histogramArr) )
- plt.bar(tab, histogramArr)
- plt.show();
- # test the histogram function
- #A = np.array([[1, 0, 0.25], [1, 0.75, 0.5], [1, 0.5, 0.25]]) * 255.0
- #print (A)
- #print (histogram( 3, A )) # [3 2 4]
- #print (histogram( 5, A )) # [1 2 2 1 3]
- # linear_expansion
- #linearExpansionArr = histogram(1000,linear_expansion(imggray))
- #tab = np.linspace(0,255,len(linearExpansionArr) )
- #plt.bar(tab, linearExpansionArr)
- #plt.show()
- #nonlinear_expansion
- #nonlinearExpansionArr = histogram(1000,nonlinear_expansion(imggray))
- #tab = np.linspace(0,255,len(nonlinearExpansionArr) )
- #plt.bar(tab, nonlinearExpansionArr)
- #plt.show()
- #plt.imshow(nonlinear_expansion(imggray), cmap=plt.cm.gray , vmin=0 , vmax=255)
- #plt.show();
- #potegowanie przyciemnia
- #pierwiastkowanie rozjasnia
- #log rozjasnia
- #ex przycieminia
- # balance
- #balanceArr = histogram(1000,balance(imggray))
- #tab = np.linspace(0,255,len(balanceArr) )
- #plt.bar(tab, balanceArr)
- #plt.show()
- #plt.imshow(balance(imggray), cmap=plt.cm.gray , vmin=0 , vmax=255)
- #plt.show();
- #rownomierny rozklad swiatla
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement