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

username: mfcs
cooked: <p>Hi everyone! <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"><br>
Could you help me to verify if I am doing right in the way that I am extracting color channels from CIE L<em>a</em>b* color space?<br>
Below is the code that I am using:</p>
<pre><code class="lang-auto">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
</code></pre>
<p>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.</p>
<p>I saw in some places, some people using the following notation:</p>
<pre><code class="lang-auto">img_l = img_ori_lab[:,:,0] # Just an example. The shape is (w, h, 3)
</code></pre>
<p>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.</p>
<p>Best regards,</p>
<p>Matheus Santos.</p>

username: mfcs
cooked: <p>Can someone help me? please…</p>

username: mfcs
cooked: <p>Anyone?<br>
Nobody? <img src="https://discuss.pytorch.org/images/emoji/apple/pensive.png?v=9" title=":pensive:" class="emoji" alt=":pensive:"></p>

username: ptrblck
cooked: <p>The code looks alright.<br>
The reference implementation and your approach should yield the same result, if you are directly indexing the array, since this dimension will be gone.</p>
<p>However, your approach should be better, since <code>img_ab</code> will have the right shape form the beginning.</p>
<p>Could you load a test image and just verify the indexing by visualizing the results and check that you see the expected LAB channels?</p>

username: mfcs
cooked: <p>Hey, sorry for delay to answer here!<br>
Yes, I can run this test. Later I will post here the results.</p>