Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #odpuszczamy czesc 2
- import numpy as np
- import matplotlib.pyplot as pl
- from scipy import misc as mc
- from numpy import *
- def histogram(image, N):
- [x,y] = image.shape
- hist = np.zeros((1,N),dtype=np.int)
- step = 255/N
- step = np.round(step)
- for pixelX in range(0,x):
- for pixelY in range(0,y):
- value = np.floor(image[pixelX,pixelY]/step)+1
- value = np.int8(value)
- hist[0][value]=(hist[0][value])+1
- return hist[0]
- #
- def histMinMax(hist):
- min = 0
- max = 255
- for i in range(0, 255):
- if(hist[i]>100):
- min = i
- break
- for j in range(0, 255):
- if(hist[-j]>100):
- max = 255 -j
- break
- return min, max
- #
- def exten(image, min, max):
- img=np.copy(image)
- [x,y]=img.shape
- vec=np.arange(255)
- for i in range(0, 255):
- if(vec[i]<min):
- vec[i]=0
- elif(vec[i]>max):
- vec[i]=255
- else:
- vec[i]=(255/(max-min)) *(i-min)
- for pixelX in range(0,x):
- for pixelY in range(0,y):
- intValue=np.int8(img[pixelX][pixelY])
- img[pixelX][pixelY]=vec[intValue]
- return img
- def normal(imageMatrix):
- h=histogram(imageMatrix,255)
- [x,y]=imageMatrix.shape
- dystr=zeros((1,255),dtype=np.float)
- dystr=dystr[0]
- pixels=x*y
- a=0.0
- for i in range(0, 255):
- a=a+h[i]
- dystr[i]=a/pixels
- vec=zeros((1,255),dtype=np.int)
- vec=vec[0]
- for i in range(0, 255):
- value = ((dystr[i]-dystr[0])/(1.0-dystr[0]))*255.0
- vec[i]=value.astype(int)
- im=np.copy(imageMatrix)
- for pixelX in range(0, x):
- for pixelY in range(0, y):
- a=np.int8(im[pixelX][pixelY])
- im[pixelX][pixelY]=vec[a]
- return im
- img = mc.imread('kierowca.png')
- img=np.mean(img,2)
- size=255
- h=histogram(img, size)
- [min,max]=histMinMax(h)
- print("min: {0}, max: {1}".format(min,max))
- img2=np.copy(img)
- img2=exten(img2,min,max)
- h2=histogram(img2, size)
- size_vec=range(size)
- img3=normal(img)
- h3=histogram(img3, size)
- pl.subplot(3, 2, 1)
- pl.ylabel('Normalny')
- pl.imshow(img,cmap='gray', vmin=0, vmax=255)
- pl.subplot(3, 2, 2)
- pl.bar(size_vec,h,1,color='blue')
- pl.subplot(3, 2, 3)
- pl.ylabel('Rozciagniety')
- pl.imshow(img2,cmap='gray', vmin=0, vmax=255)
- pl.subplot(3, 2, 4)
- pl.bar(size_vec,h2,1,color='blue')
- pl.subplot(3,2,5)
- pl.ylabel('Wyrownany')
- pl.imshow(img3,cmap='gray',vmin=0,vmax=255)
- pl.subplot(3,2,6)
- pl.bar(size_vec,h3,1,color='blue')
- pl.show()
- #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement