Created
September 20, 2022 14:21
-
-
Save arpit15/b07925d305b193eb8012663c2378d1b8 to your computer and use it in GitHub Desktop.
matplotlib image utils for point inspection
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 matplotlib.pyplot as plt | |
import numpy as np | |
from ipdb import set_trace | |
def analyze_imagepair(im1, im2): | |
fig, axs = plt.subplots(1,2) | |
# axs[0].imshow( (normals_without_bg_local+1)/2 ) | |
axs[0].imshow(im1) | |
axs[0].axis("off") | |
axs[1].imshow( im2 ) | |
axs[1].axis("off") | |
def onclick(event): | |
# print('button=%d, x=%d, y=%d, xdata=%f, ydata=%f' % | |
# (event.button, event.x, event.y, event.xdata, event.ydata)) | |
xidx = int(event.xdata) | |
yidx = int(event.ydata) | |
for ax in axs: | |
ax.plot(event.xdata, event.ydata, 'ro') | |
img = ax.images[0].get_array() | |
imval = img[yidx, xidx] | |
if img.ndim==3: | |
imvalstr = f"{imval[0]:.4f}, {imval[1]:.4f}, {imval[2]:.4f}" | |
else: | |
imvalstr = f"{imval:.4f}" | |
# print(xidx, yidx, imval) | |
ax.annotate(f"({xidx}, {yidx})-({imvalstr})", xy=(event.xdata,event.ydata), color="white") | |
fig.canvas.draw() | |
cid = fig.canvas.mpl_connect('button_press_event', onclick) | |
def analyze_im_grads_diff(rgbimg, imlist, markersize=3): | |
minclims = -1e7 | |
maxclims = 1e7 | |
for im in imlist: | |
minclims = max(minclims, im.min()) | |
maxclims = min(maxclims, im.max()) | |
print(minclims, maxclims) | |
imlist.insert(0, rgbimg) | |
numimgs = len(imlist) | |
print(numimgs) | |
fig, axs = plt.subplots(1,numimgs) | |
for i, im in enumerate(imlist): | |
if i == 0: | |
axs[i].imshow(im) | |
axs[i].axis("off") | |
else: | |
axs[i].imshow(im, vmin=minclims, vmax=maxclims) | |
axs[i].axis("off") | |
def onclick(event): | |
# print('button=%d, x=%d, y=%d, xdata=%f, ydata=%f' % | |
# (event.button, event.x, event.y, event.xdata, event.ydata)) | |
xidx = int(event.xdata) | |
yidx = int(event.ydata) | |
for ax in axs: | |
ax.plot(event.xdata, event.ydata, 'ro', markersize=markersize) | |
img = ax.images[0].get_array() | |
imval = img[yidx, xidx] | |
if img.ndim==3: | |
if imval.dtype == np.float32: | |
imvalstr = f"{imval[0]:.4f}, {imval[1]:.4f}, {imval[2]:.4f}" | |
else: | |
imvalstr = f"{imval[0]}, {imval[1]}, {imval[2]}" | |
else: | |
imvalstr = f"{imval:.4f}" | |
# print(xidx, yidx, imval) | |
ax.annotate(f"({xidx}, {yidx})-({imvalstr})", xy=(event.xdata,event.ydata), color="white") | |
fig.canvas.draw() | |
cid = fig.canvas.mpl_connect('button_press_event', onclick) | |
def analyze_imagegrp(imlist, markersize=3): | |
minclims = 1e7 | |
maxclims = -1e7 | |
for im in imlist: | |
minclims = min(minclims, im.min()) | |
maxclims = max(maxclims, im.max()) | |
print(minclims, maxclims) | |
# minclims = -1e-4 | |
# maxclims = 1e-2 | |
numimgs = len(imlist) | |
fig, axs = plt.subplots(1,numimgs) | |
for i, im in enumerate(imlist): | |
axs[i].imshow(im, vmin=minclims, vmax=maxclims) | |
axs[i].axis("off") | |
def onclick(event): | |
# print('button=%d, x=%d, y=%d, xdata=%f, ydata=%f' % | |
# (event.button, event.x, event.y, event.xdata, event.ydata)) | |
xidx = int(event.xdata) | |
yidx = int(event.ydata) | |
for ax in axs: | |
ax.plot(event.xdata, event.ydata, 'ro', markersize=markersize) | |
img = ax.images[0].get_array() | |
imval = img[yidx, xidx] | |
if img.ndim==3: | |
imvalstr = f"{imval[0]:.4f}, {imval[1]:.4f}, {imval[2]:.4f}" | |
else: | |
imvalstr = f"{imval:.4f}" | |
# print(xidx, yidx, imval) | |
ax.annotate(f"({xidx}, {yidx})-({imvalstr})", xy=(event.xdata,event.ydata), color="white") | |
fig.canvas.draw() | |
cid = fig.canvas.mpl_connect('button_press_event', onclick) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment