0

I am working on a cell counting project with a histology dataset of RGB images and their corresponding masks. However, I have been stuck for over a week now on resizing the RGB and masks images to only the FOV by cropping out the regions of zero pixels which can be clearly seen on the masks without affecting the annotations withing. Please, any suggestions will be beneficial. A screenshot of the images I obtained is shown below:

enter image description here

** My Code **

# Data Path
IMAGE_PATH = '/content/drive/MyDrive/dissertation/QCed single-rater dataset/rgb/'
MASKS_PATH = '/content/drive/MyDrive/dissertation/QCed single-rater dataset/mask/'
TRUE_LABEL_PATH = '/content/drive/MyDrive/dissertation/QCed single-rater dataset/visualization/'
dataset_path = '/content/drive/MyDrive/dissertation/NuCLS_dataset/'

# Get train and test IDs
image_ids =  sorted(os.listdir(IMAGE_PATH))  #next(os.walk(IMAGE_PATH))[2]
mask_ids =   sorted(os.listdir(MASKS_PATH))  #next(os.walk(MASKS_PATH))[2]
true_ids =   sorted(os.listdir(TRUE_LABEL_PATH))  #next(os.walk(MASKS_PATH))[2]

#training data
train_data = train_imgs[:int(train_imgs.shape[0]*0.85)] #training data = 85% train_imgs
train_mask = np.squeeze(train_masks[:int(train_masks.shape[0]*0.85)]) # train mask
train_label = true_labels[:int(true_labels.shape[0]*0.85)] #training data = 85% train_imgs

# validation data
val_data = train_imgs[int(train_imgs.shape[0]*0.85):int(train_imgs.shape[0]*0.95)] # validation data = 10%train_imgs
val_mask = np.squeeze(train_masks[int(train_masks.shape[0]*0.85):int(train_imgs.shape[0]*0.95)]) # val mask
val_label = true_labels[int(true_labels.shape[0]*0.85):int(true_labels.shape[0]*0.95)]

#test data
test_data = train_imgs[int(train_imgs.shape[0]*0.95):] # test data = 5%train_imgs
test_mask = np.squeeze(train_masks[int(train_masks.shape[0]*0.95):]) # val mask
test_label = true_labels[int(true_labels.shape[0]*0.95):]
print(val_mask.shape)
(174, 256, 256, 3)
ix = 0
for ix in range(0,5):
  print('Training example No.',ix)

  fig = plt.figure(figsize=(16, 16))
  plt.subplot(131).set_title('Original Image')
  plt.imshow(test_data[ix])
  plt.subplot(132).set_title('Mask (Target)')
  plt.imshow(test_mask[ix])
  plt.subplot(133).set_title('True Label')
  plt.imshow(test_label[ix])
  #plt.savefig(base_path +'fig- Sanity check on training dataset no {}.png'.format(ix))
  plt.show()
  ix +=1

enter image description here

Additional Information I also have a CSV file containing the dimensions of the purple region, which I want both RGB and mask to be resized to. I am just stuck with implementing this on the RGB and mask images.

enter image description here

  • I guess the code shown works, but I do not quite understand what you want your final images to look like. Would they be like "True Label" but with some parts blacked out? Resized to expand the region with purple outline? Is there space to show something you tried that did not quite do what you need? – TMBailey Aug 07 '21 at 07:12
  • Yes exactly. I am trying to resize both mask and RGB to only the purple FOV. I have a CSV file with the FOV coordinates. I am just stuck on what next to do to tackle this since the images have already been resized to 256 x 256 – Ireke Ukiwo Ireke Samuel Aug 07 '21 at 07:28
  • For the sake of simplicity, maybe focus on a single set of images. By 'resize' do you mean you want to zoom in on the same sub-region of each image when you display them with plt.imshow()? – TMBailey Aug 07 '21 at 08:16

2 Answers2

1

** My Answer ** Here is how I resolved this issue for anyone who might face similar challenges.

Firstly, I ensured my file names match that from the CSV by simply adding the suffix '.png' to the fovname column of the CSV.

df['fovname'] = df['fovname'].astype(str)+ '.png'
print (list(df['fovname']))

Then, cropping the images with the appropriate FOV coordinates solve the issue.

# RGB Images
for x in image_ids:
  im = Image.open(IMAGE_PATH + x)
  DF = df.loc[df['fovname']== x]
  DF = DF.drop_duplicates()
  xmin = DF['xmin']
  ymin = DF['ymin']
  xmax = DF['xmax']
  ymax = DF['ymax']
  print(x)
  im = im.crop((xmin, ymin, xmax, ymax))
  data_path = '/content/drive/MyDrive/dissertation/NuCLS_dataset/NEW/RGB/'
  im.save(data_path+'{}'.format(x))
  print('Saved')
  #plt.imshow(im)

enter image description here

  • I think it helps if you click the button to 'accept' your answer so the question doesn't show up as being one that doesn't yet have an answer accepted by the person who posed the question. – TMBailey Aug 07 '21 at 18:09
0

I think if you crop to the parts of the images you are interested in, then imshow() will zoom to show them in as much space as is available.

Cropping is discussed in a previous question Cropping image by the center.

TMBailey
  • 557
  • 3
  • 14