python: 'cv2.' -> 'cv.' via 'import cv2 as cv'
This commit is contained in:
@@ -34,7 +34,7 @@ Examples:
|
||||
from __future__ import print_function
|
||||
|
||||
import numpy as np
|
||||
import cv2
|
||||
import cv2 as cv
|
||||
|
||||
# local module
|
||||
from common import nothing
|
||||
@@ -42,8 +42,8 @@ from common import nothing
|
||||
|
||||
def blur_edge(img, d=31):
|
||||
h, w = img.shape[:2]
|
||||
img_pad = cv2.copyMakeBorder(img, d, d, d, d, cv2.BORDER_WRAP)
|
||||
img_blur = cv2.GaussianBlur(img_pad, (2*d+1, 2*d+1), -1)[d:-d,d:-d]
|
||||
img_pad = cv.copyMakeBorder(img, d, d, d, d, cv.BORDER_WRAP)
|
||||
img_blur = cv.GaussianBlur(img_pad, (2*d+1, 2*d+1), -1)[d:-d,d:-d]
|
||||
y, x = np.indices((h, w))
|
||||
dist = np.dstack([x, w-x-1, y, h-y-1]).min(-1)
|
||||
w = np.minimum(np.float32(dist)/d, 1.0)
|
||||
@@ -55,12 +55,12 @@ def motion_kernel(angle, d, sz=65):
|
||||
A = np.float32([[c, -s, 0], [s, c, 0]])
|
||||
sz2 = sz // 2
|
||||
A[:,2] = (sz2, sz2) - np.dot(A[:,:2], ((d-1)*0.5, 0))
|
||||
kern = cv2.warpAffine(kern, A, (sz, sz), flags=cv2.INTER_CUBIC)
|
||||
kern = cv.warpAffine(kern, A, (sz, sz), flags=cv.INTER_CUBIC)
|
||||
return kern
|
||||
|
||||
def defocus_kernel(d, sz=65):
|
||||
kern = np.zeros((sz, sz), np.uint8)
|
||||
cv2.circle(kern, (sz, sz), d, 255, -1, cv2.LINE_AA, shift=1)
|
||||
cv.circle(kern, (sz, sz), d, 255, -1, cv.LINE_AA, shift=1)
|
||||
kern = np.float32(kern) / 255.0
|
||||
return kern
|
||||
|
||||
@@ -77,52 +77,52 @@ if __name__ == '__main__':
|
||||
|
||||
win = 'deconvolution'
|
||||
|
||||
img = cv2.imread(fn, 0)
|
||||
img = cv.imread(fn, 0)
|
||||
if img is None:
|
||||
print('Failed to load fn1:', fn1)
|
||||
sys.exit(1)
|
||||
|
||||
img = np.float32(img)/255.0
|
||||
cv2.imshow('input', img)
|
||||
cv.imshow('input', img)
|
||||
|
||||
img = blur_edge(img)
|
||||
IMG = cv2.dft(img, flags=cv2.DFT_COMPLEX_OUTPUT)
|
||||
IMG = cv.dft(img, flags=cv.DFT_COMPLEX_OUTPUT)
|
||||
|
||||
defocus = '--circle' in opts
|
||||
|
||||
def update(_):
|
||||
ang = np.deg2rad( cv2.getTrackbarPos('angle', win) )
|
||||
d = cv2.getTrackbarPos('d', win)
|
||||
noise = 10**(-0.1*cv2.getTrackbarPos('SNR (db)', win))
|
||||
ang = np.deg2rad( cv.getTrackbarPos('angle', win) )
|
||||
d = cv.getTrackbarPos('d', win)
|
||||
noise = 10**(-0.1*cv.getTrackbarPos('SNR (db)', win))
|
||||
|
||||
if defocus:
|
||||
psf = defocus_kernel(d)
|
||||
else:
|
||||
psf = motion_kernel(ang, d)
|
||||
cv2.imshow('psf', psf)
|
||||
cv.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)
|
||||
PSF = cv.dft(psf_pad, flags=cv.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 = cv.mulSpectrums(IMG, iPSF, 0)
|
||||
res = cv.idft(RES, flags=cv.DFT_SCALE | cv.DFT_REAL_OUTPUT )
|
||||
res = np.roll(res, -kh//2, 0)
|
||||
res = np.roll(res, -kw//2, 1)
|
||||
cv2.imshow(win, res)
|
||||
cv.imshow(win, res)
|
||||
|
||||
cv2.namedWindow(win)
|
||||
cv2.namedWindow('psf', 0)
|
||||
cv2.createTrackbar('angle', win, int(opts.get('--angle', 135)), 180, update)
|
||||
cv2.createTrackbar('d', win, int(opts.get('--d', 22)), 50, update)
|
||||
cv2.createTrackbar('SNR (db)', win, int(opts.get('--snr', 25)), 50, update)
|
||||
cv.namedWindow(win)
|
||||
cv.namedWindow('psf', 0)
|
||||
cv.createTrackbar('angle', win, int(opts.get('--angle', 135)), 180, update)
|
||||
cv.createTrackbar('d', win, int(opts.get('--d', 22)), 50, update)
|
||||
cv.createTrackbar('SNR (db)', win, int(opts.get('--snr', 25)), 50, update)
|
||||
update(None)
|
||||
|
||||
while True:
|
||||
ch = cv2.waitKey()
|
||||
ch = cv.waitKey()
|
||||
if ch == 27:
|
||||
break
|
||||
if ch == ord(' '):
|
||||
|
||||
Reference in New Issue
Block a user