在Mathematica 8或以上版本中创建透明图像的最快方法?

Cet*_*ert 4 performance transparency wolfram-mathematica image-processing rgba

目前我使用:

transparent // ClearAll
transparent[i_] :=
 Module[{r, g, b, a},
  {r, g, b} = ImageData /@ ColorSeparate[i, "RGB"];
  a = Unitize[3. - (r + g + b)];
  (Image /@ {r, g, b, a})~ColorCombine~"RGB"
  ]
Run Code Online (Sandbox Code Playgroud)
  1. 有没有办法玩ImageData返回的形状来消除上面的ColorSeparate/ColorCombine?
  2. 是否有改进措施或完全可以提出的其他方法与上述方法一样快或更快?

注意:该功能仅使完美的白色RGB像素透明且有意.

第一个问题的更新:

使用Interleaving-> False可以消除ColorSeparate,ColorCombine

transparent0 // ClearAll
transparent0[i_] :=
 Module[{r, g, b, a},
  {r, g, b} = ImageData[i, Interleaving -> False];
  a = Unitize[3. - (r + g + b)];
  Image[{r, g, b, a}, Interleaving -> False, ColorSpace -> "RGB"]
  ]
Run Code Online (Sandbox Code Playgroud)

但表现更差:

transparent0[img]; //Timing
(* ==> {0.6490372, Null} *)
Run Code Online (Sandbox Code Playgroud)

Hei*_*ike 10

您使用的是什么版本的Mathematica?在Mathematica 8中你可以使用SetAlphaChannel.例如

transparent2[img_] := SetAlphaChannel[img, Binarize[ColorNegate[img], 0.]]
Run Code Online (Sandbox Code Playgroud)

将所有白色像素的alpha通道设置为0,将所有其他像素设置为1.我对此进行了测试

img = Image[Graphics3D[Sphere[], Background -> White], ImageSize -> 1000]
Run Code Online (Sandbox Code Playgroud)

我得到的时间是

transparent2[img]; // Timing
(* ==> {0.10067, Null} *)
Run Code Online (Sandbox Code Playgroud)

与原始代码相比

transparent[img]; //Timing
(* ==> {0.202112, Null} *)
Run Code Online (Sandbox Code Playgroud)