Skip to content

Instantly share code, notes, and snippets.

@ashishrana160796
Last active January 1, 2022 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashishrana160796/052b8c0d5fe9c0b9bc45e926020a35f6 to your computer and use it in GitHub Desktop.
Save ashishrana160796/052b8c0d5fe9c0b9bc45e926020a35f6 to your computer and use it in GitHub Desktop.
Zooming in images of cells and annotation images via matplotlib on VGG Cell count dataset. It is preferred to use Python 3.6.x for replicating the implementation.
import scipy
import numpy as np
from scipy.ndimage import zoom
import matplotlib.pyplot as plt
# Reference Solution Given By: https://stackoverflow.com/users/1461210/ali-m
#
def clipped_zoom(img, zoom_factor, **kwargs):
# assumption is that zoom factor is greater than 1
h, w = img.shape[:2]
# no RGB zoom to be applied for multi-channel image,
zoom_tuple = (zoom_factor,) * 2 + (1,) * (img.ndim - 2)
# Bounding box of the zoomed-in region within the input array
zh = int(np.round(h / zoom_factor))
zw = int(np.round(w / zoom_factor))
top = (h - zh) // 2
left = (w - zw) // 2
out = zoom(img[top:top+zh, left:left+zw], zoom_tuple, **kwargs)
# trim off any extra pixels at the edges from 'out'
trim_top = ((out.shape[0] - h) // 2)
trim_left = ((out.shape[1] - w) // 2)
out = out[trim_top:trim_top+h, trim_left:trim_left+w]
return out
# Reading, Zooming, Plotting & Saving Images
# matplotlib.pyplot.imread can also be used as imread is deprecated
im_org = scipy.ndimage.imread('001cell.png', True)
zm_org = clipped_zoom(im_org, 4.0)
im_ano = scipy.ndimage.imread('001dots.png', True)
zm_ano = clipped_zoom(im_org, 4.0)
fig, ax = plt.subplots(2, 2)
ax[0][0].imshow(im_org)
ax[0][1].imshow(zm_org)
ax[0][0].imshow(im_ano)
ax[0][0].imshow(zm_ano)
ax[0][0].set_title('(a) Normal Views')
ax[0][1].set_title('(b) Zoomed Views')
plt.savefig('cells_fig.pdf', bbox_inches='tight')
# or
# plt.savefig('cells_fig.png', dpi=300)
@ashishrana160796
Copy link
Author

Saved Image Preview

cells_fig

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