Performance 在Mathematica 8或更高版本中创建透明图像的最快方法?
目前我使用:Performance 在Mathematica 8或更高版本中创建透明图像的最快方法?,performance,image-processing,wolfram-mathematica,transparency,rgba,Performance,Image Processing,Wolfram Mathematica,Transparency,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" ] 是否有一种方法可以处理ImageData返回的形状,以消除上面提到的ColorSepa
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"
]
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} *)
你使用的是什么版本的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} *)
哇,谢谢你的回答!两个结果之间甚至没有“图像差异”。就性能而言,SetAlphaChannel必须是难以击败的。