Skip to content

Instantly share code, notes, and snippets.

@stggh
Created July 28, 2020 10:59
Show Gist options
  • Save stggh/5c5eb6d5a5587cdf8653504500ffdb81 to your computer and use it in GitHub Desktop.
Save stggh/5c5eb6d5a5587cdf8653504500ffdb81 to your computer and use it in GitHub Desktop.
testing PyAbel abel/tools/center.py/set_center()
import numpy as np
import abel
import matplotlib.pyplot as plt
from scipy.ndimage.interpolation import shift
fig, ax = plt.subplots(2, 2, figsize=(8, 8))
n = 201
n2 = n//2
geo_origin = (n2, n2)
im = abel.tools.analytical.SampleImage(n).image
# edges
im[:, 0] = 100
im[:, n-1] = 100
im[0, :] = 100
im[n-1, :] = 100
vm = im.max()
# rescale central intensity
radius = n // 10
for r in range(-radius, radius, 1):
for c in range(-radius, radius, 1):
if r**2 + c**2 < radius**2:
im[n2+r, n2+c] /= 25
# add some near edge data
im[radius+r//4, radius+c//4] = vm//45
im[n-radius+r//4, n-radius+c//4] = vm//40
im[radius+r//4, n-radius+c//4] = vm//35
im[n-radius+r//4, radius+c//4] = vm//30
# offset
dr = 20
dc = 20
imx = shift(im, (dr, dc)) # origin now (n2+dr, n2+dc)
ax0 = ax[0, 0]
ax0.imshow(imx)
ax0.set_title(f'original ({n2+dr}, {n2+dc}) {imx.shape}', fontsize='small')
ax0.plot((0, n), (n2, n2), 'r', lw=1)
ax0.plot((n2, n2), (0, n), 'r', lw=1)
ax0.axis(xmin=0, xmax=n, ymin=n, ymax=0)
i = 0
for crop in ['maintain_size', 'valid_region', 'maintain_data']:
# PyAebl reset image center to origin
imc = abel.tools.center.set_center(imx, (n2+dr, n2+dc),
crop=crop, axes=(0, 1))
i += 1
r, c = imc.shape
r2 = r//2
c2 = c//2
axi = ax[i//2, i%2]
axi.imshow(imc)
axi.plot((0, r), (r2, c2), 'r', lw=1)
axi.plot((r2, c2), (0, c), 'r', lw=1)
if crop == 'valid_region':
axi.axis(xmin=0, xmax=n, ymin=n, ymax=0)
else:
axi.axis(xmin=0, xmax=c, ymin=r, ymax=0)
axi.set_title(f'{crop} ({n2}, {n2}) {imc.shape}', fontsize='small')
plt.show()
@stggh
Copy link
Author

stggh commented Jul 28, 2020

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment