我使用佳能 EOS SDK 并希望通过相机流实现实时绿屏。
我有一个带有像素操作优化的简单绿屏算法。
public Bitmap greenScreen(Bitmap input, int tolerance)
{
FastPixel fpinput = new FastPixel(input);
fpinput.Lock();
Bitmap output = new Bitmap(input.Width, input.Height);
FastPixel fpoutput = new FastPixel(output);
fpoutput.Lock();
for (int y = 0; y < output.Height; y++)
{
for (int x = 0; x < output.Width; x++)
{
Color camColor = fpinput.GetPixel(x, y);
// Every component (red, green, and blue) can have a value from 0 to 255, so determine the extremes
byte max = Math.Max(Math.Max(camColor.R, camColor.G), camColor.B);
byte min = Math.Min(Math.Min(camColor.R, camColor.G), camColor.B);
bool replace =
camColor.G != min // green is not the smallest value
&& (camColor.G == max // green is the bsiggest value
|| max - camColor.G < 8) // or at least almost the biggest value
&& (max - min) > tolerance; // minimum difference between smallest/biggest value (avoid grays)
if (!replace)
fpoutput.SetPixel(x, y, camColor);
}
}
fpinput.Unlock(true);
fpoutput.Unlock(true);
return fpoutput.Bitmap;
}
对于 800x600 图片,这在我的 i5 上大约需要 300 毫秒。当然,这对于流畅的直播来说还不够快。
我有哪些优化选项?有机会使用 gpu 功能吗?会有什么机会?我不能在佳能相机上使用 DirectShow(不存在驱动程序)