# How to add images where each represents a different color channel?

So I have three matrices that represent say the R, G, and B channels of an image:

mR = RandomInteger[{0, 255}, {2, 2}];
mG = RandomInteger[{0, 255}, {2, 2}];
mB = RandomInteger[{0, 255}, {2, 2}];


I would like to add these to get an overall image made of these three channels, but I can't seem to be able to do that. Trying to understand the documentation under "Image", I tried

Image[{mR, mG, mB}, "Byte"]


but this gives me an image that's 3x2:

Also when I try

Image[{mR, ConstantArray[0, {2, 2}], ConstantArray[0, {2, 2}]}, "Byte"]


I don't get the red channel. I just get the first row of the image above with zeros everywhere else:

I can't understand the documentation here, and any help would be greatly appreciated!

-
@cartonn Should you want to, you could do it in one go with Image[RandomInteger[{0, 255}, {2, 2, 3}], "Byte"]... –  cormullion Dec 19 '12 at 16:30
@cormullion I assumed the RandomInteger part was just to generate a MWE, and in reality the mR, mG, and mB are more complicated. –  Eli Lansey Dec 19 '12 at 16:33
@EliLansey yes, I thought that was probably true. Still, good practice for my fingers ... :) –  cormullion Dec 19 '12 at 16:42

Use the Interleaving option for Image to specify the colors are not interleaved.

Image[{mR, mG, mB}, "Byte", Interleaving -> False]


Update:

Normally image data is in the form

imdata={
{{r11,g11,b11}, ..., {r1m,g1m,b1m}}, (* First row of pixels *)
{{r21,g21,b21}, ..., {r2m,g2m,b2m}}, (* Second row of pixels *)
...
{{rn1,gn1,bn1}, ..., {rnm,gnm,bnm}}  (* Last row of pixels *)
}


where each {rij,gij,bij} represents Red/Blue/Green value for pixel [[i,j]] and you can just use Image[imdata]

But if you have your image data like:

red = { {r11,r12, ..., r1m}, (* Amount of red for each pixel in first row *)
{r21,r22, ..., r2m}, (*  ... in second row *)
...
{rn1,rn2, ..., rnm}  (*  ... last row *)
}
green = (* Similarly *)
blue = (*  *)


And you want to make an Image out of this you let Mathematica know you have it in this form by the Image[{red,green,blue},Interleaving->False] option.

You can run the command ImageData[img,Interleaving->#]&/@{True,False} on an image to see the difference for a particular image, I recommend a small one.

For 3 pixel wide, 2 pixel high image looking like:

imdat={
{{1.,1.,0.},{1.,0.,0.},{0.,0.,1.}},
{{1.,1.,1.},{0.,1.,0.},{0.,0.,1.}}
};
{red, green, blue} = ImageData[Image[imdat], Interleaving -> False];
(* You'll now have:
red   == {{1.,1.,0.},
{1.,0.,0.}}
green == {{1.,0.,0.},
{1.,1.,0.}}
blue  == {{0.,0.,1.},
{1.,0.,1.}}
*)

-
+1 Much better than my answer. I'd consider this the "right" way. –  Eli Lansey Dec 19 '12 at 15:52
This works, but could you explain "interleaving" a bit more? What does it mean to have colors interleaved? The documentation is again superficial, as far as I can tell. –  cartonn Dec 19 '12 at 16:42
@cartonn I updated the question with an explanation of the difference –  ssch Dec 19 '12 at 17:03

First make each one of them an image:

images = Image[ #, "Byte"]&/@{mR,mG,mB}


Then use ColorCombine:

ColorCombine[images, "RGB"]


-

You could also combine the data yourself before making it into an image. We want to take the three matrices and list each corresponding component together:

MapThread[List, {mR, mG, mB}, 2]


This gives a matrix of RGB triplets as required. You can then throw that at Image.

-
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.4688282310962677, "perplexity": 3420.5950132020316}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2014-23/segments/1406510267075.55/warc/CC-MAIN-20140728011747-00271-ip-10-146-231-18.ec2.internal.warc.gz"}