質問編集履歴

5

エラーコードを修正しました。

2019/12/05 03:57

投稿

stylenanda
stylenanda

スコア10

test CHANGED
File without changes
test CHANGED
@@ -262,24 +262,36 @@
262
262
 
263
263
  ```python
264
264
 
265
- >>> from poc import *
265
+ >>> from poc import*
266
+
266
-
267
+ >>> import cv2
268
+
267
- >>> img1 = cv2.imread("lena.jpg", 0)
269
+ >>> img1=cv2.imread("lena.jpg",0)
268
-
270
+
269
- >>> img2 = cv2.imread("lena_x59_y16_7.27deg.jpg", 0)
271
+ >>> img2=cv2.imread("lena_x59_y16_7_27deg.jpg",0)
270
-
271
- >>>
272
+
272
-
273
- >>> ripoc(img1, img2)
273
+ >>> ripoc(img1,img2)
274
274
 
275
275
  Traceback (most recent call last):
276
276
 
277
277
  File "<stdin>", line 1, in <module>
278
278
 
279
+ line 103, in ripoc
280
+
281
+ FLP = logpolar(F, (F.shape[0] / 2, F.shape[1] / 2), M)
282
+
283
+ line 17, in logpolar
284
+
285
+ mat1 = cv.fromarray(numpy.float64(src))
286
+
279
- File "C:\poc.py", line 95, in ripoc
287
+ NameError: name 'cv' is not defined
280
-
281
- gg = g * hw
282
-
283
- TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'
284
288
 
285
289
  ```
290
+
291
+
292
+
293
+ というエラーが出ています。
294
+
295
+ cvとcv2の違いもいまいちわかっていないので何方か教えていただけないでしょうか
296
+
297
+ よろしく御願い致します。

4

エラーコードの修正

2019/12/05 03:57

投稿

stylenanda
stylenanda

スコア10

test CHANGED
File without changes
test CHANGED
@@ -276,7 +276,7 @@
276
276
 
277
277
  File "<stdin>", line 1, in <module>
278
278
 
279
- File "C:\Users\bb35116033\AppData\Local\Programs\Python\poc.py", line 95, in ripoc
279
+ File "C:\poc.py", line 95, in ripoc
280
280
 
281
281
  gg = g * hw
282
282
 

3

エラーを修正しました。

2019/12/03 04:06

投稿

stylenanda
stylenanda

スコア10

test CHANGED
File without changes
test CHANGED
@@ -248,14 +248,6 @@
248
248
 
249
249
 
250
250
 
251
- import poc
252
-
253
- img1 = cv2.imread("lena.jpg", 0)
254
-
255
- img2 = cv2.imread("lena_x59_y16_7.27deg.jpg", 0)
256
-
257
- ripoc(img1, img2)
258
-
259
251
 
260
252
 
261
253
  ```
@@ -270,9 +262,21 @@
270
262
 
271
263
  ```python
272
264
 
265
+ >>> from poc import *
266
+
267
+ >>> img1 = cv2.imread("lena.jpg", 0)
268
+
269
+ >>> img2 = cv2.imread("lena_x59_y16_7.27deg.jpg", 0)
270
+
273
- import poc
271
+ >>>
274
-
272
+
275
- ripoc(img1, img2)
273
+ >>> ripoc(img1, img2)
274
+
275
+ Traceback (most recent call last):
276
+
277
+ File "<stdin>", line 1, in <module>
278
+
279
+ File "C:\Users\bb35116033\AppData\Local\Programs\Python\poc.py", line 95, in ripoc
276
280
 
277
281
  gg = g * hw
278
282
 

2

プログラムを追加し、追加して新たに出たエラーコードを掲載しました。

2019/12/02 07:51

投稿

stylenanda
stylenanda

スコア10

test CHANGED
File without changes
test CHANGED
@@ -8,6 +8,264 @@
8
8
 
9
9
 
10
10
 
11
+ ```python
12
+
13
+ #!/usr/bin/env python
14
+
15
+ # -*- coding: utf-8 -*-
16
+
17
+ import sys
18
+
19
+
20
+
21
+ import numpy
22
+
23
+ from numpy import pi, sin, cos
24
+
25
+ from scipy.optimize import leastsq
26
+
27
+ import scipy, scipy.fftpack
28
+
29
+
30
+
31
+ import cv2
32
+
33
+
34
+
35
+
36
+
37
+ import matplotlib.pyplot as plt
38
+
39
+
40
+
41
+ def logpolar(src, center, magnitude_scale = 40):
42
+
43
+
44
+
45
+ mat1 = cv.fromarray(numpy.float64(src))
46
+
47
+ mat2 = cv.CreateMat(src.shape[0], src.shape[1], mat1.type)
48
+
49
+
50
+
51
+ cv.LogPolar(mat1, mat2, center, magnitude_scale, \
52
+
53
+ cv.CV_INTER_CUBIC+cv.CV_WARP_FILL_OUTLIERS)
54
+
55
+
56
+
57
+ return numpy.asarray(mat2)
58
+
59
+
60
+
61
+ def zero_padding(src, dstshape, pos = (0, 0)):
62
+
63
+ y, x = pos
64
+
65
+ dst = numpy.zeros(dstshape)
66
+
67
+ dst[y:src.shape[0] + y, x:src.shape[1] + x] = src
68
+
69
+ return dst
70
+
71
+
72
+
73
+ def pocfunc_model(alpha, delta1, delta2, r, u):
74
+
75
+ N1, N2 = r.shape
76
+
77
+ V1, V2 = map(lambda x: 2 * x + 1, u)
78
+
79
+ return lambda n1, n2: alpha / (N1 * N2) * sin((n1 + delta1) * V1 / N1 * pi) * sin((n2 + delta2) * V2 / N2 * pi)\
80
+
81
+ / (sin((n1 + delta1) * pi / N1) * sin((n2 + delta2) * pi / N2))
82
+
83
+
84
+
85
+ def pocfunc(f, g, windowfunc = numpy.hanning, withlpf = False):
86
+
87
+ m = numpy.floor(map(lambda x: x / 2.0, f.shape))
88
+
89
+ u = map(lambda x: x / 2.0, m)
90
+
91
+
92
+
93
+ # hanning window
94
+
95
+ hy = windowfunc(f.shape[0])
96
+
97
+ hx = windowfunc(f.shape[1])
98
+
99
+ hw = hy.reshape(hy.shape[0], 1) * hx
100
+
101
+ f = f * hw
102
+
103
+ g = g * hw
104
+
105
+
106
+
107
+ # compute 2d fft
108
+
109
+ F = scipy.fftpack.fft2(f)
110
+
111
+ G = scipy.fftpack.fft2(g)
112
+
113
+ G_ = numpy.conj(G)
114
+
115
+ R = F * G_ / numpy.abs(F * G_)
116
+
117
+
118
+
119
+ if withlpf == True:
120
+
121
+ R = scipy.fftpack.fftshift(R)
122
+
123
+ lpf = numpy.ones(map(lambda x: x + 1.0, m))
124
+
125
+ lpf = zero_padding(lpf, f.shape, u)
126
+
127
+ R = R * lpf
128
+
129
+ R = scipy.fftpack.fftshift(R)
130
+
131
+
132
+
133
+ return scipy.fftpack.fftshift(numpy.real(scipy.fftpack.ifft2(R)))
134
+
135
+
136
+
137
+ def poc(f, g, fitting_shape = (9, 9)):
138
+
139
+ # compute phase-only correlation
140
+
141
+ center = map(lambda x: x / 2.0, f.shape)
142
+
143
+ m = numpy.floor(map(lambda x: x / 2.0, f.shape))
144
+
145
+ u = map(lambda x: x / 2.0, m)
146
+
147
+
148
+
149
+ r = pocfunc(f, g)
150
+
151
+
152
+
153
+ # least-square fitting
154
+
155
+ max_pos = numpy.argmax(r)
156
+
157
+ peak = (max_pos / f.shape[1], max_pos % f.shape[1])
158
+
159
+ max_peak = r[peak[0], peak[1]]
160
+
161
+
162
+
163
+ mf = numpy.floor(map(lambda x: x / 2.0, fitting_shape))
164
+
165
+ fitting_area = r[peak[0] - mf[0] : peak[0] + mf[0] + 1,\
166
+
167
+ peak[1] - mf[1] : peak[1] + mf[1] + 1]
168
+
169
+
170
+
171
+ p0 = [0.5, -(peak[0] - m[0]) - 0.02, -(peak[1] - m[1]) - 0.02]
172
+
173
+ y, x = numpy.mgrid[-mf[0]:mf[0] + 1, -mf[1]:mf[1] + 1]
174
+
175
+ y = y + peak[0] - m[0]
176
+
177
+ x = x + peak[1] - m[1]
178
+
179
+ errorfunction = lambda p: numpy.ravel(pocfunc_model(p[0], p[1], p[2], r, u)(y, x) - fitting_area)
180
+
181
+ plsq = leastsq(errorfunction, p0)
182
+
183
+ return (plsq[0][0], plsq[0][1], plsq[0][2])
184
+
185
+
186
+
187
+ def ripoc(f, g, M = 50, fitting_shape = (9, 9)):
188
+
189
+
190
+
191
+ hy = numpy.hanning(f.shape[0])
192
+
193
+ hx = numpy.hanning(f.shape[1])
194
+
195
+ hw = hy.reshape(hy.shape[0], 1) * hx
196
+
197
+
198
+
199
+ ff = f * hw
200
+
201
+ gg = g * hw
202
+
203
+
204
+
205
+ F = scipy.fftpack.fft2(ff)
206
+
207
+ G = scipy.fftpack.fft2(gg)
208
+
209
+
210
+
211
+ F = scipy.fftpack.fftshift(numpy.log(numpy.abs(F)))
212
+
213
+ G = scipy.fftpack.fftshift(numpy.log(numpy.abs(G)))
214
+
215
+
216
+
217
+ FLP = logpolar(F, (F.shape[0] / 2, F.shape[1] / 2), M)
218
+
219
+ GLP = logpolar(G, (G.shape[0] / 2, G.shape[1] / 2), M)
220
+
221
+
222
+
223
+ R = poc(FLP, GLP)
224
+
225
+
226
+
227
+ angle = -R[1] / F.shape[0] * 360
228
+
229
+ scale = 1.0 - R[2] / 100
230
+
231
+
232
+
233
+ center = tuple(numpy.array(g.shape) / 2)
234
+
235
+ rot = cv2.getRotationMatrix2D(center, -angle, 1.0 + (1.0 - scale))
236
+
237
+
238
+
239
+ g_dash = cv2.warpAffine(g, rot, (g.shape[1], g.shape[0]), flags=cv2.INTER_LANCZOS4)
240
+
241
+
242
+
243
+ t = poc(f, g_dash)
244
+
245
+
246
+
247
+ return (t[1], t[2], angle, scale)
248
+
249
+
250
+
251
+ import poc
252
+
253
+ img1 = cv2.imread("lena.jpg", 0)
254
+
255
+ img2 = cv2.imread("lena_x59_y16_7.27deg.jpg", 0)
256
+
257
+ ripoc(img1, img2)
258
+
259
+
260
+
261
+ ```
262
+
263
+
264
+
265
+
266
+
267
+
268
+
11
269
  エラーの修正方法を教えてほしいです。
12
270
 
13
271
  ```python

1

エラーコードを修正しました・

2019/12/02 04:39

投稿

stylenanda
stylenanda

スコア10

test CHANGED
File without changes
test CHANGED
@@ -12,8 +12,12 @@
12
12
 
13
13
  ```python
14
14
 
15
- from poc import *
15
+ import poc
16
16
 
17
+ ripoc(img1, img2)
18
+
19
+ gg = g * hw
20
+
17
- ModuleNotFoundError: No module named 'poc'
21
+ TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'
18
22
 
19
23
  ```