File size: 12,190 Bytes
c9019cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
""" Calculates skew angle """
"""
This code is based on the following file:
https://github.com/kakul/Alyn/blob/master/alyn/skew_detect.py
"""
import os
import optparse

import numpy as np
# import matplotlib.pyplot as plt
from skimage import io
from skimage.feature import canny
from skimage.transform import hough_line, hough_line_peaks
import cv2


class SkewDetect:

    piby4 = np.pi / 4

    def __init__(
        self,
        input_file=None,
        output_file=None,
        sigma=0.50,
        display_output=None,
        num_peaks=20,
        skew_max=4.0,
        acc_deg=0.5,
        roi_w=1.0,
        roi_h=1.0,
    ):

        self.sigma = sigma
        self.input_file = input_file
        self.output_file = output_file
        self.display_output = display_output
        self.num_peaks = num_peaks
        self.skew_max = skew_max
        self.acc_deg = acc_deg
        self.roi_w = roi_w
        self.roi_h = roi_h

    def write_to_file(self, wfile, data):

        for d in data:
            wfile.write(d + ': ' + str(data[d]) + '\n')
        wfile.write('\n')

    def get_max_freq_elem(self, arr):

        max_arr = []
        freqs = {}
        for i in arr:
            if i in freqs:
                freqs[i] += 1
            else:
                freqs[i] = 1

        sorted_keys = sorted(freqs, key=freqs.get, reverse=True)
        max_freq = freqs[sorted_keys[0]]

        for k in sorted_keys:
            if freqs[k] == max_freq:
                max_arr.append(k)

        return max_arr

    def compare_sum(self, value):
        if value >= 44 and value <= 46:
            return True
        else:
            return False

    def display(self, data):

        for i in data:
            print(str(i) + ": " + str(data[i]))

    def calculate_deviation(self, angle):

        angle_in_degrees = np.abs(angle)
        deviation = np.abs(SkewDetect.piby4 - angle_in_degrees)

        return deviation

    def run(self):

        if self.display_output:
            if self.display_output.lower() == 'yes':
                self.display_output = True
            else:
                self.display_output = False

        if self.input_file is None:
            print("Invalid input, nothing to process.")
        else:
            self.process_single_file()

    def check_path(self, path):

        if os.path.isabs(path):
            full_path = path
        else:
            full_path = os.getcwd() + '/' + str(path)
        return full_path

    def process_single_file(self):

        file_path = self.check_path(self.input_file)
        res = self.determine_skew(file_path)

        if self.output_file:
            output_path = self.check_path(self.output_file)
            wfile = open(output_path, 'w')
            self.write_to_file(wfile, res)
            wfile.close()

        return res

    def determine_skew(self, img_file):

        img_ori = io.imread(img_file, as_gray=True)
        height, width = img_ori.shape
        img = img_ori[int(height*(0.5-self.roi_h/2.0)):int(height*(0.5+self.roi_h/2.0)),
                      int(width * (0.5-self.roi_w/2.0)):int(width * (0.5+self.roi_w/2.0))]

        img = cv2.resize(img, (img.shape[1]//4, img.shape[0]//4))

        edges = canny(img, sigma=self.sigma)
        range_rad = np.arange(-np.pi/2, -np.pi/2+np.deg2rad(self.skew_max),
                              step=np.deg2rad(self.acc_deg))
        range_rad = np.concatenate(
            [range_rad,
             np.arange(-np.deg2rad(self.skew_max), np.deg2rad(self.skew_max),
                       step=np.deg2rad(self.acc_deg))],
            axis=0)
        range_rad = np.concatenate(
            [range_rad,
             np.arange(np.pi/2-np.deg2rad(self.skew_max), np.pi/2,
                       step=np.deg2rad(self.acc_deg))],
            axis=0)

        h, a, d = hough_line(edges, theta=range_rad)

        th = 0.2 * h.max()
        _, ap, _ = hough_line_peaks(
            h, a, d, threshold=th, num_peaks=self.num_peaks)

        if len(ap) == 0:
            data = {
                "Image File": img_file,
                "Average Deviation from pi/4": 0.0,
                "Estimated Angle": 0.0,
                "Angle bins": [[], [], [], []],
                "Message": "Bad Quality"}
            return data

        absolute_deviations = [self.calculate_deviation(k) for k in ap]
        average_deviation = np.mean(np.rad2deg(absolute_deviations))
        ap_deg = [np.rad2deg(x) for x in ap]

        for i in range(len(ap_deg)):
            if ap_deg[i] >= 45.0:
                ap_deg[i] -= 90.0
            elif ap_deg[i] <= -45.0:
                ap_deg[i] += 90.0

        bin_0_45 = []
        bin_45_90 = []
        bin_0_45n = []
        bin_45_90n = []

        for ang in ap_deg:

            deviation_sum = (90 - ang + average_deviation)
            if self.compare_sum(deviation_sum):
                bin_45_90.append(ang)
                continue

            deviation_sum = (ang + average_deviation)
            if self.compare_sum(deviation_sum):
                bin_0_45.append(ang)
                continue

            deviation_sum = (-ang + average_deviation)
            if self.compare_sum(deviation_sum):
                bin_0_45n.append(ang)
                continue

            deviation_sum = (90 + ang + average_deviation)
            if self.compare_sum(deviation_sum):
                bin_45_90n.append(ang)

        angles = [bin_0_45, bin_45_90, bin_0_45n, bin_45_90n]
        lmax = 0

        for j in range(len(angles)):
            tmp_l = len(angles[j])
            if tmp_l > lmax:
                lmax = tmp_l
                maxi = j

        if lmax:
            ans_arr = self.get_max_freq_elem(angles[maxi])  # 最多頻度の角度array
            ans_res = np.mean(ans_arr)    # 同数最多が複数あるかもしれないのでavg

        else:  # angls が空のとき
            ans_arr = self.get_max_freq_elem(ap_deg)
            ans_res = np.mean(ans_arr)

        data = {
            "Image File": img_file,
            "Average Deviation from pi/4": average_deviation,
            "Estimated Angle": ans_res,
            "Angle bins": angles,
            "Message": "Successfully detected lines"}

        if self.display_output:
            self.display(data)

        return data

    def determine_skew_on_memory(self, img_data):

        img_ori = cv2.cvtColor(img_data, cv2.COLOR_BGR2GRAY)
        height, width = img_ori.shape
        img = img_ori[int(height*(0.5-self.roi_h/2.0)):int(height*(0.5+self.roi_h/2.0)),
                      int(width * (0.5-self.roi_w/2.0)):int(width * (0.5+self.roi_w/2.0))]

        img = cv2.resize(img, (img.shape[1]//4, img.shape[0]//4))

        edges = canny(img, sigma=self.sigma)
        range_rad = np.arange(-np.pi/2, -np.pi/2+np.deg2rad(self.skew_max),
                              step=np.deg2rad(self.acc_deg))
        range_rad = np.concatenate([range_rad,
                                    np.arange(-np.deg2rad(self.skew_max),
                                              np.deg2rad(self.skew_max),
                                              step=np.deg2rad(self.acc_deg))],
                                   axis=0)
        range_rad = np.concatenate([range_rad,
                                    np.arange(np.pi/2-np.deg2rad(self.skew_max),
                                              np.pi/2,
                                              step=np.deg2rad(self.acc_deg))],
                                   axis=0)

        h, a, d = hough_line(edges, theta=range_rad)

        th = 0.2 * h.max()
        _, ap, _ = hough_line_peaks(
            h, a, d, threshold=th, num_peaks=self.num_peaks)

        if len(ap) == 0:
            data = {
                "Average Deviation from pi/4": 0.0,
                "Estimated Angle": 0.0,
                "Angle bins": [[], [], [], []],
                "Message": "Bad Quality"}
            return data

        absolute_deviations = [self.calculate_deviation(k) for k in ap]
        average_deviation = np.mean(np.rad2deg(absolute_deviations))
        ap_deg = [np.rad2deg(x) for x in ap]

        for i in range(len(ap_deg)):
            if ap_deg[i] >= 45.0:
                ap_deg[i] -= 90.0
            elif ap_deg[i] <= -45.0:
                ap_deg[i] += 90.0

        bin_0_45 = []
        bin_45_90 = []
        bin_0_45n = []
        bin_45_90n = []

        for ang in ap_deg:

            deviation_sum = (90 - ang + average_deviation)
            if self.compare_sum(deviation_sum):
                bin_45_90.append(ang)
                continue

            deviation_sum = (ang + average_deviation)
            if self.compare_sum(deviation_sum):
                bin_0_45.append(ang)
                continue

            deviation_sum = (-ang + average_deviation)
            if self.compare_sum(deviation_sum):
                bin_0_45n.append(ang)
                continue

            deviation_sum = (90 + ang + average_deviation)
            if self.compare_sum(deviation_sum):
                bin_45_90n.append(ang)

        angles = [bin_0_45, bin_45_90, bin_0_45n, bin_45_90n]
        lmax = 0

        for j in range(len(angles)):
            tmp_l = len(angles[j])
            if tmp_l > lmax:
                lmax = tmp_l
                maxi = j

        if lmax:
            ans_arr = self.get_max_freq_elem(angles[maxi])  # 最多頻度の角度array
            ans_res = np.mean(ans_arr)    # 同数最多が複数あるかもしれないのでavg

        else:  # angls が空のとき
            ans_arr = self.get_max_freq_elem(ap_deg)
            ans_res = np.mean(ans_arr)

        data = {
            "Average Deviation from pi/4": average_deviation,
            "Estimated Angle": ans_res,
            "Angle bins": angles,
            "Message": "Successfully detected lines"}

        return data

    def determine_line(self, img_file):

        img_ori = io.imread(img_file, as_gray=True)
        height, width = img_ori.shape
        img = img_ori[int(height*(0.5-self.roi_h/2.0)):int(height*(0.5+self.roi_h/2.0)),
                      int(width * (0.5-self.roi_w/2.0)):int(width * (0.5+self.roi_w/2.0))]
        edges = canny(img, sigma=self.sigma)
        range_rad = np.arange(-np.pi/2, -np.pi/2+np.deg2rad(self.skew_max),
                              step=np.deg2rad(self.acc_deg))
        range_rad = np.concatenate([range_rad,
                                    np.arange(-np.deg2rad(self.skew_max),
                                              np.deg2rad(self.skew_max),
                                              step=np.deg2rad(self.acc_deg))],
                                   axis=0)
        range_rad = np.concatenate([range_rad,
                                    np.arange(np.pi/2-np.deg2rad(self.skew_max), np.pi/2,
                                              step=np.deg2rad(self.acc_deg))],
                                   axis=0)

        h, a, d = hough_line(edges, theta=range_rad)

        th = 0.2 * h.max()
        ac, ap, d = hough_line_peaks(
            h, a, d, threshold=th, num_peaks=self.num_peaks)

        return ac, ap, d


if __name__ == '__main__':

    parser = optparse.OptionParser()

    parser.add_option(
        '-d', '--display',
        default=None,
        dest='display_output',
        help='Display logs')
    parser.add_option(
        '-i', '--input',
        default=None,
        dest='input_file',
        help='Input file name')
    parser.add_option(
        '-o', '--output',
        default=None,
        dest='output_file',
        help='Output file name')
    parser.add_option(
        '-p', '--plot',
        default=None,
        dest='plot_hough',
        help='Plot the Hough Transform')
    parser.add_option(
        '-s', '--sigma',
        default=3.0,
        dest='sigma',
        help='Sigma for Canny Edge Detection',
        type=float)
    options, args = parser.parse_args()
    skew_obj = SkewDetect(
        options.input_file,
        options.output_file,
        options.sigma,
        options.display_output,
        options.num_peaks,
        options.plot_hough)
    skew_obj.run()