-
Notifications
You must be signed in to change notification settings - Fork 2
/
histequalfinal.py
247 lines (187 loc) · 5.82 KB
/
histequalfinal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
'''
The software aims to be an open-source package with basic, and advanced Image Processing features.
Copyright (C) 2014 Indian Institute of Remote Sensing, Dehradun
The original authors of this program (in alphabetical order) are:
---------------------------------------------------------------------------------------------------------------------------------------
Sno. NAME Email( AT gmail DOT com) Role(s)
---------------------------------------------------------------------------------------------------------------------------------------
1. Pragun Vinayak pragunvinayak GUI, Contrast, Histogram
2. Abhishek Kumar Roushan abhishek.roushan12 Histogram, Contrast
----------------------------------------------------------------------------------------------------------------------------------------
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
from osgeo import gdal
from PIL import Image
from pylab import *
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import numpy.random
import matplotlib.cm as cm
import cv2
from PIL import Image
from pylab import *
from Tkinter import *
import Tkinter as tk
from tkMessageBox import *
import ttk
import tkFileDialog
from PIL import Image
import tifwork
import sys
import matplotlib
import math
from osgeo import gdal
#import piecewise
matplotlib.use('TkAgg')
#global fx,ax
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
# implement the default mpl key bindings
from matplotlib.backend_bases import key_press_handler
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from matplotlib.figure import Figure
import pylab
#import Image, ImageTk
#import tktable
import tifwork
import matplotlib.gridspec as gridspec
fileName=tkFileDialog.askopenfilename(filetypes=[('TIF','*.tif')])
if(fileName == ''):
pass
else:
print(fileName)
dataset=(tifwork.openTIF(fileName))
def histeq(im,nbr_bins=256):
# get image histogram
imhist,bins = histogram(im.flatten(),nbr_bins,normed=True)
cdf = imhist.cumsum() # cumulative distribution function
cdf = 255 * cdf / cdf[-1] # normalize
# use linear interpolation of cdf to find new pixel values
im2 = interp(im.flatten(),bins[:-1],cdf)
return im2.reshape(im.shape), cdf
def showallhist(imgname):
im = array(Image.open(imgname).convert('L'))
print im
im3 = histeq(im)
im_output=cv2.equalizeHist(im)
combImage2= Image.fromarray(im_output)
img2=combImage2.save(imgname+'_enhance_'+'.jpeg')
print im_output
ax1=plt.subplot(221)
plt.xlim([0,255])
plt.title('Histogram BEFORE Equalization')
plt.xlabel('DN values')
plt.ylabel('Number of pixels')
ax2=plt.subplot(222)
plt.xlim([0,255])
plt.title('Histogram AFTER Equalization')
plt.xlabel('DN values')
plt.ylabel('NUmber of pixels')
ax3=plt.subplot(223)
plt.axis('off')
plt.title('Image BEFORE Equalization')
ax4=plt.subplot(224)
plt.axis('off')
plt.title('Image AFTER Equalization')
ax1.hist(im.flatten(),256)
ax2.hist(im3,256)
ax3.imshow(im,cmap=cm.Greys_r)
ax4.imshow(im_output,cmap=cm.Greys_r)
plt.tight_layout()
plt.show()
def bandPrint():
global entry_fn
savefn=entry_fn.get()
if(opt1.get()== ""):
showerror("Error", "Two same bands selected")
else:
global fileName
print fileName
ch1 = int(opt1.get())
dataset =gdal.Open(fileName,gdal.GA_ReadOnly)
band_select=dataset.GetRasterBand(ch1)
array_img=band_select.ReadAsArray()
Image_of_band= Image.fromarray(array_img)
img=Image_of_band.save(savefn +'.jpeg')
imgn=savefn+'.jpeg'
print 'completed'
print img
print array_img
showallhist(imgn)
#hist(im)
#combImage.show()
w=tk.Tk()
w.title("Histogram Equalization")
frame2=tk.Frame(w)
(cols,rows,bands,bandArray) = tifwork.detailsTIF(dataset)
optionList=[]
for k in range(1, bands+1):
optionList.append(k)
x1=tk.Label(frame2, text="Select Band")
x1.pack(side=tk.LEFT, pady = 5 , padx = 5 )
opt1= ttk.Combobox(frame2,values=optionList, width=5)
opt1.pack(side = tk.LEFT , pady = 5,padx = 5 )
frame2.pack(side=tk.TOP, pady = 5 , padx = 5 )
frame6=tk.Frame(w)
lfn=tk.Label(frame6,text='Save file As :')
lfn.pack(side=tk.LEFT)
entry_fn=tk.Entry(frame6)
entry_fn.pack(side=tk.LEFT)
frame6.pack(side=tk.TOP)
button = tk.Button(w, width=20, text="Show Histogram Equalization", command=bandPrint)
button.pack(side = tk.TOP , pady = 5 , padx = 5 )
w.mainloop()
# read image to array
#im = array(Image.open('x.jpg').convert('L'))
#figure()
# don't use colors
#gray()
# show contours with origin upper left corner
#contour(im, origin='image')
#axis('equal')
#axis('off')
#show()
"""
def showequalhist():
pass
def displayimg(im):
figure()
# don't use colors
gray()
# show contours with origin upper left corner
contour(im, origin='image')
axis('equal')
axis('off')
show()
"""
"""
def numbands(filename_extract):
dataset =gdal.Open(filename_extract,gdal.GA_ReadOnly)
if dataset is None:
print 'Could Not Open' + filename
#Alternatively Raise an Event here
sys.exit(1)
bands = dataset.RasterCount
return bands
#GUI
#end of GUI
# create a new figure
#figure()
# don't use colors
#gray()
# show contours with origin upper left corner
#contour(im, origin='image')
#axis('equal')
#axis('off')
"""