Skip to content

Instantly share code, notes, and snippets.

@nik-hil
Created October 19, 2018 09:25
Show Gist options
  • Save nik-hil/1f23c2dc9a116da61d4fa72c920b5a95 to your computer and use it in GitHub Desktop.
Save nik-hil/1f23c2dc9a116da61d4fa72c920b5a95 to your computer and use it in GitHub Desktop.
plot image with alpha, with mask
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib.patches import Rectangle
import pydicom
import numpy as np
import torchvision as tv
img = pydicom.dcmread('/Users/nikhil/workspace/src/rsna/dcm/00aecb01-a116-45a2-956c-08d2fa55433f.dcm').pixel_array
img = np.expand_dims(img, -1)
img_pil = tv.transforms.functional.to_pil_image(img)
box = 288.0, 322., 94., 135.0
x, y, w, h = box
angle = 0
patch = Rectangle((x,y), w, h, color='red', ls='dashed', angle=angle, fill=False, lw=4, joinstyle='round', alpha=0.6)
target = np.zeros((1024, 1024))
xi,yi,hi,wi = int(x), int(y), int(h), int(w)
for r in range(xi, xi+wi+1):
for c in range(yi, yi+hi+1):
target[r,c]=255
target = np.expand_dims(target, -1)
target = target.astype('uint8')
target = tv.transforms.functional.to_pil_image(target)
fig, ax = plt.subplots()
ax.plot(target)
ax.imshow(target)
ax.add_patch(patch)
plt.show()
fig, ax = plt.subplots()
ax.imshow(img_pil)
ax.imshow(target, alpha=0.5)
plt.show()
fig, ax = plt.subplots()
ax.imshow(img_pil)
ax.imshow(target, alpha=0.1)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment