Skip to content

ImageChannels

Status: Stable

documented, exercised by the test suite and/or worked examples, with no known limitations recorded.

Description

ImageChannels[image]

gives the number of colour channels: 1 for a grey image, otherwise the length of each pixel's value list (3 for RGB, 4 with an alpha channel).

Examples (37)

Every input below was run against the current Mathilda build and its output recorded.

Basic Examples (10)

In[1]:= ImageChannels[Image[{{0.5, 0.5}}]]
Out[1]= 1

In[2]:= ImageChannels[Image[{{{1., 0., 0.}, {0., 1., 0.}}}]]
Out[2]= 3

In[3]:= chk = Image[Table[If[Mod[Quotient[i - 1, 2] + Quotient[j - 1, 2], 2] == 0, 0., 1.], {i, 1, 16}, {j, 1, 16}], "Real"];

In[4]:= rgb = Image[Table[{N[i/16], N[j/16], 0.5}, {i, 1, 16}, {j, 1, 16}], "Real"];

In[5]:= bit = Image[Table[Boole[Mod[i + j, 2] == 0], {i, 1, 8}, {j, 1, 8}]];

In[6]:= byte = Image[Table[Mod[i*13 + j*7, 256], {i, 1, 16}, {j, 1, 16}]];

In[7]:= ImageChannels[chk]
Out[7]= 1

In[8]:= ImageChannels[rgb]
Out[8]= 3

In[9]:= ImageChannels[bit]
Out[9]= 1

In[10]:= ImageChannels[byte]
Out[10]= 1

Scope (16)

In[11]:= disk = Image[Table[N[Boole[(i - 8.5)^2 + (j - 8.5)^2 <= 25]], {i, 1, 16}, {j, 1, 16}], "Real"];

In[12]:= ramp = Image[Table[N[(j - 1)/15], {i, 1, 16}, {j, 1, 16}], "Real"];

In[13]:= zone = Image[Table[N[(1 + Cos[((i - 16)^2 + (j - 16)^2)/40.])/2], {i, 1, 32}, {j, 1, 32}], "Real"];

In[14]:= noise = Image[Table[N[Mod[i*37 + j*17, 101]]/101, {i, 1, 32}, {j, 1, 32}], "Real"];

In[15]:= rgb = Image[Table[{N[i/16], N[j/16], 0.5}, {i, 1, 16}, {j, 1, 16}], "Real"];

In[16]:= sky = Image[Table[{N[0.15 + 0.7 (16 - i)/16], N[0.35 + 0.45 (16 - i)/16], N[0.85 - 0.35 (16 - i)/16]}, {i, 1, 16}, {j, 1, 24}], "Real"];

In[17]:= vol = Image3D[Table[N[Mod[z*7 + y*13 + x*3, 97]]/97, {z, 1, 8}, {y, 1, 10}, {x, 1, 12}], "Real"];

In[18]:= volb = Image3D[Table[N[Boole[x <= 6 && y <= 5]], {z, 1, 8}, {y, 1, 10}, {x, 1, 12}], "Real"];

In[19]:= ImageChannels[disk]
Out[19]= 1

In[20]:= ImageChannels[ramp]
Out[20]= 1

In[21]:= ImageChannels[zone]
Out[21]= 1

In[22]:= ImageChannels[noise]
Out[22]= 1

In[23]:= ImageChannels[sky]
Out[23]= 3

In[24]:= ImageChannels[vol]
Out[24]= 1

In[25]:= ImageChannels[volb]
Out[25]= 1

In[26]:= ImageChannels[Import[Export["/tmp/mathilda_ex.png", rgb]]]
Out[26]= 3

Applications (2)

In[27]:= chk = Image[Table[If[Mod[Quotient[i - 1, 2] + Quotient[j - 1, 2], 2] == 0, 0., 1.], {i, 1, 16}, {j, 1, 16}], "Real"];

In[28]:= Table[ImageChannels[GaussianFilter[chk, r]], {r, 1, 3}]
Out[28]= {1, 1, 1}

Properties & Relations (4)

In[29]:= chk = Image[Table[If[Mod[Quotient[i - 1, 2] + Quotient[j - 1, 2], 2] == 0, 0., 1.], {i, 1, 16}, {j, 1, 16}], "Real"];

In[30]:= rgb = Image[Table[{N[i/16], N[j/16], 0.5}, {i, 1, 16}, {j, 1, 16}], "Real"];

In[31]:= ImageChannels[chk] === ImageChannels[GaussianFilter[chk, 1]]
Out[31]= True

In[32]:= ImageChannels[rgb] === ImageChannels[ImagePad[rgb, 2]]
Out[32]= True

Neat Examples (2)

In[33]:= zone = Image[Table[N[(1 + Cos[((i - 16)^2 + (j - 16)^2)/40.])/2], {i, 1, 32}, {j, 1, 32}], "Real"];

In[34]:= ImageChannels[zone]
Out[34]= 1

Applications (3)

In[35]:= ImageChannels[Image[{{{1., 0, 0}, {0, 1., 0}}}]]
Out[35]= 3

In[36]:= ImageChannels[Image[{{0., 1.}}]]
Out[36]= 1

In[37]:= ImageChannels[Image[{{{1., 0, 0, 0.5}}}]]
Out[37]= 4

Implementation notes

Algorithm. builtin_imagechannels returns the number of colour channels as an integer. It tries image3d_info first and then image_info, each of which reports the channel count as a by-product of validating the shape. The count is read off the pixel array's rank: a rank-2 (height × width) array is grey and reports 1; a rank-3 (height × width × channels) array reports its last dimension — 3 for RGB, 4 when an alpha channel is present, 2 for grey-plus-alpha. For a volume the same rule applies to the trailing axis of the depth × height × width (× channels) buffer. The channel axis is the innermost, interleaved dimension, which is the order every filter's flat image_load buffer also uses.

Data structures. Reads only the shape metadata of the canonical image node; no buffer is loaded. ImageChannels is on pack.c's AWARE list, so a packed image is inspected in place.

Complexity / limits. O(1) — the shape walk that image_info already does. Returns unevaluated for a non-image.

Attributes: Protected.

References

Notes & additional examples

Notes

ImageChannels[image] gives the number of colour channels: 1 for a grey image, otherwise the length of each pixel's value list — 3 for RGB, 4 with an alpha channel, 2 for grey-plus-alpha.

The channel count is the trailing (innermost, interleaved) dimension of the pixel array, so it is read straight off the image's shape without loading any pixels. It accepts a volumetric Image3D as well as a plane, applying the same rule to the last axis of the depth × height × width (× channels) buffer.