Search code examples
numpymatplotlibplotgoogle-colaboratory

Images and masks visualisation - Colab


Hei everyone.

I have set up an image and mask visual check in Colab using matplotlib, NumPy and Random and it was expected to print the same image number for both (image and mask), but unfortunately, it's not. For whatever reason, the images don't correspond. Both images have the same size and number/name. Does anyone have some hints on how to fix this? Thank you in advance!

import random
import numpy as np

image_number = random.randint(0,len(image_dataset))
plt.figure(figsize=(12,6))
plt.subplot(121)
plt.imshow(np.reshape(image_dataset[image_number], (patch_size,patch_size,3)))
plt.subplot(122)
plt.imshow(np.reshape(mask_dataset[image_number], (patch_size,patch_size,3)))
plt.show()

image printed using the code above

This is how I'm importing the training images. (doing the same for masks)

images_dataset = [] #TRAIN IMAGES
for path, subdirs, files in os.walk(root_directory):
   dirname = path.split(os.path.sep)[-1]
   if dirname =="images":
      images = os.listdir(path)
      for i, image_name in enumerate(images):
          if image_name.endswith('.png'):

          image = cv2.imread(path + "/" + image_name, 1)
          image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
          SIZE_X = (image.shape[1]//patch_size)*patch_size
          SIZE_Y = (image.shape[0]//patch_size)*patch_size
          image = Image.fromarray(image)
          image = image.crop((0,0,SIZE_X, SIZE_Y))
          image = np.array(image)

Solution

  • try to sort out the data.

    images = sorted(os.listdir(path))