Skip to content

Instantly share code, notes, and snippets.

@sohiniroych
Created March 10, 2021 23:34
Show Gist options
  • Select an option

  • Save sohiniroych/03996abaaa86de1651ffbe248e44960f to your computer and use it in GitHub Desktop.

Select an option

Save sohiniroych/03996abaaa86de1651ffbe248e44960f to your computer and use it in GitHub Desktop.
generate binary mask from grayscale images
def adjustData(img,mask,flag_multi_class,n_class):
if(flag_multi_class):
img /= 255
mask = mask[:,:,:,0] if(len(mask.shape) == 4) else mask[:,:,0]
new_mask = np.zeros(mask.shape + (n_class,))
for i in range(n_class):
new_mask[mask == i,i] = 1
new_mask = np.reshape(new_mask,(new_mask.shape[0],new_mask.shape[1]*new_mask.shape[2],new_mask.shape[3])) if flag_multi_class else np.reshape(new_mask,(new_mask.shape[0]*new_mask.shape[1],new_mask.shape[2]))
mask = new_mask
elif(np.max(img)>1):
img = img / 255
mask = mask /255
mask[mask > 0.1] = 1
mask[mask <= 0.1] = 0
#print(np.shape(mask),np.shape(img))
return (img,mask)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment