在 Mathematica 8 或更高版本中创建透明图像的最快方法?
目前我使用:
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"
]
- 有没有办法处理 ImageData 返回的形状来消除上面的 ColorSeparate / ColorCombine ?
- 您是否可以想出与上述方法一样快或更快的改进方法或完全其他方法?
注意:该函数仅使纯白色 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"
]
- Is there a way to play with the shape of what ImageData returns to eliminate ColorSeparate / ColorCombine in the above?
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(1)
您使用什么版本的 Mathematica?在 Mathematica 8 中,您可以使用
SetAlphaChannel
。例如,将所有白色像素的 Alpha 通道设置为 0,将所有其他像素设置为 1。我对此进行了测试
,并将我得到的计时
与原始代码进行了比较
What version of Mathematica are you using? In Mathematica 8 you could use
SetAlphaChannel
. For examplewould set the alpha channel of all white pixels to 0 and all other pixels to 1. I tested this on
and the timings I get are
compared to the original code