RGB to grayscale, easy

XxaemaethxX: <p>Hi everyone, I was wondering if anyone could explain to me why my code below did not work, I know that RGB conversion to grayscale is (R + G +B/3) so I used PyTorch to extract each channel, then add three of them and divide by 3, but the end result was a distorted image. I viewed my image output using Jupyter notebook. I was successful ultimate importing torch vision and using “transforms functional to gray scale” but still was wondering why my average of the RGB channels didn’t work, I’m assuming I didn’t actually obtain the individual 3 channels seperately or properly average them…</p>
<blockquote>
<p>panda = np.array(Image.open(‘panda.jpg’).resize( (224,224)))<br>
panda_tensor = torch.from_numpy(panda)</p>
<p>panda_tensor.size()<br>
print ( panda_tensor.size() )</p>
<h1>Display panda</h1>
<p>plt.imshow(panda)</p>
</blockquote>
<blockquote>
<p>chan_r = panda_tensor[:,:,0].numpy()<br>
chan_g = panda_tensor[:,:,1].numpy()<br>
chan_b = panda_tensor[:,:,2].numpy()</p>
<p>result = (chan_r + chan_g + chan_b/3)</p>
<p>plt.imshow(result)</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/2X/0/0252d26dff04d21a5af43e3334908c47df40cc65.png" alt="acc716d7f2b0386bb20a08bc6d595e78" data-base62-sha1="kyoOvVUrqafra0VzwxfATyO4Wp" width="305" height="257"></p>
</blockquote>
<blockquote>
<h1>torch vision grayscale</h1>
<p>torchvision.transforms.functional.to_grayscale(Image.open(‘panda.jpg’).resize((224,224)), num_output_channels=1)</p>
</blockquote>

vmirly1: <aside class="quote no-group" data-username="XxaemaethxX" data-post="1" data-topic="37990">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/xxaemaethxx/48/9470_2.png" class="avatar"> XxaemaethxX:</div>
<blockquote>
<p>result = (chan_r + chan_g + chan_b/3)</p>
</blockquote>
</aside>
<p>The division must be outside the parantheses: <code>result = (chan_r + chan_g + chan_b)/3</code></p>
<p>Furthermore, sometimes different weights are used for converting RGB to gray-scale, like this for example: <code>0.2989 * R + 0.5870 * G + 0.1140 * B </code></p>

XxaemaethxX: <p>This was the original image:</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/2X/f/ff7091836f45f80f84469add9f27728bf1521803.png" alt="a0a489b6c93aeef87a1454ae7ebcd7d1" data-base62-sha1="ArIVLm6RGY8xPgU0lG76EWQMkKf" width="452" height="329"></p>

lilsatan: <p>Ok, I just the paranthesis the way you said AND divided by 3.0 outside, but it gives me this image:<br>
Whats is going on?<br>
<img src="https://discuss.pytorch.org/uploads/default/original/2X/c/c7838643f33c5a7b196c425e5c9f533792064da3.jpeg" alt="1b793e951593b1063b269013e6109eff-png" data-base62-sha1="ssYO0cY2JcqAv8jSI5KpITI0nET" width="343" height="266"></p>

ptrblck: <p>Could you check the <code>dtype</code> of your tensor?<br>
Usually images are encoded as <code>uint8</code> values, which can overflow in such an addition.<br>
Try to cast it to float before the transformation is applied.</p>

djlamar: <p>I know this is an old post but… aside from the possible distortion mentioned, by default plt.imshow will not assume that a rank 2 tensor is a grayscale image and will use the default colormap. If you want to see it as a grayscale image, you have to set cmap=‘gray’ in imshow.</p>