Python cv2.DFT_COMPLEX_OUTPUT Examples
The following are 24
code examples of cv2.DFT_COMPLEX_OUTPUT().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
cv2
, or try the search function
.
Example #1
Source File: mosse.py From OpenCV-Python-Tutorial with MIT License | 7 votes |
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()
Example #2
Source File: spfunctions.py From spfeas with MIT License | 7 votes |
def fourier_transform(ch_bd): dft = cv2.dft(np.float32(ch_bd), flags=cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(dft) # get the Power Spectrum magnitude_spectrum = 20. * np.log(cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1])) psd1D = azimuthal_avg(magnitude_spectrum) return list(cv2.meanStdDev(psd1D))
Example #3
Source File: deconvolution.py From OpenCV-Python-Tutorial with MIT License | 6 votes |
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)
Example #4
Source File: mosse.py From OpenCV-Python-Tutorial with MIT License | 6 votes |
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)
Example #5
Source File: mosse.py From PyCV-time with MIT License | 6 votes |
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()
Example #6
Source File: deconvolution.py From PyCV-time with MIT License | 6 votes |
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)
Example #7
Source File: mosse.py From PyCV-time with MIT License | 6 votes |
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()
Example #8
Source File: mosse.py From PyCV-time with MIT License | 6 votes |
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)
Example #9
Source File: deconvolution.py From PyCV-time with MIT License | 6 votes |
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)
Example #10
Source File: kcftracker.py From KCFnb with MIT License | 5 votes |
def fftd(img, backwards=False): # shape of img can be (m,n), (m,n,1) or (m,n,2) # in my test, fft provided by numpy and scipy are slower than cv2.dft return cv2.dft(np.float32(img), flags = ((cv2.DFT_INVERSE | cv2.DFT_SCALE) if backwards else cv2.DFT_COMPLEX_OUTPUT)) # 'flags =' is necessary!
Example #11
Source File: mosse.py From PyCV-time with MIT License | 5 votes |
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
Example #12
Source File: mosse.py From PyCV-time with MIT License | 5 votes |
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
Example #13
Source File: 03_fourier_transform.py From Practical-Computer-Vision with MIT License | 5 votes |
def main(): # read an image img = cv2.imread('../figures/flower.png') # create cropped grayscale image from the original image crop_gray = cv2.cvtColor(img[100:400, 100:400], cv2.COLOR_BGR2GRAY) # take discrete fourier transform dft = cv2.dft(np.float32(crop_gray),flags = cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(dft) magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1])) # plot results plot_dft(crop_gray, magnitude_spectrum)
Example #14
Source File: kcftracker.py From KCFpy with MIT License | 5 votes |
def fftd(img, backwards=False): # shape of img can be (m,n), (m,n,1) or (m,n,2) # in my test, fft provided by numpy and scipy are slower than cv2.dft return cv2.dft(np.float32(img), flags = ((cv2.DFT_INVERSE | cv2.DFT_SCALE) if backwards else cv2.DFT_COMPLEX_OUTPUT)) # 'flags =' is necessary!
Example #15
Source File: mosse.py From open-vot with MIT License | 5 votes |
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
Example #16
Source File: mosse.py From open-vot with MIT License | 5 votes |
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])
Example #17
Source File: mosse.py From open-vot with MIT License | 5 votes |
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)
Example #18
Source File: complex.py From open-vot with MIT License | 5 votes |
def fft1(img): img = np.float32(img) if img.ndim == 1: img = img[np.newaxis, :] out = cv2.dft(img, flags=cv2.DFT_ROWS | cv2.DFT_COMPLEX_OUTPUT) out = out.squeeze(0) elif img.ndim == 2: out = cv2.dft(img, flags=cv2.DFT_ROWS | cv2.DFT_COMPLEX_OUTPUT) else: raise Exception('only supports 1 or 2 dimensional array') return out
Example #19
Source File: complex.py From open-vot with MIT License | 5 votes |
def fft2(img): img = np.float32(img) if img.ndim == 2: out = cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT) elif img.ndim == 3: out = [] for c in range(img.shape[2]): out.append(cv2.dft( img[..., c], flags=cv2.DFT_COMPLEX_OUTPUT)) out = np.stack(out, axis=2) else: raise Exception('only supports 2 or 3 dimensional array') return out
Example #20
Source File: tracker.py From KCF-DSST-py with MIT License | 5 votes |
def fftd(img, backwards=False, byRow=False): # shape of img can be (m,n), (m,n,1) or (m,n,2) # in my test, fft provided by numpy and scipy are slower than cv2.dft # return cv2.dft(np.float32(img), flags=((cv2.DFT_INVERSE | cv2.DFT_SCALE) if backwards else cv2.DFT_COMPLEX_OUTPUT)) # 'flags =' is necessary! # DFT_INVERSE: 用一维或二维逆变换取代默认的正向变换, # DFT_SCALE: 缩放比例标识符,根据数据元素个数平均求出其缩放结果,如有N个元素,则输出结果以1/N缩放输出,常与DFT_INVERSE搭配使用。 # DFT_COMPLEX_OUTPUT: 对一维或二维的实数数组进行正向变换,这样的结果虽然是复数阵列,但拥有复数的共轭对称性 if byRow: return cv2.dft(np.float32(img), flags=(cv2.DFT_ROWS | cv2.DFT_COMPLEX_OUTPUT)) else: return cv2.dft(np.float32(img), flags=((cv2.DFT_INVERSE | cv2.DFT_SCALE) if backwards else cv2.DFT_COMPLEX_OUTPUT)) # 实部图像
Example #21
Source File: mosse.py From OpenCV-Python-Tutorial with MIT License | 5 votes |
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
Example #22
Source File: spfunctions.py From spfeas with MIT License | 4 votes |
def feature_fourier(chBd, blk, scs, end_scale): rows, cols = chBd.shape scales_half = int(end_scale / 2.0) scales_blk = end_scale - blk out_len = 0 pix_ctr = 0 for i in range(0, rows-scales_blk, blk): for j in range(0, cols-scales_blk, blk): for k in scs: out_len += 2 # set the output list out_list = np.zeros(out_len, dtype='float32') for i in range(0, rows-scales_blk, blk): for j in range(0, cols-scales_blk, blk): for k in scs: k_half = int(k / 2.0) ch_bd = chBd[i+scales_half-k_half:i+scales_half-k_half+k, j+scales_half-k_half:j+scales_half-k_half+k] # get the Fourier Transform dft = cv2.dft(np.float32(ch_bd), flags=cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(dft) # get the Power Spectrum magnitude_spectrum = 20.0 * np.log(cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1])) psd1D = azimuthal_avg(magnitude_spectrum) sts = list(cv2.meanStdDev(psd1D)) # plt.subplot(121) # plt.imshow(ch_bd, cmap='gray') # plt.subplot(122) # plt.imshow(magnitude_spectrum, interpolation='nearest') # plt.show() # print psd1D # sys.exit() for st in sts: if np.isnan(st[0][0]): out_list[pix_ctr] = 0.0 else: out_list[pix_ctr] = st[0][0] pix_ctr += 1 out_list[np.isnan(out_list) | np.isinf(out_list)] = 0.0 return out_list
Example #23
Source File: saliency.py From OpenCV-Computer-Vision-Projects-with-Python with MIT License | 4 votes |
def _get_channel_sal_magn(self, channel): """Returns the log-magnitude of the Fourier spectrum This method calculates the log-magnitude of the Fourier spectrum of a single-channel image. This image could be a regular grayscale image, or a single color channel of an RGB image. :param channel: single-channel input image :returns: log-magnitude of Fourier spectrum """ # do FFT and get log-spectrum if self.use_numpy_fft: img_dft = np.fft.fft2(channel) magnitude, angle = cv2.cartToPolar(np.real(img_dft), np.imag(img_dft)) else: img_dft = cv2.dft(np.float32(channel), flags=cv2.DFT_COMPLEX_OUTPUT) magnitude, angle = cv2.cartToPolar(img_dft[:, :, 0], img_dft[:, :, 1]) # get log amplitude log_ampl = np.log10(magnitude.clip(min=1e-9)) # blur log amplitude with avg filter log_ampl_blur = cv2.blur(log_ampl, (3, 3)) # residual residual = np.exp(log_ampl - log_ampl_blur) # back to cartesian frequency domain if self.use_numpy_fft: real_part, imag_part = cv2.polarToCart(residual, angle) img_combined = np.fft.ifft2(real_part + 1j*imag_part) magnitude, _ = cv2.cartToPolar(np.real(img_combined), np.imag(img_combined)) else: img_dft[:, :, 0], img_dft[:, :, 1] = cv2.polarToCart(residual, angle) img_combined = cv2.idft(img_dft) magnitude, _ = cv2.cartToPolar(img_combined[:, :, 0], img_combined[:, :, 1]) return magnitude
Example #24
Source File: saliency.py From OpenCV-Computer-Vision-Projects-with-Python with MIT License | 4 votes |
def plot_power_spectrum(self): """Plots the power spectrum This method plots the power spectrum of the image passed to the class constructor. :returns: power spectrum """ # convert the frame to grayscale if necessary if len(self.frame_orig.shape) > 2: frame = cv2.cvtColor(self.frame_orig, cv2.COLOR_BGR2GRAY) else: frame = self.frame_orig # expand the image to an optimal size for FFT rows, cols = self.frame_orig.shape[:2] nrows = cv2.getOptimalDFTSize(rows) ncols = cv2.getOptimalDFTSize(cols) frame = cv2.copyMakeBorder(frame, 0, ncols - cols, 0, nrows - rows, cv2.BORDER_CONSTANT, value=0) # do FFT and get log-spectrum if self.use_numpy_fft: img_dft = np.fft.fft2(frame) spectrum = np.log10(np.real(np.abs(img_dft))**2) else: img_dft = cv2.dft(np.float32(frame), flags=cv2.DFT_COMPLEX_OUTPUT) spectrum = np.log10(img_dft[:, :, 0]**2+img_dft[:, :, 1]**2) # radial average L = max(frame.shape) freqs = np.fft.fftfreq(L)[:L/2] dists = np.sqrt(np.fft.fftfreq(frame.shape[0])[:, np.newaxis]**2 + np.fft.fftfreq(frame.shape[1])**2) dcount = np.histogram(dists.ravel(), bins=freqs)[0] histo, bins = np.histogram(dists.ravel(), bins=freqs, weights=spectrum.ravel()) centers = (bins[:-1] + bins[1:]) / 2 plt.plot(centers, histo/dcount) plt.xlabel('frequency') plt.ylabel('log-spectrum') plt.show()