# Generated by pandoc-plot 1.9.1
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(2019)
from skued import diffread
from pathlib import Path
def center_of_mass_masked(im, mask):
rr, cc = np.indices(im.shape)
weights = im * mask.astype(im.dtype)
r = np.average(rr, weights=weights)
c = np.average(cc, weights=weights)
return r, c
im1 = diffread(Path("images") / "autocenter" / "graphite.tif")
mask1 = diffread(Path("images") / "autocenter" / "graphite_mask.tif").astype(bool)
im2 = diffread(Path("images") / "mnxc" / "Cr_1.tif")
mask2 = np.ones_like(im2, dtype=bool)
mask2[0:1250, 950:1250] = False
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(6,3))
for ax, im, mask in zip([ax1, ax2], [im1, im2], [mask1, mask2]):
r, c = center_of_mass_masked(im, mask)
ax.imshow(im, vmin=0, vmax=200, cmap='inferno')
ax.scatter(c, r, color='r')
ax.axis('off')
plt.tight_layout()