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

发布于 2024-12-20 11:09:40 字数 862 浏览 5 评论 0原文

目前我使用:

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"
  ]
  1. 有没有办法处理 ImageData 返回的形状来消除上面的 ColorSeparate / ColorCombine ?
  2. 您是否可以想出与上述方法一样快或更快的改进方法或完全其他方法?

注意:该函数仅使纯白色 RGB 像素透明,这是有意的。

关于第一个问题的更新:

ColorSeparate、ColorCombine 可以通过使用 Interleaving->False 来消除,

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"]
  ]

但性能较差:

transparent0[img]; //Timing
(* ==> {0.6490372, Null} *)

At the moment I use:

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"
  ]
  1. Is there a way to play with the shape of what ImageData returns to eliminate ColorSeparate / ColorCombine in the above?
  2. Are there improvements or wholly other methods you could come up with that are as fast as or faster than the above?

Note: the function makes only perfectly white RGB pixels transparent and that is intended.

Update about first question:

ColorSeparate, ColorCombine can be eliminated by using Interleaving->False

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"]
  ]

but performance is worse:

transparent0[img]; //Timing
(* ==> {0.6490372, Null} *)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不奢求什么 2024-12-27 11:09:40

您使用什么版本的 Mathematica?在 Mathematica 8 中,您可以使用 SetAlphaChannel。例如,

transparent2[img_] := SetAlphaChannel[img, Binarize[ColorNegate[img], 0.]]

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

img = Image[Graphics3D[Sphere[], Background -> White], ImageSize -> 1000]

,并将我得到的计时

transparent2[img]; // Timing
(* ==> {0.10067, Null} *)

与原始代码进行了比较

transparent[img]; //Timing
(* ==> {0.202112, Null} *)

What version of Mathematica are you using? In Mathematica 8 you could use SetAlphaChannel. For example

transparent2[img_] := SetAlphaChannel[img, Binarize[ColorNegate[img], 0.]]

would set the alpha channel of all white pixels to 0 and all other pixels to 1. I tested this on

img = Image[Graphics3D[Sphere[], Background -> White], ImageSize -> 1000]

and the timings I get are

transparent2[img]; // Timing
(* ==> {0.10067, Null} *)

compared to the original code

transparent[img]; //Timing
(* ==> {0.202112, Null} *)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文