CIE L*a*b* color channels extraction using numpy and PyTorch

Hi everyone! :smiley:
Could you help me to verify if I am doing right in the way that I am extracting color channels from CIE Lab* color space?
Below is the code that I am using:

class CustomDataset(Dataset):
    """Custom Dataset."""

    def __init__(self, root_dir, transform=None):
        """
        Args:
            root_dir (string): Directory with all the images.
            transform (callable, optional): Optional transform to be applied
                on a sample.
        """
        self.root_dir = root_dir
        self.transform = transform
        self.file_list=os.listdir(root_dir)

    def __len__(self):
        return len(self.file_list)

    def __getitem__(self, idx):
        img = Image.open(self.root_dir+'/'+self.file_list[idx]) # Read the image using PIL
        
        if self.transform is not None:
            img_original = self.transform(img)
            img_ori_resized = transforms.Resize((64,64),2)(img_original)

            img_ori_lab = rgb2lab(img_original) # Convert from RGB to CIE L*a*b* color space. The image shape is (w, h, 3)
            img_ori_lab = img_ori_lab.transpose(2, 0, 1) # Change the image shape to (3, w, h)            
            img_ori_lab = np.asarray(img_ori_lab) # Convert to numpy array
            img_l = img_ori_lab[0,:,:] # Select the Luminance chanel(L channel) from the image. I suppose that the L channel is the first layer of the 3d array due to the shape (3, w, h)
            img_l = np.asarray(img_l, dtype=np.float32)
            img_l = torch.from_numpy(img_l) # Convert from numpy array to torch tensor. This tensor hold only pixels intensities of the L channel
            
            img_resized_lab = rgb2lab(img_ori_resized) # Convert from RGB to CIE L*a*b* color space. The image shape is (w, h, 3)
            img_resized_lab = img_resized_lab.transpose(2, 0, 1) # Change the image shape to (3, w, h)    
            img_resized_lab = np.asarray(img_resized_lab)
            img_ab = img_resized_lab[1:3, :, :] # Select the a and b chanels from the image. I suppose that the a and b channels are the second and third layer of the 3d array due to the shape (3, w, h)
            img_ab = np.asarray(img_ab, dtype=np.float32)
            img_ab = torch.from_numpy(img_ab) # Convert from numpy array to torch tensor. This tensor hold only pixels intensities of the a,b channels
            
            return img_l, img_ab # Return the channels in a splitted way

My question is whether I am doing this channel separation correctly and whether I am actually selecting the first matrix of the image for the luminance channel (L) and the second and third matrices for channels a and b.

I saw in some places, some people using the following notation:

img_l = img_ori_lab[:,:,0] # Just an example. The shape is (w, h, 3)

In this way shown above, I think that is wrong because pytorch will interpret that the number of channels is w instead 3…so I did the implementation that I am showing and asking for help.

Best regards,

Matheus Santos.

Can someone help me? please…

Anyone?
Nobody? :pensive:

The code looks alright.
The reference implementation and your approach should yield the same result, if you are directly indexing the array, since this dimension will be gone.

However, your approach should be better, since img_ab will have the right shape form the beginning.

Could you load a test image and just verify the indexing by visualizing the results and check that you see the expected LAB channels?

1 Like

Hey, sorry for delay to answer here!
Yes, I can run this test. Later I will post here the results.