質問編集履歴

2

コードの追加など

2019/12/27 06:54

投稿

reonald
reonald

スコア32

test CHANGED
File without changes
test CHANGED
@@ -86,4 +86,28 @@
86
86
 
87
87
 
88
88
 
89
+ 現状下記のエラーが発生しております。
90
+
91
+ 0 78.85875706214689
92
+
93
+ 1 78.33333333333333
94
+
95
+ 2 91.95197740112994
96
+
97
+ 3 80.79943502824858
98
+
99
+ 4 79.16666666666667
100
+
101
+ Traceback (most recent call last):
102
+
103
+ File "match2.py", line 37, in <module>
104
+
105
+ save_path = save_path % small_index
106
+
107
+ TypeError: not all arguments converted during string formatting
108
+
109
+
110
+
111
+
112
+
89
113
  どなたかよろしくお願いします。

1

コードの追加など

2019/12/27 06:54

投稿

reonald
reonald

スコア32

test CHANGED
File without changes
test CHANGED
@@ -1,22 +1,88 @@
1
- インデックスの値を保存するファイル名に反映させたいのですが,
1
+ 得られたインデックスの値を保存するファイル名に反映させたいのですが,
2
2
 
3
3
  ```
4
4
 
5
+ import cv2
6
+
7
+ import os
8
+
5
- small_index=rets.index(min(rets))
9
+ import glob
6
10
 
7
11
 
8
12
 
9
- save_path = './match/./b/./small_index/b_%d.jpg'
10
13
 
14
+
15
+ IMG_SIZE = (200, 200)
16
+
17
+ files = glob.glob("./match/./pura/*")
18
+
19
+ for i, f in enumerate(files):
20
+
21
+ target_img1 = cv2.imread(f)
22
+
23
+ target_img = cv2.cvtColor(target_img1 , cv2.COLOR_BGR2GRAY)
24
+
25
+ target_img = cv2.resize(target_img, IMG_SIZE)
26
+
27
+
28
+
29
+ bf = cv2.BFMatcher(cv2.NORM_HAMMING)
30
+
31
+
32
+
33
+ detector = cv2.ORB_create()
34
+
35
+ (target_kp, target_des) = detector.detectAndCompute(target_img, None)
36
+
37
+
38
+
39
+ rets=[]
40
+
41
+ files = glob.glob("./match/./1/*")
42
+
43
+ for j, f in enumerate(files):
44
+
45
+ img = cv2.imread(f)
46
+
47
+ img = cv2.resize(img, IMG_SIZE)
48
+
49
+ gray = cv2.cvtColor(img, cv2.IMREAD_GRAYSCALE)
50
+
51
+ (comparing_kp, comparing_des) = detector.detectAndCompute(gray, None)
52
+
53
+ matches = bf.match(target_des, comparing_des)
54
+
55
+ dist = [m.distance for m in matches]
56
+
57
+ ret = sum(dist) / len(dist)
58
+
59
+ print(j, ret)
60
+
61
+ rets.append(ret)
62
+
63
+
64
+
65
+ small=min(rets)
66
+
67
+ small_index=rets.index(min(rets))
68
+
69
+
70
+
71
+ save_path = './match/./b/./small_index/_%d.jpg'%i
72
+
73
+ save_path = save_path % small_index
74
+
75
+ print(save_path)
76
+
11
- cv2.imwrite(save_path, target_img1)
77
+ cv2.imwrite(save_path, target_img1)
12
78
 
13
79
  コード
14
80
 
15
81
  ```
16
82
 
17
- retsのリストに4個の数値が格納されており,この中の最小の数値のインデックスの値で保存先のファイルを決めたいです。
83
+ retsのリストに5個の数値が格納されており,この中の最小の数値のインデックスの値で保存先のファイルを決めたいです。
18
84
 
19
- match-bの中に0,1,2,3の4つのファイルを作っており,この4のどれかに保存という流れです。
85
+ match-bの中に0,1,2,3,45つのファイルを作っており,この5個のどれかに保存という流れです。
20
86
 
21
87
 
22
88