Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance 在Mathematica 8或更高版本中创建透明图像的最快方法?_Performance_Image Processing_Wolfram Mathematica_Transparency_Rgba - Fatal编程技术网

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"
  ]
  • 是否有一种方法可以处理ImageData返回的形状,以消除上面提到的ColorSeparate/ColorCombine
  • 您是否可以提出与上述方法相同或更快的改进或完全其他方法
  • 注意:该功能仅使完全白色的RGB像素透明,这是预期的

    关于第一个问题的更新:

    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"]
      ]
    
    但表现更差:

    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必须是难以击败的。