def hog(image, orientations=8, ksize=(5, 5)):
'''
returns the Histogram of Oriented Gradients
:param ksize: convolution kernel size as (y,x) - needs to be odd
:param orientations: number of orientations in between rad=0 and rad=pi
similar to http://scikit-image.org/docs/dev/auto_examples/plot_hog.html
but faster and with less options
'''
s0, s1 = image.shape[:2]
# speed up the process through saving generated kernels:
try:
k = hog.kernels[str(ksize) + str(orientations)]
except KeyError:
k = _mkConvKernel(ksize, orientations)
hog.kernels[str(ksize) + str(orientations)] = k
out = np.empty(shape=(s0, s1, orientations))
image[np.isnan(image)] = 0
for i in range(orientations):
out[:, :, i] = convolve(image, k[i])
return out
python类convolve()的实例源码
def MultiScaleSSIM(img1, img2, max_val=255, filter_size=11, filter_sigma=1.5, k1=0.01, k2=0.03, weights=None):
weights = np.array(weights if weights else [0.0448, 0.2856, 0.3001, 0.2363, 0.1333])
levels = weights.size
downsample_filter = np.ones((1, 2, 2, 1)) / 4.0
im1, im2 = [x.astype(np.float64) for x in [img1, img2]]
mssim = np.array([])
mcs = np.array([])
for _ in range(levels):
ssim, cs = _SSIMForMultiScale(im1, im2, max_val=max_val, filter_size=filter_size, filter_sigma=filter_sigma, k1=k1, k2=k2)
mssim = np.append(mssim, ssim)
mcs = np.append(mcs, cs)
filtered = [convolve(im, downsample_filter, mode='reflect') for im in [im1, im2]]
im1, im2 = [x[:, ::2, ::2, :] for x in filtered]
return np.prod(mcs[0:levels-1] ** weights[0:levels-1]) * (mssim[levels-1] ** weights[levels-1])
def calculate_sift_grid(self, image, rangeH, rangeW):
H, W = image.shape
feat = np.zeros((len(rangeH), len(rangeW), self.Nsamples*self.Nangles))
IH = filters.convolve(image, self.GH, mode='nearest')
IW = filters.convolve(image, self.GW, mode='nearest')
I_mag = np.sqrt(IH ** 2 + IW ** 2)
I_theta = np.arctan2(IH, IW)
I_orient = np.empty((H, W, self.Nangles))
for i in range(self.Nangles):
I_orient[:,:,i] = I_mag * np.maximum(
np.cos(I_theta - self.angles[i]) ** self.alpha, 0)
for i, hs in enumerate(rangeH):
for j, ws in enumerate(rangeW):
feat[i, j] = np.dot(self.weights,
I_orient[hs:hs+self.pS, ws:ws+self.pS]\
.reshape(self.pS**2, self.Nangles)
).flat
return feat
def _calc_normal_matrix(self):
x_kernel = np.array([[.25, 0, -.25], [.5, 0, -.5], [.25, 0, -.25]])
y_kernel = np.array([[-.25, -.5, -.25], [0, 0, 0], [.25, .5, .25]])
x_normal = convolve(self.working_mask.astype(float), x_kernel)
y_normal = convolve(self.working_mask.astype(float), y_kernel)
normal = np.dstack((x_normal, y_normal))
height, width = normal.shape[:2]
norm = np.sqrt(y_normal**2 + x_normal**2) \
.reshape(height, width, 1) \
.repeat(2, axis=2)
norm[norm == 0] = 1
unit_normal = normal/norm
return unit_normal
def HornSchunck(im1, im2, alpha=0.001, Niter=8, verbose=False):
"""
im1: image at t=0
im2: image at t=1
alpha: regularization constant
Niter: number of iteration
"""
im1 = im1.astype(np.float32)
im2 = im2.astype(np.float32)
#set up initial velocities
uInitial = np.zeros([im1.shape[0],im1.shape[1]])
vInitial = np.zeros([im1.shape[0],im1.shape[1]])
# Set initial value for the flow vectors
U = uInitial
V = vInitial
# Estimate derivatives
[fx, fy, ft] = computeDerivatives(im1, im2)
if verbose:
plotderiv(fx,fy,ft)
# print(fx[100,100],fy[100,100],ft[100,100])
# Iteration to reduce error
for _ in range(Niter):
#%% Compute local averages of the flow vectors
uAvg = filter2(U, HSKERN)
vAvg = filter2(V, HSKERN)
#%% common part of update step
der = (fx*uAvg + fy*vAvg + ft) / (alpha**2 + fx**2 + fy**2)
#%% iterative step
U = uAvg - fx * der
V = vAvg - fy * der
return U,V
def computeDerivatives(im1, im2):
fx = filter2(im1,kernelX) + filter2(im2,kernelX)
fy = filter2(im1,kernelY) + filter2(im2,kernelY)
# ft = im2 - im1
ft = filter2(im1,kernelT) + filter2(im2,-kernelT)
return fx,fy,ft
#%%
def conv(input, weights):
return convolve(input, weights)
def local_normalize_1ch(img, const=127.0):
mu = convolve(img, kern, mode='nearest')
mu_sq = mu * mu
im_sq = img * img
tmp = convolve(im_sq, kern, mode='nearest') - mu_sq
sigma = np.sqrt(np.abs(tmp))
structdis = (img - mu) / (sigma + const)
# Rescale within 0 and 1
# structdis = (structdis + 3) / 6
structdis = 2. * structdis / 3.
return structdis
def local_normalize(img, num_ch=1, const=127.0):
if num_ch == 1:
mu = convolve(img[:, :, 0], kern, mode='nearest')
mu_sq = mu * mu
im_sq = img[:, :, 0] * img[:, :, 0]
tmp = convolve(im_sq, kern, mode='nearest') - mu_sq
sigma = np.sqrt(np.abs(tmp))
structdis = (img[:, :, 0] - mu) / (sigma + const)
# Rescale within 0 and 1
# structdis = (structdis + 3) / 6
structdis = 2. * structdis / 3.
norm = structdis[:, :, None]
elif num_ch > 1:
norm = np.zeros(img.shape, dtype='float32')
for ch in range(num_ch):
mu = convolve(img[:, :, ch], kern, mode='nearest')
mu_sq = mu * mu
im_sq = img[:, :, ch] * img[:, :, ch]
tmp = convolve(im_sq, kern, mode='nearest') - mu_sq
sigma = np.sqrt(np.abs(tmp))
structdis = (img[:, :, ch] - mu) / (sigma + const)
# Rescale within 0 and 1
# structdis = (structdis + 3) / 6
structdis = 2. * structdis / 3.
norm[:, :, ch] = structdis
return norm
def ssim(img1, img2, cs_map=False):
"""Return the Structural Similarity Map corresponding to input images img1
and img2 (images are assumed to be uint8)
This function attempts to mimic precisely the functionality of ssim.m a
MATLAB provided by the author's of SSIM
https://ece.uwaterloo.ca/~z70wang/research/ssim/ssim_index.m
"""
# img1 = img1.astype('float32')
# img2 = img2.astype('float32')
K1 = 0.01
K2 = 0.03
L = 255
C1 = (K1 * L) ** 2
C2 = (K2 * L) ** 2
mu1 = convolve(window, img1, mode='nearest')
mu2 = convolve(window, img2, mode='nearest')
mu1_sq = mu1 * mu1
mu2_sq = mu2 * mu2
mu1_mu2 = mu1 * mu2
sigma1_sq = convolve(window, img1 * img1, mode='nearest') - mu1_sq
sigma2_sq = convolve(window, img2 * img2, mode='nearest') - mu2_sq
sigma12 = convolve(window, img1 * img2, mode='nearest') - mu1_mu2
if cs_map:
return (((2 * mu1_mu2 + C1) * (2 * sigma12 + C2)) /
((mu1_sq + mu2_sq + C1) * (sigma1_sq + sigma2_sq + C2)),
(2.0 * sigma12 + C2) / (sigma1_sq + sigma2_sq + C2))
else:
return (((2 * mu1_mu2 + C1) * (2 * sigma12 + C2)) /
((mu1_sq + mu2_sq + C1) * (sigma1_sq + sigma2_sq + C2)))
def MultiScaleSSIM(img1, img2, max_val=255, filter_size=11, filter_sigma=1.5,
k1=0.01, k2=0.03, weights=None):
"""Return the MS-SSIM score between `img1` and `img2`.
This function implements Multi-Scale Structural Similarity (MS-SSIM) Image
Quality Assessment according to Zhou Wang's paper, "Multi-scale structural
similarity for image quality assessment" (2003).
Link: https://ece.uwaterloo.ca/~z70wang/publications/msssim.pdf
Author's MATLAB implementation:
http://www.cns.nyu.edu/~lcv/ssim/msssim.zip
Args:
img1: Numpy array holding the first RGB image batch.
img2: Numpy array holding the second RGB image batch.
max_val: the dynamic range of the images (i.e., the difference between the
maximum the and minimum allowed values).
filter_size: Size of blur kernel to use (will be reduced for small images).
filter_sigma: Standard deviation for Gaussian blur kernel (will be reduced
for small images).
k1: Constant used to maintain stability in the SSIM calculation (0.01 in
the original paper).
k2: Constant used to maintain stability in the SSIM calculation (0.03 in
the original paper).
weights: List of weights for each level; if none, use five levels and the
weights from the original paper.
Returns:
MS-SSIM score between `img1` and `img2`.
Raises:
RuntimeError: If input images don't have the same shape or don't have four
dimensions: [batch_size, height, width, depth].
"""
if img1.shape != img2.shape:
raise RuntimeError('Input images must have the same shape (%s vs. %s).',
img1.shape, img2.shape)
if img1.ndim != 4:
raise RuntimeError('Input images must have four dimensions, not %d',
img1.ndim)
# Note: default weights don't sum to 1.0 but do match the paper / matlab code.
weights = np.array(weights if weights else
[0.0448, 0.2856, 0.3001, 0.2363, 0.1333])
levels = weights.size
downsample_filter = np.ones((1, 2, 2, 1)) / 4.0
im1, im2 = [x.astype(np.float64) for x in [img1, img2]]
mssim = np.array([])
mcs = np.array([])
for _ in range(levels):
ssim, cs = SSIMForMultiScale(
im1, im2, max_val=max_val, filter_size=filter_size,
filter_sigma=filter_sigma, k1=k1, k2=k2)
mssim = np.append(mssim, ssim)
mcs = np.append(mcs, cs)
filtered = [convolve(im, downsample_filter, mode='reflect')
for im in [im1, im2]]
im1, im2 = [x[:, ::2, ::2, :] for x in filtered]
return (np.prod(mcs[0:levels - 1] ** weights[0:levels - 1]) *
(mssim[levels - 1] ** weights[levels - 1]))
def MultiScaleSSIM(img1, img2, max_val=255, filter_size=11, filter_sigma=1.5,
k1=0.01, k2=0.03, weights=None):
"""Return the MS-SSIM score between `img1` and `img2`.
This function implements Multi-Scale Structural Similarity (MS-SSIM) Image
Quality Assessment according to Zhou Wang's paper, "Multi-scale structural
similarity for image quality assessment" (2003).
Link: https://ece.uwaterloo.ca/~z70wang/publications/msssim.pdf
Author's MATLAB implementation:
http://www.cns.nyu.edu/~lcv/ssim/msssim.zip
Arguments:
img1: Numpy array holding the first RGB image batch.
img2: Numpy array holding the second RGB image batch.
max_val: the dynamic range of the images (i.e., the difference between the
maximum the and minimum allowed values).
filter_size: Size of blur kernel to use (will be reduced for small images).
filter_sigma: Standard deviation for Gaussian blur kernel (will be reduced
for small images).
k1: Constant used to maintain stability in the SSIM calculation (0.01 in
the original paper).
k2: Constant used to maintain stability in the SSIM calculation (0.03 in
the original paper).
weights: List of weights for each level; if none, use five levels and the
weights from the original paper.
Returns:
MS-SSIM score between `img1` and `img2`.
Raises:
RuntimeError: If input images don't have the same shape or don't have four
dimensions: [batch_size, height, width, depth].
"""
if img1.shape != img2.shape:
raise RuntimeError('Input images must have the same shape (%s vs. %s).',
img1.shape, img2.shape)
if img1.ndim != 4:
raise RuntimeError('Input images must have four dimensions, not %d',
img1.ndim)
# Note: default weights don't sum to 1.0 but do match the paper / matlab code.
weights = np.array(weights if weights else
[0.0448, 0.2856, 0.3001, 0.2363, 0.1333])
levels = weights.size
downsample_filter = np.ones((1, 2, 2, 1)) / 4.0
im1, im2 = [x.astype(np.float64) for x in [img1, img2]]
mssim = np.array([])
mcs = np.array([])
for _ in range(levels):
ssim, cs = _SSIMForMultiScale(
im1, im2, max_val=max_val, filter_size=filter_size,
filter_sigma=filter_sigma, k1=k1, k2=k2)
mssim = np.append(mssim, ssim)
mcs = np.append(mcs, cs)
filtered = [convolve(im, downsample_filter, mode='reflect')
for im in [im1, im2]]
im1, im2 = [x[:, ::2, ::2, :] for x in filtered]
return (np.prod(mcs[0:levels-1] ** weights[0:levels-1]) *
(mssim[levels-1] ** weights[levels-1]))
def MultiScaleSSIM(img1, img2, max_val=255, filter_size=11, filter_sigma=1.5,
k1=0.01, k2=0.03, weights=None):
"""Return the MS-SSIM score between `img1` and `img2`.
This function implements Multi-Scale Structural Similarity (MS-SSIM) Image
Quality Assessment according to Zhou Wang's paper, "Multi-scale structural
similarity for image quality assessment" (2003).
Link: https://ece.uwaterloo.ca/~z70wang/publications/msssim.pdf
Author's MATLAB implementation:
http://www.cns.nyu.edu/~lcv/ssim/msssim.zip
Arguments:
img1: Numpy array holding the first RGB image batch.
img2: Numpy array holding the second RGB image batch.
max_val: the dynamic range of the images (i.e., the difference between the
maximum the and minimum allowed values).
filter_size: Size of blur kernel to use (will be reduced for small images).
filter_sigma: Standard deviation for Gaussian blur kernel (will be reduced
for small images).
k1: Constant used to maintain stability in the SSIM calculation (0.01 in
the original paper).
k2: Constant used to maintain stability in the SSIM calculation (0.03 in
the original paper).
weights: List of weights for each level; if none, use five levels and the
weights from the original paper.
Returns:
MS-SSIM score between `img1` and `img2`.
Raises:
RuntimeError: If input images don't have the same shape or don't have four
dimensions: [batch_size, height, width, depth].
"""
if img1.shape != img2.shape:
raise RuntimeError('Input images must have the same shape (%s vs. %s).',
img1.shape, img2.shape)
if img1.ndim != 4:
raise RuntimeError('Input images must have four dimensions, not %d',
img1.ndim)
# Note: default weights don't sum to 1.0 but do match the paper / matlab code.
weights = np.array(weights if weights else
[0.0448, 0.2856, 0.3001, 0.2363, 0.1333])
levels = weights.size
downsample_filter = np.ones((1, 2, 2, 1)) / 4.0
im1, im2 = [x.astype(np.float64) for x in [img1, img2]]
mssim = np.array([])
mcs = np.array([])
for _ in xrange(levels):
ssim, cs = _SSIMForMultiScale(
im1, im2, max_val=max_val, filter_size=filter_size,
filter_sigma=filter_sigma, k1=k1, k2=k2)
mssim = np.append(mssim, ssim)
mcs = np.append(mcs, cs)
filtered = [convolve(im, downsample_filter, mode='reflect')
for im in [im1, im2]]
im1, im2 = [x[:, ::2, ::2, :] for x in filtered]
return (np.prod(mcs[0:levels-1] ** weights[0:levels-1]) *
(mssim[levels-1] ** weights[levels-1]))