The right way to access red channel in Lena

12 views
Skip to first unread message

Adam Hughes

unread,
Mar 26, 2015, 7:16:12 PM3/26/15
to scikit...@googlegroups.com
I'm trying to build a filter to show only the red channel in the lena image.  I defined two masks:

(1,0,0)
(255,0,0)

Oddly, the (255,0,0) gives me the correct plot when doing imshow(), but (1,0,0) doesn't.  Why does the (1,0,0) mask lead to light regions being dark and dark regions being light?  Here's a working example:

%pylab inline
from skimage.data import lena

lena = lena()

f, (ax1, ax2) = plt.subplots(1,2, figsize=(8,6))

ax1.imshow(lena*(1,0,0))
ax2.imshow(lena*(255,0,0))


Josh Warner

unread,
Mar 26, 2015, 7:29:34 PM3/26/15
to scikit...@googlegroups.com
Hi Adam,

You can slice out just the red channel with `lena[..., 0]`, which is equivalent to `lena[:, :, 0]`. The result will be a rank 2 array representing the red channel. This ability is made possible by NumPy.

Adam Hughes

unread,
Mar 26, 2015, 7:31:58 PM3/26/15
to scikit...@googlegroups.com
I don't think the image came through.  Let me attach it
red_lena.png

Adam Hughes

unread,
Mar 26, 2015, 8:27:01 PM3/26/15
to scikit...@googlegroups.com
Thanks Josh.  I actually would normally do it that way, but I stumbled on this behavior when we were trying to build filters.  For example, scale the red channel by 50% via multiplying by (0.5, 1, 1).  Just curious what's going on, and why multiplcation by (1,0,0) doesn't work.

--
You received this message because you are subscribed to a topic in the Google Groups "scikit-image" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/scikit-image/JzmfEbBJKYU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to scikit-image...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stéfan van der Walt

unread,
Mar 26, 2015, 8:39:20 PM3/26/15
to scikit-image
Hi Adam

On Thu, Mar 26, 2015 at 4:31 PM, Adam Hughes <hughes...@gmail.com> wrote:
ax1.imshow(lena*(1,0,0))
ax2.imshow(lena*(255,0,0))

The multiplication turns your image's dtype into uint64, which causes the scaling problems.

I prefer with float images, always ensuring that they are in [0, 1] and then specifying vmin=0 and vmax=1 to matplotlib.

Regards
Stéfan

Adam Hughes

unread,
Mar 26, 2015, 8:40:36 PM3/26/15
to scikit...@googlegroups.com
Thanks!

--
Reply all
Reply to author
Forward
0 new messages