EE 610 Assignment 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Basic Image Processing GUI in Python

Suyash Bagad Saurabh Kolambe


Department of Electrical Engineering Department of Electrical Engineering
Indian Institute of Technology, Bombay Indian Institute of Technology, Bombay
15D070007@iitb.ac.in 15D070011@iitb.ac.in

Abstract—A simple Image Processing program with GUI using 6) Load frequency mask button
Python OpenCV and PyQt4. 7) Undo all changes (revert to original)
Keywords—Image Processing, GUI, Python, PyQt 8) Slider for controlling the Extent of transformation
9) Plot histogram of the image in display area
10) Mask taken as input image from user for Frequency
I. I NTRODUCTION domain filtering
The code creates a simple user friendly GUI, with 2 11) User inputs taken in Dialogue boxes
image display area, for original as well as edited image. 12) Informative Message boxes
The GUI has button to apply image transformation such
histogram equalization, gamma correction, sharpening, etc. It
also includes a slider which can be used to control the extent II. BACKGROUND READ
of of the transformations applied such as blur. The algorithm The problem statement required us to examine several basic
implemented works with the HSV image format thus the GUI image processing techniques to implement them in software.
converts the RGB to HSV, apply the transformations and then The two parts of this assignment can be summarized as GUI
again display the image in RGB format. buiding and second the image processing techniques.
GUI options :

• Tkinter
• PyQt
• MATLAB GUI creator

Tkinter is good for small applications. It has a very small


footprint and is shipped with the Python package itself so you
don’t need to install it separately. GTK3 is good option and
has a good documentation. Rather than using PyQt you can
use PySide has it has a LGPL license and can be used for any
Fig : Image Processing GUI kind of application. Pyqt has a GPL license. There is a very
slight difference between PyQt and PySide.
A. Functions implemented
The Algorithm uses tools such as,
1) Edge detection
2) Histogram Equlization • Open CV: Used for various inbuilt image transform
3) Gamma Correct with γ as a user input parameter functions such as RGB to HSV conversion and vice
4) Log transform versa, image read
5) Gaussian Blur with slider control for controlling
extent of blurring • Numpy: For mathematical calculations, such as com-
6) Image sharpening with slider control for controlling plex multiplication, addition, matrix operations
extent of sharpening
• Matplotlib: To plot the graphs and images parameters
7) 2D DFT of image
8) Fourier domain image masking
9) Inverting III. A PPROACH

B. GUI Features Our code is first comverting the RGB image into HSV
format using inbuilt BGR2HSV function from CV2 library. All
1) Original Image display area the transformations are implemented in HSV format. And the
2) Edited Image Display area final image is again converted to RGB format using HSV2BGR
3) Image Load button function to display the color image.
4) Image Save button
5) Undo Last change The functions implemented are explained below:
A. Equalize histogram

Histogram equalization is used to enhance contrast. It is not


necessary that contrast will always be increase in this. There
may be some cases were histogram equalization can be worse.
In that cases the contrast is decreased.

Fig : Histogram of Equalized image

Fig : As you can clearly see from the images that the new image
Histogram Equalization contrast has been enhanced and its histogram has also been
equalized.

B. Gamma Correct
For
equalization, We would like to create a transformation of The gamma transformation can be defined by this formula
the form y = T(x) to produce a new image y, with a flat s=cr Variation in the value of varies the enhancement of
histogram. Such an image would have a linearized cumulative the images. Different display devices / monitors have their
distribution function (CDF) across the value range where own gamma correction, thats why they display their image at
cdfmin is the minimum non-zero value of the cumulative different intensity. The gamma of different display devices is
distribution function, M N gives the image’s number of different. For example Gamma of CRT lies in between of 1.8
pixels and L is the number of grey levels used. to 2.5, that means the image displayed on CRT is dark. One of
the faster way to perform gamma correction is using the LUT
approach. All we need to do is build a table (i.e. dictionary)
that maps the input pixel values to the output gamma corrected
values. OpenCV can then take this table and quickly determine
the output value for a given pixel in O(1) time

Fig : Original and histogram equalized image Fig : Gamma correction with increasing gamma

C. Log transform

The log transformations can be defined by this formula s =


c log(r + 1). Where s and r are the pixel values of the output
and the input image and c is a constant. The value 1 is added
to each of the pixel value of the input image because if there
is a pixel intensity of 0 in the image, then log (0) is equal to
infinity. So 1 is added, to make the minimum value at least
1. During log transformation, the dark pixels in an image are
expanded as compare to the higher pixel values. The higher
pixel values are kind of compressed in log transformation. This
result in following image enhancement. The value of c in the
log transform adjust the enhancement The LUT approach is
Fig : Histogram of original image used to implement this transformation
Fig : Log transformation

D. Blur with a mechanism to control the extent of blurring

A Gaussian blur effect is generated by convolving an image


with a kernel of Gaussian values. A possible way to speed up
the convolution process, it is best to take advantage of the
Gaussian blurs separable property by dividing the process into
two passes. In the first pass, a one-dimensional kernel is used
to blur the image in only the horizontal or vertical direction.
Fig : Sharpening of image
In the second pass, the same one-dimensional kernel is used to
blur in the remaining direction. The resulting effect is the same The sharpening kernel is then convoluted with the original
as convolving with a two-dimensional kernel in a single pass, image to produce the output image using conv2d function.
but requires fewer calculations. We have used 2D convolution
for blurring effect. First a 2D 3x3 gaussian matrix is generated F. Compute 2-D DFT and display magnitude and phase
keeping the center value at 1 and standard deviation as the Because the transform kernels are separable and symmetric,
input specified by the user. And then vector calculations are the two dimensional transforms can be computed as sequential
used for computing the convolution of the gaussian filter matrix row and columnone-dimensional transforms.
with the image.

1 − x2 +y2 2
G(x, y) = e 2σ
2πσ 2 For 1D DFT, one way is to break the 1D array into smaller
arrays and apply Cooley-Tukey DFT computational approach
for fast DFT computation. The algorithm breaks the array into
2 array and apply the DFT on each array recursively, again
E. Sharpening with a mechanism to control the extent of
breaking the arrays into 2. By reusing values which have
sharpening
already been calculated the number of operations required can
be reduced drastically. The threshold for the recursion is set
Sharpening an image increases the contrast between bright at 32. That is, the slow dft will compute the DFT for 32 sized
and dark regions to bring out features. The sharpening process array and then merge the outputs to compute the dft of entire
is basically the application of a high pass filter to an image. 1D array. Reference for Cooley-Tukey approach is mentioned
The following array is a kernel for a common high pass filter in the links.
used to sharpen an image:

−1/9 −1/9 −1/9


!
−1/9 9 −1/9
−1/9 −1/9 −1/9

Fig :
Sharpening as 2-Dimensional convolution Fig : Butterfly structure in Cooley-Tukey algorithm
In this diagram, an 8 point DFT is split into two 4 point I. Edge Detection
DFTs, and the results are combined at the end. The number
of complex multiplications required at each stage is N, and Different gradient operators can be applied to estimate
the number of stages is log2N. This gives a complexity of image gradients from the input image or a smoothed version
O(N log2N), representing a significant improvement on the of it. The simplest approach is to use central differences.
complexity of the DFT Similar to edge detection algorithm, the edge detection kernel
is convolved with the image using conv2 df unction.
G. Compute and display modified image using the mask
A PPENDIX A
Given Two images, f and g with Fourier transforms F and P YTHON C ODE : BASIC GUI IN PYTHON
G, we can write, F(f) + F(g) = F(f + g) This can be used to 1 #
remove noise considering the filtered image as f and noise as ######################################################
g, from the above example, we can see the two spots in the first
fourier transform are causing noise, and thus can be removed 2 # B a s i c Image P r o c e s s i n g GUI
3 # P e r f o r m s s e v e r a l s i m p l e image p r o c e s s i n g f u n c t i o n s
by applying a black patch in place of the spots. Resulting in on
the cleared output image. 4 # an i m p o r t e d image u s i n g Python , P y s i d e , Qt , PIL
5 #
6 # S u y a s h Bagad S a u r a b h Kolambe
7 # 15 D070007 15 D070011
8 # ( Group number : 5 2 )
9 #
10 # A s s i g n m e n t 1 : B a s i c Image P r o c e s s i n g GUI
11 # EE 6 1 0 : Image P r o c e s s i n g
12 # Autumn S e m e s t e r , 2018
13 #
14 # Department of E l e c t r i c a l Engineering ,
15 # I I T Bombay
16 #
17 # C o p y r i g h t s r e s e r v e d @ S u y a s h Bagad , S a u r a b h
Kolambe
18 #
#####################################################

19
20 # Import t h e r e q u i r e d modules i n Python
21 import sys
22 from P y S i d e i m p o r t QtGui
23 from P y S i d e i m p o r t QtCore
24 from PIL i m p o r t Image
25 from random i m p o r t r a n d i n t
Fig : Frequency masking for image enhancement 26 i m p o r t numpy a s np
27 i m p o r t math
28 i m p o r t cv2
29 from m a t p l o t l i b i m p o r t p y p l o t a s p l t
30
31 # C l a s s d e f i n i n g main a p p l i c a t i o n window and a l l
other f u n c t i o n al i t i e s
32 c l a s s Main IP Window ( QtGui . QWidget ) :
33
34 def init ( self ) :
35 s u p e r ( Main IP Window , s e l f ) . init ()
36
37 self . initUI ()
38

Fig : DFT results from our implementation 39 # F u n c t i o n t o i n i t i a l i z e GUI


40 def i n i t U I ( s e l f ) :
In the above image you can see the original image is 41
42 print ( ” Initializing ”)
added with diagonal lines which are acting as a noise in this 43
case. In the DFT of input, we can see the bright spots which 44 # The GUI i s d i v i d e d i n t o 4 d i f f e r e n t
represents the diagonal noisy lines. After adding a mask as layouts
in the figure. We can remove the diagonal lines the images is 45 # F i l e P i c k Layout , Bottom box , Top box ,
L e f t S i d e box
almost noiseless and clean than the original input image. 46 #

H. Negative transform
47 # | |
This is one of the extra features we have implemented. This |
48 # | | Top box
transforms the image into its negative by replacing the values |
with their complementary values. Thus for a pixel with value 49 #
x will be replaced with a pixel with value 255-x. |−−−−−−−−−−−|−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−|
101 # D e f i n i n g l a y o u t boxes as d s c r i b e d above
50 # | | 102 # QH and QV a r e f o r H o r i z o n t a l and V e r t i c a l
| boxes r e p e c t i v e l y
51 # | | 103 bottomBox = QtGui . QHBoxLayout ( )
| 104 topBox = QtGui . QHBoxLayout ( )
52 # | Side | File pick 105 s i d e B o x = QtGui . QVBoxLayout ( )
| 106 s i d e B o x S l i d e r 1 = QtGui . QVBoxLayout ( )
53 # | box | 107 # s i d e B o x S l i d e r 2 = QtGui . QVBoxLayout ( )
| 108 # s i d e B o x S l i d e r 3 = QtGui . QVBoxLayout ( )
54 # | | 109 s i d e B o x P a d = QtGui . QHBoxLayout ( )
| 110
55 # 111 # File picker layout i s included in top
|−−−−−−−−−−−|−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−| layout
112 topBox . a d d S t r e t c h ( 1 )
56 # | | Bottom box 113 topBox . a d d L a y o u t ( f i l e P i c k )
| 114 topBox . a d d S t r e t c h ( 1 )
57 # | | 115
| 116 # Image i n m i d d l e / r i g h t ( i n b o t t o m l a y o u t )
58 117 bottomBox . a d d S t r e t c h ( 1 )
59 # F i l e Pick Layout 118 bottomBox . a d d W i d g e t ( s e l f . i m a g e L a b e l )
60 f i l e P i c k = QtGui . QHBoxLayout ( ) 119 bottomBox . a d d W i d g e t ( s e l f . i m a g e L a b e l O r g )
61 120 bottomBox . a d d S t r e t c h ( 1 )
62 # C r e a t e a l a b e l which d i s p l a y s t h e p a t h t o 121
our chosen f i l e 122 # D e f i n i n g B u t t o n s on l e f t l a y o u t
63 s e l f . f i l e L a b e l = QtGui . QLabel ( ’No f i l e 123 # N o r m a l i s i n g f u n c t i o n a p p l i e d on an image
selected ’) 124 n o r m a l B u t t o n = QtGui . Q P u s h B u t t o n ( ’
64 f i l e P i c k . addWidget ( s e l f . f i l e L a b e l ) Restore ’)
65 125 n o r m a l B u t t o n . s e t I c o n ( QtGui . QIcon ( ’ n o r m a l . png
66 # C r e a t e a p u s h b u t t o n l a b e l l e d ’ Choose f i l e ’))
’ and add i t t o o u r l a y o u t 126 n o r m a l B u t t o n . s e t I c o n S i z e ( QtCore . QSize ( 2 5 , 2 5 )
67 f i l e B t n = QtGui . Q P u s h B u t t o n ( ’ Choose f i l e ’ , )
self ) 127 normalButton . setFixedWidth (150)
68 f i l e P i c k . addWidget ( f i l e B t n ) 128 normalButton . setFixedHeight (32)
69 filePick . addStretch () 129 normalButton . c l i c k e d . connect ( s e l f .
70 normal image )
71 # Connect t h e c l i c k e d s i g n a l t o t h e 130
g e t f n a m e h a n d l e r t o open an image f i l e 131 # Button to apply ’ Histogram E q u a l i s a t i o n ’
72 f i l e B t n . c l i c k e d . connect ( s e l f . get fname ) on g i v e n image
73 132 H i s t o B t n = QtGui . Q P u s h B u t t o n ( ’ Histogram ’ )
74 # S a v i n g t h e c u r r e n t image i n c o m p u t e r 133 H i s t o B t n . s e t I c o n ( QtGui . QIcon ( ’ h i s t o . png ’ ) )
75 f i l e S a v e B t n = QtGui . Q P u s h B u t t o n ( ’ Save f i l e ’ , 134 H i s t o B t n . s e t I c o n S i z e ( QtCore . QSize ( 3 0 , 3 0 ) )
self ) 135 HistoBtn . setFixedWidth (150)
76 f i l e P i c k . addWidget ( f i l e S a v e B t n ) 136 HistoBtn . setFixedHeight (32)
77 137 HistoBtn . clicked . connect ( s e l f . histo equ )
78 # Connect t h e c l i c k e d s i g n a l t o t h e 138
s a v e f i l e h a n d l e r t o s a v e image f i l e 139 # B u t t o n b l u r a g i v e n image
79 fileSaveBtn . clicked . connect ( s e l f . s a v e f i l e ) 140 b l u r B u t t o n = QtGui . Q P u s h B u t t o n ( ’ Blur
80 ’)
81 # S e t t i n g t h e image t o be empty a t f i r s t 141 b l u r B u t t o n . s e t I c o n ( QtGui . QIcon ( ’ b . png ’ ) )
82 # pixmap i s an o b j e c t which s t o r e s t h e image 142 b l u r B u t t o n . s e t I c o n S i z e ( QtCore . QSize ( 3 0 , 3 0 ) )
, given t h e images ’ s path 143 blurButton . setFixedWidth (150)
83 # i m a g e L a b e l i s a l a b e l d e f i n e d where we ’ l l 144 blurButton . setFixedHeight (32)
d i s p l a y images 145 blurButton . clicked . connect ( s e l f .
84 pixmap = QtGui . QPixmap ( ) showDialog blur )
85 s e l f . i m a g e L a b e l = QtGui . QLabel ( ) 146
86 s e l f . i m a g e L a b e l . s e t P i x m a p ( pixmap ) 147 # Gamma t r a n s f o r m on g i v e n image
87 s e l f . imageLabel . setLineWidth ( 3 ) 148 GammaBtn = QtGui . Q P u s h B u t t o n ( ’ Gamma ’ )
88 s e l f . i m a g e L a b e l . s e t F r a m e S h a p e ( QtGui . QFrame . 149 GammaBtn . s e t I c o n ( QtGui . QIcon ( ’gamma . png ’ ) )
Panel ) 150 GammaBtn . s e t I c o n S i z e ( QtCore . QSize ( 2 5 , 2 5 ) )
89 s e l f . i m a g e L a b e l . s e t F r a m e S h a d o w ( QtGui . QFrame . 151 GammaBtn . s e t F i x e d W i d t h ( 1 5 0 )
Sunken ) 152 GammaBtn . s e t F i x e d H e i g h t ( 3 2 )
90 s e l f . imageLabel . setMidLineWidth ( 3 ) 153 GammaBtn . c l i c k e d . c o n n e c t ( s e l f .
91 # L a b e l f o r o r i g i n a l image t o be r e m a i n e d showDialog gamma )
constant 154
92 s e l f . i m a g e L a b e l O r g = QtGui . QLabel ( ) 155 # Log t r a n s f o r m on g i v e n image
93 s e l f . i m a g e L a b e l O r g . s e t P i x m a p ( pixmap ) 156 LogBtn = QtGui . Q P u s h B u t t o n ( ’ Log ’
94 s e l f . imageLabelOrg . setLineWidth ( 3 ) )
95 s e l f . i m a g e L a b e l O r g . s e t F r a m e S h a p e ( QtGui . 157 LogBtn . s e t I c o n ( QtGui . QIcon ( ’ l o g . png ’ ) )
QFrame . P a n e l ) 158 LogBtn . s e t I c o n S i z e ( QtCore . QSize ( 2 0 , 2 6 ) )
96 s e l f . i m a g e L a b e l O r g . s e t F r a m e S h a d o w ( QtGui . 159 LogBtn . setFixedWidth (150)
QFrame . Sunken ) 160 LogBtn . setFixedHeight (32)
97 s e l f . imageLabelOrg . setMidLineWidth ( 3 ) 161 LogBtn . c l i c k e d . connect ( s e l f . showDialog log )
98 s e l f . imageString = ”” 162
99 self . sliderValue = 0 163 # S h a r p e n a g i v e n image w i t h c u s t o m i z e d
100 e x t e n t of sharpening
164 sharpenButton = QtGui . Q P u s h B u t t o n ( ’ 224 sld1 . setTickInterval (10)
Sharpen ’ ) 225 s l d 1 . s e t T i c k P o s i t i o n ( QtGui . Q S l i d e r .
165 sharpenButton . s e t I c o n ( QtGui . QIcon ( ’ s h a p r . png TickPosition (5) )
’)) 226 s ld 1 . valueChanged [ i n t ] . connect ( s e l f .
166 sharpenButton . s e t I c o n S i z e ( QtCore . QSize change slider )
(26 ,26) ) 227 ’’’
167 sharpenButton . setFixedWidth (150) 228 s e l f . S l i d e r L a b e l 2 = QtGui . QLabel ( ’ S l i d e r 2 ’ )
168 sharpenButton . setFixedHeight (32) 229 s l d 2 = QtGui . Q S l i d e r ( QtCore . Qt . V e r t i c a l ,
169 sharpenButton . clicked . connect ( s e l f . sharpen ) self )
170 230 s l d 2 . s e t F o c u s P o l i c y ( QtCore . Qt . NoFocus )
171 # DFT o f a g i v e n image 231 s l d 2 . setGeometry (30 , 40 , 100 , 50)
172 DFTButton = QtGui . Q P u s h B u t t o n ( ’ DFT 232 s ld 2 . valueChanged [ i n t ] . connect ( s e l f .
’) change slider )
173 DFTButton . s e t I c o n ( QtGui . QIcon ( ’ d f t . png ’ ) ) 233
174 DFTButton . s e t I c o n S i z e ( QtCore . QSize ( 2 6 , 2 6 ) ) 234 s e l f . S l i d e r L a b e l 3 = QtGui . QLabel ( ’ S l i d e r 3 ’ )
175 DFTButton . setFixedWidth (150) 235 s l d 3 = QtGui . Q S l i d e r ( QtCore . Qt . V e r t i c a l ,
176 DFTButton . setFixedHeight (32) self )
177 DFTButton . c l i c k e d . c o n n e c t ( s e l f . DFT 2d ) 236 s l d 3 . s e t F o c u s P o l i c y ( QtCore . Qt . NoFocus )
178 237 s l d 3 . setGeometry (30 , 40 , 100 , 50)
179 # F r e q u e n c y m a s k i n g o f a g i v e n image 238 s ld 3 . valueChanged [ i n t ] . connect ( s e l f .
180 F r e q m a s k B u t t o n = QtGui . Q P u s h B u t t o n ( ’ Mask change slider )
’) 239 ’’’
181 F r e q m a s k B u t t o n . s e t I c o n ( QtGui . QIcon ( ’ mask . png 240 # A r r a n g i n g d i f f e r e n t l a y o u t s and w i d g e t s (
’)) B u t t o n s and o t h e r s )
182 FreqmaskButton . s e t I c o n S i z e ( QtCore . QSize 241 sideBox . a d d S t r e t c h ( 1 )
(30 ,26) ) 242 sideBox . addWidget ( normalButton )
183 FreqmaskButton . setFixedWidth (150) 243 sideBox . a d d S t r e t c h ( 1 )
184 FreqmaskButton . setFixedHeight (32) 244 sideBox . addWidget ( H i s t o B t n )
185 FreqmaskButton . clicked . connect ( s e l f . 245 sideBox . a d d S t r e t c h ( 1 )
showDialog mask ) 246 sideBox . addWidget ( b l u r B u t t o n )
186 247 sideBox . a d d S t r e t c h ( 1 )
187 # Edge D e t e c t i o n o f a g i v e n image 248 s i d e B o x . a d d W i d g e t ( GammaBtn )
188 edgeDetectButton = QtGui . Q P u s h B u t t o n ( ’ 249 sideBox . a d d S t r e t c h ( 1 )
Edge ’) 250 s i d e B o x . a d d W i d g e t ( LogBtn )
189 edgeDetectButton . s e t I c o n ( QtGui . QIcon ( ’ ed . png 251 sideBox . a d d S t r e t c h ( 1 )
’)) 252 sideBox . addWidget ( s h a r p e n B u t t o n )
190 edgeDetectButton . s e t I c o n S i z e ( QtCore . QSize 253 sideBox . a d d S t r e t c h ( 1 )
(25 ,25) ) 254 s i d e B o x . a d d W i d g e t ( DFTButton )
191 edgeDetectButton . setFixedWidth (150) 255 sideBox . a d d S t r e t c h ( 1 )
192 edgeDetectButton . setFixedHeight (32) 256 sideBox . addWidget ( FreqmaskButton )
193 edgeDetectButton . clicked . connect ( s e l f . 257 sideBox . a d d S t r e t c h ( 1 )
edge detect ) 258 sideBox . addWidget ( e d g e D e t e c t B u t t o n )
194 259 sideBox . a d d S t r e t c h ( 1 )
195 # I n v e r t i n g g i v e n image 260 sideBox . addWidget ( i n v e r t B u t t o n )
196 i n v e r t B u t t o n = QtGui . Q P u s h B u t t o n ( ’ Invert 261 sideBox . a d d S t r e t c h ( 1 )
’) 262 sideBox . addWidget ( p l o t B t n )
197 i n v e r t B u t t o n . s e t I c o n ( QtGui . QIcon ( ’ i n v . png ’ ) ) 263 sideBox . a d d S t r e t c h ( 1 )
198 i n v e r t B u t t o n . s e t I c o n S i z e ( QtCore . QSize ( 2 8 , 2 8 ) 264 s i d e B o x . a d d W i d g e t ( UndoBtn )
) 265 sideBox . a d d S t r e t c h ( 1 )
199 invertButton . setFixedWidth (150) 266
200 invertButton . setFixedHeight (32) 267 sideBoxPad . a d d S t r e t c h ( 1 )
201 invertButton . clicked . connect ( s e l f . i n v e r t ) 268 sideBoxPad . addLayout ( s i d e B o x S l i d e r 1 )
202 269 # sideBoxPad . addLayout ( s i d e B o x S l i d e r 2 )
203 # P l o t h i s t o g r a m o f image 270 # sideBoxPad . addLayoutsideBox . addWidget (
204 p l o t B t n = QtGui . Q P u s h B u t t o n ( ’ Plot ’) plotBtn ) ( sideBoxSlider3 )
205 p l o t B t n . s e t I c o n ( QtGui . QIcon ( ’ p l o t . png ’ ) ) 271 sideBoxPad . addLayout ( sideBox )
206 p l o t B t n . s e t I c o n S i z e ( QtCore . QSize ( 2 5 , 2 5 ) ) 272 sideBoxPad . a d d S t r e t c h ( 1 )
207 plotBtn . setFixedWidth (150) 273
208 plotBtn . setFixedHeight (32) 274 sideBoxSlider1 . addWidget ( s e l f . S l i d e r L a b e l 1 )
209 plotBtn . clicked . connect ( s e l f . plot histogram ) 275 sideBoxSlider1 . addWidget ( s l d 1 )
210 276 ’’’
211 # Undo 277 sideBoxSlider2 . addWidget ( s e l f . S l i d e r L a b e l 2 )
212 UndoBtn = QtGui . Q P u s h B u t t o n ( ) 278 sideBoxSlider2 . addWidget ( s l d 2 )
213 UndoBtn . s e t I c o n ( QtGui . QIcon ( ’ undo . png ’ ) ) 279
214 UndoBtn . s e t I c o n S i z e ( QtCore . QSize ( 2 6 , 2 6 ) ) 280 s i d e B o x S l i d e r 3 . addWidget ( s e l f . S l i d e r L a b e l 3 )
215 UndoBtn . s e t F i x e d W i d t h ( 5 2 ) 281 s i d e B o x S l i d e r 3 . addWidget ( s l d 3 )
216 UndoBtn . s e t F i x e d H e i g h t ( 3 2 ) 282 ’’’
217 UndoBtn . c l i c k e d . c o n n e c t ( s e l f . s h a r p e n ) 283 # Set grid layout
218 284 g r i d = QtGui . Q G r i d L a y o u t ( )
219 # Sliders 285 g r i d . a d d L a y o u t ( topBox , 0 , 1 )
220 s e l f . S l i d e r L a b e l 1 = QtGui . QLabel ( ’ S l i d e r 1 ’ ) 286 g r i d . addLayout ( sideBoxPad , 1 , 0)
221 s l d 1 = QtGui . Q S l i d e r ( QtCore . Qt . V e r t i c a l , 287 g r i d . a d d L a y o u t ( bottomBox , 1 , 1 )
self ) 288 s e l f . setLayout ( grid )
222 s l d 1 . s e t F o c u s P o l i c y ( QtCore . Qt . NoFocus ) 289
223 s l d 1 . setGeometry (30 , 40 , 100 , 50) 290 s e l f . setGeometry (300 , 300 , 800 , 600)
291 s e l f . s e t W i n d o w T i t l e ( ’ Image P r o c e s s i n g GUI ’ ) 355
292 s e l f . s e t W i n d o w I c o n ( QtGui . QIcon ( ” l o g o 1 . png ” ) ) 356 # D i a l o g pop−up box f o r g e t t i n g t h e v a l u e o f
293 b l u r i n d e x from u s e r
294 s e l f . show ( ) 357 def showDialog blur ( s e l f ) :
295 358 s e l f . l e = QtGui . Q L i n e E d i t ( s e l f )
296 # File Picker Function 359 t e x t , ok = QtGui . Q I n p u t D i a l o g . g e t T e x t ( s e l f ,
297 def get fname ( s e l f ) : ’ Blur ’ ,
298 ””” 360 ’ Enter the value of blur index : ’)
299 H a n d l e r c a l l e d when ’ c h o o s e f i l e ’ i s c l i c k e d 361 i f ok :
300 ””” 362 # Number e n t e r e d by u s e r s t o r e d i n ’ t e x t
301 # When you c a l l getOpenFileName , a f i l e ’
picker dialog is created 363 self . le . setText ( str ( text ) )
302 # and i f t h e u s e r s e l e c t s a f i l e , i t ’ s p a t h 364 p r i n t ( ” c o n t e n t s o f t e x t box : ”+ t e x t )
i s r e t u r n e d , and i f n o t 365 # Call the Blur o p e r a t i o n f u n c t i o n here
303 # ( i e , t h e u s e r c a n c e l s t h e o p e r a t i o n ) None 366 self . blur ( float ( text ) )
is returned 367
304 fname = QtGui . Q F i l e D i a l o g . g e t O p e n F i l e N a m e ( 368 # D i a l o g pop−up box f o r g e t t i n g t h e v a l u e o f
self , ’ Select f i l e ’ ) b l u r i n d e x from u s e r
305 s e l f . p r e v i o u s i m g = cv2 . i m r e a d ( fname [ 0 ] ) 369
306 p r i n t ( fname ) 370 # D i a l o g pop−up box f o r g e t t i n g t h e v a l u e o f
307 i f fname : c o n s t from u s e r
308 s e l f . f i l e L a b e l . s e t T e x t ( fname [ 0 ] ) 371 def showDialog log ( s e l f ) :
309 # Load name i s c a l l e d w i t h a r g u m e n t a s 372 s e l f . l e = QtGui . Q L i n e E d i t ( s e l f )
the f i l e ’ s location 373 t e x t , ok = QtGui . Q I n p u t D i a l o g . g e t T e x t ( s e l f ,
310 s e l f . l o a d i m a g e ( fname [ 0 ] ) ’ Log t r a n s f o r m ’ ,
311 else : 374 ’ Enter the value of c o n s t a n t for log
312 s e l f . f i l e L a b e l . s e t T e x t ( ”No f i l e s e l e c t e d tranformation : ’)
”) 375 i f ok :
313 376 # Number e n t e r e d by u s e r s t o r e d i n ’ t e x t
314 # Load new image f u n c t i o n ’
315 def load image ( s e l f , f i l e p a t h ) : 377 self . le . setText ( str ( text ) )
316 # Load t h e image i n t o t h e l a b e l 378 p r i n t ( ” c o n t e n t s o f t e x t box : ”+ t e x t )
317 p r i n t ( ” L o a d i n g Image ” ) 379 # Call the Blur o p e r a t i o n f u n c t i o n here
318 pixmap = QtGui . QPixmap ( f i l e p a t h ) 380 s e l f . log ( f l o a t ( t e x t ) )
319 s e l f . i m a g e L a b e l . s e t P i x m a p ( pixmap ) 381
320 s e l f . i m a g e L a b e l O r g . s e t P i x m a p ( pixmap ) 382 d e f showDialog mask ( s e l f ) :
321 s e l f . imageString = f i l e p a t h 383 msg = QtGui . QMessageBox ( )
322 384 msg . s e t I c o n ( QtGui . QMessageBox . I n f o r m a t i o n )
323 # Load new image f u n c t i o n 385
324 d e f s e t i m a g e ( s e l f , image ) : 386 msg . s e t T e x t ( ” Upload mask a s an image ” )
325 # Load t h e image i n t o t h e l a b e l 387 msg . s e t W i n d o w T i t l e ( ” F r e q u e n c y m a s k i n g ” )
326 s e l f . i m a g e L a b e l . s e t P i x m a p ( image ) 388 msg . s e t D e t a i l e d T e x t ( ” The image mask s h o u l d
327 i d e a l l y be i n g r e y s c a l e and o f t h e same s i z e a s
328 # Load n o r m a l image o f t h e o r i g i n a l image . T h i s image w i l l be u s e d
329 def normal image ( s e l f ) : a s a mask f o r f r e q u e n c y domain f i l t e r i n g o f t h e
330 # Load t h e image i n t o t h e l a b e l image . ” )
331 p r i n t ( ” L o a d i n g Image ” ) 389 msg . s e t S t a n d a r d B u t t o n s ( QtGui . QMessageBox . Ok
332 pixmap = QtGui . QPixmap ( s e l f . i m a g e S t r i n g ) | QtGui . QMessageBox . C a n c e l )
333 s e l f . i m a g e L a b e l . s e t P i x m a p ( pixmap ) 390 msg . b u t t o n C l i c k e d . c o n n e c t ( s e l f . u p l o a d i m a g e )
334 391
335 # F u n c t i o n t o s a v e image f i l e 392 r e t v a l = msg . e x e c ( )
336 def s a v e f i l e ( s e l f ) : 393
337 # Save a c t i v e i m a g e L a b e l t o image f i l e . 394 # S l i d e r Changed
338 path , = QtGui . Q F i l e D i a l o g . g e t S a v e F i l e N a m e ( 395 def change slider ( self , value ) :
s e l f , ” Save f i l e ” , ” ” , ”PNG Image f i l e ( ∗ . png ) ” ) 396 s e l f . sliderValue = value
339 397
340 i f path : 398 # D i s p l a y image on GUI
341 pixmap = s e l f . i m a g e L a b e l . pixmap ( ) 399 d e f show image ( s e l f , i m g r g b ) :
342 pixmap . s a v e ( p a t h , ”PNG” ) 400 imWidth = i m g r g b . s h a p e [ 1 ]
343 401 imHeight = img rgb . shape [ 0 ]
344 # D i a l o g pop−up box f o r g e t t i n g t h e v a l u e o f 402 newqim = QtGui . QImage ( imWidth , i m H e i g h t ,
gamma from u s e r QtGui . QImage . Format ARGB32 )
345 d e f showDialog gamma ( s e l f ) : 403 f o r x i n r a n g e ( 0 , i m H e i g h t −1) :
346 s e l f . l e = QtGui . Q L i n e E d i t ( s e l f ) 404 f o r y i n r a n g e ( 0 , imWidth −1) :
347 t e x t , ok = QtGui . Q I n p u t D i a l o g . g e t T e x t ( s e l f , 405 c o p i e d V a l u e = QtGui . qRgb ( i m g r g b [ x ] [
’Gamma C o r r e c t i o n ’ , y ] [ 0 ] , img rgb [ x ] [ y ] [ 1 ] , img rgb [ x ] [ y ] [ 2 ] )
348 ’ E n t e r t h e v a l u e o f gamma : ’) 406 # QImage u s e s p i x e l and s e t p i x e l
349 i f ok : 407 newqim . s e t P i x e l ( y , x , c o p i e d V a l u e )
350 # Number e n t e r e d by u s e r s t o r e d i n ’ t e x t 408 p i x = QtGui . QPixmap . fromImage ( newqim )
’ 409 s e l f . set image ( pix )
351 self . le . setText ( str ( text ) ) 410
352 p r i n t ( ” c o n t e n t s o f t e x t box : ”+ t e x t ) 411 # F u n c t i o n t o c o n v e r t Qimage t o CV2 a r r a y f o r
353 # C a l l t h e gamma c o r r e c t i o n f u n c t i o n optimized computations
here 412 d e f QImageToCVArr ( s e l f , i n c o m i n g I m a g e ) :
354 s e l f . gamma ( f l o a t ( t e x t ) )
413 incomingImage = incomingImage . 472 i m a g e p a d d e d [ k:−k −1,k:−k −1] = image
c o n v e r t T o F o r m a t ( QtGui . QImage . F o r m a t . Format RGB32 473 # Loop o v e r e v e r y p i x e l o f t h e image
) 474 f o r x i n r a n g e ( image . s h a p e [0] − k −1) :
414 width = incomingImage . width ( ) 475 f o r y i n r a n g e ( image . s h a p e [1] − k −1) :
415 h e i g h t = incomingImage . h e i g h t ( ) 476 # e l e m e n t −w i s e m u l t i p l i c a t i o n o f t h e
416 p t r = incomingImage . c o n s t B i t s ( ) k e r n e l and t h e image
417 a r r = np . a r r a y ( p t r ) . r e s h a p e ( h e i g h t , w i d t h , 477 s u b m a t r i x = i m a g e p a d d e d [ x : x+ k e r n e l .
4) # Copies the data s h a p e [ 1 ] , y : y+ k e r n e l . s h a p e [ 0 ] ]
418 return arr 478 # p r i n t ( submatrix . shape , k e r n e l .
419 shape , x , y , k )
420 # P l o t h i s t o g r a m o f t h e image 479 o u t p u t [ x , y ] = ( ( k e r n e l ∗ s u b m a t r i x ) . sum
421 def plot histogram ( s e l f ) : () )
422 # C o n v e r t image p r e s e n t on Image l a b e l on 480 return output
GUI t o CV2 a r r a y 481
423 pixmap = s e l f . i m a g e L a b e l . pixmap ( ) 482 # Compute t h e d i s c r e t e F o u r i e r T r a n s f o r m o f
424 qim = pixmap . t o I m a g e ( ) t h e 1D a r r a y x
425 image = s e l f . QImageToCVArr ( qim ) 483
426 484 # Normal 1−D DFT i m p l e m e n t a t i o n
427 # P lot ting the histogram 485 d e f DFT slow ( s e l f , x ) :
428 h i s t , b i n s = np . h i s t o g r a m ( image . f l a t t e n ( ) 486 x = np . a s a r r a y ( x , d t y p e = f l o a t )
,256 ,[0 ,256]) 487 N = x . shape [ 0 ]
429 c d f = h i s t . cumsum ( ) 488 n = np . a r a n g e (N)
430 c d f n o r m a l i z e d = c d f ∗ h i s t . max ( ) / c d f . max ( ) 489 k = n . r e s h a p e ( ( N, 1 ) )
431 p l t . plot ( cdf normalized , color = ’b ’ ) 490 M = np . exp (−2 j ∗ np . p i ∗ k ∗ n / N)
432 p l t . h i s t ( image . f l a t t e n ( ) , 2 5 6 , [ 0 , 2 5 6 ] , c o l o r 491 r e t u r n np . d o t (M, x )
= ’r ’) 492
433 p l t . xlim ( [ 0 , 2 5 6 ] ) 493 # A r e c u r s i v e i m p l e m e n t a t i o n o f t h e 1D Cooley−
434 p l t . t i t l e ( ’ H i s t o g r a m o f E q u a l i z e d image ’ ) Tukey FFT
435 p l t . legend ( ( ’ cdf ’ , ’ histogram ’ ) , loc = ’ 494 d e f FFT ( s e l f , x ) :
upper l e f t ’ ) 495 x = np . a s a r r a y ( x , d t y p e = f l o a t )
436 p l t . show ( ) 496 N = x . shape [ 0 ]
437 497
438 def upload image ( s e l f ) : 498 i f N % 2 > 0:
439 d i a l o g = QtGui . Q F i l e D i a l o g ( ) 499 r a i s e V a l u e E r r o r ( ” s i z e o f x must be a
440 fname = QtGui . Q F i l e D i a l o g . g e t O p e n F i l e N a m e ( power o f 2 ” )
self , ’ Select f i l e ’ ) 500 # 32 i s c h o s e n a r b i t r a r i l y and t h u s c a n be
441 p r i n t ( ” L o a d i n g Mask ” ) optimized .
442 image = cv2 . i m r e a d ( fname [ 0 ] , 0 ) 501 e l i f N <= 3 2 :
443 s e l f . mask ( image ) 502 r e t u r n s e l f . DFT slow ( x )
444 503 else :
445 # 504 # S p l i t t i n g i n even−odd s e q u e n c e s
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−a−c−c−o−r−d−i− n−g−−t−o−−Tukey−c o o l e y a l g o r i t m
505 X even = s e l f . FFT ( x [ : : 2 ] )
446 # H e l p e r math f u n c t i o n s 506 X odd = s e l f . FFT ( x [ 1 : : 2 ] )
447 # 507 f a c t o r = np . exp (−2 j ∗ np . p i ∗ np . a r a n g e (
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−N) −−−/−−N) −−−−−−−−
508 r e t u r n np . c o n c a t e n a t e ( [ X even + f a c t o r [ :
448 i n t (N / 2 ) ] ∗ X odd , X even + f a c t o r [ i n t (N / 2 ) : ] ∗
449 # Generate Gaussian matrix X odd ] )
450 d e f G a u s s i a n k e r n e l 2 d ( s e l f , s i z e , fwhm = 3 , 509
c e n t r e = ” none ” ) : 510 # I n v e r s e FFT
451 # Make a s q u a r e g a u s s i a n k e r n e l . 511 # ( 1 / N) ∗ c o n j ( DFT ( c o n j ( x ) ) ) = IDFT (X)
452 # s i z e i s t h e l e n g t h o f a s i d e o f t h e s q u a r e 512 d e f iFFT ( s e l f , y ) :
453 # fwhm i s f u l l −w i d t h−h a l f −maximum , which 513 Y = np . a s a r r a y ( y , d t y p e = f l o a t )
454 # c a n be t h o u g h t o f a s an e f f e c t i v e r a d i u s . 514 N = y . shape [ 0 ]
455 x = np . a r a n g e ( 0 , s i z e , 1 , f l o a t ) 515 conj Y = np . c o n j u g a t e (Y)
456 y = x [ : , np . n e w a x i s ] 516 i f f t = ( 1 / N) ∗ s e l f . FFT ( conj Y )
457 i f c e n t r e i s ” none ” : 517 return i f f t
458 x0 = i n t ( s i z e / 2 ) 518
459 y0 = i n t ( s i z e / 2 ) 519 # 2D I n v e r s e FFT : I m p l e m e n t a t i o n o f iFFT i n 2D
460 g = np . exp ( −(( x−x0 ) ∗∗2 + ( y−y0 ) ∗ ∗ 2 ) / fwhm i s a n a l o g o u s t o t h e 1D c a s e a s 2D−iDFT
∗ ∗ 2 ) / s i z e ∗∗2 520 # is separable transformation
461 return g 521 # Note t h a t t h e d i m e n s i o n s o f image h a v e t o be a
462 m u l t i p l e of 2 i n d i v i d u a l l y .
463 # V e c t o r i z e d 2D c o n v o l u t i o n 522 d e f iDFT 2d ( s e l f , i m a g e f f t ) :
464 d e f conv 2d ( s e l f , image , k e r n e l ) : 523 # I n i t i a l i z e IDFT i n m a t r i x form o f image
465 # T h i s f u n c t i o n which t a k e s an image and a 524 imWidth = i n t ( i m a g e f f t . s h a p e [ 1 ] )
kernel 525 imHeight = i n t ( i m a g e f f t . shape [ 0 ] )
466 # and r e t u r n s t h e c o n v o l u t i o n o f them . 526 image = np . z e r o s ( ( i m H e i g h t , imWidth ) )
467 k = i n t ( k e r n e l . shape [ 0 ] / 2) 527 # Row−w i s e IDFT c a l c u l a t i o n
468 o u t p u t = np . z e r o s l i k e ( image ) 528 f o r x in range ( imHeight ) :
469 # Convolution output 529 image [ x ] = s e l f . iFFT ( i m a g e f f t [ x ] )
470 # Add z e r o p a d d i n g t o t h e i n p u t image 530 # Column−w i s e IDFT c a l c u l a t i o n
471 i m a g e p a d d e d = np . z e r o s ( ( image . s h a p e [ 0 ] + k 531 i m a g e t = image . t r a n s p o s e ( )
∗2+1 , image . s h a p e [ 1 ] + k ∗2+1) ) 532 f o r y i n r a n g e ( imWidth ) :
533 i m a g e t [ y ] = s e l f . iFFT ( i m a g e t [ y ] ) 588 # C o n v e r t image d a t a from CV2 image i n t o
534 image = i m a g e t . t r a n s p o s e ( ) a QImage and d i s p l a y on GUI
535 r e t u r n image 589 s e l f . previous img = blur im
536 590 s e l f . show image ( b l u r i m )
537 # 591
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−592 −−−−−−−#−−H −− i s−t−
o−
g−
r−a−m−−E−q u a l i z a t i o n
593 def histo equ ( s e l f ) :
538 # Image P r o c e s s i n g F u n c t i o n s 594 i f s e l f . imageString :
539 # 595 print ( ” Processing ” )
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−596 −−−−−−−−−−−−−−−im −−− =−−cv2 . i m r e a d ( s e l f . i m a g e S t r i n g )
597 hsv im = cv2 . c v t C o l o r ( im , cv2 .
540 # Gamma T r a n s f o r m a t i o n COLOR BGR2HSV)
541 d e f gamma ( s e l f , gamma val = 1 . 0 ) : 598 h , s , v = cv2 . s p l i t ( hsv im )
542 i f s e l f . imageString : 599
543 print ( ” Processing ” ) 600 # Generating histogram r e p r e s e n t a t i o n of
544 img = cv2 . i m r e a d ( s e l f . i m a g e S t r i n g ) g i v e n image
545 invGamma = 1 . 0 / gamma val 601 h i s t , b i n s = np . h i s t o g r a m ( v . f l a t t e n ( )
546 t a b l e = np . a r r a y ( [ pow ( ( i / 2 5 5 . 0 ) , ,256 ,[0 ,256])
invGamma ) ∗ 255 f o r i i n np . a r a n g e ( 0 , 2 5 6 ) ] ) . 602 # Calculate cdf
astype ( ” uint8 ” ) 603 c d f = h i s t . cumsum ( )
547 604 # Cdf o f h i s t o g r a m and i t s n o r m a l i z a t i o n
548 # Gamma t r a n s f o r m a t i o n o p e r a t i o n a p p l i e d 605 c d f n o r m a l i z e d = c d f ∗ h i s t . max ( ) / c d f .
on v o f HSV o f g i v e n image max ( )
549 hsv img = cv2 . c v t C o l o r ( img , cv2 . 606 cdf m = np . ma . m a s k e d e q u a l ( c d f , 0 )
COLOR BGR2HSV) 607 cdf m = ( cdf m − cdf m . min ( ) ) ∗ 2 5 5 / ( cdf m
550 h , s , v = cv2 . s p l i t ( hsv img ) . max ( )−cdf m . min ( ) )
551 v gamma = cv2 . LUT( v , t a b l e ) 608 # C a l c u l a t e new c d f w i t h e q u a l i z e d
552 hsv gamma = cv2 . merge ( [ h , s , v gamma ] ) distribution
553 img gamma = cv2 . c v t C o l o r ( hsv gamma , cv2 . 609 c d f e q u a l i z e d = np . ma . f i l l e d ( cdf m , 0 ) .
COLOR HSV2BGR) astype ( ’ uint8 ’ )
554 610
555 # C o n v e r t image d a t a from CV2 image i n t o 611 # Apply t r a n s f o r m a t i o n
a QImage and d i s p l a y on GUI 612 i m g h i s t o e q = c d f e q u a l i z e d [ im ]
556 s e l f . show image ( img gamma ) 613
557 614 # D i s p l a y t h e h i s t o g r a m on GUI
558 # Log T r a n s f o r m a t i o n 615 s e l f . previous img = img histoeq
559 def log ( self , const = 1.0) : 616 s e l f . show image ( i m g h i s t o e q )
560 i f s e l f . imageString : 617 QtGui . QMessageBox . i n f o r m a t i o n ( QtGui .
561 print ( ” Processing ” ) QWidget ( ) , ” U s e f u l I n f o r m a t i o n ” , ” C l i c k t h e P l o t
562 img = cv2 . i m r e a d ( s e l f . i m a g e S t r i n g ) b u t t o n t o view h i s t o g r a m f o r e q u a l i z e d image . ” )
563 t a b l e = np . a r r a y ( [ 2 5 5 ∗ c o n s t ∗ math . l o g 1 0 ( i 618
+ 1 ) f o r i i n np . a r a n g e ( 0 , 2 5 6 ) ] ) . a s t y p e ( ” u i n t 8 619 # Edge D e t e c t
”) 620
564 621 # Edge d e t e c t i o n i n HSV
565 # Gamma transformation operation applied 622 def edge detect ( s e l f ) :
on v o f HSV o f g i v e n image 623 i f s e l f . imageString :
566 hsv img = cv2 . c v t C o l o r ( img , cv2 . 624 print ( ” Processing ” )
COLOR BGR2HSV) 625 im = cv2 . i m r e a d ( s e l f . i m a g e S t r i n g )
567 h,s ,v = cv2 . s p l i t ( hsv img ) 626
568 v log = cv2 . LUT( v , t a b l e ) 627 e d g e k e r n e l = np . a s a r r a y ([[ −1 , −1 , −1] ,
569 hsv log = cv2 . merge ( [ h , s , v l o g ] ) [ −1 ,8 , −1] , [ −1 , −1 , −1]])
570 img log = cv2 . c v t C o l o r ( h s v l o g , cv2 . 628
COLOR HSV2BGR) 629 # A p p l y i n g 2D c o n v o l u t i o n w i t h Edge
571 d e t e c t i o n k e r n e l on v o f HSV o f g i v e n image
572 # C o n v e r t image d a t a from CV2 image i n t o 630 hsv im = cv2 . c v t C o l o r ( im , cv2 .
a QImage and d i s p l a y on GUI COLOR BGR2HSV)
573 s e l f . show image ( i m g l o g ) 631 h , s , v = cv2 . s p l i t ( hsv im )
574 632 e d g e v = s e l f . conv 2d ( v , e d g e k e r n e l )
575 # Blur 633 hsv im = cv2 . merge ( [ h , s , e d g e v ] ) .
576 def blur ( self , blur index ) : astype ( ” uint8 ” )
577 i f s e l f . imageString : 634 edge im = cv2 . c v t C o l o r ( hsv im , cv2 .
578 print ( ” Processing ” ) COLOR HSV2BGR)
579 im = cv2 . i m r e a d ( s e l f . i m a g e S t r i n g ) 635
580 636 # C o n v e r t image d a t a from CV2 image i n t o
581 # A p p l y i n g 2D c o n v o l u t i o n w i t h G a u s s i a n a QImage and d i s p l a y on GUI
k e r n e l on v o f HSV o f g i v e n image 637 s e l f . p r e v i o u s i m g = edge im
582 hsv im = cv2 . c v t C o l o r ( im , cv2 . 638 s e l f . show image ( edge im )
COLOR BGR2HSV) 639
583 h , s , v = cv2 . s p l i t ( hsv im ) 640 # Sharpen
584 b l u r v = s e l f . conv 2d ( v , s e l f . 641
Gaussian kernel2d (5 , blur index ) ) 642 # Sharpening
585 hsv im = cv2 . merge ( [ h , s , b l u r v ] ) 643 def sharpen ( s e l f ) :
586 b l u r i m = cv2 . c v t C o l o r ( hsv im , cv2 . 644 i f s e l f . imageString :
COLOR HSV2BGR) 645 print ( ” Processing ” )
587 646 im = cv2 . i m r e a d ( s e l f . i m a g e S t r i n g )
647 702 i f s e l f . imageString :
648 # s h a r p e n k e r n e l = np . a s a r r a y 703 print ( ” Processing ” )
([[ −1 , −1 , −1] , [ −1 ,9 , −1] , [ −1 , −1 , −1]]) 704 im = cv2 . i m r e a d ( s e l f . i m a g e S t r i n g )
649 # Create the i d e n t i t y f i l t e r , but with 705 imWidth = i n t ( im . s h a p e [ 1 ] )
the 1 s h i f t e d to the r i g h t ! 706 i m H e i g h t = i n t ( im . s h a p e [ 0 ] )
650 k e r n e l = np . z e r o s ( ( 9 , 9 ) , np . f l o a t 3 2 ) 707 hsv im = cv2 . c v t C o l o r ( im , cv2 .
651 kernel [4 ,4] = 2.0 # I d e n t i t y , t i m e s two COLOR BGR2HSV)
! 708 h , s , v = cv2 . s p l i t ( hsv im )
652 709
653 # C r e a t e a box f i l t e r : 710 # Computing DFT i n m a t r i x form o f image
654 b o x F i l t e r = np . o n e s ( ( 9 , 9 ) , np . f l o a t 3 2 ) 711 Y = np . z e r o s ( ( i m H e i g h t , imWidth ) )
/ 81.0 712 # Row−w i s e DFT c a l c u l a t i o n
655 sharpen kernel = kernel − boxFilter 713 f o r x in range ( imHeight ) :
656 714 Y[ x ] = s e l f . FFT ( v [ x ] )
657 # A p p l y i n g 2D c o n v o l u t i o n w i t h 715 # Column−w i s e DFT c a l c u l a t i o n
S h a r p e n i n g k e r n e l on v o f HSV o f g i v e n image 716 Yt = Y . t r a n s p o s e ( )
658 hsv im = cv2 . c v t C o l o r ( im , cv2 . 717 f o r y i n r a n g e ( imWidth ) :
COLOR BGR2HSV) 718 Yt [ y ] = s e l f . FFT ( Yt [ y ] )
659 h , s , v = cv2 . s p l i t ( hsv im ) 719 Y = Yt . t r a n s p o s e ( )
660 s h a r p e n v = s e l f . conv 2d ( v , 720
sharpen kernel ) 721 # A p p l y i n g t h e mask g i v e n by u s e r on
661 hsv im = cv2 . merge ( [ h , s , s h a r p e n v ] ) . image
astype ( ” uint8 ” ) 722 H mult Y = np . m u l t i p l y (Y, f r e q m a s k )
662 s h a r p e n i m = cv2 . c v t C o l o r ( hsv im , cv2 . 723
COLOR HSV2BGR) 724 # Take iFFT o f t h e H ∗ Y t o g e t f i l t e r e d
663 output
664 # C o n v e r t image d a t a from CV2 image i n t o 725 f i l t e r e d v = s e l f . iDFT 2d ( H mult Y )
a QImage and d i s p l a y on GUI 726
665 s e l f . previous img = sharpen im 727 # Merging h , s , v
666 s e l f . show image ( s h a r p e n i m ) 728 hsv im = cv2 . merge ( [ h , s , f i l t e r e d v ] )
667 729 f i l t e r e d i m = cv2 . c v t C o l o r ( hsv im , cv2 .
668 # 2D DFT : I m p l e m e n t a t i o n o f FFT i n 2D i s COLOR HSV2BGR)
a n a l o g o u s t o t h e 1D c a s e a s 2D−DFT 730
669 # is separable transformation 731 # C o n v e r t image d a t a from CV2 image i n t o
670 # Note t h a t t h e d i m e n s i o n s o f image h a v e t o be a a QImage and d i s p l a y on GUI
m u l t i p l e of 2 i n d i v i d u a l l y . 732 s e l f . previous img = f i l t e r e d i m
671 d e f DFT 2d ( s e l f ) : 733 s e l f . show image ( f i l t e r e d i m )
672 i f s e l f . imageString : 734
673 print ( ” Processing ” ) 735 # I n v e r t i n g image
674 im = cv2 . i m r e a d ( s e l f . i m a g e S t r i n g ) 736 def i n v e r t ( s e l f ) :
675 imWidth = i n t ( im . s h a p e [ 1 ] ) 737 i f s e l f . imageString :
676 i m H e i g h t = i n t ( im . s h a p e [ 0 ] ) 738 print ( ” Processing ” )
677 hsv im = cv2 . c v t C o l o r ( im , cv2 . 739 im = cv2 . i m r e a d ( s e l f . i m a g e S t r i n g )
COLOR BGR2HSV) 740 imWidth = i n t ( im . s h a p e [ 1 ] )
678 h , s , v = cv2 . s p l i t ( hsv im ) 741 i m H e i g h t = i n t ( im . s h a p e [ 0 ] )
679 742 hsv im = cv2 . c v t C o l o r ( im , cv2 .
680 # I n i t i a l i z e DFT i n m a t r i x form o f image COLOR BGR2HSV)
681 # We h a v e u s e d s e p a r a b i l i t y p r o p e r t y o f 743 h , s , v = cv2 . s p l i t ( hsv im )
DFT o f a 2D m a t r i x s i n c e 744
682 # x and y d i r e c t i o n s a r e 745 f o r x s t e p i n r a n g e ( 0 , i m H e i g h t −1) :
independent . 746 f o r y s t e p i n r a n g e ( 0 , imWidth −1) :
683 Y = np . z e r o s ( ( i m H e i g h t , imWidth ) ) 747 pixelV = v [ xstep ] [ ystep ]
684 # Row−w i s e DFT c a l c u l a t i o n 748 p i x e l V = 255− p i x e l V
685 f o r x in range ( imHeight ) : 749 i f pixelV > 255:
686 Y[ x ] = s e l f . FFT ( v [ x ] ) 750 p i x e l V = 255
687 # Column−w i s e DFT c a l c u l a t i o n 751 i f pixelV < 0:
688 Yt = Y . t r a n s p o s e ( ) 752 pixelV = 0
689 f o r y i n r a n g e ( imWidth ) : 753 v [ xstep ] [ ystep ] = pixelV
690 Yt [ y ] = s e l f . FFT ( Yt [ y ] ) 754
691 Y = Yt . t r a n s p o s e ( ) 755 # Merging h , s , v
692 m a g n i t u d e s p e c t r u m = 20∗ np . l o g ( np . a b s (Y) 756 hsv im = cv2 . merge ( [ h , s , v ] )
) 757 i n v i m = cv2 . c v t C o l o r ( hsv im , cv2 .
693 p h a s e s p e c t r u m = np . a n g l e (Y) COLOR HSV2BGR)
694 p l t . s u b p l o t ( 1 2 1 ) , p l t . imshow ( 758 s e l f . previous img = inv im
m a g n i t u d e s p e c t r u m , cmap = ’ g r a y ’ ) 759 s e l f . show image ( i n v i m )
695 p l t . t i t l e ( ’ M a g n i t u d e S p e c t r u m ( Log s c a l e 760
) ’) , plt . xticks ([]) , plt . yticks ([]) 761 # Undo
696 p l t . s u b p l o t ( 1 2 2 ) , p l t . imshow ( 762 d e f undo ( s e l f ) :
p h a s e s p e c t r u m , cmap = ’ g r a y ’ ) 763 print (” testing ”)
697 p l t . t i t l e ( ’ P h a s e S p e c t r u m ( Log s c a l e ) ’ ) , 764
plt . xticks ([]) , plt . yticks ([]) 765
698 p l t . show ( ) 766
699 767 d e f main ( ) :
700 # Frequence masking u s i n g Gaussian F i l t e r 768
701 d e f mask ( s e l f , f r e q m a s k ) : 769 app = QtGui . Q A p p l i c a t i o n ( s y s . a r g v )
770 ex = Main IP Window ( )
771 s y s . e x i t ( app . e x e c ( ) )
772
773
774 if name == ’ main ’:
775 main ( )

R EFERENCES
[1] https://www.tutorialspoint.com/dip/histogram equalization.htm
[2] https://en.wikipedia.org/wiki/Histogram equalization
[3] http://studentnet.cs.manchester.ac.uk/resources/library/3rd-year-
projects/2014/matthew.akerman.pdf

You might also like