本文整理匯總了Python中cv2.mulSpectrums方法的典型用法代碼示例。如果您正苦於以下問題:Python cv2.mulSpectrums方法的具體用法?Python cv2.mulSpectrums怎麽用?Python cv2.mulSpectrums使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類cv2
的用法示例。
在下文中一共展示了cv2.mulSpectrums方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: update
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import mulSpectrums [as 別名]
def update(_):
ang = np.deg2rad( cv2.getTrackbarPos('angle', win) )
d = cv2.getTrackbarPos('d', win)
noise = 10**(-0.1*cv2.getTrackbarPos('SNR (db)', win))
if defocus:
psf = defocus_kernel(d)
else:
psf = motion_kernel(ang, d)
cv2.imshow('psf', psf)
psf /= psf.sum()
psf_pad = np.zeros_like(img)
kh, kw = psf.shape
psf_pad[:kh, :kw] = psf
PSF = cv2.dft(psf_pad, flags=cv2.DFT_COMPLEX_OUTPUT, nonzeroRows = kh)
PSF2 = (PSF**2).sum(-1)
iPSF = PSF / (PSF2 + noise)[...,np.newaxis]
RES = cv2.mulSpectrums(IMG, iPSF, 0)
res = cv2.idft(RES, flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT )
res = np.roll(res, -kh//2, 0)
res = np.roll(res, -kw//2, 1)
cv2.imshow(win, res)
示例2: __init__
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import mulSpectrums [as 別名]
def __init__(self, frame, rect):
x1, y1, x2, y2 = rect
w, h = map(cv2.getOptimalDFTSize, [x2-x1, y2-y1])
x1, y1 = (x1+x2-w)//2, (y1+y2-h)//2
self.pos = x, y = x1+0.5*(w-1), y1+0.5*(h-1)
self.size = w, h
img = cv2.getRectSubPix(frame, (w, h), (x, y))
self.win = cv2.createHanningWindow((w, h), cv2.CV_32F)
g = np.zeros((h, w), np.float32)
g[h//2, w//2] = 1
g = cv2.GaussianBlur(g, (-1, -1), 2.0)
g /= g.max()
self.G = cv2.dft(g, flags=cv2.DFT_COMPLEX_OUTPUT)
self.H1 = np.zeros_like(self.G)
self.H2 = np.zeros_like(self.G)
for i in xrange(128):
a = self.preprocess(rnd_warp(img))
A = cv2.dft(a, flags=cv2.DFT_COMPLEX_OUTPUT)
self.H1 += cv2.mulSpectrums(self.G, A, 0, conjB=True)
self.H2 += cv2.mulSpectrums( A, A, 0, conjB=True)
self.update_kernel()
self.update(frame)
示例3: update
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import mulSpectrums [as 別名]
def update(self, frame, rate = 0.125):
(x, y), (w, h) = self.pos, self.size
self.last_img = img = cv2.getRectSubPix(frame, (w, h), (x, y))
img = self.preprocess(img)
self.last_resp, (dx, dy), self.psr = self.correlate(img)
self.good = self.psr > 8.0
if not self.good:
return
self.pos = x+dx, y+dy
self.last_img = img = cv2.getRectSubPix(frame, (w, h), self.pos)
img = self.preprocess(img)
A = cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT)
H1 = cv2.mulSpectrums(self.G, A, 0, conjB=True)
H2 = cv2.mulSpectrums( A, A, 0, conjB=True)
self.H1 = self.H1 * (1.0-rate) + H1 * rate
self.H2 = self.H2 * (1.0-rate) + H2 * rate
self.update_kernel()
示例4: train_scale
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import mulSpectrums [as 別名]
def train_scale(self, image, ini=False):
xsf = self.get_scale_sample(image)
# Adjust ysf to the same size as xsf in the first time
if ini:
totalSize = xsf.shape[0]
self.ysf = cv2.repeat(self.ysf, totalSize, 1)
# Get new GF in the paper (delta A)
new_sf_num = cv2.mulSpectrums(self.ysf, xsf, 0, conjB=True)
new_sf_den = cv2.mulSpectrums(xsf, xsf, 0, conjB=True)
new_sf_den = cv2.reduce(real(new_sf_den), 0, cv2.REDUCE_SUM)
if ini:
self.sf_den = new_sf_den
self.sf_num = new_sf_num
else:
# Get new A and new B
self.sf_den = cv2.addWeighted(self.sf_den, (1 - self.scale_lr), new_sf_den, self.scale_lr, 0)
self.sf_num = cv2.addWeighted(self.sf_num, (1 - self.scale_lr), new_sf_num, self.scale_lr, 0)
self.update_roi()
# 檢測當前圖像尺度
示例5: correlate
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import mulSpectrums [as 別名]
def correlate(self, img):
C = cv2.mulSpectrums(cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT), self.H, 0, conjB=True)
resp = cv2.idft(C, flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT)
h, w = resp.shape
_, mval, _, (mx, my) = cv2.minMaxLoc(resp)
side_resp = resp.copy()
cv2.rectangle(side_resp, (mx-5, my-5), (mx+5, my+5), 0, -1)
smean, sstd = side_resp.mean(), side_resp.std()
psr = (mval-smean) / (sstd+eps)
return resp, (mx-w//2, my-h//2), psr
示例6: gaussianCorrelation
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import mulSpectrums [as 別名]
def gaussianCorrelation(self, x1, x2):
if self._hogfeatures:
c = np.zeros((self.size_patch[0], self.size_patch[1]), np.float32)
for i in xrange(self.size_patch[2]):
x1aux = x1[i, :].reshape((self.size_patch[0], self.size_patch[1]))
x2aux = x2[i, :].reshape((self.size_patch[0], self.size_patch[1]))
caux = cv2.mulSpectrums(fftd(x1aux), fftd(x2aux), 0, conjB=True)
caux = real(fftd(caux, True))
# caux = rearrange(caux)
c += caux
c = rearrange(c)
else:
# 'conjB=' is necessary!在做乘法之前取第二個輸入數組的共軛.
c = cv2.mulSpectrums(fftd(x1), fftd(x2), 0, conjB=True)
c = fftd(c, True)
c = real(c)
c = rearrange(c)
if x1.ndim == 3 and x2.ndim == 3:
d = (np.sum(x1[:, :, 0] * x1[:, :, 0]) + np.sum(x2[:, :, 0] * x2[:, :, 0]) - 2.0 * c) / (
self.size_patch[0] * self.size_patch[1] * self.size_patch[2])
elif x1.ndim == 2 and x2.ndim == 2:
d = (np.sum(x1 * x1) + np.sum(x2 * x2) - 2.0 * c) / (
self.size_patch[0] * self.size_patch[1] * self.size_patch[2])
d = d * (d >= 0)
d = np.exp(-d / (self.sigma * self.sigma))
return d
# 使用第一幀和它的跟蹤框,初始化KCF跟蹤器
示例7: _linear_correlation
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import mulSpectrums [as 別名]
def _linear_correlation(self, x1, x2):
xcorr = np.zeros((self.feat_sz[0], self.feat_sz[1]), np.float32)
for i in range(self.feat_sz[2]):
xcorr_ = cv2.mulSpectrums(
fft2(x1[:, :, i]), fft2(x2[:, :, i]), 0, conjB=True)
xcorr_ = real(ifft2(xcorr_))
xcorr += xcorr_
xcorr = fftshift(xcorr)
return xcorr / x1.size
示例8: _polynomial_correlation
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import mulSpectrums [as 別名]
def _polynomial_correlation(self, x1, x2, a, b):
xcorr = np.zeros((self.feat_sz[0], self.feat_sz[1]), np.float32)
for i in range(self.feat_sz[2]):
xcorr_ = cv2.mulSpectrums(
fft2(x1[:, :, i]), fft2(x2[:, :, i]), 0, conjB=True)
xcorr_ = real(ifft2(xcorr_))
xcorr += xcorr_
xcorr = fftshift(xcorr)
out = (xcorr / x1.size + a) ** b
return out
示例9: _gaussian_correlation
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import mulSpectrums [as 別名]
def _gaussian_correlation(self, x1, x2, sigma):
xcorr = np.zeros((self.feat_sz[0], self.feat_sz[1]), np.float32)
for i in range(self.feat_sz[2]):
xcorr_ = cv2.mulSpectrums(
fft2(x1[:, :, i]), fft2(x2[:, :, i]), 0, conjB=True)
xcorr_ = real(ifft2(xcorr_))
xcorr += xcorr_
xcorr = fftshift(xcorr)
out = (np.sum(x1 * x1) + np.sum(x2 * x2) - 2.0 * xcorr) / x1.size
out[out < 0] = 0
out = np.exp(-out / self.cfg.sigma ** 2)
return out
示例10: _linear_correlation
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import mulSpectrums [as 別名]
def _linear_correlation(self, x1, x2):
xcorr = cv2.mulSpectrums(fft2(x1), fft2(x2), 0, conjB=True)
xcorr = fftshift(real(ifft2(xcorr)))
return xcorr / x1.size
示例11: _polynomial_correlation
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import mulSpectrums [as 別名]
def _polynomial_correlation(self, x1, x2, a, b):
xcorr = cv2.mulSpectrums(fft2(x1), fft2(x2), 0, conjB=True)
xcorr = fftshift(real(ifft2(xcorr)))
out = (xcorr / x1.size + a) ** b
return out
示例12: init
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import mulSpectrums [as 別名]
def init(self, image, init_rect):
if image.ndim == 3:
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
init_rect = init_rect.astype(int)
init_rect[2:] += init_rect[:2]
x1, y1, x2, y2 = init_rect
w, h = map(cv2.getOptimalDFTSize, [x2 - x1, y2 - y1])
x1, y1 = (x1 + x2 - w) // 2, (y1 + y2 - h) // 2
self.t_center = x, y = x1 + 0.5 * (w - 1), y1 + 0.5 * (h - 1)
self.t_sz = w, h
img = cv2.getRectSubPix(image, (w, h), (x, y))
self.win = cv2.createHanningWindow((w, h), cv2.CV_32F)
g = np.zeros((h, w), np.float32)
g[h // 2, w // 2] = 1
g = cv2.GaussianBlur(g, (-1, -1), self.cfg.sigma)
g /= g.max()
self.G = cv2.dft(g, flags=cv2.DFT_COMPLEX_OUTPUT)
self.A = np.zeros_like(self.G)
self.B = np.zeros_like(self.G)
for _i in range(128):
patch = self._preprocess(self._random_warp(img))
F = cv2.dft(patch, flags=cv2.DFT_COMPLEX_OUTPUT)
self.A += cv2.mulSpectrums(self.G, F, 0, conjB=True)
self.B += cv2.mulSpectrums(F, F, 0, conjB=True)
self._update_kernel()
self.update(image)
示例13: update
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import mulSpectrums [as 別名]
def update(self, image):
if image.ndim == 3:
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
(x, y), (w, h) = self.t_center, self.t_sz
self.last_img = img = cv2.getRectSubPix(image, (w, h), (x, y))
img = self._preprocess(img)
self.last_resp, (dx, dy), self.psr = self._linear_correlation(img)
self.good = self.psr > self.cfg.psr_thr
if self.good:
self.t_center = x + dx, y + dy
self.last_img = img = cv2.getRectSubPix(
image, (w, h), self.t_center)
img = self._preprocess(img)
F = cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT)
A = cv2.mulSpectrums(self.G, F, 0, conjB=True)
B = cv2.mulSpectrums(F, F, 0, conjB=True)
self.A = self.A * (1.0 - self.cfg.interp_factor) + \
A * self.cfg.interp_factor
self.B = self.B * (1.0 - self.cfg.interp_factor) + \
B * self.cfg.interp_factor
self._update_kernel()
return np.array([x - 0.5 * (w - 1), y - 0.5 * (h - 1), w, h])
示例14: _linear_correlation
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import mulSpectrums [as 別名]
def _linear_correlation(self, img):
C = cv2.mulSpectrums(
cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT), self.H, 0, conjB=True)
resp = cv2.idft(C, flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT)
h, w = resp.shape
_, mval, _, (mx, my) = cv2.minMaxLoc(resp)
side_resp = resp.copy()
cv2.rectangle(side_resp, (mx - 5, my - 5), (mx + 5, my + 5), 0, -1)
smean, sstd = side_resp.mean(), side_resp.std()
psr = (mval - smean) / (sstd + self.cfg.eps)
return resp, (mx - w // 2, my - h // 2), psr
示例15: gaussianCorrelation
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import mulSpectrums [as 別名]
def gaussianCorrelation(self, x1, x2):
#t0 = time()
if(self._hogfeatures):
c = np.zeros((self.size_patch[0], self.size_patch[1]), np.float32)
for i in xrange(self.size_patch[2]):
x1aux = x1[i, :].reshape((self.size_patch[0], self.size_patch[1]))
x2aux = x2[i, :].reshape((self.size_patch[0], self.size_patch[1]))
caux = cv2.mulSpectrums(fftd(x1aux), fftd(x2aux), 0, conjB = True)
caux = real(fftd(caux, True))
#caux = rearrange(caux)
c += caux
c = rearrange(c)
else:
c = cv2.mulSpectrums(fftd(x1), fftd(x2), 0, conjB = True) # 'conjB=' is necessary!
c = fftd(c, True)
c = real(c)
c = rearrange(c)
#t1 = time()
#self.tt = 0.9*self.tt + 0.1*(t1-t0)
#print self.tt
if(x1.ndim==3 and x2.ndim==3):
d = (np.sum(x1[:,:,0]*x1[:,:,0]) + np.sum(x2[:,:,0]*x2[:,:,0]) - 2.0*c) / (self.size_patch[0]*self.size_patch[1]*self.size_patch[2])
elif(x1.ndim==2 and x2.ndim==2):
d = (np.sum(x1*x1) + np.sum(x2*x2) - 2.0*c) / (self.size_patch[0]*self.size_patch[1]*self.size_patch[2])
d = d * (d>=0)
d = np.exp(-d / (self.sigma*self.sigma))
return d