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