commit | author | age
|
9efc18
|
1 |
import cv2 |
SP |
2 |
import numpy as np |
|
3 |
from skimage import morphology,img_as_ubyte |
02e0f7
|
4 |
from sklearn import svm |
SP |
5 |
from sklearn.externals import joblib |
|
6 |
|
|
7 |
|
9efc18
|
8 |
|
SP |
9 |
""" |
|
10 |
(1) The text is an array of chars (in row-major order) where |
|
11 |
* each char can be one of the following: |
|
12 |
* 'x': hit |
|
13 |
* 'o': miss |
|
14 |
* ' ': don't-care |
|
15 |
* (2) When the origin falls on a hit or miss, use an upper case |
|
16 |
* char (e.g., 'X' or 'O') to indicate it. When the origin |
|
17 |
* falls on a don't-care, indicate this with a 'C'. |
|
18 |
* The string must have exactly one origin specified. |
|
19 |
* (3) The advantage of this method is that the text can be input |
|
20 |
* in a format that shows the 2D layout of the Sel; e.g., |
|
21 |
|
|
22 |
|
|
23 |
:::: AND :::: |
|
24 |
|
|
25 |
|
|
26 |
(10) The sequence string is formatted as follows: |
|
27 |
* ~ An arbitrary number of operations, each separated |
|
28 |
* by a '+' character. White space is ignored. |
|
29 |
* ~ Each operation begins with a case-independent character |
|
30 |
* specifying the operation: |
|
31 |
* d or D (dilation) |
|
32 |
* e or E (erosion) |
|
33 |
* o or O (opening) |
|
34 |
* c or C (closing) |
|
35 |
* r or R (rank binary reduction) |
|
36 |
* x or X (replicative binary expansion) |
|
37 |
* b or B (add a border of 0 pixels of this size) |
|
38 |
* ~ The args to the morphological operations are bricks of hits, |
|
39 |
* and are formatted as a.b, where a and b are horizontal and |
|
40 |
* vertical dimensions, rsp. |
|
41 |
* ~ The args to the reduction are a sequence of up to 4 integers, |
|
42 |
* each from 1 to 4. |
|
43 |
* ~ The arg to the expansion is a power of two, in the set |
|
44 |
* {2, 4, 8, 16}. |
|
45 |
* (11) An example valid sequence is: |
|
46 |
* "b32 + o1.3 + C3.1 + r23 + e2.2 + D3.2 + X4" |
|
47 |
* In this example, the following operation sequence is carried out: |
|
48 |
* * b32: Add a 32 pixel border around the input image |
|
49 |
* * o1.3: Opening with vert sel of length 3 (e.g., 1 x 3) |
|
50 |
* * C3.1: Closing with horiz sel of length 3 (e.g., 3 x 1) |
|
51 |
* * r23: Two successive 2x2 reductions with rank 2 in the first |
|
52 |
* and rank 3 in the second. The result is a 4x reduced pix. |
|
53 |
* * e2.2: Erosion with a 2x2 sel (origin will be at x,y: 0,0) |
|
54 |
* * d3.2: Dilation with a 3x2 sel (origin will be at x,y: 1,0) |
|
55 |
* * X4: 4x replicative expansion, back to original resolution |
|
56 |
|
|
57 |
""" |
|
58 |
|
|
59 |
|
|
60 |
def kernel(x, y): |
|
61 |
return np.ones((x, y), np.uint8) |
|
62 |
|
|
63 |
|
02e0f7
|
64 |
def getSID(image, classifier): |
9efc18
|
65 |
image=255-image |
SP |
66 |
image=img_as_ubyte(image>100) |
|
67 |
cv2.imwrite("enSID0.png", image) |
|
68 |
# Remove noise |
|
69 |
image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel(2,2), iterations=1) |
|
70 |
# Closing. Connect non connected parts |
02e0f7
|
71 |
image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel(5, 3), iterations=4) |
9efc18
|
72 |
# Again noise removal after closing |
02e0f7
|
73 |
|
9efc18
|
74 |
image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel(8,8), iterations=1) |
SP |
75 |
# Skeletonization |
|
76 |
image = img_as_ubyte(morphology.thin(image>128)) |
|
77 |
cv2.imwrite("enSID1.png",image) |
|
78 |
# Stub removal (might not be necessary if thinning instead of skeletonize is used above |
|
79 |
# Making lines stronger |
|
80 |
image = cv2.morphologyEx(image, cv2.MORPH_DILATE, kernel(5, 5), iterations=1) |
|
81 |
|
|
82 |
image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel(10, 10)) |
|
83 |
# Thining again |
|
84 |
image = img_as_ubyte(morphology.skeletonize(image>0.5)) |
|
85 |
image = cv2.morphologyEx(image, cv2.MORPH_DILATE, kernel(10, 10)) |
02e0f7
|
86 |
|
SP |
87 |
im2,ctrs, hier = cv2.findContours(image.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) |
|
88 |
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0]) |
|
89 |
|
|
90 |
#classifier = joblib.load('filename.joblib') |
|
91 |
|
|
92 |
sid_no="" |
|
93 |
for i, ctr in enumerate(sorted_ctrs): |
|
94 |
# Get bounding box |
|
95 |
x, y, w, h = cv2.boundingRect(ctr) |
|
96 |
# Getting ROI |
|
97 |
if(w<h/2): |
|
98 |
sid_no=sid_no+"1" |
|
99 |
continue |
|
100 |
roi = image[y:y+h, x:x+w] |
|
101 |
roi = img_as_ubyte(roi < 128) |
|
102 |
roi = cv2.resize(roi,(32,32)) |
|
103 |
|
|
104 |
#cv2.rectangle(image,(x,y),( x + w, y + h ),(0,255,0),2) |
|
105 |
cv2.imwrite('sid_no_{}.png'.format(i), roi) |
|
106 |
sid_no=sid_no+str(classifier.predict(roi.reshape(1,-1)/255.0)[0]) |
|
107 |
print(sid_no) |
9efc18
|
108 |
return image |