如何通过裁剪掉两个图像中不需要的区域来调整蒙版和 RGB 图像的大小以匹配

How can I resize a mask and RGB image to match by cropping out unwanted regions in both images

我正在使用 RGB 图像及其相应掩码的组织学数据集进行细胞计数项目。然而,我已经坚持了一个多星期,通过裁剪掉零像素区域来调整 RGB 和蒙版图像到仅 FOV,这些区域可以在蒙版上清楚地看到,而不会影响其中的注释。请,任何建议将是有益的。我得到的图片截图如下:

** 我的代码 **

# 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

附加信息 我还有一个 CSV 文件,其中包含紫色区域的尺寸,我希望将 RGB 和蒙版的大小都调整到该尺寸。我只是坚持在 RGB 和蒙版图像上实现它。

我认为如果您裁剪到您感兴趣的图像部分,那么 imshow() 将尽可能放大显示它们 space。

在上一个问题中讨论了裁剪

** 我的回答 ** 以下是我为可能面临类似挑战的任何人解决此问题的方法。

首先,我通过简单地将后缀“.png”添加到 CSV 的 fovname 列来确保我的文件名与 CSV 中的文件名匹配。

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

然后,使用适当的 FOV 坐标裁剪图像即可解决问题。

# 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)