Last active
March 1, 2023 01:04
-
-
Save jackdoerner/3dc8ece00deea604b3ae to your computer and use it in GitHub Desktop.
Riesz Pyramid Creation and Reconstruction in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
riesz_pyramid.py | |
Conversion between Riesz and Laplacian image pyramids | |
Based on the data structures and methodoligies described in: | |
Riesz Pyramids for Fast Phase-Based Video Magnification | |
Neal Wadhwa, Michael Rubinstein, Fredo Durand and William T. Freeman | |
Computational Photography (ICCP), 2014 IEEE International Conference on | |
Copyright (c) 2016 Jack Doerner | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
""" | |
import numpy, math | |
import scipy, scipy.signal | |
#riesz_band_filter = numpy.asarray([[-0.5, 0, 0.5]]) | |
#riesz_band_filter = numpy.asarray([[-0.2,-0.48, 0, 0.48,0.2]]) | |
riesz_band_filter = numpy.asarray([[-0.12,0,0.12],[-0.34, 0, 0.34],[-0.12,0,0.12]]) | |
def laplacian_to_riesz(pyr): | |
newpyr = {'I':pyr[:-1], 'R1':[], 'R2':[]} | |
for ii in range(len(pyr) - 1): | |
newpyr['R1'].append( scipy.signal.convolve2d(pyr[ii], riesz_band_filter, mode='same', boundary='symm') ) | |
newpyr['R2'].append( scipy.signal.convolve2d(pyr[ii], riesz_band_filter.T, mode='same', boundary='symm') ) | |
newpyr['base'] = pyr[-1] | |
return newpyr | |
def riesz_to_spherical(pyr): | |
newpyr = {'A':[],'theta':[],'phi':[],'Q':[],'base':pyr['base']} | |
for ii in range(len(pyr['I']) ): | |
I = pyr['I'][ii] | |
R1 = pyr['R1'][ii] | |
R2 = pyr['R2'][ii] | |
A = numpy.sqrt(I*I + R1*R1 + R2*R2) | |
theta = numpy.arctan2(R2,R1) | |
Q = R1 * numpy.cos(theta) + R2 * numpy.sin(theta) | |
phi = numpy.arctan2(Q,I) | |
newpyr['A'].append( A ) | |
newpyr['theta'].append( theta ) | |
newpyr['phi'].append( phi ) | |
newpyr['Q'].append( Q ) | |
return newpyr | |
def riesz_spherical_to_laplacian(pyr): | |
newpyr = [] | |
for ii in range(len(pyr['A'])): | |
newpyr.append( pyr['A'][ii] * numpy.cos( pyr['phi'][ii] ) ) | |
newpyr.append(pyr['base']) | |
return newpyr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
def symmetrical_boundary(img): | |
#manually set up a symmetrical boundary condition so we can use fftconvolve | |
#but avoid edge effects | |
(h,w) = img.shape | |
imgsymm = numpy.empty((h*2, w*2)) | |
imgsymm[h/2:-(h+1)/2, w/2:-(w+1)/2] = img.copy() | |
imgsymm[0:h/2, 0:w/2] = img[0:h/2, 0:w/2][::-1,::-1].copy() | |
imgsymm[-(h+1)/2:, -(w+1)/2:] = img[-(h+1)/2:, -(w+1)/2:][::-1,::-1].copy() | |
imgsymm[0:h/2, -(w+1)/2:] = img[0:h/2, -(w+1)/2:][::-1,::-1].copy() | |
imgsymm[-(h+1)/2:, 0:w/2] = img[-(h+1)/2:, 0:w/2][::-1,::-1].copy() | |
imgsymm[h/2:-(h+1)/2, 0:w/2] = img[:, 0:w/2][:,::-1].copy() | |
imgsymm[h/2:-(h+1)/2, -(w+1)/2:] = img[:, -(w+1)/2:][:,::-1].copy() | |
imgsymm[0:h/2, w/2:-(w+1)/2] = img[0:h/2, :][::-1,:].copy() | |
imgsymm[-(h+1)/2:, w/2:-(w+1)/2] = img[-(h+1)/2:, :][::-1,:].copy() | |
return imgsymm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
rp_laplacian_like.py | |
Conversion between image and laplacian-like pyramids | |
Based on the data structures and methodoligies described in: | |
Riesz Pyramids for Fast Phase-Based Video Magnification | |
Neal Wadhwa, Michael Rubinstein, Fredo Durand and William T. Freeman | |
Computational Photography (ICCP), 2014 IEEE International Conference on | |
Copyright (c) 2016 Jack Doerner | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
""" | |
import numpy, cv2, scipy.signal | |
from rp_boundary import * | |
lowpass = numpy.asarray([ | |
[-0.0001, -0.0007, -0.0023, -0.0046, -0.0057, -0.0046, -0.0023, -0.0007, -0.0001], | |
[-0.0007, -0.0030, -0.0047, -0.0025, -0.0003, -0.0025, -0.0047, -0.0030, -0.0007], | |
[-0.0023, -0.0047, 0.0054, 0.0272, 0.0387, 0.0272, 0.0054, -0.0047, -0.0023], | |
[-0.0046, -0.0025, 0.0272, 0.0706, 0.0910, 0.0706, 0.0272, -0.0025, -0.0046], | |
[-0.0057, -0.0003, 0.0387, 0.0910, 0.1138, 0.0910, 0.0387, -0.0003, -0.0057], | |
[-0.0046, -0.0025, 0.0272, 0.0706, 0.0910, 0.0706, 0.0272, -0.0025, -0.0046], | |
[-0.0023, -0.0047, 0.0054, 0.0272, 0.0387, 0.0272, 0.0054, -0.0047, -0.0023], | |
[-0.0007, -0.0030, -0.0047, -0.0025, -0.0003, -0.0025, -0.0047, -0.0030, -0.0007], | |
[-0.0001, -0.0007, -0.0023, -0.0046, -0.0057, -0.0046, -0.0023, -0.0007, -0.0001] | |
]) | |
highpass = numpy.asarray([ | |
[0.0000, 0.0003, 0.0011, 0.0022, 0.0027, 0.0022, 0.0011, 0.0003, 0.0000], | |
[0.0003, 0.0020, 0.0059, 0.0103, 0.0123, 0.0103, 0.0059, 0.0020, 0.0003], | |
[0.0011, 0.0059, 0.0151, 0.0249, 0.0292, 0.0249, 0.0151, 0.0059, 0.0011], | |
[0.0022, 0.0103, 0.0249, 0.0402, 0.0469, 0.0402, 0.0249, 0.0103, 0.0022], | |
[0.0027, 0.0123, 0.0292, 0.0469, -0.9455, 0.0469, 0.0292, 0.0123, 0.0027], | |
[0.0022, 0.0103, 0.0249, 0.0402, 0.0469, 0.0402, 0.0249, 0.0103, 0.0022], | |
[0.0011, 0.0059, 0.0151, 0.0249, 0.0292, 0.0249, 0.0151, 0.0059, 0.0011], | |
[0.0003, 0.0020, 0.0059, 0.0103, 0.0123, 0.0103, 0.0059, 0.0020, 0.0003], | |
[0.0000, 0.0003, 0.0011, 0.0022, 0.0027, 0.0022, 0.0011, 0.0003, 0.0000] | |
]) | |
def getsize(img): | |
h, w = img.shape[:2] | |
return w, h | |
def build_laplacian(img, minsize=2, convolutionThreshold=500, dtype=numpy.float64): | |
img = dtype(img) | |
levels = [] | |
while (min(img.shape) > minsize): | |
if (img.size < convolutionThreshold): | |
convolutionFunction = scipy.signal.convolve2d | |
else: | |
convolutionFunction = scipy.signal.fftconvolve | |
w, h = getsize(img) | |
symmimg = symmetrical_boundary(img) | |
hp_img = convolutionFunction(symmimg, highpass, mode='same')[h/2:-(h+1)/2,w/2:-(w+1)/2] | |
lp_img = convolutionFunction(symmimg, lowpass, mode='same')[h/2:-(h+1)/2,w/2:-(w+1)/2] | |
levels.append(hp_img) | |
img = cv2.pyrDown(lp_img) | |
levels.append(img) | |
return levels | |
def collapse_laplacian(levels, convolutionThreshold=500): | |
img = levels[-1] | |
for ii in range(len(levels)-2,-1,-1): | |
lev_img = levels[ii] | |
img = cv2.pyrUp(img, dstsize=getsize(lev_img)) | |
if (img.size < convolutionThreshold): | |
convolutionFunction = scipy.signal.convolve2d | |
else: | |
convolutionFunction = scipy.signal.fftconvolve | |
w, h = getsize(img) | |
symmimg = symmetrical_boundary(img) | |
symmlev = symmetrical_boundary(lev_img) | |
img = convolutionFunction(symmimg, lowpass, mode='same')[h/2:-(h+1)/2,w/2:-(w+1)/2] | |
img += convolutionFunction(symmlev, highpass, mode='same')[h/2:-(h+1)/2,w/2:-(w+1)/2] | |
return img |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@evenlund
Good to hear that the code worked.
If you want to handle boundary with convolution, the following modification would do the trick:
I used numpy.pad function to apply mirror (reflection) padding. The original code by @jackdoerner includes a function to apply symmetric padding, but the symmetric padding can cause another artifact in my modified code. (e.g., it amplifies energy at the top-left corner)