-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
188 lines (134 loc) · 4.47 KB
/
utils.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
#!usr/bin/env python
# @Date: 2017-03-22
# UE14CS348 Digital Image Processing Mini Project
# Indian paper currency detection
# utils.py
# contains utility functions
import cv2
import math
import numpy as np
import matplotlib.pyplot as plt
from pprint import pprint
# read image as is
def read_img(file_name):
img = cv2.imread(file_name)
return img
# resize image with fixed aspect ratio
def resize_img(image, scale):
res = cv2.resize(image, None, fx=scale, fy=scale, interpolation = cv2.INTER_AREA)
return res
# convert image to grayscale
def img_to_gray(image):
img_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
return img_gray
# gaussian blurred grayscale
def img_to_gaussian_gray(image):
img_gray = cv2.GaussianBlur(img_to_gray(image), (5, 5), 0)
return img_gray
# convert image to negative
def img_to_neg(image):
img_neg = 255 - image
return img_neg
# binarize (threshold)
# retval not used currently
def binary_thresh(image, threshold):
retval, img_thresh = cv2.threshold(image, threshold, 255, cv2.THRESH_BINARY)
return img_thresh
# NO IDEA HOW THIS WPRKS
def adaptive_thresh(image):
img_thresh = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 8)
# cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst]) → dsta
return img_thresh
# sobel edge operator
def sobel_edge(image, align):
img_horiz = cv2.Sobel(image, cv2.CV_8U, 0, 1)
img_vert = cv2.Sobel(image, cv2.CV_8U, 1, 0)
if align == 'h':
return img_horiz
elif align == 'v':
return img_vert
else:
print('use h or v')
# sobel edge x + y
def sobel_edge2(image):
# ksize = size of extended sobel kernel
grad_x = cv2.Sobel(image, cv2.CV_16S, 1, 0, ksize=3, borderType = cv2.BORDER_DEFAULT)
grad_y = cv2.Sobel(image, cv2.CV_16S, 0, 1, ksize=3, borderType = cv2.BORDER_DEFAULT)
abs_grad_x = cv2.convertScaleAbs(grad_x)
abs_grad_y = cv2.convertScaleAbs(grad_y)
dst = cv2.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)
return dst
# canny edge operator
def canny_edge(image, block_size, ksize):
# block_size => Neighborhood size
# ksize => Aperture parameter for the Sobel operator
# 350, 350 => for smaller 500
# 720, 350 => Devnagari 500, Reserve bank of India
img = cv2.Canny(image, block_size, ksize)
# dilate to fill up the numbers
#img = cv2.dilate(img, None)
return img
# laplacian edge
def laplacian_edge(image):
# good for text
img = cv2.Laplacian(image, cv2.CV_8U)
return img
# detect countours
def find_contours(image):
(_, contours, _) = cv2.findContours(image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key = cv2.contourArea, reverse = True)[:5]
return contours
# median blur
def median_blur(image):
blurred_img = cv2.medianBlur(image, 3)
return blurred_img
# dialte image to close lines
def dilate_img(image):
img = cv2.dilate(image, np.ones((5,5), np.uint8))
return img
# erode image
def close(image):
img = cv2.Canny(image, 75, 300)
img = cv2.dilate(img, None)
img = cv2.erode(img, None)
return img
def harris_edge(image):
img_gray = np.float32(image)
corners = cv2.goodFeaturesToTrack(img_gray, 4, 0.03, 200, None, None, 2,useHarrisDetector=True, k=0.04)
corners = np.int0(corners)
for corner in corners:
x, y = corner.ravel()
cv2.circle(image, (x, y), 3, 255, -1)
return image
# calculate histogram
def histogram(image):
hist = cv2.calcHist([image], [0], None, [256], [0, 256])
# cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]])
plt.plot(hist)
plt.show()
# fast fourier transform
def fourier(image):
f = np.fft.fft2(image)
fshift = np.fft.fftshift(f)
magnitude_spectrum = 20 * np.log(np.abs(fshift))
plt.subplot(121), plt.imshow(image, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(magnitude_spectrum, cmap='gray')
plt.title('FFT'), plt.xticks([]), plt.yticks([])
plt.show()
# calculate scale and fit into display
def display(window_name, image):
screen_res = 1440, 900 # MacBook Air
scale_width = screen_res[0] / image.shape[1]
scale_height = screen_res[1] / image.shape[0]
scale = min(scale_width, scale_height)
window_width = int(image.shape[1] * scale)
window_height = int(image.shape[0] * scale)
# reescale the resolution of the window
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.resizeWindow(window_name, window_width, window_height)
# display image
cv2.imshow(window_name, image)
# wait for any key to quit the program
cv2.waitKey(0)
cv2.destroyAllWindows()