質問編集履歴

1

for文を途中からしか記載していなかったので、全文入力しました。

2019/03/06 14:53

投稿

zuzu1984
zuzu1984

スコア31

test CHANGED
File without changes
test CHANGED
@@ -26,57 +26,67 @@
26
26
 
27
27
  ```python
28
28
 
29
+ picno = 1
30
+
29
- # -*^cording: utf-8 -*-
31
+ for i in range(5):
32
+
33
+ event_pic = testfld + '/Cut/988-3_no' + str(picno) + '.png'
34
+
35
+ print(picno)
30
36
 
31
37
 
32
38
 
33
- import cv2
39
+ #======================================
34
40
 
35
- import pyocr
41
+ # グレースケール
36
42
 
37
- import pyocr.builders
43
+ mov_in = cv2.imread(event_pic, cv2.IMREAD_GRAYSCALE) # 画像ファイrの読み込み
38
44
 
39
- import numpy as np
45
+ if mov_in is None:
40
46
 
41
- from matplotlib import pyplot as plt
47
+ print('ファイルが読み込めません')
42
48
 
43
- from PIL import Image, ImageFilter
49
+ import sys
44
50
 
45
- import time
51
+ sys.exit()
46
52
 
47
53
 
48
54
 
49
- import glob
55
+ cv2.imwrite(testfld + '/mov_in_gray.png', mov_in)
50
56
 
51
- import os
52
-
53
- import sys
57
+ cv2.waitKey(0)
54
-
55
- import win32com.client
56
-
57
- from datetime import datetime
58
58
 
59
59
 
60
60
 
61
+ #======================================
61
62
 
63
+ # 2値化
62
64
 
63
- #===============================================================================
65
+ img_gry = (testfld + '/mov_in_gray.png')
64
66
 
65
- testfld = 'C:/python/test/Cap'
67
+ org_img = cv2.imread(img_gry, 0)
66
68
 
67
69
 
68
70
 
71
+ THRESHOLD = 215 # 閾値はNumpyで調べた
72
+
69
- #===============================================================================
73
+ MAXVALUE = 255 # 255 = white
70
74
 
71
75
 
72
76
 
73
- #-------------------------------------------------------------------------------
77
+ _, bin_cv2 = cv2.threshold(org_img, THRESHOLD, MAXVALUE, cv2.THRESH_BINARY_INV)
74
78
 
75
- if __name__ == '__main__':
79
+ # [_INV]で背景白、黒文字にすることができる(Tesseractで読める絶対条件!)
76
80
 
77
81
 
78
82
 
83
+ bin_npy = np.zeros(org_img.shape, org_img.dtype)
84
+
85
+ bin_npy[np.where(org_img > THRESHOLD)] = MAXVALUE
86
+
87
+
88
+
79
- cv2.imwrite(testfld + '/binary_no' +str(picno)+ '.png', bin_cv2)
89
+ cv2.imwrite(testfld + '/binary_no' +str(picno)+ '.png', bin_cv2)
80
90
 
81
91
 
82
92
 
@@ -160,22 +170,4 @@
160
170
 
161
171
  text = [ev01, ev02, ev03, ev04, ev05]
162
172
 
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
173
  ```