Re: combine red green and blue channels
- To: mathgroup at smc.vnet.net
- Subject: [mg83859] Re: combine red green and blue channels
- From: thomas <thomas.muench at gmail.com>
- Date: Mon, 3 Dec 2007 05:38:55 -0500 (EST)
- References: <fitsi8$54i$1@smc.vnet.net>
Here is a more general approach than the specific question you are asking for: Let's say your three matrices are called ch1, ch2 and ch3, and they all have Dimension 100 x 90 (I only use these numbers so that I can explain the approach more easily) You can combine them to a single image img={ch1,ch2,ch3}; img has the Dimension 3x100x90. Transpose this array as follows: imgnew=Transpose[img,{3,1,2}]; imgnew has Dimension 100x90x3, i.e. it is a 100x90 matrix, like our original channels, but each entry consists of 3 numbers (from the 3 different channels) In principle this is all you need. You can now display your image with Show[Graphics[Raster[imgnew]]] Or in one line: Show[Graphics[Raster[Transpose[{ch1,ch2,ch3},{3,1,2}]]]] Be aware, however, that your original values should be scaled to be contained in the interval [0,1]. Also, this only works if you really do have 3 channels to start with, not more and not less. ch1, ch2 and ch3 then correspond to the RGB values in the image that is finally displayed. Now the generalization: You can easily assign your 3 (or more or less) channels any color you want, say ch1 should be plotted in Orange, ch2 in Red, ch3 in White, etc. (This is quite useful if you have images that have more or less than 3 channels, for example if you deal with data from fluorescent microscopy) For this you need a colorlist in which you define your desired colors (corresponding to your original channels). Each color should be represented as a number-triplet corresponding to its RGB-definition, e.g. Red would be {1,0,0}, Blue would be {0,0,1}, White would be {1,1,1}, Orange would be {1,0.5,0}, and so on. For example colorlist={{1,.05,0},{0,0,1},{1,1,1}} (* this is {Orange,Blue,White} *) Then you change each entry (=pixel) in imgnew so that it displays in the correct color: maxC=Max[imgnew]; displayimg=Clip[Map[Dot[#,colorlist] &, imgnew, {2}]/maxC]; Show[Graphics[Raster[displayimg]]] displayimg now always has Dimension 100x90x3, even if you would originally start out with only 2 channel (i.e. Dimensions[imgnew] is 100x90x2) or 4 channels (i.e. Dimensions[imgnew] is 100x90x4) or any other number of channels. You just have to make sure that that the number of colors in colorlist corresponds to your number of channels. How does it work? The dot-product basically scales your defined colors by the pixel-value of your channels, the Map[...,{2}] makes sure that this is applied at the second level of imagenew (i.e. your pixels). Division by maxC allows you to use any original data range (for example 0...255) and doesn't restrict you to 0...1. And finally, Clip[...] restricts the result to [-1...1], which, because of the positive nature of your original data, effectively is [0...1] . Why is this last step necessary? Imagine that one of your colors is white, and the white channel has maximal intensity in a certain pixel. Then the resulting image should be white (i.e. {1,1,1}) in that pixel. If that pixel also has a positive value in the orange channel, that will be added by the dot product (result e.g. {1.5,1.25,1}, and Clip[..] brings that back down to {1,1,1}. In summary: plotImgFromChannels[channelList_,colorList_:{{1,0,0},{0,1,0}, {0,0,1}}]:= Module[{maxC=Max[channelList],imgnew=Transpose[channelList, {3,1,2}],displayimg}, displayimg=Clip[Map[Dot[#,colorList] &, imgnew, {2}]/maxC]; Show@Graphics[Raster[displayimg]] ] I hope that helps, thomas On Dec 2, 10:06 am, tdoxm... at gmail.com wrote: > Hello > > I am a user of Mathematica 5.2. I wanted to know that if i have three > arrays of same size that are actually representing three different > channels that is red , green and blue channels, what is the best way > to combine them to display a image, that has all three colors. > > Gracias