Re: Need faster way to combine arrays
- To: mathgroup at smc.vnet.net
- Subject: [mg36075] Re: Need faster way to combine arrays
- From: "Mariusz Jankowski" <mjkcc at usm.maine.edu>
- Date: Thu, 15 Aug 2002 02:36:29 -0400 (EDT)
- References: <ajd7sn$ekc$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Kevin, in version 4.2 you can get approx a factor of 2 improvement by using Transpose[Transpose/@{r,g,b},{3,2,1}] This is currently the fastest method. However, if you are using an earlier version of Mathematica, the code above will unpack the data and therefore run slowly and eat up over 6 times more memory. In that case compile your code and you will also get close to a factor of 2 improvement. As an example, here is the code used in the DIP package versions 1.0 and 1.1: cplanarInt = Compile[{{x,_Integer,3}}, With[{d = Rest[Dimensions[x]}, Table[ x[[All, i, j]], {i, d[[1]]}, {j, d[[2]]}]]] One final note, in your own work with large images, set $HistoryLength=1, and check your code to see if it is unpacking your data using either Developer`PackedArrayQ or ByteCount at intermediate steps in your calculations. Regards, Mariusz -- =============================== Mariusz Jankowski University of Southern Maine mjkcc at usm.maine.edu 207-780-5580 .. "Kevin Gross" <kcgross at sbcglobal.net> wrote in message news:ajd7sn$ekc$1 at smc.vnet.net... > Hello all, > > I've been using Mathematica for a while, but only recently for image > processing. Suppose I have the following 2D intensity arrays: > > r={{a1,b1},{c1,d1}}; > g={{a2,b2},{c2,d2}}; > b={{a3,b3},{c3,d3}}; > > Now I'd like to combine them to produce a color image. > > color=Table[{r[[i,j]],g[[i,j]],b[[i,j]]},{i,2},{j,2}] > > This returns > > {{{a1, a2, a3}, {b1, b2, b3}}, {{c1, c2, c3}, {d1, d2, d3}}} > > which can be viewed as a color image with > > Graphics[Raster[color,ColorFunction->RGBColor]]//Show > > When the 2D arrays are large, combining them to form the color tensor as > above is very time consuming. Surely there is a more efficient, clever > way to combine them than my use of Table, right? > > Many thanks, > > Kevin Gross > > PS Any comments pro/con from users of the Digital Image Processing > package are also welcome. >