'ValueError: could not broadcast input array from shape (256,256,4) into shape (256,256)

I'm trying to convert a set of images for a segmentation task. However, when i try to execute the code for the masks i get the following error:

ValueError: could not broadcast input array from shape (256,256,4) into shape (256,256)

It works fine for the set of images above. Here's the code:

image_dataset = []
mask_dataset = [] 



images = sorted(os.listdir(path1)) 
for i, image_name in enumerate(images):  
   if image_name.endswith(".png"):                   
       image = skimage.io.imread(path1+"/"+image_name)  #Read image
      # image = image[:,:,1] #selecting green channel
      # image = clahe_equalized(image) #applying CLAHE
       image = Image.fromarray(image)        
       image = image.resize((256, 256)) #resize image  
       image = np.array(image) 
       image = image.astype('float32') / 255. 
       image_dataset.append(image)

len(image_dataset)
imgplot = plt.imshow(image_dataset[9])

masks = sorted(os.listdir(path2))
for i, mask_name in enumerate(masks):  
   if mask_name.endswith(".png"):                   
       mask = skimage.io.imread(path2+"/"+mask_name)  #Read mask
       mask = Image.fromarray(mask)
       mask = mask.resize((256, 256))
       mask = np.array(mask) 
       mask = mask.astype('float32') / 255. 
       mask_dataset.append(mask)

len(mask_dataset)
imgplot = plt.imshow(mask_dataset[9])

image_dataset = np.array(image_dataset)
mask_dataset = np.array(mask_dataset)

#image_dataset = np.expand_dims(image_dataset,axis=-1)
#mask_dataset =  np.expand_dims(mask_dataset,axis=-1)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source