-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
104 lines (73 loc) · 2.62 KB
/
Copy pathtrain.py
File metadata and controls
104 lines (73 loc) · 2.62 KB
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
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 20 18:28:19 2020
@author: Ryan
"""
import numpy as np
from sklearn.metrics import confusion_matrix
from scipy.spatial.distance import cdist
from skimage.measure import label, regionprops, moments, moments_central, moments_normalized, moments_hu
from skimage import io, exposure
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import pickle
from scipy import ndimage
from PIL import Image, ImageEnhance
import skimage
from skimage.viewer import ImageViewer
import sys
def train(filename, Features, CharList):
#reading an image file
img = io.imread(filename);
#visualizing an image/matrix
#testing convolution
blurFilter = np.array([[1, 0, 0, 0, 0],
[0, 1, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 0],
[0, 0, 0, 0, 1]])
ndimage.convolve(img, blurFilter, img)
io.imshow(img)
plt.title('Original Image')
io.show()
#image histogram
hist = exposure.histogram(img)
plt.bar(hist[1], hist[0])
plt.title('Histogram')
plt.show()
#Binarization by Thresholding
th = 200
img_binary = (img < th).astype(np.double)
#displaying binary image
io.imshow(img_binary)
plt.title('Binary Image')
io.show()
#extracting characters and their features
img_label = label(img_binary, background = 0)
io.imshow(img_label)
plt.title('Labeled Image')
io.show()
#storing features
#Features=[]
#CharList = []
#displaying component bounding boxes
regions = regionprops(img_label)
io.imshow(img_binary)
ax = plt.gca()
for props in regions:
minr, minc, maxr, maxc = props.bbox
if (maxc - minc > 15) & (maxr - minr > 15) & (maxc - minc < 65) & (maxr - minr < 65):
ax.add_patch(Rectangle((minc-4, minr-4), maxc - minc+8, maxr - minr+8, fill = False, edgecolor = 'red', linewidth = 1))
#computing hu moments and removing small components
roi = img_binary[minr-4:maxr+4, minc-4:maxc+4]
m = moments(roi)
cc = m[0,1] / m[0,0]
cr = m[1, 0] / m[0, 0]
mu = moments_central(roi, center=(cr, cc))
nu = moments_normalized(mu)
hu = moments_hu(nu)
Features.append(hu)
CharList.append(filename[0])
ax.set_title('Bounding Boxes')
io.show()
#train(train)