質問編集履歴
5
エラーコードを修正しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -130,14 +130,20 @@
|
|
130
130
|
|
131
131
|
エラーの修正方法を教えてほしいです。
|
132
132
|
```python
|
133
|
-
>>> from poc import
|
133
|
+
>>> from poc import*
|
134
|
+
>>> import cv2
|
134
|
-
>>> img1
|
135
|
+
>>> img1=cv2.imread("lena.jpg",0)
|
135
|
-
>>> img2
|
136
|
+
>>> img2=cv2.imread("lena_x59_y16_7_27deg.jpg",0)
|
136
|
-
>>>
|
137
|
-
>>> ripoc(img1,
|
137
|
+
>>> ripoc(img1,img2)
|
138
138
|
Traceback (most recent call last):
|
139
139
|
File "<stdin>", line 1, in <module>
|
140
|
+
line 103, in ripoc
|
141
|
+
FLP = logpolar(F, (F.shape[0] / 2, F.shape[1] / 2), M)
|
142
|
+
line 17, in logpolar
|
143
|
+
mat1 = cv.fromarray(numpy.float64(src))
|
140
|
-
|
144
|
+
NameError: name 'cv' is not defined
|
141
|
-
gg = g * hw
|
142
|
-
TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'
|
143
|
-
```
|
145
|
+
```
|
146
|
+
|
147
|
+
というエラーが出ています。
|
148
|
+
cvとcv2の違いもいまいちわかっていないので何方か教えていただけないでしょうか
|
149
|
+
よろしく御願い致します。
|
4
エラーコードの修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -137,7 +137,7 @@
|
|
137
137
|
>>> ripoc(img1, img2)
|
138
138
|
Traceback (most recent call last):
|
139
139
|
File "<stdin>", line 1, in <module>
|
140
|
-
File "C:\
|
140
|
+
File "C:\poc.py", line 95, in ripoc
|
141
141
|
gg = g * hw
|
142
142
|
TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'
|
143
143
|
```
|
3
エラーを修正しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -123,10 +123,6 @@
|
|
123
123
|
|
124
124
|
return (t[1], t[2], angle, scale)
|
125
125
|
|
126
|
-
import poc
|
127
|
-
img1 = cv2.imread("lena.jpg", 0)
|
128
|
-
img2 = cv2.imread("lena_x59_y16_7.27deg.jpg", 0)
|
129
|
-
ripoc(img1, img2)
|
130
126
|
|
131
127
|
```
|
132
128
|
|
@@ -134,8 +130,14 @@
|
|
134
130
|
|
135
131
|
エラーの修正方法を教えてほしいです。
|
136
132
|
```python
|
133
|
+
>>> from poc import *
|
134
|
+
>>> img1 = cv2.imread("lena.jpg", 0)
|
135
|
+
>>> img2 = cv2.imread("lena_x59_y16_7.27deg.jpg", 0)
|
137
|
-
|
136
|
+
>>>
|
138
|
-
ripoc(img1, img2)
|
137
|
+
>>> ripoc(img1, img2)
|
138
|
+
Traceback (most recent call last):
|
139
|
+
File "<stdin>", line 1, in <module>
|
140
|
+
File "C:\Users\bb35116033\AppData\Local\Programs\Python\poc.py", line 95, in ripoc
|
139
141
|
gg = g * hw
|
140
142
|
TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'
|
141
143
|
```
|
2
プログラムを追加し、追加して新たに出たエラーコードを掲載しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,6 +3,135 @@
|
|
3
3
|
上のリンクを参考にpythonで回転不変位相限定相関を行っています。
|
4
4
|
上記のプログラム、上記の画像をそのまま利用して実行したのですが、エラーが出現してしまいます。
|
5
5
|
|
6
|
+
```python
|
7
|
+
#!/usr/bin/env python
|
8
|
+
# -*- coding: utf-8 -*-
|
9
|
+
import sys
|
10
|
+
|
11
|
+
import numpy
|
12
|
+
from numpy import pi, sin, cos
|
13
|
+
from scipy.optimize import leastsq
|
14
|
+
import scipy, scipy.fftpack
|
15
|
+
|
16
|
+
import cv2
|
17
|
+
|
18
|
+
|
19
|
+
import matplotlib.pyplot as plt
|
20
|
+
|
21
|
+
def logpolar(src, center, magnitude_scale = 40):
|
22
|
+
|
23
|
+
mat1 = cv.fromarray(numpy.float64(src))
|
24
|
+
mat2 = cv.CreateMat(src.shape[0], src.shape[1], mat1.type)
|
25
|
+
|
26
|
+
cv.LogPolar(mat1, mat2, center, magnitude_scale, \
|
27
|
+
cv.CV_INTER_CUBIC+cv.CV_WARP_FILL_OUTLIERS)
|
28
|
+
|
29
|
+
return numpy.asarray(mat2)
|
30
|
+
|
31
|
+
def zero_padding(src, dstshape, pos = (0, 0)):
|
32
|
+
y, x = pos
|
33
|
+
dst = numpy.zeros(dstshape)
|
34
|
+
dst[y:src.shape[0] + y, x:src.shape[1] + x] = src
|
35
|
+
return dst
|
36
|
+
|
37
|
+
def pocfunc_model(alpha, delta1, delta2, r, u):
|
38
|
+
N1, N2 = r.shape
|
39
|
+
V1, V2 = map(lambda x: 2 * x + 1, u)
|
40
|
+
return lambda n1, n2: alpha / (N1 * N2) * sin((n1 + delta1) * V1 / N1 * pi) * sin((n2 + delta2) * V2 / N2 * pi)\
|
41
|
+
/ (sin((n1 + delta1) * pi / N1) * sin((n2 + delta2) * pi / N2))
|
42
|
+
|
43
|
+
def pocfunc(f, g, windowfunc = numpy.hanning, withlpf = False):
|
44
|
+
m = numpy.floor(map(lambda x: x / 2.0, f.shape))
|
45
|
+
u = map(lambda x: x / 2.0, m)
|
46
|
+
|
47
|
+
# hanning window
|
48
|
+
hy = windowfunc(f.shape[0])
|
49
|
+
hx = windowfunc(f.shape[1])
|
50
|
+
hw = hy.reshape(hy.shape[0], 1) * hx
|
51
|
+
f = f * hw
|
52
|
+
g = g * hw
|
53
|
+
|
54
|
+
# compute 2d fft
|
55
|
+
F = scipy.fftpack.fft2(f)
|
56
|
+
G = scipy.fftpack.fft2(g)
|
57
|
+
G_ = numpy.conj(G)
|
58
|
+
R = F * G_ / numpy.abs(F * G_)
|
59
|
+
|
60
|
+
if withlpf == True:
|
61
|
+
R = scipy.fftpack.fftshift(R)
|
62
|
+
lpf = numpy.ones(map(lambda x: x + 1.0, m))
|
63
|
+
lpf = zero_padding(lpf, f.shape, u)
|
64
|
+
R = R * lpf
|
65
|
+
R = scipy.fftpack.fftshift(R)
|
66
|
+
|
67
|
+
return scipy.fftpack.fftshift(numpy.real(scipy.fftpack.ifft2(R)))
|
68
|
+
|
69
|
+
def poc(f, g, fitting_shape = (9, 9)):
|
70
|
+
# compute phase-only correlation
|
71
|
+
center = map(lambda x: x / 2.0, f.shape)
|
72
|
+
m = numpy.floor(map(lambda x: x / 2.0, f.shape))
|
73
|
+
u = map(lambda x: x / 2.0, m)
|
74
|
+
|
75
|
+
r = pocfunc(f, g)
|
76
|
+
|
77
|
+
# least-square fitting
|
78
|
+
max_pos = numpy.argmax(r)
|
79
|
+
peak = (max_pos / f.shape[1], max_pos % f.shape[1])
|
80
|
+
max_peak = r[peak[0], peak[1]]
|
81
|
+
|
82
|
+
mf = numpy.floor(map(lambda x: x / 2.0, fitting_shape))
|
83
|
+
fitting_area = r[peak[0] - mf[0] : peak[0] + mf[0] + 1,\
|
84
|
+
peak[1] - mf[1] : peak[1] + mf[1] + 1]
|
85
|
+
|
86
|
+
p0 = [0.5, -(peak[0] - m[0]) - 0.02, -(peak[1] - m[1]) - 0.02]
|
87
|
+
y, x = numpy.mgrid[-mf[0]:mf[0] + 1, -mf[1]:mf[1] + 1]
|
88
|
+
y = y + peak[0] - m[0]
|
89
|
+
x = x + peak[1] - m[1]
|
90
|
+
errorfunction = lambda p: numpy.ravel(pocfunc_model(p[0], p[1], p[2], r, u)(y, x) - fitting_area)
|
91
|
+
plsq = leastsq(errorfunction, p0)
|
92
|
+
return (plsq[0][0], plsq[0][1], plsq[0][2])
|
93
|
+
|
94
|
+
def ripoc(f, g, M = 50, fitting_shape = (9, 9)):
|
95
|
+
|
96
|
+
hy = numpy.hanning(f.shape[0])
|
97
|
+
hx = numpy.hanning(f.shape[1])
|
98
|
+
hw = hy.reshape(hy.shape[0], 1) * hx
|
99
|
+
|
100
|
+
ff = f * hw
|
101
|
+
gg = g * hw
|
102
|
+
|
103
|
+
F = scipy.fftpack.fft2(ff)
|
104
|
+
G = scipy.fftpack.fft2(gg)
|
105
|
+
|
106
|
+
F = scipy.fftpack.fftshift(numpy.log(numpy.abs(F)))
|
107
|
+
G = scipy.fftpack.fftshift(numpy.log(numpy.abs(G)))
|
108
|
+
|
109
|
+
FLP = logpolar(F, (F.shape[0] / 2, F.shape[1] / 2), M)
|
110
|
+
GLP = logpolar(G, (G.shape[0] / 2, G.shape[1] / 2), M)
|
111
|
+
|
112
|
+
R = poc(FLP, GLP)
|
113
|
+
|
114
|
+
angle = -R[1] / F.shape[0] * 360
|
115
|
+
scale = 1.0 - R[2] / 100
|
116
|
+
|
117
|
+
center = tuple(numpy.array(g.shape) / 2)
|
118
|
+
rot = cv2.getRotationMatrix2D(center, -angle, 1.0 + (1.0 - scale))
|
119
|
+
|
120
|
+
g_dash = cv2.warpAffine(g, rot, (g.shape[1], g.shape[0]), flags=cv2.INTER_LANCZOS4)
|
121
|
+
|
122
|
+
t = poc(f, g_dash)
|
123
|
+
|
124
|
+
return (t[1], t[2], angle, scale)
|
125
|
+
|
126
|
+
import poc
|
127
|
+
img1 = cv2.imread("lena.jpg", 0)
|
128
|
+
img2 = cv2.imread("lena_x59_y16_7.27deg.jpg", 0)
|
129
|
+
ripoc(img1, img2)
|
130
|
+
|
131
|
+
```
|
132
|
+
|
133
|
+
|
134
|
+
|
6
135
|
エラーの修正方法を教えてほしいです。
|
7
136
|
```python
|
8
137
|
import poc
|
1
エラーコードを修正しました・
title
CHANGED
File without changes
|
body
CHANGED
@@ -5,6 +5,8 @@
|
|
5
5
|
|
6
6
|
エラーの修正方法を教えてほしいです。
|
7
7
|
```python
|
8
|
+
import poc
|
8
|
-
|
9
|
+
ripoc(img1, img2)
|
10
|
+
gg = g * hw
|
9
|
-
|
11
|
+
TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'
|
10
12
|
```
|